Skip to content

Commit ad0eb33

Browse files
committed
✨ Add active-class-name example
1 parent 86a01fa commit ad0eb33

File tree

16 files changed

+178
-0
lines changed

16 files changed

+178
-0
lines changed

examples/active-class-name/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# activeClassName example
2+
3+
Nullstack [Context](https://nullstack.app/context)'s [Router](https://nullstack.app/routes-and-params#router) object bring info about the current route. This example compares the `router.path` with a link's `href` to dinamically set a active class on it.
4+
5+
## Deploy your own
6+
7+
Deploy it now with [Vercel](https://vercel.com):
8+
9+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/GuiDevloper/nullstack-examples/tree/master/examples/active-class-name&project-name=active-class-name&repo-name=active-class-name&demo-title=Nullstack+activeClassName&demo-description=Nullstack+example+of+active+class+name&demo-url=https://github.com/GuiDevloper/nullstack-examples/tree/master/examples/active-class-name&demo-image=https://nullstack.app/image-1200x630.png)
10+
11+
## How to use
12+
13+
Execute [`nulla create`](https://github.com/GuiDevloper/nulla) with [npm](https://docs.npmjs.com/cli/init):
14+
15+
```bash
16+
npx nulla create --example active-class-name active-class-name-app
17+
```
18+
19+
Then deploy it with [Vercel](https://github.com/GuiDevloper/nulla/blob/master/docs/en-US/deploy-vercel.md), [Heroku](https://github.com/GuiDevloper/nulla/blob/master/docs/en-US/deploy-heroku.md) or [Netlify](https://github.com/GuiDevloper/nulla/blob/master/docs/en-US/deploy-netlify.md).
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import application from '../.production/server';
2+
3+
export default application.server;

examples/active-class-name/client.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Nullstack from 'nullstack';
2+
import Application from './src/Application';
3+
4+
export default Nullstack.start(Application);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"private": true,
3+
"devDependencies": {
4+
"nullstack": "~0.15.0"
5+
},
6+
"scripts": {
7+
"start": "npx nullstack start --port=3000",
8+
"build": "npx nullstack build --mode=ssr",
9+
"vercel-build": "npm run build"
10+
}
11+
}
930 Bytes
Loading

examples/active-class-name/server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import Nullstack from 'nullstack';
2+
import Application from './src/Application';
3+
4+
export default Nullstack.start(Application);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
background-color: #111827;
3+
color: #fff;
4+
font-family: 'Roboto', sans-serif;
5+
}
6+
7+
a {
8+
color: #f15d9f;
9+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import Nullstack from 'nullstack';
2+
import './Application.css';
3+
import Nav from './components/Nav';
4+
import Home from './pages/Home';
5+
import About from './pages/About';
6+
import Blog from './pages/Blog';
7+
import Dynamic from './pages/Dynamic';
8+
9+
class Application extends Nullstack {
10+
11+
prepare({ page }) {
12+
page.title = 'Nullstack active-class-name';
13+
}
14+
15+
render() {
16+
return (
17+
<>
18+
<Nav/>
19+
<Home route="/"/>
20+
<About route="/about"/>
21+
<Blog route="/blog"/>
22+
<Dynamic route="/*"/>
23+
</>
24+
)
25+
}
26+
27+
}
28+
29+
export default Application;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { NullstackClientContext } from 'nullstack';
2+
3+
interface ActiveLinkProps extends NullstackClientContext {
4+
href?: string,
5+
class?: string
6+
}
7+
8+
function ActiveLink(context: ActiveLinkProps) {
9+
const { href, children, router } = context;
10+
const activeClass = router.path === href ? ' active' : '';
11+
const classes = `${context.class}${activeClass}`;
12+
return (
13+
<a href={href} class={classes}>
14+
{children}
15+
</a>
16+
)
17+
}
18+
19+
export default ActiveLink;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.nav-link {
2+
text-decoration: none;
3+
}
4+
5+
.active:after {
6+
content: ' (current page)';
7+
}

0 commit comments

Comments
 (0)