[lit-labs/router] Add onRouteChanged callback to Router #2529
LasseRosenow
started this conversation in
Ideas
Replies: 2 comments 5 replies
-
Second this. I'd go with an event--it's more general and in line with the rest of the API: class MyElement extends LitElement {
private _routes = new Routes(this, {
routes: [
{path: '/', render: () => html`<h1>Home</h1>`},
{path: '/projects', render: () => html`<h1>Projects</h1>`},
{path: '/about', render: () => html`<h1>About</h3>`},
]
});
connectedCallback() {
super.connectedCallback();
this.addEventListener('lit-route-changed', this.onRouteChange);
}
onRouteChange(event: RouteEvent) {
window.scroll({
top: 0,
left: 0,
});
}
render() {
return html`
<header>...</header>
<main>${this._routes.outlet()}</main>
<footer>...</footer>
`;
}
} This way you can also have multiple subscriptions including from other components (e.g. listening on window when the event bubbles). |
Beta Was this translation helpful? Give feedback.
3 replies
-
Wouldn’t using |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Feature Request
I like the new
@lit-labs/router
package very much, but it is missing on one functionality that I really need, which is the ability to execute a callback whenever the route changes.Use Case
In my application whenever I navigate from one page to the other, I need to set the windows scroll position to 0, as it is expected to be on any page based website. Also when I have a single page navigation that uses the scroll position as navigation, I don't want to set the global scroll position to 0, when the route changes, but scroll to some specific element.
So this cannot be a default setting within the router, but needs to be made configurable. This is why I would lilke to have some sort of callback to realize this, kinda like a
common enter
Possible API design:
Beta Was this translation helpful? Give feedback.
All reactions