diff --git a/ui/element/src/index.ts b/ui/element/src/index.ts index 5e2883fc2..14463b40d 100644 --- a/ui/element/src/index.ts +++ b/ui/element/src/index.ts @@ -4,6 +4,7 @@ export * from './dummy-element.js'; export * from './smart-element.js'; export * from './mixins/localize.js'; +export * from './mixins/router.js'; export * from './mixins/direction.js'; export * from './mixins/logging.js'; export * from './mixins/signal.js'; diff --git a/ui/element/src/mixins/localize.ts b/ui/element/src/mixins/localize.ts index bc61936e5..4067e7072 100644 --- a/ui/element/src/mixins/localize.ts +++ b/ui/element/src/mixins/localize.ts @@ -19,7 +19,7 @@ export function LocalizeMixin>( } /** - * On localization resource context updated. + * On localization resource context update. */ protected _l18eContextUpdated(l18eContext: L18eContext): void { this._logger.logMethodArgs('_l18eContextUpdated', l18eContext.meta); diff --git a/ui/element/src/mixins/router.ts b/ui/element/src/mixins/router.ts new file mode 100644 index 000000000..d5f7159f7 --- /dev/null +++ b/ui/element/src/mixins/router.ts @@ -0,0 +1,29 @@ +import {routeContextConsumer, type RouteContext} from '@alwatr/router'; + +import type {SignalMixinInterface} from './signal.js'; +import type {Constructor} from '@alwatr/type'; + +export declare class RouterMixinInterface extends SignalMixinInterface {} + +export function RouterMixin>( + superClass: T, +): Constructor & T { + class RouterMixinClass extends superClass { + override connectedCallback(): void { + super.connectedCallback(); + this._signalListenerList.push( + routeContextConsumer.subscribe(this._routeContextUpdated), + ); + } + + /** + * On route context update. + */ + protected _routeContextUpdated(routeContext: RouteContext): void { + this._logger.logMethodArgs('_routeContextUpdated', routeContext); + this.requestUpdate(); + } + } + + return RouterMixinClass as unknown as Constructor & T; +}