Created
March 3, 2017 05:22
-
-
Save hikoz/02e1e86763717e5b2cd912d7b21bee67 to your computer and use it in GitHub Desktop.
active tab with preact-router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { h, Component } from 'preact'; | |
import { Router, Link } from 'preact-router'; | |
function Header({url}) { | |
const NavItem = ({href, children}, state) => ( | |
<li className={href==url ? 'is-active' : ''}><Link href={href}>{children}</Link></li> | |
) | |
return ( | |
<section class="hero is-primary"> | |
<div class="hero-foot"> | |
<div class="container"> | |
<nav class="tabs is-boxed"> | |
<ul> | |
<NavItem href="/">Home</NavItem> | |
<NavItem href="/blog">Blog</NavItem> | |
<NavItem href="/404">404</NavItem> | |
</ul> | |
</nav> | |
</div> | |
</div> | |
</section> | |
) | |
} | |
class Layout extends Component { | |
render(props, state) { | |
const routeChange = ({url}) => this.setState({url}) | |
return ( | |
<div> | |
<Header url={state.url} className="nav"/> | |
<main className="section"> | |
<Router onChange={routeChange}> | |
<Home path="/"/> | |
<Blog path="/blog"/> | |
<Article path="/blog/:title"/> | |
<E404 default/> | |
</Router> | |
</main> | |
</div> | |
) | |
} | |
} | |
export default <Layout/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment