@molecule/app-angular 3.3.1 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3880 -1264
- package/dist/hooks/external-store.d.ts +32 -0
- package/dist/hooks/external-store.d.ts.map +1 -0
- package/dist/hooks/external-store.js +62 -0
- package/dist/hooks/external-store.js.map +1 -0
- package/dist/hooks/index.d.ts +24 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/index.js +24 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/use-ai-models.d.ts +46 -0
- package/dist/hooks/use-ai-models.d.ts.map +1 -0
- package/dist/hooks/use-ai-models.js +142 -0
- package/dist/hooks/use-ai-models.js.map +1 -0
- package/dist/hooks/use-async.d.ts +125 -0
- package/dist/hooks/use-async.d.ts.map +1 -0
- package/dist/hooks/use-async.js +189 -0
- package/dist/hooks/use-async.js.map +1 -0
- package/dist/hooks/use-auth-flows.d.ts +207 -0
- package/dist/hooks/use-auth-flows.d.ts.map +1 -0
- package/dist/hooks/use-auth-flows.js +460 -0
- package/dist/hooks/use-auth-flows.js.map +1 -0
- package/dist/hooks/use-auth.d.ts +80 -0
- package/dist/hooks/use-auth.d.ts.map +1 -0
- package/dist/hooks/use-auth.js +89 -0
- package/dist/hooks/use-auth.js.map +1 -0
- package/dist/hooks/use-forms.d.ts +177 -0
- package/dist/hooks/use-forms.d.ts.map +1 -0
- package/dist/hooks/use-forms.js +185 -0
- package/dist/hooks/use-forms.js.map +1 -0
- package/dist/hooks/use-http.d.ts +155 -0
- package/dist/hooks/use-http.d.ts.map +1 -0
- package/dist/hooks/use-http.js +212 -0
- package/dist/hooks/use-http.js.map +1 -0
- package/dist/hooks/use-ide.d.ts +208 -0
- package/dist/hooks/use-ide.d.ts.map +1 -0
- package/dist/hooks/use-ide.js +217 -0
- package/dist/hooks/use-ide.js.map +1 -0
- package/dist/hooks/use-logger.d.ts +40 -0
- package/dist/hooks/use-logger.d.ts.map +1 -0
- package/dist/hooks/use-logger.js +56 -0
- package/dist/hooks/use-logger.js.map +1 -0
- package/dist/hooks/use-native.d.ts +213 -0
- package/dist/hooks/use-native.d.ts.map +1 -0
- package/dist/hooks/use-native.js +158 -0
- package/dist/hooks/use-native.js.map +1 -0
- package/dist/hooks/use-profile-form.d.ts +113 -0
- package/dist/hooks/use-profile-form.d.ts.map +1 -0
- package/dist/hooks/use-profile-form.js +253 -0
- package/dist/hooks/use-profile-form.js.map +1 -0
- package/dist/hooks/use-router.d.ts +212 -0
- package/dist/hooks/use-router.d.ts.map +1 -0
- package/dist/hooks/use-router.js +251 -0
- package/dist/hooks/use-router.js.map +1 -0
- package/dist/hooks/use-storage.d.ts +128 -0
- package/dist/hooks/use-storage.d.ts.map +1 -0
- package/dist/hooks/use-storage.js +217 -0
- package/dist/hooks/use-storage.js.map +1 -0
- package/dist/hooks/use-store.d.ts +74 -0
- package/dist/hooks/use-store.d.ts.map +1 -0
- package/dist/hooks/use-store.js +103 -0
- package/dist/hooks/use-store.js.map +1 -0
- package/dist/hooks/use-theme.d.ts +59 -0
- package/dist/hooks/use-theme.d.ts.map +1 -0
- package/dist/hooks/use-theme.js +117 -0
- package/dist/hooks/use-theme.js.map +1 -0
- package/dist/hooks/use-translation.d.ts +86 -0
- package/dist/hooks/use-translation.d.ts.map +1 -0
- package/dist/hooks/use-translation.js +146 -0
- package/dist/hooks/use-translation.js.map +1 -0
- package/dist/hooks/use-verify-payment-return.d.ts +86 -0
- package/dist/hooks/use-verify-payment-return.d.ts.map +1 -0
- package/dist/hooks/use-verify-payment-return.js +167 -0
- package/dist/hooks/use-verify-payment-return.js.map +1 -0
- package/dist/hooks/use-version.d.ts +49 -0
- package/dist/hooks/use-version.d.ts.map +1 -0
- package/dist/hooks/use-version.js +70 -0
- package/dist/hooks/use-version.js.map +1 -0
- package/dist/i18n-reactive.d.ts +2 -5
- package/dist/i18n-reactive.d.ts.map +1 -1
- package/dist/i18n-reactive.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/inject.d.ts +24 -0
- package/dist/inject.d.ts.map +1 -0
- package/dist/inject.js +33 -0
- package/dist/inject.js.map +1 -0
- package/dist/providers.d.ts +24 -0
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +44 -3
- package/dist/providers.js.map +1 -1
- package/dist/session-ready.d.ts +41 -0
- package/dist/session-ready.d.ts.map +1 -0
- package/dist/session-ready.js +79 -0
- package/dist/session-ready.js.map +1 -0
- package/dist/site.d.ts +51 -0
- package/dist/site.d.ts.map +1 -0
- package/dist/site.js +69 -0
- package/dist/site.js.map +1 -0
- package/dist/tokens.d.ts +15 -0
- package/dist/tokens.d.ts.map +1 -1
- package/dist/tokens.js +12 -0
- package/dist/tokens.js.map +1 -1
- package/dist/types.d.ts +14 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for state stores, mirroring `useStateProvider`, `useStore`,
|
|
3
|
+
* `useSetStore` and `useStoreAction` from `@molecule/app-react`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { type Signal } from '@angular/core';
|
|
8
|
+
import type { StateProvider, Store } from '@molecule/app-state';
|
|
9
|
+
/**
|
|
10
|
+
* Options for {@link useStore}.
|
|
11
|
+
*
|
|
12
|
+
* @template T - The store's state shape.
|
|
13
|
+
* @template S - The selected value's shape.
|
|
14
|
+
*/
|
|
15
|
+
export interface UseStoreOptions<T, S = T> {
|
|
16
|
+
/**
|
|
17
|
+
* Derives the hook's value from the whole state. Receives the full state on
|
|
18
|
+
* every read.
|
|
19
|
+
*/
|
|
20
|
+
selector?: (state: T) => S;
|
|
21
|
+
/**
|
|
22
|
+
* Compares the previous selected value with the next one; an equal pair keeps
|
|
23
|
+
* the previous result. Without it, identity decides.
|
|
24
|
+
*/
|
|
25
|
+
equalityFn?: (previous: S, next: S) => boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* The state provider registered by `provideMolecule({ state })` (by default the
|
|
29
|
+
* provider bonded under `@molecule/app-state`). Call it in an injection context.
|
|
30
|
+
*
|
|
31
|
+
* Only hooks that read the registration call this; `useStore(store)` takes its
|
|
32
|
+
* store as an argument and works with no provider at all.
|
|
33
|
+
*
|
|
34
|
+
* @returns The state provider.
|
|
35
|
+
* @throws {Error} When no state provider is registered — the message names the
|
|
36
|
+
* fix (which registration to add, and the call for it).
|
|
37
|
+
*/
|
|
38
|
+
export declare function useStateProvider(): StateProvider;
|
|
39
|
+
/**
|
|
40
|
+
* Subscribes to a store with an optional selector and equality function.
|
|
41
|
+
* Call it in an injection context.
|
|
42
|
+
*
|
|
43
|
+
* The store is the argument, so this function needs NO registration: it reads
|
|
44
|
+
* `store.getState()` and subscribes with `store.subscribe()`, and never touches
|
|
45
|
+
* the state registration. A store minted outside the `state` bond — the direct
|
|
46
|
+
* `createStoreWithActions()` / `createStore()` a provider package exports — is
|
|
47
|
+
* therefore as valid here as one `@molecule/app-state`'s `createStore()` made.
|
|
48
|
+
*
|
|
49
|
+
* @param store - The store to subscribe to.
|
|
50
|
+
* @param options - Hook options (`selector`, `equalityFn`).
|
|
51
|
+
* @returns A signal of the selected state.
|
|
52
|
+
*/
|
|
53
|
+
export declare function useStore<T, S = T>(store: Store<T>, options?: UseStoreOptions<T, S>): Signal<S>;
|
|
54
|
+
/**
|
|
55
|
+
* The store's `setState` function. Call it in an injection context.
|
|
56
|
+
*
|
|
57
|
+
* @param store - The store to get `setState` from.
|
|
58
|
+
* @returns The `setState` function.
|
|
59
|
+
*/
|
|
60
|
+
export declare function useSetStore<T>(store: Store<T>): Store<T>['setState'];
|
|
61
|
+
/**
|
|
62
|
+
* A function bound to a store's `setState` and `getState`. Call it in an
|
|
63
|
+
* injection context.
|
|
64
|
+
*
|
|
65
|
+
* The action receives the store's `setState` and `getState` on every call, so
|
|
66
|
+
* it reads fresh state each time (like React's, which re-runs
|
|
67
|
+
* `action(setState, getState)` per call).
|
|
68
|
+
*
|
|
69
|
+
* @param store - The store to bind to.
|
|
70
|
+
* @param action - The action function that receives `setState` and `getState`.
|
|
71
|
+
* @returns A bound action function.
|
|
72
|
+
*/
|
|
73
|
+
export declare function useStoreAction<T, Args extends unknown[], R>(store: Store<T>, action: (setState: Store<T>['setState'], getState: Store<T>['getState']) => (...args: Args) => R): (...args: Args) => R;
|
|
74
|
+
//# sourceMappingURL=use-store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-store.d.ts","sourceRoot":"","sources":["../../src/hooks/use-store.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAU,KAAK,MAAM,EAAE,MAAM,eAAe,CAAA;AAGnD,OAAO,KAAK,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAA;AAK/D;;;;;GAKG;AACH,MAAM,WAAW,eAAe,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC;IACvC;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAA;IAC1B;;;OAGG;IACH,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,KAAK,OAAO,CAAA;CAC/C;AAiBD;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAUhD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,CAoB9F;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,CAEpE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,IAAI,SAAS,OAAO,EAAE,EAAE,CAAC,EACzD,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,EACf,MAAM,EAAE,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,GAC/F,CAAC,GAAG,IAAI,EAAE,IAAI,KAAK,CAAC,CAKtB"}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for state stores, mirroring `useStateProvider`, `useStore`,
|
|
3
|
+
* `useSetStore` and `useStoreAction` from `@molecule/app-react`.
|
|
4
|
+
*
|
|
5
|
+
* @module
|
|
6
|
+
*/
|
|
7
|
+
import { inject } from '@angular/core';
|
|
8
|
+
import { t } from '@molecule/app-i18n';
|
|
9
|
+
import { STATE_PROVIDER } from '../tokens.js';
|
|
10
|
+
import { useExternalStore } from './external-store.js';
|
|
11
|
+
/**
|
|
12
|
+
* The fix, appended to the error below — this function is the one place that
|
|
13
|
+
* READS the token, so it is the one place that has to say where the provider
|
|
14
|
+
* comes from. The sentences are code (a function call, a bootstrap array), so
|
|
15
|
+
* they are appended in English rather than translated, exactly like React's.
|
|
16
|
+
*/
|
|
17
|
+
const STATE_PROVIDER_FIX = ' To fix: register the provider before this component injects it — ' +
|
|
18
|
+
"`import { provideMolecule } from '@molecule/app-angular'`; " +
|
|
19
|
+
'provideMolecule passes the provider bonded under `@molecule/app-state` ' +
|
|
20
|
+
'(bond one with `setProvider(provider)` from `@molecule/app-state`), and ' +
|
|
21
|
+
'provideMolecule({ state: provider }) passes one you hold. ' +
|
|
22
|
+
'To read a store you already have, call useStore(store) instead — it subscribes to the ' +
|
|
23
|
+
'store it is given and needs no provider.';
|
|
24
|
+
/**
|
|
25
|
+
* The state provider registered by `provideMolecule({ state })` (by default the
|
|
26
|
+
* provider bonded under `@molecule/app-state`). Call it in an injection context.
|
|
27
|
+
*
|
|
28
|
+
* Only hooks that read the registration call this; `useStore(store)` takes its
|
|
29
|
+
* store as an argument and works with no provider at all.
|
|
30
|
+
*
|
|
31
|
+
* @returns The state provider.
|
|
32
|
+
* @throws {Error} When no state provider is registered — the message names the
|
|
33
|
+
* fix (which registration to add, and the call for it).
|
|
34
|
+
*/
|
|
35
|
+
export function useStateProvider() {
|
|
36
|
+
const provider = inject(STATE_PROVIDER, { optional: true });
|
|
37
|
+
if (!provider) {
|
|
38
|
+
throw new Error(t('angular.error.useStoreOutsideProvider', undefined, {
|
|
39
|
+
defaultValue: 'useStateProvider must be used within a StateProvider',
|
|
40
|
+
}) + STATE_PROVIDER_FIX);
|
|
41
|
+
}
|
|
42
|
+
return provider;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Subscribes to a store with an optional selector and equality function.
|
|
46
|
+
* Call it in an injection context.
|
|
47
|
+
*
|
|
48
|
+
* The store is the argument, so this function needs NO registration: it reads
|
|
49
|
+
* `store.getState()` and subscribes with `store.subscribe()`, and never touches
|
|
50
|
+
* the state registration. A store minted outside the `state` bond — the direct
|
|
51
|
+
* `createStoreWithActions()` / `createStore()` a provider package exports — is
|
|
52
|
+
* therefore as valid here as one `@molecule/app-state`'s `createStore()` made.
|
|
53
|
+
*
|
|
54
|
+
* @param store - The store to subscribe to.
|
|
55
|
+
* @param options - Hook options (`selector`, `equalityFn`).
|
|
56
|
+
* @returns A signal of the selected state.
|
|
57
|
+
*/
|
|
58
|
+
export function useStore(store, options) {
|
|
59
|
+
const { selector, equalityFn } = options ?? {};
|
|
60
|
+
// The previous selected value for the equality comparison, exactly like
|
|
61
|
+
// React's ref: an equal selection keeps the previous result (its identity).
|
|
62
|
+
let prevSelected;
|
|
63
|
+
const read = () => {
|
|
64
|
+
const state = store.getState();
|
|
65
|
+
const selected = selector ? selector(state) : state;
|
|
66
|
+
if (equalityFn && prevSelected !== undefined) {
|
|
67
|
+
if (equalityFn(prevSelected, selected)) {
|
|
68
|
+
return prevSelected;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
prevSelected = selected;
|
|
72
|
+
return selected;
|
|
73
|
+
};
|
|
74
|
+
return useExternalStore(read, (onStoreChange) => store.subscribe(onStoreChange));
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* The store's `setState` function. Call it in an injection context.
|
|
78
|
+
*
|
|
79
|
+
* @param store - The store to get `setState` from.
|
|
80
|
+
* @returns The `setState` function.
|
|
81
|
+
*/
|
|
82
|
+
export function useSetStore(store) {
|
|
83
|
+
return store.setState;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* A function bound to a store's `setState` and `getState`. Call it in an
|
|
87
|
+
* injection context.
|
|
88
|
+
*
|
|
89
|
+
* The action receives the store's `setState` and `getState` on every call, so
|
|
90
|
+
* it reads fresh state each time (like React's, which re-runs
|
|
91
|
+
* `action(setState, getState)` per call).
|
|
92
|
+
*
|
|
93
|
+
* @param store - The store to bind to.
|
|
94
|
+
* @param action - The action function that receives `setState` and `getState`.
|
|
95
|
+
* @returns A bound action function.
|
|
96
|
+
*/
|
|
97
|
+
export function useStoreAction(store, action) {
|
|
98
|
+
// The action receives the store's setState and getState on every CALL, so it
|
|
99
|
+
// reads fresh state each time (React re-runs action(setState, getState) per
|
|
100
|
+
// call through its useCallback for the same reason).
|
|
101
|
+
return (...args) => action(store.setState, store.getState)(...args);
|
|
102
|
+
}
|
|
103
|
+
//# sourceMappingURL=use-store.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-store.js","sourceRoot":"","sources":["../../src/hooks/use-store.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,MAAM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AAA;YACrB,CAAC;QACH,CAAC;QACD,YAAY,GAAG,QAAQ,CAAA;QACvB,OAAO,QAAQ,CAAA;IACjB,CAAC,CAAA;IAED,OAAO,gBAAgB,CAAC,IAAI,EAAE,CAAC,aAAa,EAAE,EAAE,CAAC,KAAK,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAA;AAClF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,WAAW,CAAI,KAAe;IAC5C,OAAO,KAAK,CAAC,QAAQ,CAAA;AACvB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,cAAc,CAC5B,KAAe,EACf,MAAgG;IAEhG,6EAA6E;IAC7E,4EAA4E;IAC5E,qDAAqD;IACrD,OAAO,CAAC,GAAG,IAAU,EAAK,EAAE,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;AAC9E,CAAC"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for theming, mirroring `useTheme`, `useThemeProvider`,
|
|
3
|
+
* `useCurrentTheme`, `useThemeMode` and `useThemeColors` from
|
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Signal } from '@angular/core';
|
|
9
|
+
import type { Theme, ThemeProvider } from '@molecule/app-theme';
|
|
10
|
+
/**
|
|
11
|
+
* The theme provider registered by `provideMolecule({ theme })` or
|
|
12
|
+
* `provideTheme`. Call it in an injection context.
|
|
13
|
+
*
|
|
14
|
+
* @returns The theme provider.
|
|
15
|
+
* @throws {Error} When no theme provider is registered.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useThemeProvider(): ThemeProvider;
|
|
18
|
+
/**
|
|
19
|
+
* What {@link useTheme} returns: the current theme, its name and mode as
|
|
20
|
+
* signals, and the actions as plain functions.
|
|
21
|
+
*/
|
|
22
|
+
export interface UseThemeResult {
|
|
23
|
+
/** The current theme object. */
|
|
24
|
+
readonly theme: Signal<Theme>;
|
|
25
|
+
/** The current theme's name. */
|
|
26
|
+
readonly themeName: Signal<string>;
|
|
27
|
+
/** The current theme's mode (`'light'` or `'dark'`). */
|
|
28
|
+
readonly mode: Signal<'light' | 'dark'>;
|
|
29
|
+
/** Sets the theme by name. */
|
|
30
|
+
setTheme: (name: string) => void;
|
|
31
|
+
/** Toggles the theme mode, preferring the provider's `toggleMode()`. */
|
|
32
|
+
toggleTheme: () => void;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Theme state and actions. Call it in an injection context (a component field
|
|
36
|
+
* initializer or constructor).
|
|
37
|
+
*
|
|
38
|
+
* @returns The theme, its name and mode as signals, and the actions.
|
|
39
|
+
*/
|
|
40
|
+
export declare function useTheme(): UseThemeResult;
|
|
41
|
+
/**
|
|
42
|
+
* The current theme object alone. Call it in an injection context.
|
|
43
|
+
*
|
|
44
|
+
* @returns A signal of the current theme.
|
|
45
|
+
*/
|
|
46
|
+
export declare function useCurrentTheme(): Signal<Theme>;
|
|
47
|
+
/**
|
|
48
|
+
* The current theme mode alone. Call it in an injection context.
|
|
49
|
+
*
|
|
50
|
+
* @returns A signal of `'light'` or `'dark'`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function useThemeMode(): Signal<'light' | 'dark'>;
|
|
53
|
+
/**
|
|
54
|
+
* The current theme's color palette. Call it in an injection context.
|
|
55
|
+
*
|
|
56
|
+
* @returns A signal of the current theme's colors.
|
|
57
|
+
*/
|
|
58
|
+
export declare function useThemeColors(): Signal<Theme['colors']>;
|
|
59
|
+
//# sourceMappingURL=use-theme.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-theme.d.ts","sourceRoot":"","sources":["../../src/hooks/use-theme.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgC,KAAK,MAAM,EAAU,MAAM,eAAe,CAAA;AAEjF,OAAO,KAAK,EAAE,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAK/D;;;;;;GAMG;AACH,wBAAgB,gBAAgB,IAAI,aAAa,CAMhD;AAgCD;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,gCAAgC;IAChC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC,KAAK,CAAC,CAAA;IAC7B,gCAAgC;IAChC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IAClC,wDAAwD;IACxD,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,CAAA;IACvC,8BAA8B;IAC9B,QAAQ,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAChC,wEAAwE;IACxE,WAAW,EAAE,MAAM,IAAI,CAAA;CACxB;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,IAAI,cAAc,CA4CzC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,IAAI,MAAM,CAAC,KAAK,CAAC,CAE/C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,CAEvD;AAED;;;;GAIG;AACH,wBAAgB,cAAc,IAAI,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,CAGxD"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for theming, mirroring `useTheme`, `useThemeProvider`,
|
|
3
|
+
* `useCurrentTheme`, `useThemeMode` and `useThemeColors` from
|
|
4
|
+
* `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { computed, DestroyRef, inject, signal } from '@angular/core';
|
|
9
|
+
import { injectProvider } from '../inject.js';
|
|
10
|
+
import { THEME_PROVIDER } from '../tokens.js';
|
|
11
|
+
/**
|
|
12
|
+
* The theme provider registered by `provideMolecule({ theme })` or
|
|
13
|
+
* `provideTheme`. Call it in an injection context.
|
|
14
|
+
*
|
|
15
|
+
* @returns The theme provider.
|
|
16
|
+
* @throws {Error} When no theme provider is registered.
|
|
17
|
+
*/
|
|
18
|
+
export function useThemeProvider() {
|
|
19
|
+
return injectProvider(THEME_PROVIDER, 'useThemeOutsideProvider', 'useThemeProvider must be used within a ThemeProvider');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Subscribes to a theme provider, supporting both `subscribe` and the legacy
|
|
23
|
+
* `onThemeChange` method, exactly like React's hook.
|
|
24
|
+
*
|
|
25
|
+
* @param provider - The theme provider to subscribe to.
|
|
26
|
+
* @param callback - Listener invoked whenever the active theme changes.
|
|
27
|
+
* @returns An unsubscribe function that removes the listener.
|
|
28
|
+
*/
|
|
29
|
+
function subscribeToThemeProvider(provider, callback) {
|
|
30
|
+
if (typeof provider.subscribe === 'function') {
|
|
31
|
+
return provider.subscribe(callback);
|
|
32
|
+
}
|
|
33
|
+
if (typeof provider
|
|
34
|
+
.onThemeChange === 'function') {
|
|
35
|
+
return provider.onThemeChange(callback);
|
|
36
|
+
}
|
|
37
|
+
return () => { };
|
|
38
|
+
}
|
|
39
|
+
/** Reads the provider's current theme name, `getThemeName` when it has one. */
|
|
40
|
+
const themeNameOf = (provider) => typeof provider.getThemeName ===
|
|
41
|
+
'function'
|
|
42
|
+
? provider.getThemeName()
|
|
43
|
+
: provider.getTheme().name;
|
|
44
|
+
/**
|
|
45
|
+
* Theme state and actions. Call it in an injection context (a component field
|
|
46
|
+
* initializer or constructor).
|
|
47
|
+
*
|
|
48
|
+
* @returns The theme, its name and mode as signals, and the actions.
|
|
49
|
+
*/
|
|
50
|
+
export function useTheme() {
|
|
51
|
+
const provider = useThemeProvider();
|
|
52
|
+
// The provider as an external store, read through a cache: a provider that
|
|
53
|
+
// builds a fresh theme object per call keeps the snapshot stable between
|
|
54
|
+
// notifications.
|
|
55
|
+
const theme = signal(provider.getTheme(), /* @ts-ignore */
|
|
56
|
+
...(ngDevMode ? [{ debugName: "theme" }] : /* istanbul ignore next */ []));
|
|
57
|
+
const themeName = signal(themeNameOf(provider), /* @ts-ignore */
|
|
58
|
+
...(ngDevMode ? [{ debugName: "themeName" }] : /* istanbul ignore next */ []));
|
|
59
|
+
const refresh = () => {
|
|
60
|
+
theme.set(provider.getTheme());
|
|
61
|
+
themeName.set(themeNameOf(provider));
|
|
62
|
+
};
|
|
63
|
+
const stop = subscribeToThemeProvider(provider, refresh);
|
|
64
|
+
inject(DestroyRef).onDestroy(stop);
|
|
65
|
+
const setTheme = (name) => {
|
|
66
|
+
provider.setTheme(name);
|
|
67
|
+
};
|
|
68
|
+
const toggleTheme = () => {
|
|
69
|
+
// Prefer the provider's purpose-built light/dark toggle; the index-cycle
|
|
70
|
+
// below stays only as a fallback for providers predating `toggleMode`.
|
|
71
|
+
if (typeof provider.toggleMode === 'function') {
|
|
72
|
+
provider.toggleMode();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
const themes = provider.getThemes?.() ?? [];
|
|
76
|
+
if (themes.length === 0)
|
|
77
|
+
return;
|
|
78
|
+
const currentName = themeNameOf(provider);
|
|
79
|
+
const currentIndex = themes.findIndex((entry) => typeof entry === 'string' ? entry === currentName : entry.name === currentName);
|
|
80
|
+
const nextIndex = (currentIndex + 1) % themes.length;
|
|
81
|
+
const next = themes[nextIndex];
|
|
82
|
+
provider.setTheme(typeof next === 'string' ? next : next.name);
|
|
83
|
+
};
|
|
84
|
+
return {
|
|
85
|
+
theme: theme.asReadonly(),
|
|
86
|
+
themeName: themeName.asReadonly(),
|
|
87
|
+
mode: computed(() => theme().mode),
|
|
88
|
+
setTheme,
|
|
89
|
+
toggleTheme,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* The current theme object alone. Call it in an injection context.
|
|
94
|
+
*
|
|
95
|
+
* @returns A signal of the current theme.
|
|
96
|
+
*/
|
|
97
|
+
export function useCurrentTheme() {
|
|
98
|
+
return useTheme().theme;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The current theme mode alone. Call it in an injection context.
|
|
102
|
+
*
|
|
103
|
+
* @returns A signal of `'light'` or `'dark'`.
|
|
104
|
+
*/
|
|
105
|
+
export function useThemeMode() {
|
|
106
|
+
return useTheme().mode;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* The current theme's color palette. Call it in an injection context.
|
|
110
|
+
*
|
|
111
|
+
* @returns A signal of the current theme's colors.
|
|
112
|
+
*/
|
|
113
|
+
export function useThemeColors() {
|
|
114
|
+
const { theme } = useTheme();
|
|
115
|
+
return computed(() => theme().colors);
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=use-theme.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-theme.js","sourceRoot":"","sources":["../../src/hooks/use-theme.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,EAAe,MAAM,EAAE,MAAM,eAAe,CAAA;AAIjF,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAE7C;;;;;;GAMG;AACH,MAAM,UAAU,gBAAgB;IAC9B,OAAO,cAAc,CACnB,cAAc,EACd,yBAAyB,EACzB,sDAAsD,CACvD,CAAA;AACH,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,wBAAwB,CAAC,QAAuB,EAAE,QAAoB;IAC7E,IAAI,OAAO,QAAQ,CAAC,SAAS,KAAK,UAAU,EAAE,CAAC;QAC7C,OAAO,QAAQ,CAAC,SAAS,CAAC,QAA6C,CAAC,CAAA;IAC1E,CAAC;IACD,IACE,OAAQ,QAAuE;SAC5E,aAAa,KAAK,UAAU,EAC/B,CAAC;QACD,OACE,QACD,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAA;IAC3B,CAAC;IACD,OAAO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1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for internationalization, mirroring `useTranslation`,
|
|
3
|
+
* `useT`, `useLocale`, `useDirection`, `useI18nError` and `useI18nProvider`
|
|
4
|
+
* from `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { type Signal } from '@angular/core';
|
|
9
|
+
import type { DateFormatOptions, I18nProvider, InterpolationValues, NumberFormatOptions, TranslateOptions } from '@molecule/app-i18n';
|
|
10
|
+
/**
|
|
11
|
+
* The i18n provider registered by `provideMolecule({ i18n })` or
|
|
12
|
+
* `provideI18n`, otherwise the bonded `@molecule/app-i18n` provider — the one
|
|
13
|
+
* the static `t` reads. Call it in an injection context.
|
|
14
|
+
*
|
|
15
|
+
* @returns The i18n provider from the injector, or the bonded provider.
|
|
16
|
+
*/
|
|
17
|
+
export declare function useI18nProvider(): I18nProvider;
|
|
18
|
+
/**
|
|
19
|
+
* What {@link useTranslation} returns.
|
|
20
|
+
*/
|
|
21
|
+
export interface UseTranslationResult {
|
|
22
|
+
/**
|
|
23
|
+
* The translation function. A plain function (a signal would be read as the
|
|
24
|
+
* method itself) that reads the provider on every call AND the locale
|
|
25
|
+
* notifications signal, so a template, `computed` or `effect` that calls it
|
|
26
|
+
* re-runs on a locale switch the way React re-renders on one.
|
|
27
|
+
*/
|
|
28
|
+
readonly t: I18nProvider['t'];
|
|
29
|
+
/** The active locale code. */
|
|
30
|
+
readonly locale: Signal<string>;
|
|
31
|
+
/** Switches the active locale; resolves when the provider has switched it. */
|
|
32
|
+
setLocale: I18nProvider['setLocale'];
|
|
33
|
+
/** The available locales; a fresh list once per notification. */
|
|
34
|
+
readonly locales: Signal<I18nProvider['getLocales'] extends () => infer L ? L : never>;
|
|
35
|
+
/** Formats a number for the active locale. */
|
|
36
|
+
formatNumber: (value: number, options?: NumberFormatOptions) => string;
|
|
37
|
+
/** Formats a date for the active locale. */
|
|
38
|
+
formatDate: (value: Date | number | string, options?: DateFormatOptions) => string;
|
|
39
|
+
/** The active locale's text direction. */
|
|
40
|
+
readonly direction: Signal<'ltr' | 'rtl'>;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Translation, locale state and the formatters. Call it in an injection
|
|
44
|
+
* context (a component field initializer or constructor).
|
|
45
|
+
*
|
|
46
|
+
* @returns The translation function, the locale fields and the formatters.
|
|
47
|
+
*/
|
|
48
|
+
export declare function useTranslation(): UseTranslationResult;
|
|
49
|
+
/**
|
|
50
|
+
* The translation function alone. Call it in an injection context.
|
|
51
|
+
*
|
|
52
|
+
* The returned function keeps ONE identity for as long as the provider is
|
|
53
|
+
* unchanged; it reads the active locale and the locale notifications on
|
|
54
|
+
* every CALL, so its text is current and its caller re-runs on a switch.
|
|
55
|
+
*
|
|
56
|
+
* Call it in either of two compatible forms:
|
|
57
|
+
* - `t(key, values?, options?)` — the provider signature.
|
|
58
|
+
* - `t(key, fallback, options?)` — a string second argument is the
|
|
59
|
+
* `defaultValue`.
|
|
60
|
+
*
|
|
61
|
+
* @returns The translation function.
|
|
62
|
+
*/
|
|
63
|
+
export declare function useT(): (key: string, valuesOrDefault?: InterpolationValues | string, options?: TranslateOptions) => string;
|
|
64
|
+
/**
|
|
65
|
+
* The active locale code alone. Call it in an injection context.
|
|
66
|
+
*
|
|
67
|
+
* @returns A signal of the locale code.
|
|
68
|
+
*/
|
|
69
|
+
export declare function useLocale(): Signal<string>;
|
|
70
|
+
/**
|
|
71
|
+
* The active locale's text direction alone. Call it in an injection context.
|
|
72
|
+
*
|
|
73
|
+
* @returns A signal of `'ltr'` or `'rtl'`.
|
|
74
|
+
*/
|
|
75
|
+
export declare function useDirection(): Signal<'ltr' | 'rtl'>;
|
|
76
|
+
/**
|
|
77
|
+
* Translates an error at read time, so the displayed message follows locale
|
|
78
|
+
* changes. An `I18nError` is translated through its key (its own message as
|
|
79
|
+
* the default value); a plain `Error` returns its message unchanged; no error
|
|
80
|
+
* returns `null`. Call it in an injection context.
|
|
81
|
+
*
|
|
82
|
+
* @param error - The error to translate, or `null`/`undefined`.
|
|
83
|
+
* @returns A signal of the error's display text, or `null`.
|
|
84
|
+
*/
|
|
85
|
+
export declare function useI18nError(error: Error | null | undefined): Signal<string | null>;
|
|
86
|
+
//# sourceMappingURL=use-translation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-translation.d.ts","sourceRoot":"","sources":["../../src/hooks/use-translation.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAgC,KAAK,MAAM,EAAU,MAAM,eAAe,CAAA;AAEjF,OAAO,KAAK,EACV,iBAAiB,EACjB,YAAY,EACZ,mBAAmB,EACnB,mBAAmB,EACnB,gBAAgB,EACjB,MAAM,oBAAoB,CAAA;AAK3B;;;;;;GAMG;AACH,wBAAgB,eAAe,IAAI,YAAY,CAG9C;AAiBD;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC;;;;;OAKG;IACH,QAAQ,CAAC,CAAC,EAAE,YAAY,CAAC,GAAG,CAAC,CAAA;IAC7B,8BAA8B;IAC9B,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,CAAA;IAC/B,8EAA8E;IAC9E,SAAS,EAAE,YAAY,CAAC,WAAW,CAAC,CAAA;IACpC,iEAAiE;IACjE,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC,YAAY,CAAC,YAAY,CAAC,SAAS,MAAM,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC,CAAA;IACtF,8CAA8C;IAC9C,YAAY,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,mBAAmB,KAAK,MAAM,CAAA;IACtE,4CAA4C;IAC5C,UAAU,EAAE,CAAC,KAAK,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,OAAO,CAAC,EAAE,iBAAiB,KAAK,MAAM,CAAA;IAClF,0CAA0C;IAC1C,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,CAAA;CAC1C;AAED;;;;;GAKG;AACH,wBAAgB,cAAc,IAAI,oBAAoB,CA6CrD;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,IAAI,IAAI,CACtB,GAAG,EAAE,MAAM,EACX,eAAe,CAAC,EAAE,mBAAmB,GAAG,MAAM,EAC9C,OAAO,CAAC,EAAE,gBAAgB,KACvB,MAAM,CAcV;AAED;;;;GAIG;AACH,wBAAgB,SAAS,IAAI,MAAM,CAAC,MAAM,CAAC,CAE1C;AAED;;;;GAIG;AACH,wBAAgB,YAAY,IAAI,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,CAEpD;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,SAAS,GAAG,MAAM,CAAC,MAAM,GAAG,IAAI,CAAC,CAanF"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Angular functions for internationalization, mirroring `useTranslation`,
|
|
3
|
+
* `useT`, `useLocale`, `useDirection`, `useI18nError` and `useI18nProvider`
|
|
4
|
+
* from `@molecule/app-react`.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { computed, DestroyRef, inject, signal } from '@angular/core';
|
|
9
|
+
import { getProvider, I18nError } from '@molecule/app-i18n';
|
|
10
|
+
import { I18N_PROVIDER } from '../tokens.js';
|
|
11
|
+
/**
|
|
12
|
+
* The i18n provider registered by `provideMolecule({ i18n })` or
|
|
13
|
+
* `provideI18n`, otherwise the bonded `@molecule/app-i18n` provider — the one
|
|
14
|
+
* the static `t` reads. Call it in an injection context.
|
|
15
|
+
*
|
|
16
|
+
* @returns The i18n provider from the injector, or the bonded provider.
|
|
17
|
+
*/
|
|
18
|
+
export function useI18nProvider() {
|
|
19
|
+
const provided = inject(I18N_PROVIDER, { optional: true });
|
|
20
|
+
return provided ?? getProvider();
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Subscribes to the provider's locale notifications and returns a signal that
|
|
24
|
+
* changes on every notification (a locale switch, `addTranslations` for the
|
|
25
|
+
* active locale, a revision bump).
|
|
26
|
+
*
|
|
27
|
+
* @param provider - The i18n provider.
|
|
28
|
+
* @returns A signal that changes on every locale notification.
|
|
29
|
+
*/
|
|
30
|
+
function useLocaleNotifications(provider) {
|
|
31
|
+
const notifications = signal(0, /* @ts-ignore */
|
|
32
|
+
...(ngDevMode ? [{ debugName: "notifications" }] : /* istanbul ignore next */ []));
|
|
33
|
+
const stop = provider.onLocaleChange(() => notifications.update((count) => count + 1));
|
|
34
|
+
inject(DestroyRef).onDestroy(stop);
|
|
35
|
+
return notifications.asReadonly();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Translation, locale state and the formatters. Call it in an injection
|
|
39
|
+
* context (a component field initializer or constructor).
|
|
40
|
+
*
|
|
41
|
+
* @returns The translation function, the locale fields and the formatters.
|
|
42
|
+
*/
|
|
43
|
+
export function useTranslation() {
|
|
44
|
+
const provider = useI18nProvider();
|
|
45
|
+
const notifications = useLocaleNotifications(provider);
|
|
46
|
+
// Read from the provider on every read, never copied into state: a replaced
|
|
47
|
+
// provider shows its own values immediately.
|
|
48
|
+
const locale = computed(() => {
|
|
49
|
+
void notifications();
|
|
50
|
+
return provider.getLocale();
|
|
51
|
+
}, /* @ts-ignore */
|
|
52
|
+
...(ngDevMode ? [{ debugName: "locale" }] : /* istanbul ignore next */ []));
|
|
53
|
+
const direction = computed(() => {
|
|
54
|
+
void notifications();
|
|
55
|
+
return provider.getDirection();
|
|
56
|
+
}, /* @ts-ignore */
|
|
57
|
+
...(ngDevMode ? [{ debugName: "direction" }] : /* istanbul ignore next */ []));
|
|
58
|
+
// A new list once per notification, the same list otherwise, so a computed
|
|
59
|
+
// that depends on it stays stable.
|
|
60
|
+
const locales = computed(() => {
|
|
61
|
+
void notifications();
|
|
62
|
+
return provider.getLocales();
|
|
63
|
+
}, /* @ts-ignore */
|
|
64
|
+
...(ngDevMode ? [{ debugName: "locales" }] : /* istanbul ignore next */ []));
|
|
65
|
+
// A plain function (never a signal: the conformance kit reads a method field
|
|
66
|
+
// by calling the value itself, and an Angular signal IS a function). Reading
|
|
67
|
+
// the notifications signal inside it makes the CALLER depend on the locale, so
|
|
68
|
+
// a view that renders `t(…)` (directly or through a method) refreshes on a
|
|
69
|
+
// switch without reading `locale()` itself.
|
|
70
|
+
const t = (key, values, options) => {
|
|
71
|
+
void notifications();
|
|
72
|
+
return provider.t(key, values, options);
|
|
73
|
+
};
|
|
74
|
+
// The provider's own promise is returned (a caller may await the switch, and
|
|
75
|
+
// a rejected switch surfaces to the caller exactly like React's).
|
|
76
|
+
const setLocale = (code) => provider.setLocale(code);
|
|
77
|
+
const formatNumber = (value, options) => provider.formatNumber(value, options);
|
|
78
|
+
const formatDate = (value, options) => provider.formatDate(value, options);
|
|
79
|
+
return { t, locale, setLocale, locales, formatNumber, formatDate, direction };
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* The translation function alone. Call it in an injection context.
|
|
83
|
+
*
|
|
84
|
+
* The returned function keeps ONE identity for as long as the provider is
|
|
85
|
+
* unchanged; it reads the active locale and the locale notifications on
|
|
86
|
+
* every CALL, so its text is current and its caller re-runs on a switch.
|
|
87
|
+
*
|
|
88
|
+
* Call it in either of two compatible forms:
|
|
89
|
+
* - `t(key, values?, options?)` — the provider signature.
|
|
90
|
+
* - `t(key, fallback, options?)` — a string second argument is the
|
|
91
|
+
* `defaultValue`.
|
|
92
|
+
*
|
|
93
|
+
* @returns The translation function.
|
|
94
|
+
*/
|
|
95
|
+
export function useT() {
|
|
96
|
+
const provider = useI18nProvider();
|
|
97
|
+
const notifications = useLocaleNotifications(provider);
|
|
98
|
+
return (key, valuesOrDefault, options) => {
|
|
99
|
+
// The caller depends on the locale through this read (see useTranslation).
|
|
100
|
+
void notifications();
|
|
101
|
+
return typeof valuesOrDefault === 'string'
|
|
102
|
+
? provider.t(key, undefined, { ...options, defaultValue: valuesOrDefault })
|
|
103
|
+
: provider.t(key, valuesOrDefault, options);
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The active locale code alone. Call it in an injection context.
|
|
108
|
+
*
|
|
109
|
+
* @returns A signal of the locale code.
|
|
110
|
+
*/
|
|
111
|
+
export function useLocale() {
|
|
112
|
+
return useTranslation().locale;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The active locale's text direction alone. Call it in an injection context.
|
|
116
|
+
*
|
|
117
|
+
* @returns A signal of `'ltr'` or `'rtl'`.
|
|
118
|
+
*/
|
|
119
|
+
export function useDirection() {
|
|
120
|
+
return useTranslation().direction;
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Translates an error at read time, so the displayed message follows locale
|
|
124
|
+
* changes. An `I18nError` is translated through its key (its own message as
|
|
125
|
+
* the default value); a plain `Error` returns its message unchanged; no error
|
|
126
|
+
* returns `null`. Call it in an injection context.
|
|
127
|
+
*
|
|
128
|
+
* @param error - The error to translate, or `null`/`undefined`.
|
|
129
|
+
* @returns A signal of the error's display text, or `null`.
|
|
130
|
+
*/
|
|
131
|
+
export function useI18nError(error) {
|
|
132
|
+
const provider = useI18nProvider();
|
|
133
|
+
const notifications = useLocaleNotifications(provider);
|
|
134
|
+
return computed(() => {
|
|
135
|
+
// The notification count is the recompute key: the provider is read live
|
|
136
|
+
// inside, so the text follows locale changes like React's render-time read.
|
|
137
|
+
void notifications();
|
|
138
|
+
if (!error)
|
|
139
|
+
return null;
|
|
140
|
+
if (error instanceof I18nError) {
|
|
141
|
+
return provider.t(error.i18nKey, error.i18nValues, { defaultValue: error.message });
|
|
142
|
+
}
|
|
143
|
+
return error.message;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=use-translation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-translation.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