-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui/element/i18n): localization mixin
- Loading branch information
1 parent
5f8f57a
commit 999e1b1
Showing
1 changed file
with
53 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {l10n} from '@alwatr/i18n'; | ||
import {LitElement} from 'lit'; | ||
|
||
import type {Constructor} from '../type.js'; | ||
import type {ListenerInterface} from '@alwatr/signal'; | ||
|
||
export declare class I18nResourceChangeMixinInterface extends LitElement { | ||
private __i18nResourceChangeMixinListener?: ListenerInterface<'l10n-resource-change'>; | ||
} | ||
export declare class I18nLocaleChangeMixinInterface extends LitElement { | ||
private __i18nLocaleChangeMixinListener?: ListenerInterface<'locale-change'>; | ||
} | ||
|
||
export function I18nResourceChangeMixin<ClassType extends Constructor<LitElement>>( | ||
superClass: ClassType, | ||
): Constructor<I18nResourceChangeMixinInterface> & ClassType { | ||
class I18nResourceChangeMixinClass extends superClass { | ||
private __i18nResourceChangeMixinListener = l10n.resourceChangeSignal.addListener(() => this.requestUpdate()); | ||
|
||
override disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
|
||
this.__i18nResourceChangeMixinListener.remove(); | ||
} | ||
} | ||
|
||
return I18nResourceChangeMixinClass as unknown as Constructor<I18nResourceChangeMixinInterface> & ClassType; | ||
} | ||
export function I18nLocaleChangeMixin<ClassType extends Constructor<LitElement>>( | ||
superClass: ClassType, | ||
): Constructor<I18nLocaleChangeMixinInterface> & ClassType { | ||
class I18nLocaleChangeMixinClass extends superClass { | ||
private __i18nLocaleChangeMixinListener = l10n.localeChangeSignal.addListener((locale) => { | ||
this.dir = locale.direction; | ||
}); | ||
|
||
override connectedCallback(): void { | ||
super.connectedCallback(); | ||
|
||
if (l10n.localeChangeSignal.value != null) { | ||
this.dir = l10n.localeChangeSignal.value.direction; | ||
} | ||
} | ||
|
||
override disconnectedCallback(): void { | ||
super.disconnectedCallback(); | ||
|
||
this.__i18nLocaleChangeMixinListener.remove(); | ||
} | ||
} | ||
|
||
return I18nLocaleChangeMixinClass as unknown as Constructor<I18nLocaleChangeMixinInterface> & ClassType; | ||
} |