@neetechs/ui 0.0.3 → 0.0.4
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 +118 -16
- package/dist/ai/ai-action-button/ai-action-button.css +36 -35
- package/dist/ai/ai-approval-card/ai-approval-card.css +20 -17
- package/dist/ai/ai-conversation-panel/ai-conversation-panel.css +70 -26
- package/dist/ai/ai-prompt-input/ai-prompt-input.css +36 -33
- package/dist/ai/ai-proposal-card/ai-proposal-card.css +17 -17
- package/dist/ai/ai-suggestion-card/ai-suggestion-card.css +27 -25
- package/dist/ai/ai-tool-call/ai-tool-call.css +15 -14
- package/dist/appearance/cache.d.ts +6 -1
- package/dist/appearance/cache.d.ts.map +1 -1
- package/dist/appearance/cache.js +57 -1
- package/dist/appearance/cache.js.map +1 -1
- package/dist/appearance/cookie.d.ts +16 -1
- package/dist/appearance/cookie.d.ts.map +1 -1
- package/dist/appearance/cookie.js +73 -4
- package/dist/appearance/cookie.js.map +1 -1
- package/dist/appearance/index.d.ts +7 -6
- package/dist/appearance/index.d.ts.map +1 -1
- package/dist/appearance/index.js +4 -4
- package/dist/appearance/index.js.map +1 -1
- package/dist/appearance/preference.d.ts +9 -1
- package/dist/appearance/preference.d.ts.map +1 -1
- package/dist/appearance/preference.js +54 -0
- package/dist/appearance/preference.js.map +1 -1
- package/dist/appearance/theme-controller.d.ts +31 -49
- package/dist/appearance/theme-controller.d.ts.map +1 -1
- package/dist/appearance/theme-controller.js +150 -145
- package/dist/appearance/theme-controller.js.map +1 -1
- package/dist/appearance/types.d.ts +29 -2
- package/dist/appearance/types.d.ts.map +1 -1
- package/dist/behaviors/announcer.d.ts +34 -0
- package/dist/behaviors/announcer.d.ts.map +1 -0
- package/dist/behaviors/announcer.js +150 -0
- package/dist/behaviors/announcer.js.map +1 -0
- package/dist/behaviors/calendar.d.ts +15 -0
- package/dist/behaviors/calendar.d.ts.map +1 -0
- package/dist/behaviors/calendar.js +77 -0
- package/dist/behaviors/calendar.js.map +1 -0
- package/dist/behaviors/data-grid.d.ts +18 -0
- package/dist/behaviors/data-grid.d.ts.map +1 -0
- package/dist/behaviors/data-grid.js +114 -0
- package/dist/behaviors/data-grid.js.map +1 -0
- package/dist/behaviors/dialog.d.ts +14 -0
- package/dist/behaviors/dialog.d.ts.map +1 -1
- package/dist/behaviors/dialog.js +24 -6
- package/dist/behaviors/dialog.js.map +1 -1
- package/dist/behaviors/direction.d.ts +10 -0
- package/dist/behaviors/direction.d.ts.map +1 -0
- package/dist/behaviors/direction.js +24 -0
- package/dist/behaviors/direction.js.map +1 -0
- package/dist/behaviors/drawer.d.ts +15 -3
- package/dist/behaviors/drawer.d.ts.map +1 -1
- package/dist/behaviors/drawer.js +25 -10
- package/dist/behaviors/drawer.js.map +1 -1
- package/dist/behaviors/dropdown.d.ts +11 -0
- package/dist/behaviors/dropdown.d.ts.map +1 -1
- package/dist/behaviors/dropdown.js +53 -11
- package/dist/behaviors/dropdown.js.map +1 -1
- package/dist/behaviors/focus-trap.d.ts.map +1 -1
- package/dist/behaviors/focus-trap.js +45 -5
- package/dist/behaviors/focus-trap.js.map +1 -1
- package/dist/behaviors/forms.d.ts +32 -0
- package/dist/behaviors/forms.d.ts.map +1 -0
- package/dist/behaviors/forms.js +147 -0
- package/dist/behaviors/forms.js.map +1 -0
- package/dist/behaviors/index.d.ts +8 -0
- package/dist/behaviors/index.d.ts.map +1 -1
- package/dist/behaviors/index.js +8 -0
- package/dist/behaviors/index.js.map +1 -1
- package/dist/behaviors/keyboard.d.ts.map +1 -1
- package/dist/behaviors/keyboard.js +4 -2
- package/dist/behaviors/keyboard.js.map +1 -1
- package/dist/behaviors/overlay.d.ts +6 -0
- package/dist/behaviors/overlay.d.ts.map +1 -1
- package/dist/behaviors/overlay.js +93 -3
- package/dist/behaviors/overlay.js.map +1 -1
- package/dist/behaviors/state.d.ts +49 -0
- package/dist/behaviors/state.d.ts.map +1 -0
- package/dist/behaviors/state.js +220 -0
- package/dist/behaviors/state.js.map +1 -0
- package/dist/behaviors/tabs.d.ts +18 -0
- package/dist/behaviors/tabs.d.ts.map +1 -0
- package/dist/behaviors/tabs.js +103 -0
- package/dist/behaviors/tabs.js.map +1 -0
- package/dist/behaviors/viewport.d.ts +40 -0
- package/dist/behaviors/viewport.d.ts.map +1 -0
- package/dist/behaviors/viewport.js +47 -0
- package/dist/behaviors/viewport.js.map +1 -0
- package/dist/components/alert/alert.css +13 -10
- package/dist/components/avatar/avatar.css +16 -15
- package/dist/components/badge/badge.css +6 -6
- package/dist/components/breadcrumb/breadcrumb.css +12 -10
- package/dist/components/button/button.css +65 -37
- package/dist/components/card/card.css +15 -14
- package/dist/components/checkbox/checkbox.css +13 -11
- package/dist/components/chip/chip.css +22 -20
- package/dist/components/data-grid/data-grid.css +167 -16
- package/dist/components/dialog/dialog.css +127 -63
- package/dist/components/drawer/drawer.css +117 -36
- package/dist/components/dropdown/dropdown.css +32 -14
- package/dist/components/icon-button/icon-button.css +41 -37
- package/dist/components/index.css +1 -2
- package/dist/components/input/input.css +10 -10
- package/dist/components/menu/menu.css +18 -17
- package/dist/components/pagination/pagination.css +13 -12
- package/dist/components/panel/panel.css +13 -12
- package/dist/components/progress/progress.css +20 -12
- package/dist/components/radio/radio.css +11 -9
- package/dist/components/segmented-control/segmented-control.css +20 -20
- package/dist/components/select/select.css +23 -9
- package/dist/components/skeleton/skeleton.css +14 -14
- package/dist/components/split-button/split-button.css +38 -31
- package/dist/components/switch/switch.css +25 -11
- package/dist/components/table/table.css +220 -0
- package/dist/components/tabs/tabs.css +36 -32
- package/dist/components/textarea/textarea.css +13 -13
- package/dist/components/toast/toast.css +43 -21
- package/dist/components/tooltip/tooltip.css +22 -19
- package/dist/domain/billing/index.css +19 -19
- package/dist/domain/calendar/agenda-list/agenda-list.css +224 -0
- package/dist/domain/calendar/day-slice/day-slice.css +18 -17
- package/dist/domain/calendar/day-timeline/day-timeline.css +42 -34
- package/dist/domain/calendar/event-card/event-card.css +20 -18
- package/dist/domain/calendar/index.css +2 -1
- package/dist/domain/calendar/month-grid/month-grid.css +34 -28
- package/dist/domain/calendar/week-grid/week-grid.css +63 -53
- package/dist/domain/companies/index.css +30 -29
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/localization/index.d.ts +8 -0
- package/dist/localization/index.d.ts.map +1 -0
- package/dist/localization/index.js +22 -0
- package/dist/localization/index.js.map +1 -0
- package/dist/patterns/command-palette/command-palette.css +39 -31
- package/dist/patterns/company-switcher/company-switcher.css +112 -85
- package/dist/patterns/content-state/content-state.css +236 -0
- package/dist/patterns/empty-state/empty-state.css +16 -15
- package/dist/patterns/error-state/error-state.css +14 -13
- package/dist/patterns/form-layout/form-layout.css +106 -0
- package/dist/patterns/index.css +8 -3
- package/dist/patterns/loading-state/loading-state.css +9 -9
- package/dist/patterns/master-detail/master-detail.css +103 -0
- package/dist/patterns/notification-center/notification-center.css +38 -33
- package/dist/patterns/page/page.css +219 -0
- package/dist/patterns/profile-menu/profile-menu.css +15 -14
- package/dist/patterns/search-bar/search-bar.css +38 -25
- package/dist/patterns/settings-layout/settings-layout.css +150 -0
- package/dist/patterns/workflow/workflow.css +168 -0
- package/dist/shell/app-shell/app-shell.css +8 -1
- package/dist/shell/header/header.css +14 -12
- package/dist/shell/index.css +2 -2
- package/dist/shell/mobile-nav/mobile-nav.css +18 -16
- package/dist/shell/sidebar/sidebar.css +10 -10
- package/dist/styles/accessibility.css +116 -0
- package/dist/styles/animations/motion.css +2 -2
- package/dist/styles/appearance/accent.css +173 -0
- package/dist/styles/appearance/density.css +61 -0
- package/dist/styles/appearance/motion.css +40 -0
- package/dist/styles/base.css +6 -5
- package/dist/styles/index.css +8 -1
- package/dist/styles/localization.css +56 -0
- package/dist/styles/reset.css +1 -0
- package/dist/styles/responsive.css +101 -0
- package/dist/styles/themes/dark.css +95 -13
- package/dist/styles/themes/high-contrast.css +182 -11
- package/dist/styles/themes/light.css +97 -15
- package/dist/styles/tokens/breakpoints.css +29 -7
- package/dist/styles/tokens/compatibility.css +33 -0
- package/dist/styles/tokens/elevation.css +1 -7
- package/dist/styles/tokens/index.css +2 -1
- package/dist/styles/tokens/radius.css +6 -1
- package/dist/styles/tokens/spacing.css +12 -6
- package/dist/styles/tokens/typography.css +3 -2
- package/dist/styles/utilities/layout.css +20 -2
- package/dist/styles/utilities/text.css +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/docs/accessibility.md +218 -0
- package/docs/appearance-and-theming.md +118 -0
- package/docs/component-architecture.md +137 -0
- package/docs/component-guidelines.md +64 -0
- package/docs/content-and-terminology.md +207 -0
- package/docs/design-system-baseline-audit.md +226 -0
- package/docs/design-token-contract.md +279 -0
- package/docs/localization-and-rtl.md +147 -0
- package/docs/migration.md +142 -0
- package/docs/page-patterns-and-workflows.md +260 -0
- package/docs/responsive-and-adaptive-design.md +351 -0
- package/docs/semantic-role-migration-audit.md +77 -0
- package/docs/states-feedback-and-recovery.md +206 -0
- package/docs/theming.md +3 -225
- package/package.json +32 -3
- package/src/ai/ai-action-button/ai-action-button.css +36 -35
- package/src/ai/ai-approval-card/ai-approval-card.css +20 -17
- package/src/ai/ai-conversation-panel/ai-conversation-panel.css +70 -26
- package/src/ai/ai-prompt-input/ai-prompt-input.css +36 -33
- package/src/ai/ai-proposal-card/ai-proposal-card.css +17 -17
- package/src/ai/ai-suggestion-card/ai-suggestion-card.css +27 -25
- package/src/ai/ai-tool-call/ai-tool-call.css +15 -14
- package/src/appearance/cache.ts +78 -2
- package/src/appearance/cookie.ts +123 -7
- package/src/appearance/index.ts +44 -2
- package/src/appearance/preference.ts +87 -1
- package/src/appearance/theme-controller.ts +221 -233
- package/src/appearance/types.ts +40 -2
- package/src/behaviors/announcer.ts +222 -0
- package/src/behaviors/calendar.ts +92 -0
- package/src/behaviors/data-grid.ts +133 -0
- package/src/behaviors/dialog.ts +42 -7
- package/src/behaviors/direction.ts +34 -0
- package/src/behaviors/drawer.ts +48 -15
- package/src/behaviors/dropdown.ts +76 -13
- package/src/behaviors/focus-trap.ts +49 -6
- package/src/behaviors/forms.ts +205 -0
- package/src/behaviors/index.ts +9 -1
- package/src/behaviors/keyboard.ts +5 -3
- package/src/behaviors/overlay.ts +116 -3
- package/src/behaviors/state.ts +282 -0
- package/src/behaviors/tabs.ts +137 -0
- package/src/behaviors/viewport.ts +61 -0
- package/src/components/alert/alert.css +13 -10
- package/src/components/avatar/avatar.css +16 -15
- package/src/components/badge/badge.css +6 -6
- package/src/components/breadcrumb/breadcrumb.css +12 -10
- package/src/components/button/button.css +65 -37
- package/src/components/card/card.css +15 -14
- package/src/components/checkbox/checkbox.css +13 -11
- package/src/components/chip/chip.css +22 -20
- package/src/components/data-grid/data-grid.css +167 -16
- package/src/components/dialog/dialog.css +127 -63
- package/src/components/drawer/drawer.css +117 -36
- package/src/components/dropdown/dropdown.css +32 -14
- package/src/components/icon-button/icon-button.css +41 -37
- package/src/components/index.css +1 -2
- package/src/components/input/input.css +10 -10
- package/src/components/menu/menu.css +18 -17
- package/src/components/pagination/pagination.css +13 -12
- package/src/components/panel/panel.css +13 -12
- package/src/components/progress/progress.css +20 -12
- package/src/components/radio/radio.css +11 -9
- package/src/components/segmented-control/segmented-control.css +20 -20
- package/src/components/select/select.css +23 -9
- package/src/components/skeleton/skeleton.css +14 -14
- package/src/components/split-button/split-button.css +38 -31
- package/src/components/switch/switch.css +25 -11
- package/src/components/table/table.css +220 -0
- package/src/components/tabs/tabs.css +36 -32
- package/src/components/textarea/textarea.css +13 -13
- package/src/components/toast/toast.css +43 -21
- package/src/components/tooltip/tooltip.css +22 -19
- package/src/domain/billing/index.css +19 -19
- package/src/domain/calendar/agenda-list/agenda-list.css +224 -0
- package/src/domain/calendar/day-slice/day-slice.css +18 -17
- package/src/domain/calendar/day-timeline/day-timeline.css +42 -34
- package/src/domain/calendar/event-card/event-card.css +20 -18
- package/src/domain/calendar/index.css +2 -1
- package/src/domain/calendar/month-grid/month-grid.css +34 -28
- package/src/domain/calendar/week-grid/week-grid.css +63 -53
- package/src/domain/companies/index.css +30 -29
- package/src/index.ts +3 -2
- package/src/localization/index.ts +56 -0
- package/src/patterns/command-palette/command-palette.css +39 -31
- package/src/patterns/company-switcher/company-switcher.css +112 -85
- package/src/patterns/content-state/content-state.css +236 -0
- package/src/patterns/empty-state/empty-state.css +16 -15
- package/src/patterns/error-state/error-state.css +14 -13
- package/src/patterns/form-layout/form-layout.css +106 -0
- package/src/patterns/index.css +8 -3
- package/src/patterns/loading-state/loading-state.css +9 -9
- package/src/patterns/master-detail/master-detail.css +103 -0
- package/src/patterns/notification-center/notification-center.css +38 -33
- package/src/patterns/page/page.css +219 -0
- package/src/patterns/profile-menu/profile-menu.css +15 -14
- package/src/patterns/search-bar/search-bar.css +38 -25
- package/src/patterns/settings-layout/settings-layout.css +150 -0
- package/src/patterns/workflow/workflow.css +168 -0
- package/src/shell/app-shell/app-shell.css +8 -1
- package/src/shell/header/header.css +14 -12
- package/src/shell/index.css +2 -2
- package/src/shell/mobile-nav/mobile-nav.css +18 -16
- package/src/shell/sidebar/sidebar.css +10 -10
- package/src/styles/accessibility.css +116 -0
- package/src/styles/animations/motion.css +2 -2
- package/src/styles/appearance/accent.css +173 -0
- package/src/styles/appearance/density.css +61 -0
- package/src/styles/appearance/motion.css +40 -0
- package/src/styles/base.css +6 -5
- package/src/styles/index.css +8 -1
- package/src/styles/localization.css +56 -0
- package/src/styles/reset.css +1 -0
- package/src/styles/responsive.css +101 -0
- package/src/styles/themes/dark.css +95 -13
- package/src/styles/themes/high-contrast.css +182 -11
- package/src/styles/themes/light.css +97 -15
- package/src/styles/tokens/breakpoints.css +29 -7
- package/src/styles/tokens/compatibility.css +33 -0
- package/src/styles/tokens/elevation.css +1 -7
- package/src/styles/tokens/index.css +2 -1
- package/src/styles/tokens/radius.css +6 -1
- package/src/styles/tokens/spacing.css +12 -6
- package/src/styles/tokens/typography.css +3 -2
- package/src/styles/utilities/layout.css +20 -2
- package/src/styles/utilities/text.css +1 -1
- package/src/types/index.ts +2 -1
- package/dist/components/modal/modal.css +0 -318
- package/dist/patterns/workspace-switcher/workspace-switcher.css +0 -380
- package/dist/shell/right-drawer/right-drawer.css +0 -108
- package/src/components/modal/modal.css +0 -318
- package/src/patterns/workspace-switcher/workspace-switcher.css +0 -380
- package/src/shell/right-drawer/right-drawer.css +0 -108
|
@@ -1,4 +1,20 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
NtAccentPreference,
|
|
3
|
+
NtAppearancePreference,
|
|
4
|
+
NtDensityPreference,
|
|
5
|
+
NtSystemPreference,
|
|
6
|
+
ResolvedTheme,
|
|
7
|
+
ThemePreference,
|
|
8
|
+
} from './types.js';
|
|
9
|
+
|
|
10
|
+
export const NT_DEFAULT_APPEARANCE_PREFERENCE: Readonly<NtAppearancePreference> =
|
|
11
|
+
Object.freeze({
|
|
12
|
+
theme: 'system',
|
|
13
|
+
accent: 'orange',
|
|
14
|
+
density: 'comfortable',
|
|
15
|
+
reduceMotion: 'system',
|
|
16
|
+
highContrast: 'system',
|
|
17
|
+
});
|
|
2
18
|
|
|
3
19
|
/**
|
|
4
20
|
* Validates an untrusted value (cookie, cache, backend payload) into a
|
|
@@ -33,3 +49,73 @@ export function resolveTheme(
|
|
|
33
49
|
|
|
34
50
|
return preference;
|
|
35
51
|
}
|
|
52
|
+
|
|
53
|
+
export function normalizeAccentPreference(
|
|
54
|
+
value: unknown,
|
|
55
|
+
): NtAccentPreference | null {
|
|
56
|
+
return value === 'orange' ||
|
|
57
|
+
value === 'blue' ||
|
|
58
|
+
value === 'green' ||
|
|
59
|
+
value === 'purple' ||
|
|
60
|
+
value === 'neutral'
|
|
61
|
+
? value
|
|
62
|
+
: null;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function normalizeDensityPreference(
|
|
66
|
+
value: unknown,
|
|
67
|
+
): NtDensityPreference | null {
|
|
68
|
+
return value === 'compact' ||
|
|
69
|
+
value === 'comfortable' ||
|
|
70
|
+
value === 'spacious'
|
|
71
|
+
? value
|
|
72
|
+
: null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function normalizeSystemPreference(
|
|
76
|
+
value: unknown,
|
|
77
|
+
): NtSystemPreference | null {
|
|
78
|
+
return value === true || value === false || value === 'system' ? value : null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Validates an untrusted backend, cookie or cache appearance payload. */
|
|
82
|
+
export function normalizeAppearancePreference(
|
|
83
|
+
value: unknown,
|
|
84
|
+
fallback: NtAppearancePreference = NT_DEFAULT_APPEARANCE_PREFERENCE,
|
|
85
|
+
): NtAppearancePreference | null {
|
|
86
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
const input = value as Record<string, unknown>;
|
|
91
|
+
const theme = normalizeThemePreference(input.theme);
|
|
92
|
+
const accent = normalizeAccentPreference(input.accent);
|
|
93
|
+
const density = normalizeDensityPreference(input.density);
|
|
94
|
+
const reduceMotion =
|
|
95
|
+
input.reduceMotion === undefined
|
|
96
|
+
? fallback.reduceMotion ?? 'system'
|
|
97
|
+
: normalizeSystemPreference(input.reduceMotion);
|
|
98
|
+
const highContrast =
|
|
99
|
+
input.highContrast === undefined
|
|
100
|
+
? fallback.highContrast ?? 'system'
|
|
101
|
+
: normalizeSystemPreference(input.highContrast);
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
theme === null ||
|
|
105
|
+
accent === null ||
|
|
106
|
+
density === null ||
|
|
107
|
+
reduceMotion === null ||
|
|
108
|
+
highContrast === null
|
|
109
|
+
) {
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return { theme, accent, density, reduceMotion, highContrast };
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** Expands a legacy theme preference into the complete safe appearance defaults. */
|
|
117
|
+
export function appearanceFromTheme(
|
|
118
|
+
theme: ThemePreference,
|
|
119
|
+
): NtAppearancePreference {
|
|
120
|
+
return { ...NT_DEFAULT_APPEARANCE_PREFERENCE, theme };
|
|
121
|
+
}
|
|
@@ -1,159 +1,155 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getDefaultThemeStorage,
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
readAppearanceCache,
|
|
4
|
+
writeAppearanceCache,
|
|
5
5
|
type ThemeStorage,
|
|
6
6
|
} from './cache.js';
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
7
|
+
import { readAppearanceCookie } from './cookie.js';
|
|
8
|
+
import {
|
|
9
|
+
NT_DEFAULT_APPEARANCE_PREFERENCE,
|
|
10
|
+
normalizeAppearancePreference,
|
|
11
|
+
normalizeThemePreference,
|
|
12
|
+
resolveTheme,
|
|
13
|
+
} from './preference.js';
|
|
9
14
|
import type {
|
|
15
|
+
NtAppearancePreference,
|
|
16
|
+
NtResolvedAppearance,
|
|
17
|
+
NtResolvedAppearanceState,
|
|
10
18
|
ResolvedTheme,
|
|
11
19
|
ResolvedThemeState,
|
|
12
20
|
ThemePreference,
|
|
13
21
|
ThemeSource,
|
|
14
22
|
} from './types.js';
|
|
15
23
|
|
|
16
|
-
/** Canonical rendering attribute consumed by the theme CSS. */
|
|
17
24
|
export const NT_THEME_ATTRIBUTE = 'data-nt-theme';
|
|
18
|
-
|
|
19
|
-
/** Informational attribute exposing the stored preference. */
|
|
20
25
|
export const NT_THEME_PREFERENCE_ATTRIBUTE = 'data-nt-theme-preference';
|
|
26
|
+
export const NT_ACCENT_ATTRIBUTE = 'data-nt-accent';
|
|
27
|
+
export const NT_DENSITY_ATTRIBUTE = 'data-nt-density';
|
|
28
|
+
export const NT_CONTRAST_ATTRIBUTE = 'data-nt-contrast';
|
|
29
|
+
export const NT_MOTION_ATTRIBUTE = 'data-nt-motion';
|
|
21
30
|
|
|
22
|
-
const PREFERS_DARK_QUERY = '(prefers-color-scheme: dark)';
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Fallback resolved theme when the system preference cannot be detected
|
|
26
|
-
* (SSR, no matchMedia). Matches the package `:root` default token set.
|
|
27
|
-
*/
|
|
28
31
|
export const NT_THEME_DEFAULT_RESOLVED: ResolvedTheme = 'dark';
|
|
29
32
|
|
|
33
|
+
export const NT_APPEARANCE_MEDIA_QUERIES = Object.freeze({
|
|
34
|
+
dark: '(prefers-color-scheme: dark)',
|
|
35
|
+
reducedMotion: '(prefers-reduced-motion: reduce)',
|
|
36
|
+
highContrast: '(prefers-contrast: more)',
|
|
37
|
+
forcedColors: '(forced-colors: active)',
|
|
38
|
+
});
|
|
39
|
+
|
|
30
40
|
export type MatchMediaRef = (query: string) => MediaQueryList;
|
|
31
41
|
|
|
32
42
|
export interface ThemeEnvironmentOptions {
|
|
33
|
-
/** Explicit document (or `null` to force SSR behavior). Defaults to the global `document` when one exists. */
|
|
34
43
|
documentRef?: Document | null;
|
|
35
|
-
/** Explicit matchMedia (or `null` to disable system detection). Defaults to `window.matchMedia` when available. */
|
|
36
44
|
matchMediaRef?: MatchMediaRef | null;
|
|
37
|
-
/** Explicit storage (or `null` to disable the cache). Defaults to `window.localStorage` when accessible. */
|
|
38
45
|
storage?: ThemeStorage | null;
|
|
39
|
-
/** Explicit cookie string for SSR (e.g. the request `Cookie` header). Defaults to `document.cookie`. */
|
|
40
46
|
cookieString?: string;
|
|
41
|
-
/** Set to false to skip reading and writing the local cache. Default true. */
|
|
42
47
|
useCache?: boolean;
|
|
43
|
-
/** Resolved theme used when the system preference is unknown. Default `'dark'`. */
|
|
44
48
|
fallbackResolved?: ResolvedTheme;
|
|
49
|
+
/** Backend/server-resolved value. When supplied it outranks cookie and cache. */
|
|
50
|
+
serverPreference?: NtAppearancePreference | null;
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
export type AppearanceControllerOptions = ThemeEnvironmentOptions;
|
|
47
54
|
export type ThemeControllerOptions = ThemeEnvironmentOptions;
|
|
48
|
-
|
|
55
|
+
export type BootstrapAppearanceOptions = ThemeEnvironmentOptions;
|
|
49
56
|
export type BootstrapThemeOptions = ThemeEnvironmentOptions;
|
|
50
57
|
|
|
51
58
|
export interface SetPreferenceOptions {
|
|
52
|
-
/** Where the preference came from. Default `'backend'`. */
|
|
53
59
|
source?: ThemeSource;
|
|
54
|
-
/** Set to false to skip updating the local cache. Default true. */
|
|
55
60
|
updateCache?: boolean;
|
|
56
61
|
}
|
|
57
62
|
|
|
63
|
+
export type AppearanceChangeListener = (state: NtResolvedAppearanceState) => void;
|
|
58
64
|
export type ThemeChangeListener = (state: ResolvedThemeState) => void;
|
|
59
65
|
|
|
66
|
+
export interface AppearanceController {
|
|
67
|
+
getPreference(): NtAppearancePreference;
|
|
68
|
+
getResolvedAppearance(): NtResolvedAppearanceState;
|
|
69
|
+
initialize(): NtResolvedAppearanceState;
|
|
70
|
+
setPreference(
|
|
71
|
+
preference: NtAppearancePreference,
|
|
72
|
+
options?: SetPreferenceOptions,
|
|
73
|
+
): NtResolvedAppearanceState;
|
|
74
|
+
applyResolvedPreference(
|
|
75
|
+
preference: NtAppearancePreference,
|
|
76
|
+
options?: SetPreferenceOptions,
|
|
77
|
+
): NtResolvedAppearanceState;
|
|
78
|
+
subscribe(listener: AppearanceChangeListener): () => void;
|
|
79
|
+
destroy(): void;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** @deprecated Use AppearanceController/createAppearanceController for new integrations. */
|
|
60
83
|
export interface ThemeController {
|
|
61
|
-
/** Current state without side effects. */
|
|
62
84
|
getState(): ResolvedThemeState;
|
|
63
|
-
/**
|
|
64
|
-
* Reads bootstrap sources (cookie, then cache, then system), applies the
|
|
65
|
-
* theme to the document and attaches the system-theme listener. Calling
|
|
66
|
-
* it again while initialized is a no-op returning the current state.
|
|
67
|
-
*/
|
|
68
85
|
initialize(): ResolvedThemeState;
|
|
69
|
-
/**
|
|
70
|
-
* Applies a preference, typically the backend account setting after it
|
|
71
|
-
* loads. Overrides whatever bootstrap source is active. Invalid values
|
|
72
|
-
* are ignored.
|
|
73
|
-
*/
|
|
74
86
|
setPreference(
|
|
75
87
|
preference: ThemePreference,
|
|
76
88
|
options?: SetPreferenceOptions,
|
|
77
89
|
): ResolvedThemeState;
|
|
78
|
-
/** Notifies on every applied state change. Returns an unsubscribe function. */
|
|
79
90
|
subscribe(listener: ThemeChangeListener): () => void;
|
|
80
|
-
/** Removes listeners and subscribers. The controller can be initialized again. */
|
|
81
91
|
destroy(): void;
|
|
82
92
|
}
|
|
83
93
|
|
|
84
|
-
/**
|
|
85
|
-
* Applies the resolved theme to the document root:
|
|
86
|
-
*
|
|
87
|
-
* <html data-nt-theme="dark" data-nt-theme-preference="system">
|
|
88
|
-
*
|
|
89
|
-
* The document root is the canonical target — applications must not set
|
|
90
|
-
* theme attributes on `body` or app roots independently.
|
|
91
|
-
*/
|
|
92
94
|
export function applyThemeToDocument(
|
|
93
95
|
documentRef: Document,
|
|
94
96
|
state: ResolvedThemeState,
|
|
95
97
|
): void {
|
|
96
98
|
const root = documentRef?.documentElement;
|
|
97
99
|
|
|
98
|
-
if (!root)
|
|
99
|
-
return;
|
|
100
|
-
}
|
|
100
|
+
if (!root) return;
|
|
101
101
|
|
|
102
102
|
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved);
|
|
103
103
|
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference);
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
|
|
106
|
+
export function applyAppearanceToDocument(
|
|
107
|
+
documentRef: Document,
|
|
108
|
+
state: NtResolvedAppearanceState,
|
|
109
|
+
): void {
|
|
110
|
+
const root = documentRef?.documentElement;
|
|
111
|
+
|
|
112
|
+
if (!root) return;
|
|
113
|
+
|
|
114
|
+
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved.theme);
|
|
115
|
+
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference.theme);
|
|
116
|
+
root.setAttribute(NT_ACCENT_ATTRIBUTE, state.resolved.accent);
|
|
117
|
+
root.setAttribute(NT_DENSITY_ATTRIBUTE, state.resolved.density);
|
|
118
|
+
root.setAttribute(NT_CONTRAST_ATTRIBUTE, state.resolved.contrast);
|
|
119
|
+
root.setAttribute(NT_MOTION_ATTRIBUTE, state.resolved.motion);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
interface AppearanceEnvironment {
|
|
107
123
|
getDocument(): Document | null;
|
|
108
124
|
getMatchMedia(): MatchMediaRef | null;
|
|
109
125
|
getStorage(): ThemeStorage | null;
|
|
110
|
-
readCookie():
|
|
126
|
+
readCookie(): NtAppearancePreference | null;
|
|
111
127
|
useCache: boolean;
|
|
112
128
|
fallbackResolved: ResolvedTheme;
|
|
129
|
+
serverPreference: NtAppearancePreference | null;
|
|
113
130
|
}
|
|
114
131
|
|
|
115
|
-
function createEnvironment(options: ThemeEnvironmentOptions):
|
|
132
|
+
function createEnvironment(options: ThemeEnvironmentOptions): AppearanceEnvironment {
|
|
116
133
|
const getDocument = (): Document | null => {
|
|
117
|
-
if (options.documentRef !== undefined)
|
|
118
|
-
return options.documentRef;
|
|
119
|
-
}
|
|
120
|
-
|
|
134
|
+
if (options.documentRef !== undefined) return options.documentRef;
|
|
121
135
|
return typeof document === 'undefined' ? null : document;
|
|
122
136
|
};
|
|
123
137
|
|
|
124
138
|
const getMatchMedia = (): MatchMediaRef | null => {
|
|
125
|
-
if (options.matchMediaRef !== undefined)
|
|
126
|
-
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
|
|
139
|
+
if (options.matchMediaRef !== undefined) return options.matchMediaRef;
|
|
140
|
+
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return null;
|
|
133
141
|
return window.matchMedia.bind(window);
|
|
134
142
|
};
|
|
135
143
|
|
|
136
|
-
const getStorage = (): ThemeStorage | null =>
|
|
137
|
-
|
|
138
|
-
return options.storage;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
return getDefaultThemeStorage();
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
const readCookie = (): ThemePreference | null => {
|
|
145
|
-
if (options.cookieString !== undefined) {
|
|
146
|
-
return readThemeCookie(options.cookieString);
|
|
147
|
-
}
|
|
144
|
+
const getStorage = (): ThemeStorage | null =>
|
|
145
|
+
options.storage !== undefined ? options.storage : getDefaultThemeStorage();
|
|
148
146
|
|
|
147
|
+
const readCookie = (): NtAppearancePreference | null => {
|
|
148
|
+
if (options.cookieString !== undefined) return readAppearanceCookie(options.cookieString);
|
|
149
149
|
const doc = getDocument();
|
|
150
|
-
|
|
151
|
-
if (!doc) {
|
|
152
|
-
return null;
|
|
153
|
-
}
|
|
154
|
-
|
|
150
|
+
if (!doc) return null;
|
|
155
151
|
try {
|
|
156
|
-
return
|
|
152
|
+
return readAppearanceCookie(doc.cookie);
|
|
157
153
|
} catch {
|
|
158
154
|
return null;
|
|
159
155
|
}
|
|
@@ -166,218 +162,210 @@ function createEnvironment(options: ThemeEnvironmentOptions): ThemeEnvironment {
|
|
|
166
162
|
readCookie,
|
|
167
163
|
useCache: options.useCache !== false,
|
|
168
164
|
fallbackResolved: options.fallbackResolved ?? NT_THEME_DEFAULT_RESOLVED,
|
|
165
|
+
serverPreference: normalizeAppearancePreference(options.serverPreference),
|
|
169
166
|
};
|
|
170
167
|
}
|
|
171
168
|
|
|
172
|
-
function
|
|
169
|
+
function mediaMatches(environment: AppearanceEnvironment, query: string): boolean | null {
|
|
173
170
|
const matchMedia = environment.getMatchMedia();
|
|
174
|
-
|
|
175
|
-
if (!matchMedia) {
|
|
176
|
-
return null;
|
|
177
|
-
}
|
|
178
|
-
|
|
171
|
+
if (!matchMedia) return null;
|
|
179
172
|
try {
|
|
180
|
-
return matchMedia(
|
|
173
|
+
return matchMedia(query).matches;
|
|
181
174
|
} catch {
|
|
182
175
|
return null;
|
|
183
176
|
}
|
|
184
177
|
}
|
|
185
178
|
|
|
186
179
|
function computeState(
|
|
187
|
-
environment:
|
|
188
|
-
preference:
|
|
180
|
+
environment: AppearanceEnvironment,
|
|
181
|
+
preference: NtAppearancePreference,
|
|
189
182
|
source: ThemeSource,
|
|
190
|
-
):
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
183
|
+
): NtResolvedAppearanceState {
|
|
184
|
+
const systemDark = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.dark);
|
|
185
|
+
const forcedColors = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.forcedColors) === true;
|
|
186
|
+
const systemContrast = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.highContrast) === true;
|
|
187
|
+
const systemReducedMotion = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.reducedMotion) === true;
|
|
188
|
+
const theme =
|
|
189
|
+
preference.theme === 'system'
|
|
190
|
+
? resolveTheme('system', systemDark ?? environment.fallbackResolved === 'dark')
|
|
191
|
+
: preference.theme;
|
|
192
|
+
const highContrast =
|
|
193
|
+
forcedColors ||
|
|
194
|
+
preference.highContrast === true ||
|
|
195
|
+
(preference.highContrast !== false && systemContrast);
|
|
196
|
+
const reduceMotion =
|
|
197
|
+
preference.reduceMotion === true ||
|
|
198
|
+
(preference.reduceMotion !== false && systemReducedMotion);
|
|
194
199
|
|
|
195
|
-
|
|
200
|
+
return {
|
|
201
|
+
preference: { ...preference },
|
|
202
|
+
resolved: {
|
|
203
|
+
theme,
|
|
204
|
+
accent: preference.accent,
|
|
205
|
+
density: preference.density,
|
|
206
|
+
contrast: highContrast ? 'high' : 'normal',
|
|
207
|
+
motion: reduceMotion ? 'reduced' : 'full',
|
|
208
|
+
forcedColors,
|
|
209
|
+
},
|
|
210
|
+
source:
|
|
211
|
+
preference.theme === 'system' && systemDark === null && source === 'system'
|
|
212
|
+
? 'default'
|
|
213
|
+
: source,
|
|
214
|
+
};
|
|
215
|
+
}
|
|
196
216
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
resolved: environment.fallbackResolved,
|
|
201
|
-
source: source === 'system' ? 'default' : source,
|
|
202
|
-
};
|
|
217
|
+
function resolveBootstrapState(environment: AppearanceEnvironment): NtResolvedAppearanceState {
|
|
218
|
+
if (environment.serverPreference) {
|
|
219
|
+
return computeState(environment, environment.serverPreference, 'backend');
|
|
203
220
|
}
|
|
204
221
|
|
|
205
|
-
return { preference, resolved: resolveTheme('system', systemDark), source };
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function resolveBootstrapState(environment: ThemeEnvironment): ResolvedThemeState {
|
|
209
222
|
const cookiePreference = environment.readCookie();
|
|
210
|
-
|
|
211
|
-
if (cookiePreference !== null) {
|
|
212
|
-
return computeState(environment, cookiePreference, 'cookie');
|
|
213
|
-
}
|
|
223
|
+
if (cookiePreference) return computeState(environment, cookiePreference, 'cookie');
|
|
214
224
|
|
|
215
225
|
if (environment.useCache) {
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
if (cachedPreference !== null) {
|
|
219
|
-
return computeState(environment, cachedPreference, 'cache');
|
|
220
|
-
}
|
|
226
|
+
const cached = readAppearanceCache(environment.getStorage());
|
|
227
|
+
if (cached) return computeState(environment, cached, 'cache');
|
|
221
228
|
}
|
|
222
229
|
|
|
223
|
-
return computeState(environment,
|
|
230
|
+
return computeState(environment, { ...NT_DEFAULT_APPEARANCE_PREFERENCE }, 'system');
|
|
224
231
|
}
|
|
225
232
|
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
* Reads the `nt_theme` cookie, then the local cache, then the system
|
|
230
|
-
* preference; applies `data-nt-theme` to the document root and returns
|
|
231
|
-
* the resolved state. No network requests, no listeners, SSR-safe no-op
|
|
232
|
-
* on the DOM when no document exists.
|
|
233
|
-
*/
|
|
234
|
-
export function bootstrapNeetechsTheme(
|
|
235
|
-
options: BootstrapThemeOptions = {},
|
|
236
|
-
): ResolvedThemeState {
|
|
233
|
+
export function bootstrapNeetechsAppearance(
|
|
234
|
+
options: BootstrapAppearanceOptions = {},
|
|
235
|
+
): NtResolvedAppearanceState {
|
|
237
236
|
const environment = createEnvironment(options);
|
|
238
237
|
const state = resolveBootstrapState(environment);
|
|
239
238
|
const doc = environment.getDocument();
|
|
240
|
-
|
|
241
|
-
if (doc) {
|
|
242
|
-
applyThemeToDocument(doc, state);
|
|
243
|
-
}
|
|
244
|
-
|
|
239
|
+
if (doc) applyAppearanceToDocument(doc, state);
|
|
245
240
|
return state;
|
|
246
241
|
}
|
|
247
242
|
|
|
248
|
-
/**
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
options: ThemeControllerOptions = {},
|
|
255
|
-
): ThemeController {
|
|
256
|
-
const environment = createEnvironment(options);
|
|
257
|
-
const listeners = new Set<ThemeChangeListener>();
|
|
243
|
+
/** Legacy theme-only bootstrap; it also applies accent, density, contrast and motion defaults. */
|
|
244
|
+
export function bootstrapNeetechsTheme(
|
|
245
|
+
options: BootstrapThemeOptions = {},
|
|
246
|
+
): ResolvedThemeState {
|
|
247
|
+
return toThemeState(bootstrapNeetechsAppearance(options));
|
|
248
|
+
}
|
|
258
249
|
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
250
|
+
function toThemeState(state: NtResolvedAppearanceState): ResolvedThemeState {
|
|
251
|
+
return {
|
|
252
|
+
preference: state.preference.theme,
|
|
253
|
+
resolved: state.resolved.theme,
|
|
254
|
+
source: state.source,
|
|
263
255
|
};
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
export function createAppearanceController(
|
|
259
|
+
options: AppearanceControllerOptions = {},
|
|
260
|
+
): AppearanceController {
|
|
261
|
+
const environment = createEnvironment(options);
|
|
262
|
+
const listeners = new Set<AppearanceChangeListener>();
|
|
263
|
+
const detachMediaListeners: Array<() => void> = [];
|
|
264
264
|
let initialized = false;
|
|
265
|
-
let
|
|
265
|
+
let state = computeState(
|
|
266
|
+
environment,
|
|
267
|
+
{ ...NT_DEFAULT_APPEARANCE_PREFERENCE },
|
|
268
|
+
'default',
|
|
269
|
+
);
|
|
266
270
|
|
|
267
|
-
function commit(next:
|
|
271
|
+
function commit(next: NtResolvedAppearanceState): NtResolvedAppearanceState {
|
|
268
272
|
state = next;
|
|
269
|
-
|
|
270
273
|
const doc = environment.getDocument();
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
applyThemeToDocument(doc, state);
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
for (const listener of [...listeners]) {
|
|
277
|
-
listener(state);
|
|
278
|
-
}
|
|
279
|
-
|
|
274
|
+
if (doc) applyAppearanceToDocument(doc, state);
|
|
275
|
+
for (const listener of [...listeners]) listener(state);
|
|
280
276
|
return state;
|
|
281
277
|
}
|
|
282
278
|
|
|
283
|
-
function
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
const resolved: ResolvedTheme = event.matches ? 'dark' : 'light';
|
|
289
|
-
|
|
290
|
-
if (resolved === state.resolved) {
|
|
291
|
-
return;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
// The stored preference stays `system`; only the resolved theme moves.
|
|
295
|
-
commit({ ...state, resolved });
|
|
279
|
+
function handleMediaChange(): void {
|
|
280
|
+
const next = computeState(environment, state.preference, state.source);
|
|
281
|
+
if (JSON.stringify(next.resolved) !== JSON.stringify(state.resolved)) commit(next);
|
|
296
282
|
}
|
|
297
283
|
|
|
298
|
-
function
|
|
299
|
-
if (
|
|
300
|
-
return;
|
|
301
|
-
}
|
|
302
|
-
|
|
284
|
+
function attachMediaListeners(): void {
|
|
285
|
+
if (detachMediaListeners.length > 0) return;
|
|
303
286
|
const matchMedia = environment.getMatchMedia();
|
|
287
|
+
if (!matchMedia) return;
|
|
288
|
+
|
|
289
|
+
for (const query of Object.values(NT_APPEARANCE_MEDIA_QUERIES)) {
|
|
290
|
+
let mediaQueryList: MediaQueryList;
|
|
291
|
+
try {
|
|
292
|
+
mediaQueryList = matchMedia(query);
|
|
293
|
+
} catch {
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
304
296
|
|
|
305
|
-
|
|
306
|
-
|
|
297
|
+
if (typeof mediaQueryList.addEventListener === 'function') {
|
|
298
|
+
mediaQueryList.addEventListener('change', handleMediaChange);
|
|
299
|
+
detachMediaListeners.push(() =>
|
|
300
|
+
mediaQueryList.removeEventListener('change', handleMediaChange),
|
|
301
|
+
);
|
|
302
|
+
} else if (typeof mediaQueryList.addListener === 'function') {
|
|
303
|
+
mediaQueryList.addListener(handleMediaChange);
|
|
304
|
+
detachMediaListeners.push(() => mediaQueryList.removeListener(handleMediaChange));
|
|
305
|
+
}
|
|
307
306
|
}
|
|
307
|
+
}
|
|
308
308
|
|
|
309
|
-
|
|
309
|
+
function applyPreference(
|
|
310
|
+
preference: NtAppearancePreference,
|
|
311
|
+
setOptions: SetPreferenceOptions = {},
|
|
312
|
+
): NtResolvedAppearanceState {
|
|
313
|
+
const normalized = normalizeAppearancePreference(preference);
|
|
314
|
+
if (!normalized) return state;
|
|
310
315
|
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
} catch {
|
|
314
|
-
return;
|
|
316
|
+
if (environment.useCache && setOptions.updateCache !== false) {
|
|
317
|
+
writeAppearanceCache(normalized, environment.getStorage());
|
|
315
318
|
}
|
|
316
319
|
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
detachSystemListener = () => {
|
|
320
|
-
mediaQueryList.removeEventListener('change', handleSystemChange);
|
|
321
|
-
};
|
|
322
|
-
} else if (typeof mediaQueryList.addListener === 'function') {
|
|
323
|
-
// Fallback for engines without MediaQueryList EventTarget support.
|
|
324
|
-
mediaQueryList.addListener(handleSystemChange);
|
|
325
|
-
detachSystemListener = () => {
|
|
326
|
-
mediaQueryList.removeListener(handleSystemChange);
|
|
327
|
-
};
|
|
328
|
-
}
|
|
320
|
+
attachMediaListeners();
|
|
321
|
+
return commit(computeState(environment, normalized, setOptions.source ?? 'backend'));
|
|
329
322
|
}
|
|
330
323
|
|
|
331
324
|
return {
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
initialize(): ResolvedThemeState {
|
|
337
|
-
if (initialized) {
|
|
338
|
-
return state;
|
|
339
|
-
}
|
|
340
|
-
|
|
325
|
+
getPreference: () => ({ ...state.preference }),
|
|
326
|
+
getResolvedAppearance: () => state,
|
|
327
|
+
initialize(): NtResolvedAppearanceState {
|
|
328
|
+
if (initialized) return state;
|
|
341
329
|
initialized = true;
|
|
342
|
-
|
|
343
|
-
|
|
330
|
+
attachMediaListeners();
|
|
344
331
|
return commit(resolveBootstrapState(environment));
|
|
345
332
|
},
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
setOptions: SetPreferenceOptions = {},
|
|
350
|
-
): ResolvedThemeState {
|
|
351
|
-
const normalized = normalizeThemePreference(preference);
|
|
352
|
-
|
|
353
|
-
if (normalized === null) {
|
|
354
|
-
return state;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (environment.useCache && setOptions.updateCache !== false) {
|
|
358
|
-
writeThemeCache(normalized, environment.getStorage());
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
attachSystemListener();
|
|
362
|
-
|
|
363
|
-
return commit(
|
|
364
|
-
computeState(environment, normalized, setOptions.source ?? 'backend'),
|
|
365
|
-
);
|
|
366
|
-
},
|
|
367
|
-
|
|
368
|
-
subscribe(listener: ThemeChangeListener): () => void {
|
|
333
|
+
setPreference: applyPreference,
|
|
334
|
+
applyResolvedPreference: applyPreference,
|
|
335
|
+
subscribe(listener: AppearanceChangeListener): () => void {
|
|
369
336
|
listeners.add(listener);
|
|
370
|
-
|
|
371
|
-
return () => {
|
|
372
|
-
listeners.delete(listener);
|
|
373
|
-
};
|
|
337
|
+
return () => listeners.delete(listener);
|
|
374
338
|
},
|
|
375
|
-
|
|
376
339
|
destroy(): void {
|
|
377
|
-
|
|
378
|
-
detachSystemListener = null;
|
|
340
|
+
for (const detach of detachMediaListeners.splice(0)) detach();
|
|
379
341
|
listeners.clear();
|
|
380
342
|
initialized = false;
|
|
381
343
|
},
|
|
382
344
|
};
|
|
383
345
|
}
|
|
346
|
+
|
|
347
|
+
/** @deprecated New applications should create one AppearanceController. */
|
|
348
|
+
export function createThemeController(
|
|
349
|
+
options: ThemeControllerOptions = {},
|
|
350
|
+
): ThemeController {
|
|
351
|
+
const controller = createAppearanceController(options);
|
|
352
|
+
|
|
353
|
+
return {
|
|
354
|
+
getState: () => toThemeState(controller.getResolvedAppearance()),
|
|
355
|
+
initialize: () => toThemeState(controller.initialize()),
|
|
356
|
+
setPreference(preference, setOptions = {}) {
|
|
357
|
+
const theme = normalizeThemePreference(preference);
|
|
358
|
+
if (!theme) return toThemeState(controller.getResolvedAppearance());
|
|
359
|
+
return toThemeState(
|
|
360
|
+
controller.setPreference(
|
|
361
|
+
{ ...controller.getPreference(), theme },
|
|
362
|
+
setOptions,
|
|
363
|
+
),
|
|
364
|
+
);
|
|
365
|
+
},
|
|
366
|
+
subscribe(listener) {
|
|
367
|
+
return controller.subscribe((next) => listener(toThemeState(next)));
|
|
368
|
+
},
|
|
369
|
+
destroy: () => controller.destroy(),
|
|
370
|
+
};
|
|
371
|
+
}
|