diff options
Diffstat (limited to 'preact-router/src/match.js')
-rw-r--r-- | preact-router/src/match.js | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/preact-router/src/match.js b/preact-router/src/match.js new file mode 100644 index 0000000..90243d0 --- /dev/null +++ b/preact-router/src/match.js @@ -0,0 +1,36 @@ +import { h, Component } from 'preact'; +import { subscribers, getCurrentUrl, Link as StaticLink, exec } from 'preact-router'; + +export class Match extends Component { + update = url => { + this.nextUrl = url; + this.setState({}); + }; + componentDidMount() { + subscribers.push(this.update); + } + componentWillUnmount() { + subscribers.splice(subscribers.indexOf(this.update)>>>0, 1); + } + render(props) { + let url = this.nextUrl || getCurrentUrl(), + path = url.replace(/\?.+$/,''); + this.nextUrl = null; + return props.children({ + url, + path, + matches: exec(path, props.path, {}) !== false + }); + } +} + +export const Link = ({ activeClassName, path, ...props }) => ( + <Match path={path || props.href}> + { ({ matches }) => ( + <StaticLink {...props} class={[props.class || props.className, matches && activeClassName].filter(Boolean).join(' ')} /> + ) } + </Match> +); + +export default Match; +Match.Link = Link; |