@rexezuge/web 0.0.0-stage → 1.0.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/LICENSE +21 -0
- package/dist/Badge.d.ts +42 -0
- package/dist/Badge.d.ts.map +1 -0
- package/dist/Badge.js +25 -0
- package/dist/Badge.js.map +1 -0
- package/dist/Button.d.ts +69 -0
- package/dist/Button.d.ts.map +1 -0
- package/dist/Button.js +39 -0
- package/dist/Button.js.map +1 -0
- package/dist/Card.d.ts +42 -0
- package/dist/Card.d.ts.map +1 -0
- package/dist/Card.js +42 -0
- package/dist/Card.js.map +1 -0
- package/dist/FocusInput.d.ts +32 -0
- package/dist/FocusInput.d.ts.map +1 -0
- package/dist/FocusInput.js +19 -0
- package/dist/FocusInput.js.map +1 -0
- package/dist/Input.d.ts +52 -0
- package/dist/Input.d.ts.map +1 -0
- package/dist/Input.js +49 -0
- package/dist/Input.js.map +1 -0
- package/dist/LoadingButton.d.ts +46 -0
- package/dist/LoadingButton.d.ts.map +1 -0
- package/dist/LoadingButton.js +30 -0
- package/dist/LoadingButton.js.map +1 -0
- package/dist/Pagination.d.ts +63 -0
- package/dist/Pagination.d.ts.map +1 -0
- package/dist/Pagination.js +75 -0
- package/dist/Pagination.js.map +1 -0
- package/dist/Spinner.d.ts +31 -0
- package/dist/Spinner.d.ts.map +1 -0
- package/dist/Spinner.js +14 -0
- package/dist/Spinner.js.map +1 -0
- package/dist/api.d.ts +132 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +258 -0
- package/dist/api.js.map +1 -0
- package/dist/constants.d.ts +49 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +49 -0
- package/dist/constants.js.map +1 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/theme.d.ts +79 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +134 -0
- package/dist/theme.js.map +1 -0
- package/dist/useCurrentUser.d.ts +67 -0
- package/dist/useCurrentUser.d.ts.map +1 -0
- package/dist/useCurrentUser.js +49 -0
- package/dist/useCurrentUser.js.map +1 -0
- package/dist/useNotice.d.ts +60 -0
- package/dist/useNotice.d.ts.map +1 -0
- package/dist/useNotice.js +34 -0
- package/dist/useNotice.js.map +1 -0
- package/dist/useSpaLanguage.d.ts +81 -0
- package/dist/useSpaLanguage.d.ts.map +1 -0
- package/dist/useSpaLanguage.js +149 -0
- package/dist/useSpaLanguage.js.map +1 -0
- package/dist/utils.d.ts +53 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +346 -0
- package/dist/utils.js.map +1 -0
- package/package.json +51 -3
- package/README.md +0 -3
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
import { NOTICE_TIMEOUT_MS } from './constants';
|
|
3
|
+
function useNotice({ dismissAfterMs = NOTICE_TIMEOUT_MS, initial = null } = {}) {
|
|
4
|
+
const [notice, setNotice] = useState(initial);
|
|
5
|
+
const timerRef = useRef(null);
|
|
6
|
+
const clearPendingDismissal = useCallback(() => {
|
|
7
|
+
if (timerRef.current === null) {
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
clearTimeout(timerRef.current);
|
|
11
|
+
timerRef.current = null;
|
|
12
|
+
}, []);
|
|
13
|
+
const dismissNotice = useCallback(() => {
|
|
14
|
+
clearPendingDismissal();
|
|
15
|
+
setNotice(null);
|
|
16
|
+
}, [clearPendingDismissal]);
|
|
17
|
+
const showNotice = useCallback((kind, message) => {
|
|
18
|
+
// Replace the pending dismissal rather than stacking a second timer: two
|
|
19
|
+
// live timers mean the first notice's timeout fires while the second is
|
|
20
|
+
// still on screen.
|
|
21
|
+
clearPendingDismissal();
|
|
22
|
+
setNotice({ kind, message });
|
|
23
|
+
timerRef.current = setTimeout(() => {
|
|
24
|
+
timerRef.current = null;
|
|
25
|
+
setNotice(null);
|
|
26
|
+
}, dismissAfterMs);
|
|
27
|
+
}, [clearPendingDismissal, dismissAfterMs]);
|
|
28
|
+
// Unmounting cancels the dismissal. Without this the timer outlives the
|
|
29
|
+
// component and calls `setState` on a tree that is gone.
|
|
30
|
+
useEffect(() => clearPendingDismissal, [clearPendingDismissal]);
|
|
31
|
+
return { notice, showNotice, dismissNotice };
|
|
32
|
+
}
|
|
33
|
+
export { useNotice };
|
|
34
|
+
//# sourceMappingURL=useNotice.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useNotice.js","sourceRoot":"","sources":["../src/useNotice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACjE,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAgEhD,SAAS,SAAS,CAAC,EAAE,cAAc,GAAG,iBAAiB,EAAE,OAAO,GAAG,IAAI,KAAiC,EAAE;IACxG,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAAG,QAAQ,CAAgB,OAAO,CAAC,CAAC;IAC7D,MAAM,QAAQ,GAAG,MAAM,CAAuC,IAAI,CAAC,CAAC;IAEpE,MAAM,qBAAqB,GAAG,WAAW,CAAC,GAAG,EAAE;QAC7C,IAAI,QAAQ,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;YAC9B,OAAO;QACT,CAAC;QACD,YAAY,CAAC,QAAQ,CAAC,OAAO,CAAC,CAAC;QAC/B,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAC;IAC1B,CAAC,EAAE,EAAE,CAAC,CAAC;IAEP,MAAM,aAAa,GAAG,WAAW,CAAC,GAAG,EAAE;QACrC,qBAAqB,EAAE,CAAC;QACxB,SAAS,CAAC,IAAI,CAAC,CAAC;IAClB,CAAC,EAAE,CAAC,qBAAqB,CAAC,CAAC,CAAC;IAE5B,MAAM,UAAU,GAAG,WAAW,CAC5B,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE;QAChB,yEAAyE;QACzE,wEAAwE;QACxE,mBAAmB;QACnB,qBAAqB,EAAE,CAAC;QACxB,SAAS,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC;QAC7B,QAAQ,CAAC,OAAO,GAAG,UAAU,CAAC,GAAG,EAAE;YACjC,QAAQ,CAAC,OAAO,GAAG,IAAI,CAAC;YACxB,SAAS,CAAC,IAAI,CAAC,CAAC;QAClB,CAAC,EAAE,cAAc,CAAC,CAAC;IACrB,CAAC,EACD,CAAC,qBAAqB,EAAE,cAAc,CAAC,CACxC,CAAC;IAEF,wEAAwE;IACxE,yDAAyD;IACzD,SAAS,CAAC,GAAG,EAAE,CAAC,qBAAqB,EAAE,CAAC,qBAAqB,CAAC,CAAC,CAAC;IAEhE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC;AAC/C,CAAC;AAED,OAAO,EAAE,SAAS,EAAE,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { canonicalizeLanguageTag } from '@rexezuge/shared';
|
|
2
|
+
import type { LanguageTag } from '@rexezuge/shared';
|
|
3
|
+
/**
|
|
4
|
+
* The language the SPA is rendering, and the setter that changes it.
|
|
5
|
+
*
|
|
6
|
+
* ## Provenance
|
|
7
|
+
*
|
|
8
|
+
* `hooks/useSpaLanguage.ts` in **AWS-AccessBridge**, **Durable-DAV**,
|
|
9
|
+
* **Durable-DAV-Router**, **Edge-Git**, **Edge-Sonic** and **Mail-Otter** — six
|
|
10
|
+
* copies that agree on the detection precedence and disagree on everything
|
|
11
|
+
* else, because each one also owned its app's i18next wiring:
|
|
12
|
+
*
|
|
13
|
+
* - **Detection precedence.** All six: backend `preferredLanguage` when present
|
|
14
|
+
* > `localStorage` > `navigator.language` > `en`, with the *first present
|
|
15
|
+
* source* winning rather than the first *supported* one — AWS's
|
|
16
|
+
* `lib/language.ts` spells out why: an unsupported backend value falls to
|
|
17
|
+
* `en` rather than to the next source, because the backend value is the
|
|
18
|
+
* user's own choice and silently replacing it with whatever the browser
|
|
19
|
+
* reports would flip the UI the moment the profile loaded.
|
|
20
|
+
* - **`<html lang>` sync**, in all six, guarded because a non-browser
|
|
21
|
+
* environment has no `document`.
|
|
22
|
+
* - **Bundle loading, `languageChanged` bridging, and the backend write** —
|
|
23
|
+
* Mail-Otter/AWS `await updatePreferredLanguage(...)`, the others
|
|
24
|
+
* localStorage-only. These are *left to the app*: the kit hook keeps
|
|
25
|
+
* i18next out so it can be exercised without an i18n instance, and a caller
|
|
26
|
+
* that wants the authoritative save calls its own API from its `setLanguage`
|
|
27
|
+
* wrapper.
|
|
28
|
+
* - **`languageStatus`/`languagePending`** — left out for the same reason: both
|
|
29
|
+
* exist to describe *bundle* loading, which the app now owns.
|
|
30
|
+
*
|
|
31
|
+
* ## Normalization
|
|
32
|
+
*
|
|
33
|
+
* The hook returns `{ language, setLanguage }`. `setLanguage` is synchronous and
|
|
34
|
+
* total: it resolves the tag, persists it, and syncs `<html lang>`; the caller
|
|
35
|
+
* awaits its own bundle load before or after calling it. `supported` comes back
|
|
36
|
+
* too, so a `LanguageSelector` does not need a second import to render the list.
|
|
37
|
+
*/
|
|
38
|
+
/** The tags offered when a caller does not say. English-only is the safe default. */
|
|
39
|
+
declare const DEFAULT_SUPPORTED_LANGUAGES: readonly LanguageTag[];
|
|
40
|
+
/** The tag used when nothing supported can be resolved. */
|
|
41
|
+
declare const DEFAULT_FALLBACK_LANGUAGE: LanguageTag;
|
|
42
|
+
/** The `localStorage` key used when a caller does not say. */
|
|
43
|
+
declare const DEFAULT_LANGUAGE_STORAGE_KEY = "language";
|
|
44
|
+
/**
|
|
45
|
+
* Resolves a tag against the supported list: an exact match wins, then the
|
|
46
|
+
* primary subtag (`de-DE` resolves to a supported `de`), then the fallback.
|
|
47
|
+
*
|
|
48
|
+
* The prefix step is why a browser reporting `en-US` does not defeat an
|
|
49
|
+
* English-only app, and why a browser reporting `zh-TW` does not land on
|
|
50
|
+
* `zh-CN` when both are supported.
|
|
51
|
+
*/
|
|
52
|
+
declare function resolveLanguageTag(value: string | null | undefined, supported: readonly LanguageTag[], fallback: LanguageTag): LanguageTag;
|
|
53
|
+
interface UseSpaLanguageOptions {
|
|
54
|
+
/**
|
|
55
|
+
* The backend-authoritative preference (a user's `preferredLanguage`), or
|
|
56
|
+
* `null`/`undefined` while the profile is not known. Adopted when it changes.
|
|
57
|
+
*/
|
|
58
|
+
preferred?: string | null;
|
|
59
|
+
/** The tags the app ships bundles for. Memoize it: it is a dependency. */
|
|
60
|
+
supported?: readonly LanguageTag[];
|
|
61
|
+
/** The `localStorage` key the choice is persisted under. */
|
|
62
|
+
storageKey?: string;
|
|
63
|
+
/** The tag used when nothing supported resolves. Defaults to `en`. */
|
|
64
|
+
fallbackLanguage?: LanguageTag;
|
|
65
|
+
}
|
|
66
|
+
interface UseSpaLanguageResult {
|
|
67
|
+
/** The tag currently in effect — a member of `supported`. */
|
|
68
|
+
language: LanguageTag;
|
|
69
|
+
/**
|
|
70
|
+
* Switches the language: resolves the tag, persists it, and syncs
|
|
71
|
+
* `<html lang>`. An unsupported tag resolves to the fallback rather than
|
|
72
|
+
* being ignored, so a mistake is visible instead of silent.
|
|
73
|
+
*/
|
|
74
|
+
setLanguage: (tag: string) => void;
|
|
75
|
+
/** The tags offered, as resolved — render this list in a selector. */
|
|
76
|
+
supported: readonly LanguageTag[];
|
|
77
|
+
}
|
|
78
|
+
declare function useSpaLanguage({ preferred, supported, storageKey, fallbackLanguage, }?: Readonly<UseSpaLanguageOptions>): UseSpaLanguageResult;
|
|
79
|
+
export { canonicalizeLanguageTag, DEFAULT_FALLBACK_LANGUAGE, DEFAULT_LANGUAGE_STORAGE_KEY, DEFAULT_SUPPORTED_LANGUAGES, resolveLanguageTag, useSpaLanguage, };
|
|
80
|
+
export type { LanguageTag, UseSpaLanguageOptions, UseSpaLanguageResult };
|
|
81
|
+
//# sourceMappingURL=useSpaLanguage.d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oBAAoB,CAoD7D;AAED,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,4BAA4B,EAC5B,2BAA2B,EAC3B,kBAAkB,EAClB,cAAc,GACf,CAAC;AACF,YAAY,EAAE,WAAW,EAAE,qBAAqB,EAAE,oBAAoB,EAAE,CAAC"}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useState } from 'react';
|
|
2
|
+
import { canonicalizeLanguageTag } from '@rexezuge/shared';
|
|
3
|
+
/**
|
|
4
|
+
* The language the SPA is rendering, and the setter that changes it.
|
|
5
|
+
*
|
|
6
|
+
* ## Provenance
|
|
7
|
+
*
|
|
8
|
+
* `hooks/useSpaLanguage.ts` in **AWS-AccessBridge**, **Durable-DAV**,
|
|
9
|
+
* **Durable-DAV-Router**, **Edge-Git**, **Edge-Sonic** and **Mail-Otter** — six
|
|
10
|
+
* copies that agree on the detection precedence and disagree on everything
|
|
11
|
+
* else, because each one also owned its app's i18next wiring:
|
|
12
|
+
*
|
|
13
|
+
* - **Detection precedence.** All six: backend `preferredLanguage` when present
|
|
14
|
+
* > `localStorage` > `navigator.language` > `en`, with the *first present
|
|
15
|
+
* source* winning rather than the first *supported* one — AWS's
|
|
16
|
+
* `lib/language.ts` spells out why: an unsupported backend value falls to
|
|
17
|
+
* `en` rather than to the next source, because the backend value is the
|
|
18
|
+
* user's own choice and silently replacing it with whatever the browser
|
|
19
|
+
* reports would flip the UI the moment the profile loaded.
|
|
20
|
+
* - **`<html lang>` sync**, in all six, guarded because a non-browser
|
|
21
|
+
* environment has no `document`.
|
|
22
|
+
* - **Bundle loading, `languageChanged` bridging, and the backend write** —
|
|
23
|
+
* Mail-Otter/AWS `await updatePreferredLanguage(...)`, the others
|
|
24
|
+
* localStorage-only. These are *left to the app*: the kit hook keeps
|
|
25
|
+
* i18next out so it can be exercised without an i18n instance, and a caller
|
|
26
|
+
* that wants the authoritative save calls its own API from its `setLanguage`
|
|
27
|
+
* wrapper.
|
|
28
|
+
* - **`languageStatus`/`languagePending`** — left out for the same reason: both
|
|
29
|
+
* exist to describe *bundle* loading, which the app now owns.
|
|
30
|
+
*
|
|
31
|
+
* ## Normalization
|
|
32
|
+
*
|
|
33
|
+
* The hook returns `{ language, setLanguage }`. `setLanguage` is synchronous and
|
|
34
|
+
* total: it resolves the tag, persists it, and syncs `<html lang>`; the caller
|
|
35
|
+
* awaits its own bundle load before or after calling it. `supported` comes back
|
|
36
|
+
* too, so a `LanguageSelector` does not need a second import to render the list.
|
|
37
|
+
*/
|
|
38
|
+
/** The tags offered when a caller does not say. English-only is the safe default. */
|
|
39
|
+
const DEFAULT_SUPPORTED_LANGUAGES = ['en'];
|
|
40
|
+
/** The tag used when nothing supported can be resolved. */
|
|
41
|
+
const DEFAULT_FALLBACK_LANGUAGE = 'en';
|
|
42
|
+
/** The `localStorage` key used when a caller does not say. */
|
|
43
|
+
const DEFAULT_LANGUAGE_STORAGE_KEY = 'language';
|
|
44
|
+
/** The supported list in canonical form, computed once per render of the hook. */
|
|
45
|
+
function describeSupported(supported) {
|
|
46
|
+
return supported.map((tag) => {
|
|
47
|
+
const key = canonicalizeLanguageTag(tag);
|
|
48
|
+
return { tag, key, primary: key.split('-')[0] ?? key };
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Resolves a tag against the supported list: an exact match wins, then the
|
|
53
|
+
* primary subtag (`de-DE` resolves to a supported `de`), then the fallback.
|
|
54
|
+
*
|
|
55
|
+
* The prefix step is why a browser reporting `en-US` does not defeat an
|
|
56
|
+
* English-only app, and why a browser reporting `zh-TW` does not land on
|
|
57
|
+
* `zh-CN` when both are supported.
|
|
58
|
+
*/
|
|
59
|
+
function resolveLanguageTag(value, supported, fallback) {
|
|
60
|
+
if (typeof value !== 'string' || value.trim() === '') {
|
|
61
|
+
return fallback;
|
|
62
|
+
}
|
|
63
|
+
// The shared canonicalizer maps a degenerate tag to `en`, so the empty check
|
|
64
|
+
// above has to happen first — otherwise an empty stored value would silently
|
|
65
|
+
// resolve to `en` instead of the caller's fallback.
|
|
66
|
+
const key = canonicalizeLanguageTag(value);
|
|
67
|
+
const primary = key.split('-')[0] ?? key;
|
|
68
|
+
let prefixMatch = null;
|
|
69
|
+
for (const entry of describeSupported(supported)) {
|
|
70
|
+
if (entry.key === key) {
|
|
71
|
+
return entry.tag;
|
|
72
|
+
}
|
|
73
|
+
if (prefixMatch === null && entry.primary === primary) {
|
|
74
|
+
prefixMatch = entry.tag;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return prefixMatch ?? fallback;
|
|
78
|
+
}
|
|
79
|
+
function readStoredLanguage(storageKey) {
|
|
80
|
+
try {
|
|
81
|
+
return globalThis.localStorage?.getItem(storageKey) ?? null;
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
// Private-mode Safari and a blocked-storage context both throw. Detection
|
|
85
|
+
// carries on without the stored value; the language still works.
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
function writeStoredLanguage(storageKey, language) {
|
|
90
|
+
try {
|
|
91
|
+
globalThis.localStorage?.setItem(storageKey, language);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// Non-fatal: the selection still applies to this session, it just does not
|
|
95
|
+
// survive a reload.
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function readNavigatorLanguage() {
|
|
99
|
+
try {
|
|
100
|
+
return globalThis.navigator?.language ?? null;
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
function useSpaLanguage({ preferred, supported = DEFAULT_SUPPORTED_LANGUAGES, storageKey = DEFAULT_LANGUAGE_STORAGE_KEY, fallbackLanguage = DEFAULT_FALLBACK_LANGUAGE, } = {}) {
|
|
107
|
+
// Serialized so an inline `supported` array literal does not change identity
|
|
108
|
+
// on every render and re-run the effects below. Tags never contain spaces.
|
|
109
|
+
const supportedKey = supported.join(' ');
|
|
110
|
+
const supportedLanguages = useMemo(() => (supportedKey === '' ? [] : supportedKey.split(' ')), [supportedKey]);
|
|
111
|
+
const [language, setLanguageState] = useState(() =>
|
|
112
|
+
// Precedence: backend > stored > navigator > fallback. The backend value is
|
|
113
|
+
// undefined on a first paint and arrives later, which is what the effect
|
|
114
|
+
// below is for.
|
|
115
|
+
resolveLanguageTag(preferred ?? readStoredLanguage(storageKey) ?? readNavigatorLanguage(), supportedLanguages, fallbackLanguage));
|
|
116
|
+
// Adopt the backend preference once it is known. `preferred` is the
|
|
117
|
+
// authoritative source, so it overrides a value detection picked earlier —
|
|
118
|
+
// which is also what makes a returning user land on the language they chose
|
|
119
|
+
// on another device.
|
|
120
|
+
useEffect(() => {
|
|
121
|
+
if (preferred === undefined || preferred === null || preferred.trim() === '') {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const next = resolveLanguageTag(preferred, supportedLanguages, fallbackLanguage);
|
|
125
|
+
setLanguageState((current) => (current === next ? current : next));
|
|
126
|
+
}, [preferred, supportedLanguages, fallbackLanguage]);
|
|
127
|
+
// `<html lang>` drives hyphenation, font choice and screen-reader
|
|
128
|
+
// pronunciation, so it has to follow the tag even when the change came from
|
|
129
|
+
// detection rather than from `setLanguage`.
|
|
130
|
+
useEffect(() => {
|
|
131
|
+
try {
|
|
132
|
+
document.documentElement.lang = language;
|
|
133
|
+
}
|
|
134
|
+
catch {
|
|
135
|
+
// No DOM: server rendering, or a worker-ish test environment.
|
|
136
|
+
}
|
|
137
|
+
}, [language]);
|
|
138
|
+
const setLanguage = useCallback((tag) => {
|
|
139
|
+
const next = resolveLanguageTag(tag, supportedLanguages, fallbackLanguage);
|
|
140
|
+
setLanguageState((current) => (current === next ? current : next));
|
|
141
|
+
// Persisted only on an explicit choice. Writing on mount would turn "the
|
|
142
|
+
// browser happened to report `de`" into a stored preference, which is a
|
|
143
|
+
// different claim.
|
|
144
|
+
writeStoredLanguage(storageKey, next);
|
|
145
|
+
}, [supportedLanguages, fallbackLanguage, storageKey]);
|
|
146
|
+
return { language, setLanguage, supported: supportedLanguages };
|
|
147
|
+
}
|
|
148
|
+
export { canonicalizeLanguageTag, DEFAULT_FALLBACK_LANGUAGE, DEFAULT_LANGUAGE_STORAGE_KEY, DEFAULT_SUPPORTED_LANGUAGES, resolveLanguageTag, useSpaLanguage, };
|
|
149
|
+
//# sourceMappingURL=useSpaLanguage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSpaLanguage.js","sourceRoot":"","sources":["../src/useSpaLanguage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAClE,OAAO,EAAE,uBAAuB,EAAE,MAAM,kBAAkB,CAAC;AAG3D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,qFAAqF;AACrF,MAAM,2BAA2B,GAA2B,CAAC,IAAI,CAAC,CAAC;AAEnE,2DAA2D;AAC3D,MAAM,yBAAyB,GAAgB,IAAI,CAAC;AAEpD,8DAA8D;AAC9D,MAAM,4BAA4B,GAAG,UAAU,CAAC;AAYhD,kFAAkF;AAClF,SAAS,iBAAiB,CAAC,SAAiC;IAC1D,OAAO,SAAS,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE;QAC3B,MAAM,GAAG,GAAG,uBAAuB,CAAC,GAAG,CAAC,CAAC;QACzC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,GAAG,EAAE,CAAC;IACzD,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,kBAAkB,CAAC,KAAgC,EAAE,SAAiC,EAAE,QAAqB;IACpH,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,IAAI,EAAE,KAA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CAAC,CAAC,CAAC;IAEtD,kEAAkE;IAClE,4EAA4E;IAC5E,4CAA4C;IAC5C,SAAS,CAAC,GAAG,EAAE;QACb,IAAI,CAAC;YACH,QAAQ,CAAC,eAAe,CAAC,IAAI,GAAG,QAAQ,CAAC;QAC3C,CAAC;QAAC,MAAM,CAAC;YACP,8DAA8D;QAChE,CAAC;IACH,CAAC,EAAE,CAAC,QAAQ,CAAC,CAAC,CAAC;IAEf,MAAM,WAAW,GAAG,WAAW,CAC7B,CAAC,GAAW,EAAE,EAAE;QACd,MAAM,IAAI,GAAG,kBAAkB,CAAC,GAAG,EAAE,kBAAkB,EAAE,gBAAgB,CAAC,CAAC;QAC3E,gBAAgB,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,IAAI,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC;QACnE,yEAAyE;QACzE,wEAAwE;QACxE,mBAAmB;QACnB,mBAAmB,CAAC,UAAU,EAAE,IAAI,CAAC,CAAC;IACxC,CAAC,EACD,CAAC,kBAAkB,EAAE,gBAAgB,EAAE,UAAU,CAAC,CACnD,CAAC;IAEF,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,EAAE,kBAAkB,EAAE,CAAC;AAClE,CAAC;AAED,OAAO,EACL,uBAAuB,EACvB,yBAAyB,EACzB,4BAA4B,EAC5B,2BAA2B,EAC3B,kBAAkB,EAClB,cAAc,GACf,CAAC"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Class-name composition.
|
|
3
|
+
*
|
|
4
|
+
* ## Provenance
|
|
5
|
+
*
|
|
6
|
+
* `lib/utils.ts` in **CalDAV-Bridge**, **Durable-DAV**, **Durable-DAV-Router**,
|
|
7
|
+
* **Edge-Git**, **Edge-Sonic** and **Mail-Otter** — six byte-identical copies of
|
|
8
|
+
*
|
|
9
|
+
* ```ts
|
|
10
|
+
* export function cn(...inputs: ClassValue[]) {
|
|
11
|
+
* return twMerge(clsx(inputs));
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* The kit cannot import `clsx` or `tailwind-merge` (this package declares no
|
|
16
|
+
* dependency on either, and the consumers' own copies stay theirs), so the same
|
|
17
|
+
* contract is implemented here in-dependency-free form:
|
|
18
|
+
*
|
|
19
|
+
* - **clsx semantics** — strings, numbers, arrays and `{ [class]: active }`
|
|
20
|
+
* dictionaries are flattened, and falsy entries are dropped.
|
|
21
|
+
* - **tailwind-merge semantics, for a documented subset** — when a later class
|
|
22
|
+
* belongs to the same utility family as an earlier one, the earlier one is
|
|
23
|
+
* dropped, so `<Button className="px-6">` really does override the base
|
|
24
|
+
* `px-4` instead of shipping both and letting stylesheet order decide.
|
|
25
|
+
*
|
|
26
|
+
* The merged families are the ones whose value is a length or a count —
|
|
27
|
+
* spacing, sizing, border width, radius, transitions — plus the single-word
|
|
28
|
+
* utilities (position, display, flex direction, justification, alignment, text
|
|
29
|
+
* alignment, font weight). Families whose suffix is *not* a value
|
|
30
|
+
* (`text-red-500`, `bg-[var(--color-accent)]`, `border-white`) are left in
|
|
31
|
+
* source order on purpose: telling `text-sm` from `text-red-500` takes
|
|
32
|
+
* tailwind-merge's full class table, and guessing wrong would silently drop a
|
|
33
|
+
* colour. A consumer that needs the complete table can wrap this one:
|
|
34
|
+
* `const cn = (...values: ClassValue[]) => twMerge(kitCn(...values))`.
|
|
35
|
+
*/
|
|
36
|
+
/** Everything `cn` accepts, mirroring clsx's `ClassValue`. */
|
|
37
|
+
type ClassValue = string | number | boolean | null | undefined | ClassDictionary | readonly ClassValue[];
|
|
38
|
+
/** `{ 'px-4': isActive }` — a class is present when its value is truthy. */
|
|
39
|
+
interface ClassDictionary {
|
|
40
|
+
[className: string]: boolean | null | undefined;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Joins class names, dropping falsy entries, exact duplicates, and superseded
|
|
44
|
+
* utilities.
|
|
45
|
+
*
|
|
46
|
+
* ```ts
|
|
47
|
+
* cn('px-4', undefined, ['py-2', { 'font-bold': isActive }], 'px-6'); // 'py-2 px-6'
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
declare function cn(...inputs: ClassValue[]): string;
|
|
51
|
+
export { cn };
|
|
52
|
+
export type { ClassDictionary, ClassValue };
|
|
53
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AAEH,8DAA8D;AAC9D,KAAK,UAAU,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,GAAG,eAAe,GAAG,SAAS,UAAU,EAAE,CAAC;AAEzG,4EAA4E;AAC5E,UAAU,eAAe;IACvB,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC;CACjD;AAkTD;;;;;;;GAOG;AACH,iBAAS,EAAE,CAAC,GAAG,MAAM,EAAE,UAAU,EAAE,GAAG,MAAM,CAI3C;AAED,OAAO,EAAE,EAAE,EAAE,CAAC;AACd,YAAY,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC"}
|