Skip to content

Commit

Permalink
feat(element): router mixin
Browse files Browse the repository at this point in the history
Co-authored-by: S. Amir Mohammad Najafi <[email protected]>
  • Loading branch information
alimd and njfamirm committed Feb 15, 2023
1 parent ed03cf9 commit 5e80f82
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions ui/element/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
2 changes: 1 addition & 1 deletion ui/element/src/mixins/localize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function LocalizeMixin<T extends Constructor<SignalMixinInterface>>(
}

/**
* On localization resource context updated.
* On localization resource context update.
*/
protected _l18eContextUpdated(l18eContext: L18eContext): void {
this._logger.logMethodArgs('_l18eContextUpdated', l18eContext.meta);
Expand Down
29 changes: 29 additions & 0 deletions ui/element/src/mixins/router.ts
Original file line number Diff line number Diff line change
@@ -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<T extends Constructor<SignalMixinInterface>>(
superClass: T,
): Constructor<RouterMixinInterface> & 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<RouterMixinInterface> & T;
}

0 comments on commit 5e80f82

Please sign in to comment.