@neetechs/ui 0.2.0 → 0.3.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/CHANGELOG.md +39 -0
- package/LICENSE +21 -21
- package/README.md +862 -861
- package/dist/ai/ai-action-button/ai-action-button.css +271 -271
- package/dist/ai/ai-approval-card/ai-approval-card.css +323 -323
- package/dist/ai/ai-conversation-panel/ai-conversation-panel.css +447 -447
- package/dist/ai/ai-prompt-input/ai-prompt-input.css +380 -380
- package/dist/ai/ai-proposal-card/ai-proposal-card.css +358 -358
- package/dist/ai/ai-suggestion-card/ai-suggestion-card.css +295 -295
- package/dist/ai/ai-tool-call/ai-tool-call.css +256 -256
- package/dist/ai/index.css +7 -7
- package/dist/behaviors/index.d.ts +1 -0
- package/dist/behaviors/index.d.ts.map +1 -1
- package/dist/behaviors/index.js +1 -0
- package/dist/behaviors/index.js.map +1 -1
- package/dist/behaviors/selection-indicator.d.ts +35 -0
- package/dist/behaviors/selection-indicator.d.ts.map +1 -0
- package/dist/behaviors/selection-indicator.js +133 -0
- package/dist/behaviors/selection-indicator.js.map +1 -0
- package/dist/behaviors/sidebar.d.ts +3 -1
- package/dist/behaviors/sidebar.d.ts.map +1 -1
- package/dist/behaviors/sidebar.js +10 -3
- package/dist/behaviors/sidebar.js.map +1 -1
- package/dist/components/alert/alert.css +226 -226
- package/dist/components/avatar/avatar.css +298 -298
- package/dist/components/badge/badge.css +180 -180
- package/dist/components/breadcrumb/breadcrumb.css +207 -207
- package/dist/components/button/button.css +278 -278
- package/dist/components/card/card.css +336 -284
- package/dist/components/checkbox/checkbox.css +189 -189
- package/dist/components/chip/chip.css +269 -269
- package/dist/components/data-grid/data-grid.css +520 -520
- package/dist/components/description-list/description-list.css +187 -187
- package/dist/components/dialog/dialog.css +348 -339
- package/dist/components/dock/dock.css +319 -0
- package/dist/components/drawer/drawer.css +493 -484
- package/dist/components/dropdown/dropdown.css +325 -316
- package/dist/components/fab/fab.css +281 -0
- package/dist/components/icon-button/icon-button.css +162 -162
- package/dist/components/index.css +2 -0
- package/dist/components/input/input.css +163 -163
- package/dist/components/menu/menu.css +315 -315
- package/dist/components/pagination/pagination.css +250 -250
- package/dist/components/panel/panel.css +316 -284
- package/dist/components/popover/popover.css +22 -0
- package/dist/components/progress/progress.css +268 -268
- package/dist/components/radio/radio.css +166 -166
- package/dist/components/segmented-control/segmented-control.css +264 -205
- package/dist/components/select/select.css +117 -117
- package/dist/components/skeleton/skeleton.css +267 -267
- package/dist/components/split-button/split-button.css +292 -292
- package/dist/components/stat/stat.css +433 -433
- package/dist/components/switch/switch.css +195 -195
- package/dist/components/table/table.css +220 -220
- package/dist/components/tabs/tabs.css +393 -293
- package/dist/components/textarea/textarea.css +86 -86
- package/dist/components/toast/toast.css +345 -345
- package/dist/components/tooltip/tooltip.css +267 -258
- package/dist/domain/billing/index.css +629 -629
- package/dist/domain/calendar/agenda-list/agenda-list.css +224 -224
- package/dist/domain/calendar/day-slice/day-slice.css +384 -384
- package/dist/domain/calendar/day-timeline/day-timeline.css +332 -332
- package/dist/domain/calendar/event-card/event-card.css +338 -338
- package/dist/domain/calendar/index.css +5 -5
- package/dist/domain/calendar/month-grid/month-grid.css +426 -426
- package/dist/domain/calendar/week-grid/week-grid.css +487 -480
- package/dist/domain/companies/index.css +526 -526
- package/dist/domain/index.css +3 -3
- package/dist/index.css +5 -5
- package/dist/patterns/command-palette/command-palette.css +488 -447
- package/dist/patterns/company-switcher/company-switcher.css +390 -390
- package/dist/patterns/content-state/content-state.css +236 -236
- package/dist/patterns/empty-state/empty-state.css +260 -260
- package/dist/patterns/error-state/error-state.css +272 -272
- package/dist/patterns/form-layout/form-layout.css +106 -106
- package/dist/patterns/loading-state/loading-state.css +287 -287
- package/dist/patterns/master-detail/master-detail.css +103 -103
- package/dist/patterns/notification-center/notification-center.css +443 -443
- package/dist/patterns/page/page.css +303 -303
- package/dist/patterns/profile-menu/profile-menu.css +298 -298
- package/dist/patterns/record-list/record-list.css +302 -302
- package/dist/patterns/search-bar/search-bar.css +457 -457
- package/dist/patterns/settings-layout/settings-layout.css +283 -283
- package/dist/patterns/workflow/workflow.css +168 -168
- package/dist/shell/app-shell/app-shell.css +429 -211
- package/dist/shell/content-frame/content-frame.css +93 -93
- package/dist/shell/header/header.css +328 -328
- package/dist/shell/index.css +8 -8
- package/dist/shell/mobile-nav/mobile-nav.css +254 -254
- package/dist/shell/sidebar/sidebar.css +923 -314
- package/dist/styles/accessibility.css +150 -150
- package/dist/styles/animations/motion.css +162 -162
- package/dist/styles/appearance/accent.css +215 -215
- package/dist/styles/appearance/density.css +61 -61
- package/dist/styles/appearance/motion.css +48 -40
- package/dist/styles/base.css +83 -83
- package/dist/styles/index.css +29 -28
- package/dist/styles/localization.css +56 -56
- package/dist/styles/reset.css +51 -51
- package/dist/styles/responsive.css +101 -101
- package/dist/styles/themes/dark.css +225 -210
- package/dist/styles/themes/high-contrast.css +378 -298
- package/dist/styles/themes/light.css +224 -209
- package/dist/styles/tokens/breakpoints.css +37 -37
- package/dist/styles/tokens/color.css +102 -102
- package/dist/styles/tokens/compatibility.css +33 -33
- package/dist/styles/tokens/elevation.css +33 -26
- package/dist/styles/tokens/index.css +9 -9
- package/dist/styles/tokens/motion.css +55 -36
- package/dist/styles/tokens/radius.css +26 -26
- package/dist/styles/tokens/spacing.css +56 -56
- package/dist/styles/tokens/typography.css +85 -85
- package/dist/styles/tokens/z-index.css +14 -14
- package/dist/styles/utilities/glass.css +97 -0
- package/dist/styles/utilities/layout.css +153 -153
- package/dist/styles/utilities/text.css +152 -152
- package/dist/styles/utilities/visibility.css +47 -47
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/docs/NEETECHS_UI_UPGRADE_PLAN.md +312 -312
- package/docs/accessibility.md +218 -218
- package/docs/appearance-and-theming.md +150 -150
- package/docs/brand-palette.md +36 -36
- package/docs/component-architecture.md +137 -137
- package/docs/component-guidelines.md +64 -64
- package/docs/components.md +326 -321
- package/docs/content-and-terminology.md +207 -207
- package/docs/design-system-baseline-audit.md +226 -226
- package/docs/design-token-contract.md +279 -279
- package/docs/glass-and-motion.md +310 -0
- package/docs/localization-and-rtl.md +147 -147
- package/docs/migration.md +193 -193
- package/docs/page-patterns-and-workflows.md +301 -301
- package/docs/responsive-and-adaptive-design.md +351 -351
- package/docs/semantic-role-migration-audit.md +77 -77
- package/docs/site-chrome.md +320 -317
- package/docs/states-feedback-and-recovery.md +206 -206
- package/docs/theming.md +5 -5
- package/package.json +2 -1
- package/src/ai/ai-action-button/ai-action-button.css +271 -271
- package/src/ai/ai-approval-card/ai-approval-card.css +323 -323
- package/src/ai/ai-conversation-panel/ai-conversation-panel.css +447 -447
- package/src/ai/ai-prompt-input/ai-prompt-input.css +380 -380
- package/src/ai/ai-proposal-card/ai-proposal-card.css +358 -358
- package/src/ai/ai-suggestion-card/ai-suggestion-card.css +295 -295
- package/src/ai/ai-tool-call/ai-tool-call.css +256 -256
- package/src/ai/index.css +7 -7
- package/src/appearance/cache.ts +236 -236
- package/src/appearance/cookie.ts +184 -184
- package/src/appearance/index.ts +86 -86
- package/src/appearance/preference.ts +121 -121
- package/src/appearance/theme-controller.ts +384 -384
- package/src/appearance/types.ts +61 -61
- package/src/behaviors/announcer.ts +222 -222
- package/src/behaviors/calendar.ts +92 -92
- package/src/behaviors/data-grid.ts +133 -133
- package/src/behaviors/dialog.ts +203 -203
- package/src/behaviors/direction.ts +34 -34
- package/src/behaviors/drawer.ts +210 -210
- package/src/behaviors/dropdown.ts +344 -344
- package/src/behaviors/focus-trap.ts +304 -304
- package/src/behaviors/forms.ts +205 -205
- package/src/behaviors/ids.ts +47 -47
- package/src/behaviors/index.ts +1 -0
- package/src/behaviors/keyboard.ts +305 -305
- package/src/behaviors/overlay.ts +344 -344
- package/src/behaviors/selection-indicator.ts +162 -0
- package/src/behaviors/sidebar.ts +14 -4
- package/src/behaviors/state.ts +282 -282
- package/src/behaviors/tabs.ts +137 -137
- package/src/behaviors/viewport.ts +61 -61
- package/src/components/alert/alert.css +226 -226
- package/src/components/avatar/avatar.css +298 -298
- package/src/components/badge/badge.css +180 -180
- package/src/components/breadcrumb/breadcrumb.css +207 -207
- package/src/components/button/button.css +278 -278
- package/src/components/card/card.css +336 -284
- package/src/components/checkbox/checkbox.css +189 -189
- package/src/components/chip/chip.css +269 -269
- package/src/components/data-grid/data-grid.css +520 -520
- package/src/components/description-list/description-list.css +187 -187
- package/src/components/dialog/dialog.css +348 -339
- package/src/components/dock/dock.css +319 -0
- package/src/components/drawer/drawer.css +493 -484
- package/src/components/dropdown/dropdown.css +325 -316
- package/src/components/fab/fab.css +281 -0
- package/src/components/icon-button/icon-button.css +162 -162
- package/src/components/index.css +2 -0
- package/src/components/input/input.css +163 -163
- package/src/components/menu/menu.css +315 -315
- package/src/components/pagination/pagination.css +250 -250
- package/src/components/panel/panel.css +316 -284
- package/src/components/popover/popover.css +22 -0
- package/src/components/progress/progress.css +268 -268
- package/src/components/radio/radio.css +166 -166
- package/src/components/segmented-control/segmented-control.css +264 -205
- package/src/components/select/select.css +117 -117
- package/src/components/skeleton/skeleton.css +267 -267
- package/src/components/split-button/split-button.css +292 -292
- package/src/components/stat/stat.css +433 -433
- package/src/components/switch/switch.css +195 -195
- package/src/components/table/table.css +220 -220
- package/src/components/tabs/tabs.css +393 -293
- package/src/components/textarea/textarea.css +86 -86
- package/src/components/toast/toast.css +345 -345
- package/src/components/tooltip/tooltip.css +267 -258
- package/src/domain/billing/index.css +629 -629
- package/src/domain/calendar/agenda-list/agenda-list.css +224 -224
- package/src/domain/calendar/day-slice/day-slice.css +384 -384
- package/src/domain/calendar/day-timeline/day-timeline.css +332 -332
- package/src/domain/calendar/event-card/event-card.css +338 -338
- package/src/domain/calendar/index.css +5 -5
- package/src/domain/calendar/month-grid/month-grid.css +426 -426
- package/src/domain/calendar/week-grid/week-grid.css +487 -480
- package/src/domain/companies/index.css +526 -526
- package/src/domain/index.css +3 -3
- package/src/index.css +5 -5
- package/src/index.ts +7 -7
- package/src/localization/index.ts +56 -56
- package/src/patterns/command-palette/command-palette.css +488 -447
- package/src/patterns/company-switcher/company-switcher.css +390 -390
- package/src/patterns/content-state/content-state.css +236 -236
- package/src/patterns/empty-state/empty-state.css +260 -260
- package/src/patterns/error-state/error-state.css +272 -272
- package/src/patterns/form-layout/form-layout.css +106 -106
- package/src/patterns/loading-state/loading-state.css +287 -287
- package/src/patterns/master-detail/master-detail.css +103 -103
- package/src/patterns/notification-center/notification-center.css +443 -443
- package/src/patterns/page/page.css +303 -303
- package/src/patterns/profile-menu/profile-menu.css +298 -298
- package/src/patterns/record-list/record-list.css +302 -302
- package/src/patterns/search-bar/search-bar.css +457 -457
- package/src/patterns/settings-layout/settings-layout.css +283 -283
- package/src/patterns/workflow/workflow.css +168 -168
- package/src/shell/app-shell/app-shell.css +429 -211
- package/src/shell/content-frame/content-frame.css +93 -93
- package/src/shell/header/header.css +328 -328
- package/src/shell/index.css +8 -8
- package/src/shell/mobile-nav/mobile-nav.css +254 -254
- package/src/shell/sidebar/sidebar.css +923 -314
- package/src/styles/accessibility.css +150 -150
- package/src/styles/animations/motion.css +162 -162
- package/src/styles/appearance/accent.css +215 -215
- package/src/styles/appearance/density.css +61 -61
- package/src/styles/appearance/motion.css +48 -40
- package/src/styles/base.css +83 -83
- package/src/styles/index.css +29 -28
- package/src/styles/localization.css +56 -56
- package/src/styles/reset.css +51 -51
- package/src/styles/responsive.css +101 -101
- package/src/styles/themes/dark.css +225 -210
- package/src/styles/themes/high-contrast.css +378 -298
- package/src/styles/themes/light.css +224 -209
- package/src/styles/tokens/breakpoints.css +37 -37
- package/src/styles/tokens/color.css +102 -102
- package/src/styles/tokens/compatibility.css +33 -33
- package/src/styles/tokens/elevation.css +33 -26
- package/src/styles/tokens/index.css +9 -9
- package/src/styles/tokens/motion.css +55 -36
- package/src/styles/tokens/radius.css +26 -26
- package/src/styles/tokens/spacing.css +56 -56
- package/src/styles/tokens/typography.css +85 -85
- package/src/styles/tokens/z-index.css +14 -14
- package/src/styles/utilities/glass.css +97 -0
- package/src/styles/utilities/layout.css +153 -153
- package/src/styles/utilities/text.css +152 -152
- package/src/styles/utilities/visibility.css +47 -47
- package/src/types/index.ts +126 -126
- package/src/version.ts +1 -1
|
@@ -1,384 +1,384 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getDefaultThemeStorage,
|
|
3
|
-
readAppearanceCache,
|
|
4
|
-
writeAppearanceCache,
|
|
5
|
-
type ThemeStorage,
|
|
6
|
-
} from './cache.js';
|
|
7
|
-
import { readAppearanceCookie } from './cookie.js';
|
|
8
|
-
import {
|
|
9
|
-
NT_DEFAULT_APPEARANCE_PREFERENCE,
|
|
10
|
-
normalizeAppearancePreference,
|
|
11
|
-
normalizeThemePreference,
|
|
12
|
-
resolveTheme,
|
|
13
|
-
} from './preference.js';
|
|
14
|
-
import type {
|
|
15
|
-
NtAppearancePreference,
|
|
16
|
-
NtResolvedAppearance,
|
|
17
|
-
NtResolvedAppearanceState,
|
|
18
|
-
ResolvedTheme,
|
|
19
|
-
ResolvedThemeState,
|
|
20
|
-
ThemePreference,
|
|
21
|
-
ThemeSource,
|
|
22
|
-
} from './types.js';
|
|
23
|
-
|
|
24
|
-
export const NT_THEME_ATTRIBUTE = 'data-nt-theme';
|
|
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';
|
|
30
|
-
|
|
31
|
-
export const NT_THEME_DEFAULT_RESOLVED: ResolvedTheme = 'dark';
|
|
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
|
-
|
|
40
|
-
export type MatchMediaRef = (query: string) => MediaQueryList;
|
|
41
|
-
|
|
42
|
-
export interface ThemeEnvironmentOptions {
|
|
43
|
-
documentRef?: Document | null;
|
|
44
|
-
matchMediaRef?: MatchMediaRef | null;
|
|
45
|
-
storage?: ThemeStorage | null;
|
|
46
|
-
cookieString?: string;
|
|
47
|
-
useCache?: boolean;
|
|
48
|
-
fallbackResolved?: ResolvedTheme;
|
|
49
|
-
/** Backend/server-resolved value. When supplied it outranks cookie and cache. */
|
|
50
|
-
serverPreference?: NtAppearancePreference | null;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export type AppearanceControllerOptions = ThemeEnvironmentOptions;
|
|
54
|
-
export type ThemeControllerOptions = ThemeEnvironmentOptions;
|
|
55
|
-
export type BootstrapAppearanceOptions = ThemeEnvironmentOptions;
|
|
56
|
-
export type BootstrapThemeOptions = ThemeEnvironmentOptions;
|
|
57
|
-
|
|
58
|
-
export interface SetPreferenceOptions {
|
|
59
|
-
source?: ThemeSource;
|
|
60
|
-
updateCache?: boolean;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
export type AppearanceChangeListener = (state: NtResolvedAppearanceState) => void;
|
|
64
|
-
export type ThemeChangeListener = (state: ResolvedThemeState) => void;
|
|
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
|
-
/**
|
|
79
|
-
* Merges a partial change (for example `{ theme: 'light' }`) onto the current
|
|
80
|
-
* preference, so switching one dimension never resets saved accent, density,
|
|
81
|
-
* motion, or contrast choices. Invalid partial values leave state unchanged.
|
|
82
|
-
*/
|
|
83
|
-
updatePreference(
|
|
84
|
-
partial: Partial<NtAppearancePreference>,
|
|
85
|
-
options?: SetPreferenceOptions,
|
|
86
|
-
): NtResolvedAppearanceState;
|
|
87
|
-
subscribe(listener: AppearanceChangeListener): () => void;
|
|
88
|
-
destroy(): void;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/** @deprecated Use AppearanceController/createAppearanceController for new integrations. */
|
|
92
|
-
export interface ThemeController {
|
|
93
|
-
getState(): ResolvedThemeState;
|
|
94
|
-
initialize(): ResolvedThemeState;
|
|
95
|
-
setPreference(
|
|
96
|
-
preference: ThemePreference,
|
|
97
|
-
options?: SetPreferenceOptions,
|
|
98
|
-
): ResolvedThemeState;
|
|
99
|
-
subscribe(listener: ThemeChangeListener): () => void;
|
|
100
|
-
destroy(): void;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function applyThemeToDocument(
|
|
104
|
-
documentRef: Document,
|
|
105
|
-
state: ResolvedThemeState,
|
|
106
|
-
): void {
|
|
107
|
-
const root = documentRef?.documentElement;
|
|
108
|
-
|
|
109
|
-
if (!root) return;
|
|
110
|
-
|
|
111
|
-
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved);
|
|
112
|
-
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
export function applyAppearanceToDocument(
|
|
116
|
-
documentRef: Document,
|
|
117
|
-
state: NtResolvedAppearanceState,
|
|
118
|
-
): void {
|
|
119
|
-
const root = documentRef?.documentElement;
|
|
120
|
-
|
|
121
|
-
if (!root) return;
|
|
122
|
-
|
|
123
|
-
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved.theme);
|
|
124
|
-
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference.theme);
|
|
125
|
-
root.setAttribute(NT_ACCENT_ATTRIBUTE, state.resolved.accent);
|
|
126
|
-
root.setAttribute(NT_DENSITY_ATTRIBUTE, state.resolved.density);
|
|
127
|
-
root.setAttribute(NT_CONTRAST_ATTRIBUTE, state.resolved.contrast);
|
|
128
|
-
root.setAttribute(NT_MOTION_ATTRIBUTE, state.resolved.motion);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
interface AppearanceEnvironment {
|
|
132
|
-
getDocument(): Document | null;
|
|
133
|
-
getMatchMedia(): MatchMediaRef | null;
|
|
134
|
-
getStorage(): ThemeStorage | null;
|
|
135
|
-
readCookie(): NtAppearancePreference | null;
|
|
136
|
-
useCache: boolean;
|
|
137
|
-
fallbackResolved: ResolvedTheme;
|
|
138
|
-
serverPreference: NtAppearancePreference | null;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
function createEnvironment(options: ThemeEnvironmentOptions): AppearanceEnvironment {
|
|
142
|
-
const getDocument = (): Document | null => {
|
|
143
|
-
if (options.documentRef !== undefined) return options.documentRef;
|
|
144
|
-
return typeof document === 'undefined' ? null : document;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
const getMatchMedia = (): MatchMediaRef | null => {
|
|
148
|
-
if (options.matchMediaRef !== undefined) return options.matchMediaRef;
|
|
149
|
-
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return null;
|
|
150
|
-
return window.matchMedia.bind(window);
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
const getStorage = (): ThemeStorage | null =>
|
|
154
|
-
options.storage !== undefined ? options.storage : getDefaultThemeStorage();
|
|
155
|
-
|
|
156
|
-
const readCookie = (): NtAppearancePreference | null => {
|
|
157
|
-
if (options.cookieString !== undefined) return readAppearanceCookie(options.cookieString);
|
|
158
|
-
const doc = getDocument();
|
|
159
|
-
if (!doc) return null;
|
|
160
|
-
try {
|
|
161
|
-
return readAppearanceCookie(doc.cookie);
|
|
162
|
-
} catch {
|
|
163
|
-
return null;
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
return {
|
|
168
|
-
getDocument,
|
|
169
|
-
getMatchMedia,
|
|
170
|
-
getStorage,
|
|
171
|
-
readCookie,
|
|
172
|
-
useCache: options.useCache !== false,
|
|
173
|
-
fallbackResolved: options.fallbackResolved ?? NT_THEME_DEFAULT_RESOLVED,
|
|
174
|
-
serverPreference: normalizeAppearancePreference(options.serverPreference),
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
function mediaMatches(environment: AppearanceEnvironment, query: string): boolean | null {
|
|
179
|
-
const matchMedia = environment.getMatchMedia();
|
|
180
|
-
if (!matchMedia) return null;
|
|
181
|
-
try {
|
|
182
|
-
return matchMedia(query).matches;
|
|
183
|
-
} catch {
|
|
184
|
-
return null;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
function computeState(
|
|
189
|
-
environment: AppearanceEnvironment,
|
|
190
|
-
preference: NtAppearancePreference,
|
|
191
|
-
source: ThemeSource,
|
|
192
|
-
): NtResolvedAppearanceState {
|
|
193
|
-
const systemDark = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.dark);
|
|
194
|
-
const forcedColors = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.forcedColors) === true;
|
|
195
|
-
const systemContrast = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.highContrast) === true;
|
|
196
|
-
const systemReducedMotion = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.reducedMotion) === true;
|
|
197
|
-
const theme =
|
|
198
|
-
preference.theme === 'system'
|
|
199
|
-
? resolveTheme('system', systemDark ?? environment.fallbackResolved === 'dark')
|
|
200
|
-
: preference.theme;
|
|
201
|
-
const highContrast =
|
|
202
|
-
forcedColors ||
|
|
203
|
-
preference.highContrast === true ||
|
|
204
|
-
(preference.highContrast !== false && systemContrast);
|
|
205
|
-
const reduceMotion =
|
|
206
|
-
preference.reduceMotion === true ||
|
|
207
|
-
(preference.reduceMotion !== false && systemReducedMotion);
|
|
208
|
-
|
|
209
|
-
return {
|
|
210
|
-
preference: { ...preference },
|
|
211
|
-
resolved: {
|
|
212
|
-
theme,
|
|
213
|
-
accent: preference.accent,
|
|
214
|
-
density: preference.density,
|
|
215
|
-
contrast: highContrast ? 'high' : 'normal',
|
|
216
|
-
motion: reduceMotion ? 'reduced' : 'full',
|
|
217
|
-
forcedColors,
|
|
218
|
-
},
|
|
219
|
-
source:
|
|
220
|
-
preference.theme === 'system' && systemDark === null && source === 'system'
|
|
221
|
-
? 'default'
|
|
222
|
-
: source,
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function resolveBootstrapState(environment: AppearanceEnvironment): NtResolvedAppearanceState {
|
|
227
|
-
if (environment.serverPreference) {
|
|
228
|
-
return computeState(environment, environment.serverPreference, 'backend');
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
const cookiePreference = environment.readCookie();
|
|
232
|
-
if (cookiePreference) return computeState(environment, cookiePreference, 'cookie');
|
|
233
|
-
|
|
234
|
-
if (environment.useCache) {
|
|
235
|
-
const cached = readAppearanceCache(environment.getStorage());
|
|
236
|
-
if (cached) return computeState(environment, cached, 'cache');
|
|
237
|
-
}
|
|
238
|
-
|
|
239
|
-
return computeState(environment, { ...NT_DEFAULT_APPEARANCE_PREFERENCE }, 'system');
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
export function bootstrapNeetechsAppearance(
|
|
243
|
-
options: BootstrapAppearanceOptions = {},
|
|
244
|
-
): NtResolvedAppearanceState {
|
|
245
|
-
const environment = createEnvironment(options);
|
|
246
|
-
const state = resolveBootstrapState(environment);
|
|
247
|
-
const doc = environment.getDocument();
|
|
248
|
-
if (doc) applyAppearanceToDocument(doc, state);
|
|
249
|
-
return state;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/** Legacy theme-only bootstrap; it also applies accent, density, contrast and motion defaults. */
|
|
253
|
-
export function bootstrapNeetechsTheme(
|
|
254
|
-
options: BootstrapThemeOptions = {},
|
|
255
|
-
): ResolvedThemeState {
|
|
256
|
-
return toThemeState(bootstrapNeetechsAppearance(options));
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
function toThemeState(state: NtResolvedAppearanceState): ResolvedThemeState {
|
|
260
|
-
return {
|
|
261
|
-
preference: state.preference.theme,
|
|
262
|
-
resolved: state.resolved.theme,
|
|
263
|
-
source: state.source,
|
|
264
|
-
};
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function createAppearanceController(
|
|
268
|
-
options: AppearanceControllerOptions = {},
|
|
269
|
-
): AppearanceController {
|
|
270
|
-
const environment = createEnvironment(options);
|
|
271
|
-
const listeners = new Set<AppearanceChangeListener>();
|
|
272
|
-
const detachMediaListeners: Array<() => void> = [];
|
|
273
|
-
let initialized = false;
|
|
274
|
-
let state = computeState(
|
|
275
|
-
environment,
|
|
276
|
-
{ ...NT_DEFAULT_APPEARANCE_PREFERENCE },
|
|
277
|
-
'default',
|
|
278
|
-
);
|
|
279
|
-
|
|
280
|
-
function commit(next: NtResolvedAppearanceState): NtResolvedAppearanceState {
|
|
281
|
-
state = next;
|
|
282
|
-
const doc = environment.getDocument();
|
|
283
|
-
if (doc) applyAppearanceToDocument(doc, state);
|
|
284
|
-
for (const listener of [...listeners]) listener(state);
|
|
285
|
-
return state;
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
function handleMediaChange(): void {
|
|
289
|
-
const next = computeState(environment, state.preference, state.source);
|
|
290
|
-
if (JSON.stringify(next.resolved) !== JSON.stringify(state.resolved)) commit(next);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
function attachMediaListeners(): void {
|
|
294
|
-
if (detachMediaListeners.length > 0) return;
|
|
295
|
-
const matchMedia = environment.getMatchMedia();
|
|
296
|
-
if (!matchMedia) return;
|
|
297
|
-
|
|
298
|
-
for (const query of Object.values(NT_APPEARANCE_MEDIA_QUERIES)) {
|
|
299
|
-
let mediaQueryList: MediaQueryList;
|
|
300
|
-
try {
|
|
301
|
-
mediaQueryList = matchMedia(query);
|
|
302
|
-
} catch {
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
if (typeof mediaQueryList.addEventListener === 'function') {
|
|
307
|
-
mediaQueryList.addEventListener('change', handleMediaChange);
|
|
308
|
-
detachMediaListeners.push(() =>
|
|
309
|
-
mediaQueryList.removeEventListener('change', handleMediaChange),
|
|
310
|
-
);
|
|
311
|
-
} else if (typeof mediaQueryList.addListener === 'function') {
|
|
312
|
-
mediaQueryList.addListener(handleMediaChange);
|
|
313
|
-
detachMediaListeners.push(() => mediaQueryList.removeListener(handleMediaChange));
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
function applyPreference(
|
|
319
|
-
preference: NtAppearancePreference,
|
|
320
|
-
setOptions: SetPreferenceOptions = {},
|
|
321
|
-
): NtResolvedAppearanceState {
|
|
322
|
-
const normalized = normalizeAppearancePreference(preference);
|
|
323
|
-
if (!normalized) return state;
|
|
324
|
-
|
|
325
|
-
if (environment.useCache && setOptions.updateCache !== false) {
|
|
326
|
-
writeAppearanceCache(normalized, environment.getStorage());
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
attachMediaListeners();
|
|
330
|
-
return commit(computeState(environment, normalized, setOptions.source ?? 'backend'));
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
return {
|
|
334
|
-
getPreference: () => ({ ...state.preference }),
|
|
335
|
-
getResolvedAppearance: () => state,
|
|
336
|
-
initialize(): NtResolvedAppearanceState {
|
|
337
|
-
if (initialized) return state;
|
|
338
|
-
initialized = true;
|
|
339
|
-
attachMediaListeners();
|
|
340
|
-
return commit(resolveBootstrapState(environment));
|
|
341
|
-
},
|
|
342
|
-
setPreference: applyPreference,
|
|
343
|
-
applyResolvedPreference: applyPreference,
|
|
344
|
-
updatePreference(partial, setOptions = {}) {
|
|
345
|
-
if (!partial || typeof partial !== 'object') return state;
|
|
346
|
-
return applyPreference({ ...state.preference, ...partial }, setOptions);
|
|
347
|
-
},
|
|
348
|
-
subscribe(listener: AppearanceChangeListener): () => void {
|
|
349
|
-
listeners.add(listener);
|
|
350
|
-
return () => listeners.delete(listener);
|
|
351
|
-
},
|
|
352
|
-
destroy(): void {
|
|
353
|
-
for (const detach of detachMediaListeners.splice(0)) detach();
|
|
354
|
-
listeners.clear();
|
|
355
|
-
initialized = false;
|
|
356
|
-
},
|
|
357
|
-
};
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
/** @deprecated New applications should create one AppearanceController. */
|
|
361
|
-
export function createThemeController(
|
|
362
|
-
options: ThemeControllerOptions = {},
|
|
363
|
-
): ThemeController {
|
|
364
|
-
const controller = createAppearanceController(options);
|
|
365
|
-
|
|
366
|
-
return {
|
|
367
|
-
getState: () => toThemeState(controller.getResolvedAppearance()),
|
|
368
|
-
initialize: () => toThemeState(controller.initialize()),
|
|
369
|
-
setPreference(preference, setOptions = {}) {
|
|
370
|
-
const theme = normalizeThemePreference(preference);
|
|
371
|
-
if (!theme) return toThemeState(controller.getResolvedAppearance());
|
|
372
|
-
return toThemeState(
|
|
373
|
-
controller.setPreference(
|
|
374
|
-
{ ...controller.getPreference(), theme },
|
|
375
|
-
setOptions,
|
|
376
|
-
),
|
|
377
|
-
);
|
|
378
|
-
},
|
|
379
|
-
subscribe(listener) {
|
|
380
|
-
return controller.subscribe((next) => listener(toThemeState(next)));
|
|
381
|
-
},
|
|
382
|
-
destroy: () => controller.destroy(),
|
|
383
|
-
};
|
|
384
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
getDefaultThemeStorage,
|
|
3
|
+
readAppearanceCache,
|
|
4
|
+
writeAppearanceCache,
|
|
5
|
+
type ThemeStorage,
|
|
6
|
+
} from './cache.js';
|
|
7
|
+
import { readAppearanceCookie } from './cookie.js';
|
|
8
|
+
import {
|
|
9
|
+
NT_DEFAULT_APPEARANCE_PREFERENCE,
|
|
10
|
+
normalizeAppearancePreference,
|
|
11
|
+
normalizeThemePreference,
|
|
12
|
+
resolveTheme,
|
|
13
|
+
} from './preference.js';
|
|
14
|
+
import type {
|
|
15
|
+
NtAppearancePreference,
|
|
16
|
+
NtResolvedAppearance,
|
|
17
|
+
NtResolvedAppearanceState,
|
|
18
|
+
ResolvedTheme,
|
|
19
|
+
ResolvedThemeState,
|
|
20
|
+
ThemePreference,
|
|
21
|
+
ThemeSource,
|
|
22
|
+
} from './types.js';
|
|
23
|
+
|
|
24
|
+
export const NT_THEME_ATTRIBUTE = 'data-nt-theme';
|
|
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';
|
|
30
|
+
|
|
31
|
+
export const NT_THEME_DEFAULT_RESOLVED: ResolvedTheme = 'dark';
|
|
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
|
+
|
|
40
|
+
export type MatchMediaRef = (query: string) => MediaQueryList;
|
|
41
|
+
|
|
42
|
+
export interface ThemeEnvironmentOptions {
|
|
43
|
+
documentRef?: Document | null;
|
|
44
|
+
matchMediaRef?: MatchMediaRef | null;
|
|
45
|
+
storage?: ThemeStorage | null;
|
|
46
|
+
cookieString?: string;
|
|
47
|
+
useCache?: boolean;
|
|
48
|
+
fallbackResolved?: ResolvedTheme;
|
|
49
|
+
/** Backend/server-resolved value. When supplied it outranks cookie and cache. */
|
|
50
|
+
serverPreference?: NtAppearancePreference | null;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type AppearanceControllerOptions = ThemeEnvironmentOptions;
|
|
54
|
+
export type ThemeControllerOptions = ThemeEnvironmentOptions;
|
|
55
|
+
export type BootstrapAppearanceOptions = ThemeEnvironmentOptions;
|
|
56
|
+
export type BootstrapThemeOptions = ThemeEnvironmentOptions;
|
|
57
|
+
|
|
58
|
+
export interface SetPreferenceOptions {
|
|
59
|
+
source?: ThemeSource;
|
|
60
|
+
updateCache?: boolean;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type AppearanceChangeListener = (state: NtResolvedAppearanceState) => void;
|
|
64
|
+
export type ThemeChangeListener = (state: ResolvedThemeState) => void;
|
|
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
|
+
/**
|
|
79
|
+
* Merges a partial change (for example `{ theme: 'light' }`) onto the current
|
|
80
|
+
* preference, so switching one dimension never resets saved accent, density,
|
|
81
|
+
* motion, or contrast choices. Invalid partial values leave state unchanged.
|
|
82
|
+
*/
|
|
83
|
+
updatePreference(
|
|
84
|
+
partial: Partial<NtAppearancePreference>,
|
|
85
|
+
options?: SetPreferenceOptions,
|
|
86
|
+
): NtResolvedAppearanceState;
|
|
87
|
+
subscribe(listener: AppearanceChangeListener): () => void;
|
|
88
|
+
destroy(): void;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** @deprecated Use AppearanceController/createAppearanceController for new integrations. */
|
|
92
|
+
export interface ThemeController {
|
|
93
|
+
getState(): ResolvedThemeState;
|
|
94
|
+
initialize(): ResolvedThemeState;
|
|
95
|
+
setPreference(
|
|
96
|
+
preference: ThemePreference,
|
|
97
|
+
options?: SetPreferenceOptions,
|
|
98
|
+
): ResolvedThemeState;
|
|
99
|
+
subscribe(listener: ThemeChangeListener): () => void;
|
|
100
|
+
destroy(): void;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function applyThemeToDocument(
|
|
104
|
+
documentRef: Document,
|
|
105
|
+
state: ResolvedThemeState,
|
|
106
|
+
): void {
|
|
107
|
+
const root = documentRef?.documentElement;
|
|
108
|
+
|
|
109
|
+
if (!root) return;
|
|
110
|
+
|
|
111
|
+
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved);
|
|
112
|
+
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
export function applyAppearanceToDocument(
|
|
116
|
+
documentRef: Document,
|
|
117
|
+
state: NtResolvedAppearanceState,
|
|
118
|
+
): void {
|
|
119
|
+
const root = documentRef?.documentElement;
|
|
120
|
+
|
|
121
|
+
if (!root) return;
|
|
122
|
+
|
|
123
|
+
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved.theme);
|
|
124
|
+
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference.theme);
|
|
125
|
+
root.setAttribute(NT_ACCENT_ATTRIBUTE, state.resolved.accent);
|
|
126
|
+
root.setAttribute(NT_DENSITY_ATTRIBUTE, state.resolved.density);
|
|
127
|
+
root.setAttribute(NT_CONTRAST_ATTRIBUTE, state.resolved.contrast);
|
|
128
|
+
root.setAttribute(NT_MOTION_ATTRIBUTE, state.resolved.motion);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
interface AppearanceEnvironment {
|
|
132
|
+
getDocument(): Document | null;
|
|
133
|
+
getMatchMedia(): MatchMediaRef | null;
|
|
134
|
+
getStorage(): ThemeStorage | null;
|
|
135
|
+
readCookie(): NtAppearancePreference | null;
|
|
136
|
+
useCache: boolean;
|
|
137
|
+
fallbackResolved: ResolvedTheme;
|
|
138
|
+
serverPreference: NtAppearancePreference | null;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function createEnvironment(options: ThemeEnvironmentOptions): AppearanceEnvironment {
|
|
142
|
+
const getDocument = (): Document | null => {
|
|
143
|
+
if (options.documentRef !== undefined) return options.documentRef;
|
|
144
|
+
return typeof document === 'undefined' ? null : document;
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const getMatchMedia = (): MatchMediaRef | null => {
|
|
148
|
+
if (options.matchMediaRef !== undefined) return options.matchMediaRef;
|
|
149
|
+
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return null;
|
|
150
|
+
return window.matchMedia.bind(window);
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const getStorage = (): ThemeStorage | null =>
|
|
154
|
+
options.storage !== undefined ? options.storage : getDefaultThemeStorage();
|
|
155
|
+
|
|
156
|
+
const readCookie = (): NtAppearancePreference | null => {
|
|
157
|
+
if (options.cookieString !== undefined) return readAppearanceCookie(options.cookieString);
|
|
158
|
+
const doc = getDocument();
|
|
159
|
+
if (!doc) return null;
|
|
160
|
+
try {
|
|
161
|
+
return readAppearanceCookie(doc.cookie);
|
|
162
|
+
} catch {
|
|
163
|
+
return null;
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
getDocument,
|
|
169
|
+
getMatchMedia,
|
|
170
|
+
getStorage,
|
|
171
|
+
readCookie,
|
|
172
|
+
useCache: options.useCache !== false,
|
|
173
|
+
fallbackResolved: options.fallbackResolved ?? NT_THEME_DEFAULT_RESOLVED,
|
|
174
|
+
serverPreference: normalizeAppearancePreference(options.serverPreference),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
function mediaMatches(environment: AppearanceEnvironment, query: string): boolean | null {
|
|
179
|
+
const matchMedia = environment.getMatchMedia();
|
|
180
|
+
if (!matchMedia) return null;
|
|
181
|
+
try {
|
|
182
|
+
return matchMedia(query).matches;
|
|
183
|
+
} catch {
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function computeState(
|
|
189
|
+
environment: AppearanceEnvironment,
|
|
190
|
+
preference: NtAppearancePreference,
|
|
191
|
+
source: ThemeSource,
|
|
192
|
+
): NtResolvedAppearanceState {
|
|
193
|
+
const systemDark = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.dark);
|
|
194
|
+
const forcedColors = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.forcedColors) === true;
|
|
195
|
+
const systemContrast = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.highContrast) === true;
|
|
196
|
+
const systemReducedMotion = mediaMatches(environment, NT_APPEARANCE_MEDIA_QUERIES.reducedMotion) === true;
|
|
197
|
+
const theme =
|
|
198
|
+
preference.theme === 'system'
|
|
199
|
+
? resolveTheme('system', systemDark ?? environment.fallbackResolved === 'dark')
|
|
200
|
+
: preference.theme;
|
|
201
|
+
const highContrast =
|
|
202
|
+
forcedColors ||
|
|
203
|
+
preference.highContrast === true ||
|
|
204
|
+
(preference.highContrast !== false && systemContrast);
|
|
205
|
+
const reduceMotion =
|
|
206
|
+
preference.reduceMotion === true ||
|
|
207
|
+
(preference.reduceMotion !== false && systemReducedMotion);
|
|
208
|
+
|
|
209
|
+
return {
|
|
210
|
+
preference: { ...preference },
|
|
211
|
+
resolved: {
|
|
212
|
+
theme,
|
|
213
|
+
accent: preference.accent,
|
|
214
|
+
density: preference.density,
|
|
215
|
+
contrast: highContrast ? 'high' : 'normal',
|
|
216
|
+
motion: reduceMotion ? 'reduced' : 'full',
|
|
217
|
+
forcedColors,
|
|
218
|
+
},
|
|
219
|
+
source:
|
|
220
|
+
preference.theme === 'system' && systemDark === null && source === 'system'
|
|
221
|
+
? 'default'
|
|
222
|
+
: source,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function resolveBootstrapState(environment: AppearanceEnvironment): NtResolvedAppearanceState {
|
|
227
|
+
if (environment.serverPreference) {
|
|
228
|
+
return computeState(environment, environment.serverPreference, 'backend');
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const cookiePreference = environment.readCookie();
|
|
232
|
+
if (cookiePreference) return computeState(environment, cookiePreference, 'cookie');
|
|
233
|
+
|
|
234
|
+
if (environment.useCache) {
|
|
235
|
+
const cached = readAppearanceCache(environment.getStorage());
|
|
236
|
+
if (cached) return computeState(environment, cached, 'cache');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
return computeState(environment, { ...NT_DEFAULT_APPEARANCE_PREFERENCE }, 'system');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export function bootstrapNeetechsAppearance(
|
|
243
|
+
options: BootstrapAppearanceOptions = {},
|
|
244
|
+
): NtResolvedAppearanceState {
|
|
245
|
+
const environment = createEnvironment(options);
|
|
246
|
+
const state = resolveBootstrapState(environment);
|
|
247
|
+
const doc = environment.getDocument();
|
|
248
|
+
if (doc) applyAppearanceToDocument(doc, state);
|
|
249
|
+
return state;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Legacy theme-only bootstrap; it also applies accent, density, contrast and motion defaults. */
|
|
253
|
+
export function bootstrapNeetechsTheme(
|
|
254
|
+
options: BootstrapThemeOptions = {},
|
|
255
|
+
): ResolvedThemeState {
|
|
256
|
+
return toThemeState(bootstrapNeetechsAppearance(options));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function toThemeState(state: NtResolvedAppearanceState): ResolvedThemeState {
|
|
260
|
+
return {
|
|
261
|
+
preference: state.preference.theme,
|
|
262
|
+
resolved: state.resolved.theme,
|
|
263
|
+
source: state.source,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
export function createAppearanceController(
|
|
268
|
+
options: AppearanceControllerOptions = {},
|
|
269
|
+
): AppearanceController {
|
|
270
|
+
const environment = createEnvironment(options);
|
|
271
|
+
const listeners = new Set<AppearanceChangeListener>();
|
|
272
|
+
const detachMediaListeners: Array<() => void> = [];
|
|
273
|
+
let initialized = false;
|
|
274
|
+
let state = computeState(
|
|
275
|
+
environment,
|
|
276
|
+
{ ...NT_DEFAULT_APPEARANCE_PREFERENCE },
|
|
277
|
+
'default',
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
function commit(next: NtResolvedAppearanceState): NtResolvedAppearanceState {
|
|
281
|
+
state = next;
|
|
282
|
+
const doc = environment.getDocument();
|
|
283
|
+
if (doc) applyAppearanceToDocument(doc, state);
|
|
284
|
+
for (const listener of [...listeners]) listener(state);
|
|
285
|
+
return state;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function handleMediaChange(): void {
|
|
289
|
+
const next = computeState(environment, state.preference, state.source);
|
|
290
|
+
if (JSON.stringify(next.resolved) !== JSON.stringify(state.resolved)) commit(next);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function attachMediaListeners(): void {
|
|
294
|
+
if (detachMediaListeners.length > 0) return;
|
|
295
|
+
const matchMedia = environment.getMatchMedia();
|
|
296
|
+
if (!matchMedia) return;
|
|
297
|
+
|
|
298
|
+
for (const query of Object.values(NT_APPEARANCE_MEDIA_QUERIES)) {
|
|
299
|
+
let mediaQueryList: MediaQueryList;
|
|
300
|
+
try {
|
|
301
|
+
mediaQueryList = matchMedia(query);
|
|
302
|
+
} catch {
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (typeof mediaQueryList.addEventListener === 'function') {
|
|
307
|
+
mediaQueryList.addEventListener('change', handleMediaChange);
|
|
308
|
+
detachMediaListeners.push(() =>
|
|
309
|
+
mediaQueryList.removeEventListener('change', handleMediaChange),
|
|
310
|
+
);
|
|
311
|
+
} else if (typeof mediaQueryList.addListener === 'function') {
|
|
312
|
+
mediaQueryList.addListener(handleMediaChange);
|
|
313
|
+
detachMediaListeners.push(() => mediaQueryList.removeListener(handleMediaChange));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function applyPreference(
|
|
319
|
+
preference: NtAppearancePreference,
|
|
320
|
+
setOptions: SetPreferenceOptions = {},
|
|
321
|
+
): NtResolvedAppearanceState {
|
|
322
|
+
const normalized = normalizeAppearancePreference(preference);
|
|
323
|
+
if (!normalized) return state;
|
|
324
|
+
|
|
325
|
+
if (environment.useCache && setOptions.updateCache !== false) {
|
|
326
|
+
writeAppearanceCache(normalized, environment.getStorage());
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
attachMediaListeners();
|
|
330
|
+
return commit(computeState(environment, normalized, setOptions.source ?? 'backend'));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return {
|
|
334
|
+
getPreference: () => ({ ...state.preference }),
|
|
335
|
+
getResolvedAppearance: () => state,
|
|
336
|
+
initialize(): NtResolvedAppearanceState {
|
|
337
|
+
if (initialized) return state;
|
|
338
|
+
initialized = true;
|
|
339
|
+
attachMediaListeners();
|
|
340
|
+
return commit(resolveBootstrapState(environment));
|
|
341
|
+
},
|
|
342
|
+
setPreference: applyPreference,
|
|
343
|
+
applyResolvedPreference: applyPreference,
|
|
344
|
+
updatePreference(partial, setOptions = {}) {
|
|
345
|
+
if (!partial || typeof partial !== 'object') return state;
|
|
346
|
+
return applyPreference({ ...state.preference, ...partial }, setOptions);
|
|
347
|
+
},
|
|
348
|
+
subscribe(listener: AppearanceChangeListener): () => void {
|
|
349
|
+
listeners.add(listener);
|
|
350
|
+
return () => listeners.delete(listener);
|
|
351
|
+
},
|
|
352
|
+
destroy(): void {
|
|
353
|
+
for (const detach of detachMediaListeners.splice(0)) detach();
|
|
354
|
+
listeners.clear();
|
|
355
|
+
initialized = false;
|
|
356
|
+
},
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/** @deprecated New applications should create one AppearanceController. */
|
|
361
|
+
export function createThemeController(
|
|
362
|
+
options: ThemeControllerOptions = {},
|
|
363
|
+
): ThemeController {
|
|
364
|
+
const controller = createAppearanceController(options);
|
|
365
|
+
|
|
366
|
+
return {
|
|
367
|
+
getState: () => toThemeState(controller.getResolvedAppearance()),
|
|
368
|
+
initialize: () => toThemeState(controller.initialize()),
|
|
369
|
+
setPreference(preference, setOptions = {}) {
|
|
370
|
+
const theme = normalizeThemePreference(preference);
|
|
371
|
+
if (!theme) return toThemeState(controller.getResolvedAppearance());
|
|
372
|
+
return toThemeState(
|
|
373
|
+
controller.setPreference(
|
|
374
|
+
{ ...controller.getPreference(), theme },
|
|
375
|
+
setOptions,
|
|
376
|
+
),
|
|
377
|
+
);
|
|
378
|
+
},
|
|
379
|
+
subscribe(listener) {
|
|
380
|
+
return controller.subscribe((next) => listener(toThemeState(next)));
|
|
381
|
+
},
|
|
382
|
+
destroy: () => controller.destroy(),
|
|
383
|
+
};
|
|
384
|
+
}
|