🐛 fixes routing

This commit is contained in:
2026-05-20 14:40:47 +02:00
parent 7d6240e28a
commit e4e41197b9
7 changed files with 78 additions and 22 deletions
+9
View File
@@ -0,0 +1,9 @@
import { css } from "lit";
export const styles = css`
:host {
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-start;
}
`;
+14
View File
@@ -0,0 +1,14 @@
import { LitElement, html } from "lit";
import { property, customElement } from "lit/decorators.js";
import { styles } from "./index.css.js";
@customElement("app-episode")
export class AppEpisode extends LitElement {
static override styles = styles;
@property({ type: String }) override id = "";
override render() {
return html`
Episode ${this.id}
`;
}
}
+16
View File
@@ -0,0 +1,16 @@
import { route } from "../../core/route.js";
import { html } from "lit";
export const path = "/episodes" as const;
export const routes = route(
{
path: "/episodes/:id",
async enter() {
await import("./index.js");
return true;
},
render({ id }){
return html`<app-episode id=${id}></app-episode>`;
},
},
);
+5 -4
View File
@@ -1,15 +1,16 @@
import { LitElement, html } from "lit"; import { LitElement, html } from "lit";
import { property, customElement } from "lit/decorators.js"; import { customElement } from "lit/decorators.js";
import { styles } from "./index.css.js"; import { styles } from "./index.css.js";
const logo = import.meta.resolve("../../../assets/logo.svg"); const logo = new URL("../../../assets/logo.svg", import.meta.url);
@customElement("app-episodes") @customElement("app-episodes")
export class AppEpisodes extends LitElement { export class AppEpisodes extends LitElement {
static styles = styles; static override styles = styles;
render() { override render() {
return html` return html`
<div class="logo"><img alt="open-wc logo" src=${logo} /></div> <div class="logo"><img alt="open-wc logo" src=${logo} /></div>
<a href="/episodes/1">Episode 1</a>
`; `;
} }
} }
+9 -7
View File
@@ -1,8 +1,11 @@
import type { RouteConfig } from "@lit-labs/router"; import { route } from "../core/route.js";
import { routes as routeEpisode } from "./:id/routes.js";
import { html } from "lit"; import { html } from "lit";
export const route = { export const path = "/episodes" as const;
path: "/episodes" as const, export const routes = route(
{
path,
async enter() { async enter() {
await import("./index.js"); await import("./index.js");
return true; return true;
@@ -10,7 +13,6 @@ export const route = {
render(){ render(){
return html`<app-episodes></app-episodes>`; return html`<app-episodes></app-episodes>`;
}, },
}; },
export default [ routeEpisode,
route, );
] as RouteConfig[];
+13
View File
@@ -0,0 +1,13 @@
import type { PathRouteConfig } from "@lit-labs/router";
export function route(...routes: (PathRouteConfig|PathRouteConfig[])[]): PathRouteConfig[] {
return routes.flatMap(function process(route){
if (Array.isArray(route)) // already processed
return route;
const { path, ...rest }= route;
return [
{ path, ...rest },
{ path: path + "/", rest },
];
});
}
+6 -5
View File
@@ -1,17 +1,18 @@
import { LitElement, html } from "lit"; import { LitElement, html } from "lit";
import { property, customElement } from "lit/decorators.js"; import { customElement } from "lit/decorators.js";
import { styles } from "./index.css.js"; import { styles } from "./index.css.js";
import { Router } from "@lit-labs/router"; import { Router } from "@lit-labs/router";
import { default as routesEpisodes, route as routeEpisodes } from "./app-episodes/routes.js"; import * as routeEpisodes from "./app-episodes/routes.js";
@customElement("app-cfpodcasts") @customElement("app-cfpodcasts")
export class AppCfpodcasts extends LitElement { export class AppCfpodcasts extends LitElement {
static styles = styles; static override styles = styles;
private _routes = new Router(this, [ private _routes = new Router(this, [
{ path: "/", render: () => html`Hello world` }, { path: "/", render: () => html`Hello world` },
...routesEpisodes ...routeEpisodes.routes,
]); ]);
render() { override render() {
console.log(this._routes);
return html` return html`
<main>${this._routes.outlet()}</main> <main>${this._routes.outlet()}</main>
<nav> <nav>