diff --git a/package/router/README.md b/package/router/README.md index acb172eec..f26142df2 100644 --- a/package/router/README.md +++ b/package/router/README.md @@ -86,3 +86,76 @@ Make anchor valid href from route. ```html ``` + +### Full example with [lit-element](https://lit.dev) + +```ts +import {css, html, LitElement} from 'lit'; +import {customElement} from 'lit/decorators/custom-element.js'; +import {router} from '@alwatr/router'; + +import type {ListenerInterface} from '@alwatr/signal'; +import type {RoutesConfig} from '@alwatr/router'; +import type {TemplateResult} from 'lit'; + +@customElement('my-element') +export class MyElement extends LitElement { + static override styles = css` + :host { + display: block; + } + `; + + private _routes: RoutesConfig = { + map: (route) => route.sectionList[0]?.toString(), + list: { + 'home': { + render: () => html`Page Home ...`, + }, + 'about': { + render: () => html`Page About ...`, + }, + 'article': { + render: (route) => + route.sectionList[1] != null ? + html`Page Article ${route.sectionList[1]} ...` : + this._routes.list['404'], + }, + }, + }; + + constructor() { + super(); + router.initial(); + } + + private _listenerList: Array = []; + + override connectedCallback(): void { + super.connectedCallback(); + this._listenerList.push(router.signal.addListener(() => this.requestUpdate())); + } + + override disconnectedCallback(): void { + super.disconnectedCallback(); + this._listenerList.forEach((listener) => (listener as ListenerInterface).remove()); + } + + override render(): TemplateResult { + return html` +

Hello World!

+ +
+
  • Home
  • +
  • About
  • +
  • Article 100
  • +
  • Contact
  • +
    + +
    + ${router.outlet(this._routes)} +
    + `; + } +} +```