-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
143 additions
and
97 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { Component, FunctionalComponent, h, Prop, State } from '@stencil/core'; | ||
import { URLPattern } from 'urlpattern-polyfill'; | ||
|
||
type MatchedComponent = { | ||
name: string; | ||
props?: Record<string, string | number>; | ||
}; | ||
|
||
type RoutedComponent = { | ||
path: string; | ||
name: string; | ||
props?: Record<string, string | number>; | ||
}; | ||
|
||
const ComponentTemplate: FunctionalComponent<MatchedComponent> = ( | ||
props, | ||
children, | ||
) => { | ||
const Name = props.name; | ||
|
||
return <Name {...props.props}>{children}</Name>; | ||
}; | ||
|
||
@Component({ | ||
tag: 'kompendium-router', | ||
shadow: true, | ||
}) | ||
export class Router { | ||
@Prop() | ||
public routes: RoutedComponent[]; | ||
|
||
@State() | ||
private matchedComponent: MatchedComponent | undefined; | ||
|
||
public componentWillLoad() { | ||
const location = window.location; | ||
this.setMatchedComponent(location); | ||
} | ||
|
||
public render() { | ||
if (!this.matchedComponent) { | ||
return '404'; | ||
} | ||
|
||
return <ComponentTemplate {...this.matchedComponent} />; | ||
} | ||
|
||
private setMatchedComponent(location: Location) { | ||
const component = this.findComponent(location); | ||
|
||
if (component !== this.matchedComponent) { | ||
this.matchedComponent = component; | ||
} | ||
} | ||
|
||
private findComponent(location: Location): MatchedComponent | undefined { | ||
const matchedRoute = this.routes.find(this.isMatched(location)); | ||
if (!matchedRoute) { | ||
return; | ||
} | ||
|
||
const path = matchedRoute.path; | ||
const pattern = new URLPattern({ hash: path }); | ||
const match = pattern.exec(location.href); | ||
const props = this.parseProps(match?.hash.groups); | ||
|
||
return { | ||
name: matchedRoute.name, | ||
props: { | ||
...props, | ||
...matchedRoute.props, | ||
}, | ||
}; | ||
} | ||
|
||
private isMatched = (location: Location) => (route: RoutedComponent) => { | ||
const pattern = new URLPattern({ | ||
hash: route.path, | ||
}); | ||
|
||
return pattern.test(location.href); | ||
}; | ||
|
||
private parseProps( | ||
groups: Record<string, string | undefined> = {}, | ||
): Record<string, string | number | undefined> { | ||
return Object.fromEntries( | ||
Object.entries(groups).map(([key, value]) => [ | ||
key, | ||
isNaN(Number(value)) ? value : Number(value), | ||
]), | ||
); | ||
} | ||
} |
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
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
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