@neetechs/ui 0.0.2 → 0.0.3
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/README.md +10 -0
- package/dist/ai/ai-action-button/ai-action-button.css +269 -269
- package/dist/ai/ai-approval-card/ai-approval-card.css +320 -320
- package/dist/ai/ai-conversation-panel/ai-conversation-panel.css +403 -403
- package/dist/ai/ai-prompt-input/ai-prompt-input.css +377 -377
- package/dist/ai/ai-proposal-card/ai-proposal-card.css +358 -358
- package/dist/ai/ai-suggestion-card/ai-suggestion-card.css +293 -293
- package/dist/ai/ai-tool-call/ai-tool-call.css +255 -255
- package/dist/ai/index.css +1 -1
- package/dist/appearance/cache.d.ts +50 -0
- package/dist/appearance/cache.d.ts.map +1 -0
- package/dist/appearance/cache.js +126 -0
- package/dist/appearance/cache.js.map +1 -0
- package/dist/appearance/cookie.d.ts +18 -0
- package/dist/appearance/cookie.d.ts.map +1 -0
- package/dist/appearance/cookie.js +56 -0
- package/dist/appearance/cookie.js.map +1 -0
- package/dist/appearance/index.d.ts +8 -0
- package/dist/appearance/index.d.ts.map +1 -0
- package/dist/appearance/index.js +5 -0
- package/dist/appearance/index.js.map +1 -0
- package/dist/appearance/preference.d.ts +13 -0
- package/dist/appearance/preference.d.ts.map +1 -0
- package/dist/appearance/preference.js +26 -0
- package/dist/appearance/preference.js.map +1 -0
- package/dist/appearance/theme-controller.d.ts +80 -0
- package/dist/appearance/theme-controller.d.ts.map +1 -0
- package/dist/appearance/theme-controller.js +235 -0
- package/dist/appearance/theme-controller.js.map +1 -0
- package/dist/appearance/types.d.ts +21 -0
- package/dist/appearance/types.d.ts.map +1 -0
- package/dist/appearance/types.js +2 -0
- package/dist/appearance/types.js.map +1 -0
- package/dist/components/alert/alert.css +216 -216
- package/dist/components/avatar/avatar.css +218 -218
- package/dist/components/badge/badge.css +174 -174
- package/dist/components/breadcrumb/breadcrumb.css +204 -204
- package/dist/components/button/button.css +166 -166
- package/dist/components/card/card.css +219 -219
- package/dist/components/checkbox/checkbox.css +186 -186
- package/dist/components/chip/chip.css +267 -267
- package/dist/components/data-grid/data-grid.css +368 -368
- package/dist/components/dialog/dialog.css +274 -274
- package/dist/components/drawer/drawer.css +402 -402
- package/dist/components/dropdown/dropdown.css +297 -297
- package/dist/components/icon-button/icon-button.css +157 -157
- package/dist/components/input/input.css +162 -162
- package/dist/components/menu/menu.css +313 -313
- package/dist/components/modal/modal.css +317 -317
- package/dist/components/pagination/pagination.css +249 -249
- package/dist/components/panel/panel.css +282 -282
- package/dist/components/progress/progress.css +247 -247
- package/dist/components/radio/radio.css +163 -163
- package/dist/components/segmented-control/segmented-control.css +204 -204
- package/dist/components/select/select.css +102 -102
- package/dist/components/skeleton/skeleton.css +262 -262
- package/dist/components/split-button/split-button.css +284 -284
- package/dist/components/switch/switch.css +180 -180
- package/dist/components/tabs/tabs.css +288 -288
- package/dist/components/textarea/textarea.css +85 -85
- package/dist/components/toast/toast.css +322 -322
- package/dist/components/tooltip/tooltip.css +254 -254
- package/dist/domain/billing/index.css +497 -497
- package/dist/domain/calendar/day-slice/day-slice.css +382 -382
- package/dist/domain/calendar/day-timeline/day-timeline.css +323 -323
- package/dist/domain/calendar/event-card/event-card.css +336 -336
- package/dist/domain/calendar/month-grid/month-grid.css +419 -419
- package/dist/domain/calendar/week-grid/week-grid.css +469 -469
- package/dist/domain/companies/index.css +525 -525
- package/dist/index.css +5 -5
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/patterns/command-palette/command-palette.css +438 -438
- package/dist/patterns/company-switcher/company-switcher.css +362 -362
- package/dist/patterns/empty-state/empty-state.css +258 -258
- package/dist/patterns/error-state/error-state.css +271 -271
- package/dist/patterns/loading-state/loading-state.css +287 -287
- package/dist/patterns/notification-center/notification-center.css +437 -437
- package/dist/patterns/profile-menu/profile-menu.css +296 -296
- package/dist/patterns/search-bar/search-bar.css +443 -443
- package/dist/patterns/workspace-switcher/workspace-switcher.css +379 -379
- package/dist/shell/app-shell/app-shell.css +67 -67
- package/dist/shell/content-frame/content-frame.css +93 -93
- package/dist/shell/header/header.css +177 -177
- package/dist/shell/index.css +5 -5
- package/dist/shell/mobile-nav/mobile-nav.css +170 -170
- package/dist/shell/right-drawer/right-drawer.css +107 -107
- package/dist/shell/sidebar/sidebar.css +145 -145
- package/dist/styles/animations/motion.css +161 -161
- package/dist/styles/base.css +81 -81
- package/dist/styles/reset.css +50 -50
- package/dist/styles/themes/dark.css +119 -119
- package/dist/styles/themes/high-contrast.css +118 -118
- package/dist/styles/themes/light.css +118 -118
- package/dist/styles/tokens/breakpoints.css +14 -14
- package/dist/styles/tokens/color.css +96 -96
- package/dist/styles/tokens/elevation.css +31 -31
- package/dist/styles/tokens/motion.css +35 -35
- package/dist/styles/tokens/radius.css +20 -20
- package/dist/styles/tokens/spacing.css +49 -49
- package/dist/styles/tokens/typography.css +83 -83
- package/dist/styles/tokens/z-index.css +14 -14
- package/dist/styles/utilities/layout.css +134 -134
- package/dist/styles/utilities/text.css +151 -151
- package/dist/styles/utilities/visibility.css +47 -47
- package/docs/theming.md +227 -0
- package/package.json +23 -16
- package/src/ai/ai-action-button/ai-action-button.css +269 -269
- package/src/ai/ai-approval-card/ai-approval-card.css +320 -320
- package/src/ai/ai-conversation-panel/ai-conversation-panel.css +403 -403
- package/src/ai/ai-prompt-input/ai-prompt-input.css +377 -377
- package/src/ai/ai-proposal-card/ai-proposal-card.css +358 -358
- package/src/ai/ai-suggestion-card/ai-suggestion-card.css +293 -293
- package/src/ai/ai-tool-call/ai-tool-call.css +255 -255
- package/src/ai/index.css +1 -1
- package/src/appearance/cache.ts +160 -0
- package/src/appearance/cookie.ts +68 -0
- package/src/appearance/index.ts +38 -0
- package/src/appearance/preference.ts +35 -0
- package/src/appearance/theme-controller.ts +383 -0
- package/src/appearance/types.ts +23 -0
- package/src/behaviors/dialog.ts +167 -167
- package/src/behaviors/drawer.ts +176 -176
- package/src/behaviors/dropdown.ts +280 -280
- package/src/behaviors/focus-trap.ts +260 -260
- package/src/behaviors/ids.ts +47 -47
- package/src/behaviors/index.ts +6 -6
- package/src/behaviors/keyboard.ts +302 -302
- package/src/behaviors/overlay.ts +230 -230
- package/src/components/alert/alert.css +216 -216
- package/src/components/avatar/avatar.css +218 -218
- package/src/components/badge/badge.css +174 -174
- package/src/components/breadcrumb/breadcrumb.css +204 -204
- package/src/components/button/button.css +166 -166
- package/src/components/card/card.css +219 -219
- package/src/components/checkbox/checkbox.css +186 -186
- package/src/components/chip/chip.css +267 -267
- package/src/components/data-grid/data-grid.css +368 -368
- package/src/components/dialog/dialog.css +274 -274
- package/src/components/drawer/drawer.css +402 -402
- package/src/components/dropdown/dropdown.css +297 -297
- package/src/components/icon-button/icon-button.css +157 -157
- package/src/components/input/input.css +162 -162
- package/src/components/menu/menu.css +313 -313
- package/src/components/modal/modal.css +317 -317
- package/src/components/pagination/pagination.css +249 -249
- package/src/components/panel/panel.css +282 -282
- package/src/components/progress/progress.css +247 -247
- package/src/components/radio/radio.css +163 -163
- package/src/components/segmented-control/segmented-control.css +204 -204
- package/src/components/select/select.css +102 -102
- package/src/components/skeleton/skeleton.css +262 -262
- package/src/components/split-button/split-button.css +284 -284
- package/src/components/switch/switch.css +180 -180
- package/src/components/tabs/tabs.css +288 -288
- package/src/components/textarea/textarea.css +85 -85
- package/src/components/toast/toast.css +322 -322
- package/src/components/tooltip/tooltip.css +254 -254
- package/src/domain/billing/index.css +497 -497
- package/src/domain/calendar/day-slice/day-slice.css +382 -382
- package/src/domain/calendar/day-timeline/day-timeline.css +323 -323
- package/src/domain/calendar/event-card/event-card.css +336 -336
- package/src/domain/calendar/month-grid/month-grid.css +419 -419
- package/src/domain/calendar/week-grid/week-grid.css +469 -469
- package/src/domain/companies/index.css +525 -525
- package/src/index.css +5 -5
- package/src/index.ts +6 -5
- package/src/patterns/command-palette/command-palette.css +438 -438
- package/src/patterns/company-switcher/company-switcher.css +362 -362
- package/src/patterns/empty-state/empty-state.css +258 -258
- package/src/patterns/error-state/error-state.css +271 -271
- package/src/patterns/loading-state/loading-state.css +287 -287
- package/src/patterns/notification-center/notification-center.css +437 -437
- package/src/patterns/profile-menu/profile-menu.css +296 -296
- package/src/patterns/search-bar/search-bar.css +443 -443
- package/src/patterns/workspace-switcher/workspace-switcher.css +379 -379
- package/src/shell/app-shell/app-shell.css +67 -67
- package/src/shell/content-frame/content-frame.css +93 -93
- package/src/shell/header/header.css +177 -177
- package/src/shell/index.css +5 -5
- package/src/shell/mobile-nav/mobile-nav.css +170 -170
- package/src/shell/right-drawer/right-drawer.css +107 -107
- package/src/shell/sidebar/sidebar.css +145 -145
- package/src/styles/animations/motion.css +161 -161
- package/src/styles/base.css +81 -81
- package/src/styles/reset.css +50 -50
- package/src/styles/themes/dark.css +119 -119
- package/src/styles/themes/high-contrast.css +118 -118
- package/src/styles/themes/light.css +118 -118
- package/src/styles/tokens/breakpoints.css +14 -14
- package/src/styles/tokens/color.css +96 -96
- package/src/styles/tokens/elevation.css +31 -31
- package/src/styles/tokens/motion.css +35 -35
- package/src/styles/tokens/radius.css +20 -20
- package/src/styles/tokens/spacing.css +49 -49
- package/src/styles/tokens/typography.css +83 -83
- package/src/styles/tokens/z-index.css +14 -14
- package/src/styles/utilities/layout.css +134 -134
- package/src/styles/utilities/text.css +151 -151
- package/src/styles/utilities/visibility.css +47 -47
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { normalizeThemePreference } from './preference.js';
|
|
2
|
+
import type { ThemePreference } from './types.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Shared-domain bootstrap cookie set by the backend
|
|
6
|
+
* (`Domain=.neetechs.com; Secure; SameSite=Lax` in production).
|
|
7
|
+
* It only reduces theme flashing; the backend account setting is the
|
|
8
|
+
* source of truth.
|
|
9
|
+
*/
|
|
10
|
+
export const NT_THEME_COOKIE_NAME = 'nt_theme';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Reads the `nt_theme` bootstrap cookie. Only reads this single cookie —
|
|
14
|
+
* never authentication or other sensitive cookies.
|
|
15
|
+
*
|
|
16
|
+
* Pass `cookieString` explicitly during SSR (e.g. the request `Cookie`
|
|
17
|
+
* header). When omitted, `document.cookie` is used if a document exists;
|
|
18
|
+
* otherwise `null` is returned.
|
|
19
|
+
*/
|
|
20
|
+
export function readThemeCookie(cookieString?: string): ThemePreference | null {
|
|
21
|
+
const source = cookieString ?? readDocumentCookieString();
|
|
22
|
+
|
|
23
|
+
if (!source) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
for (const pair of source.split(';')) {
|
|
28
|
+
const separatorIndex = pair.indexOf('=');
|
|
29
|
+
|
|
30
|
+
if (separatorIndex === -1) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (pair.slice(0, separatorIndex).trim() !== NT_THEME_COOKIE_NAME) {
|
|
35
|
+
continue;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const preference = normalizeThemePreference(
|
|
39
|
+
decodeCookieValue(pair.slice(separatorIndex + 1).trim()),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
if (preference !== null) {
|
|
43
|
+
return preference;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function decodeCookieValue(value: string): string {
|
|
51
|
+
try {
|
|
52
|
+
return decodeURIComponent(value);
|
|
53
|
+
} catch {
|
|
54
|
+
return value;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function readDocumentCookieString(): string | null {
|
|
59
|
+
if (typeof document === 'undefined') {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
return document.cookie;
|
|
65
|
+
} catch {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
ResolvedTheme,
|
|
3
|
+
ResolvedThemeState,
|
|
4
|
+
ThemePreference,
|
|
5
|
+
ThemeSource,
|
|
6
|
+
} from './types.js';
|
|
7
|
+
|
|
8
|
+
export { normalizeThemePreference, resolveTheme } from './preference.js';
|
|
9
|
+
|
|
10
|
+
export { NT_THEME_COOKIE_NAME, readThemeCookie } from './cookie.js';
|
|
11
|
+
|
|
12
|
+
export {
|
|
13
|
+
NT_LEGACY_THEME_CACHE_KEYS,
|
|
14
|
+
NT_THEME_CACHE_KEY,
|
|
15
|
+
clearThemeCache,
|
|
16
|
+
getDefaultThemeStorage,
|
|
17
|
+
migrateLegacyThemeCache,
|
|
18
|
+
readThemeCache,
|
|
19
|
+
writeThemeCache,
|
|
20
|
+
} from './cache.js';
|
|
21
|
+
export type { MigrateLegacyThemeCacheOptions, ThemeStorage } from './cache.js';
|
|
22
|
+
|
|
23
|
+
export {
|
|
24
|
+
NT_THEME_ATTRIBUTE,
|
|
25
|
+
NT_THEME_DEFAULT_RESOLVED,
|
|
26
|
+
NT_THEME_PREFERENCE_ATTRIBUTE,
|
|
27
|
+
applyThemeToDocument,
|
|
28
|
+
bootstrapNeetechsTheme,
|
|
29
|
+
createThemeController,
|
|
30
|
+
} from './theme-controller.js';
|
|
31
|
+
export type {
|
|
32
|
+
BootstrapThemeOptions,
|
|
33
|
+
MatchMediaRef,
|
|
34
|
+
SetPreferenceOptions,
|
|
35
|
+
ThemeChangeListener,
|
|
36
|
+
ThemeController,
|
|
37
|
+
ThemeControllerOptions,
|
|
38
|
+
} from './theme-controller.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { ResolvedTheme, ThemePreference } from './types.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Validates an untrusted value (cookie, cache, backend payload) into a
|
|
5
|
+
* ThemePreference. Anything other than `system`, `light` or `dark` is
|
|
6
|
+
* rejected with `null`.
|
|
7
|
+
*/
|
|
8
|
+
export function normalizeThemePreference(value: unknown): ThemePreference | null {
|
|
9
|
+
if (typeof value !== 'string') {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const normalized = value.trim().toLowerCase();
|
|
14
|
+
|
|
15
|
+
if (normalized === 'system' || normalized === 'light' || normalized === 'dark') {
|
|
16
|
+
return normalized;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Resolves a stored preference to the theme rendered on the document.
|
|
24
|
+
* `systemDark` is the current `(prefers-color-scheme: dark)` match state.
|
|
25
|
+
*/
|
|
26
|
+
export function resolveTheme(
|
|
27
|
+
preference: ThemePreference,
|
|
28
|
+
systemDark: boolean,
|
|
29
|
+
): ResolvedTheme {
|
|
30
|
+
if (preference === 'system') {
|
|
31
|
+
return systemDark ? 'dark' : 'light';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return preference;
|
|
35
|
+
}
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import {
|
|
2
|
+
getDefaultThemeStorage,
|
|
3
|
+
readThemeCache,
|
|
4
|
+
writeThemeCache,
|
|
5
|
+
type ThemeStorage,
|
|
6
|
+
} from './cache.js';
|
|
7
|
+
import { readThemeCookie } from './cookie.js';
|
|
8
|
+
import { normalizeThemePreference, resolveTheme } from './preference.js';
|
|
9
|
+
import type {
|
|
10
|
+
ResolvedTheme,
|
|
11
|
+
ResolvedThemeState,
|
|
12
|
+
ThemePreference,
|
|
13
|
+
ThemeSource,
|
|
14
|
+
} from './types.js';
|
|
15
|
+
|
|
16
|
+
/** Canonical rendering attribute consumed by the theme CSS. */
|
|
17
|
+
export const NT_THEME_ATTRIBUTE = 'data-nt-theme';
|
|
18
|
+
|
|
19
|
+
/** Informational attribute exposing the stored preference. */
|
|
20
|
+
export const NT_THEME_PREFERENCE_ATTRIBUTE = 'data-nt-theme-preference';
|
|
21
|
+
|
|
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
|
+
export const NT_THEME_DEFAULT_RESOLVED: ResolvedTheme = 'dark';
|
|
29
|
+
|
|
30
|
+
export type MatchMediaRef = (query: string) => MediaQueryList;
|
|
31
|
+
|
|
32
|
+
export interface ThemeEnvironmentOptions {
|
|
33
|
+
/** Explicit document (or `null` to force SSR behavior). Defaults to the global `document` when one exists. */
|
|
34
|
+
documentRef?: Document | null;
|
|
35
|
+
/** Explicit matchMedia (or `null` to disable system detection). Defaults to `window.matchMedia` when available. */
|
|
36
|
+
matchMediaRef?: MatchMediaRef | null;
|
|
37
|
+
/** Explicit storage (or `null` to disable the cache). Defaults to `window.localStorage` when accessible. */
|
|
38
|
+
storage?: ThemeStorage | null;
|
|
39
|
+
/** Explicit cookie string for SSR (e.g. the request `Cookie` header). Defaults to `document.cookie`. */
|
|
40
|
+
cookieString?: string;
|
|
41
|
+
/** Set to false to skip reading and writing the local cache. Default true. */
|
|
42
|
+
useCache?: boolean;
|
|
43
|
+
/** Resolved theme used when the system preference is unknown. Default `'dark'`. */
|
|
44
|
+
fallbackResolved?: ResolvedTheme;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export type ThemeControllerOptions = ThemeEnvironmentOptions;
|
|
48
|
+
|
|
49
|
+
export type BootstrapThemeOptions = ThemeEnvironmentOptions;
|
|
50
|
+
|
|
51
|
+
export interface SetPreferenceOptions {
|
|
52
|
+
/** Where the preference came from. Default `'backend'`. */
|
|
53
|
+
source?: ThemeSource;
|
|
54
|
+
/** Set to false to skip updating the local cache. Default true. */
|
|
55
|
+
updateCache?: boolean;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type ThemeChangeListener = (state: ResolvedThemeState) => void;
|
|
59
|
+
|
|
60
|
+
export interface ThemeController {
|
|
61
|
+
/** Current state without side effects. */
|
|
62
|
+
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
|
+
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
|
+
setPreference(
|
|
75
|
+
preference: ThemePreference,
|
|
76
|
+
options?: SetPreferenceOptions,
|
|
77
|
+
): ResolvedThemeState;
|
|
78
|
+
/** Notifies on every applied state change. Returns an unsubscribe function. */
|
|
79
|
+
subscribe(listener: ThemeChangeListener): () => void;
|
|
80
|
+
/** Removes listeners and subscribers. The controller can be initialized again. */
|
|
81
|
+
destroy(): void;
|
|
82
|
+
}
|
|
83
|
+
|
|
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
|
+
export function applyThemeToDocument(
|
|
93
|
+
documentRef: Document,
|
|
94
|
+
state: ResolvedThemeState,
|
|
95
|
+
): void {
|
|
96
|
+
const root = documentRef?.documentElement;
|
|
97
|
+
|
|
98
|
+
if (!root) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
root.setAttribute(NT_THEME_ATTRIBUTE, state.resolved);
|
|
103
|
+
root.setAttribute(NT_THEME_PREFERENCE_ATTRIBUTE, state.preference);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
interface ThemeEnvironment {
|
|
107
|
+
getDocument(): Document | null;
|
|
108
|
+
getMatchMedia(): MatchMediaRef | null;
|
|
109
|
+
getStorage(): ThemeStorage | null;
|
|
110
|
+
readCookie(): ThemePreference | null;
|
|
111
|
+
useCache: boolean;
|
|
112
|
+
fallbackResolved: ResolvedTheme;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function createEnvironment(options: ThemeEnvironmentOptions): ThemeEnvironment {
|
|
116
|
+
const getDocument = (): Document | null => {
|
|
117
|
+
if (options.documentRef !== undefined) {
|
|
118
|
+
return options.documentRef;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return typeof document === 'undefined' ? null : document;
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const getMatchMedia = (): MatchMediaRef | null => {
|
|
125
|
+
if (options.matchMediaRef !== undefined) {
|
|
126
|
+
return options.matchMediaRef;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') {
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return window.matchMedia.bind(window);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const getStorage = (): ThemeStorage | null => {
|
|
137
|
+
if (options.storage !== undefined) {
|
|
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
|
+
}
|
|
148
|
+
|
|
149
|
+
const doc = getDocument();
|
|
150
|
+
|
|
151
|
+
if (!doc) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
try {
|
|
156
|
+
return readThemeCookie(doc.cookie);
|
|
157
|
+
} catch {
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
return {
|
|
163
|
+
getDocument,
|
|
164
|
+
getMatchMedia,
|
|
165
|
+
getStorage,
|
|
166
|
+
readCookie,
|
|
167
|
+
useCache: options.useCache !== false,
|
|
168
|
+
fallbackResolved: options.fallbackResolved ?? NT_THEME_DEFAULT_RESOLVED,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function readSystemDark(environment: ThemeEnvironment): boolean | null {
|
|
173
|
+
const matchMedia = environment.getMatchMedia();
|
|
174
|
+
|
|
175
|
+
if (!matchMedia) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
try {
|
|
180
|
+
return matchMedia(PREFERS_DARK_QUERY).matches;
|
|
181
|
+
} catch {
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function computeState(
|
|
187
|
+
environment: ThemeEnvironment,
|
|
188
|
+
preference: ThemePreference,
|
|
189
|
+
source: ThemeSource,
|
|
190
|
+
): ResolvedThemeState {
|
|
191
|
+
if (preference !== 'system') {
|
|
192
|
+
return { preference, resolved: preference, source };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const systemDark = readSystemDark(environment);
|
|
196
|
+
|
|
197
|
+
if (systemDark === null) {
|
|
198
|
+
return {
|
|
199
|
+
preference,
|
|
200
|
+
resolved: environment.fallbackResolved,
|
|
201
|
+
source: source === 'system' ? 'default' : source,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return { preference, resolved: resolveTheme('system', systemDark), source };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function resolveBootstrapState(environment: ThemeEnvironment): ResolvedThemeState {
|
|
209
|
+
const cookiePreference = environment.readCookie();
|
|
210
|
+
|
|
211
|
+
if (cookiePreference !== null) {
|
|
212
|
+
return computeState(environment, cookiePreference, 'cookie');
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
if (environment.useCache) {
|
|
216
|
+
const cachedPreference = readThemeCache(environment.getStorage());
|
|
217
|
+
|
|
218
|
+
if (cachedPreference !== null) {
|
|
219
|
+
return computeState(environment, cachedPreference, 'cache');
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return computeState(environment, 'system', 'system');
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Minimal early bootstrap, safe to run before framework rendering (from
|
|
228
|
+
* `main.ts`, `main.tsx`, an inline script or an Angular initializer).
|
|
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 {
|
|
237
|
+
const environment = createEnvironment(options);
|
|
238
|
+
const state = resolveBootstrapState(environment);
|
|
239
|
+
const doc = environment.getDocument();
|
|
240
|
+
|
|
241
|
+
if (doc) {
|
|
242
|
+
applyThemeToDocument(doc, state);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return state;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Creates the single per-application theme controller. Framework-neutral:
|
|
250
|
+
* no browser globals are touched until `initialize`, `setPreference` or
|
|
251
|
+
* `getState` run, and every global can be injected for SSR and tests.
|
|
252
|
+
*/
|
|
253
|
+
export function createThemeController(
|
|
254
|
+
options: ThemeControllerOptions = {},
|
|
255
|
+
): ThemeController {
|
|
256
|
+
const environment = createEnvironment(options);
|
|
257
|
+
const listeners = new Set<ThemeChangeListener>();
|
|
258
|
+
|
|
259
|
+
let state: ResolvedThemeState = {
|
|
260
|
+
preference: 'system',
|
|
261
|
+
resolved: environment.fallbackResolved,
|
|
262
|
+
source: 'default',
|
|
263
|
+
};
|
|
264
|
+
let initialized = false;
|
|
265
|
+
let detachSystemListener: (() => void) | null = null;
|
|
266
|
+
|
|
267
|
+
function commit(next: ResolvedThemeState): ResolvedThemeState {
|
|
268
|
+
state = next;
|
|
269
|
+
|
|
270
|
+
const doc = environment.getDocument();
|
|
271
|
+
|
|
272
|
+
if (doc) {
|
|
273
|
+
applyThemeToDocument(doc, state);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
for (const listener of [...listeners]) {
|
|
277
|
+
listener(state);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
return state;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function handleSystemChange(event: MediaQueryListEvent): void {
|
|
284
|
+
if (state.preference !== 'system') {
|
|
285
|
+
return;
|
|
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 });
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function attachSystemListener(): void {
|
|
299
|
+
if (detachSystemListener) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const matchMedia = environment.getMatchMedia();
|
|
304
|
+
|
|
305
|
+
if (!matchMedia) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let mediaQueryList: MediaQueryList;
|
|
310
|
+
|
|
311
|
+
try {
|
|
312
|
+
mediaQueryList = matchMedia(PREFERS_DARK_QUERY);
|
|
313
|
+
} catch {
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (typeof mediaQueryList.addEventListener === 'function') {
|
|
318
|
+
mediaQueryList.addEventListener('change', handleSystemChange);
|
|
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
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return {
|
|
332
|
+
getState(): ResolvedThemeState {
|
|
333
|
+
return state;
|
|
334
|
+
},
|
|
335
|
+
|
|
336
|
+
initialize(): ResolvedThemeState {
|
|
337
|
+
if (initialized) {
|
|
338
|
+
return state;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
initialized = true;
|
|
342
|
+
attachSystemListener();
|
|
343
|
+
|
|
344
|
+
return commit(resolveBootstrapState(environment));
|
|
345
|
+
},
|
|
346
|
+
|
|
347
|
+
setPreference(
|
|
348
|
+
preference: ThemePreference,
|
|
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 {
|
|
369
|
+
listeners.add(listener);
|
|
370
|
+
|
|
371
|
+
return () => {
|
|
372
|
+
listeners.delete(listener);
|
|
373
|
+
};
|
|
374
|
+
},
|
|
375
|
+
|
|
376
|
+
destroy(): void {
|
|
377
|
+
detachSystemListener?.();
|
|
378
|
+
detachSystemListener = null;
|
|
379
|
+
listeners.clear();
|
|
380
|
+
initialized = false;
|
|
381
|
+
},
|
|
382
|
+
};
|
|
383
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stored appearance preference. Persisted by the Django backend and edited
|
|
3
|
+
* in MyAccount. The shared package only bootstraps, resolves and applies it.
|
|
4
|
+
*/
|
|
5
|
+
export type ThemePreference = 'system' | 'light' | 'dark';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Theme actually rendered on the document. `system` always resolves to one
|
|
9
|
+
* of these before touching the DOM.
|
|
10
|
+
*/
|
|
11
|
+
export type ResolvedTheme = 'light' | 'dark';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Where the currently applied preference came from, in source-of-truth
|
|
15
|
+
* order: backend > cookie > cache > system > default.
|
|
16
|
+
*/
|
|
17
|
+
export type ThemeSource = 'backend' | 'cookie' | 'cache' | 'system' | 'default';
|
|
18
|
+
|
|
19
|
+
export interface ResolvedThemeState {
|
|
20
|
+
preference: ThemePreference;
|
|
21
|
+
resolved: ResolvedTheme;
|
|
22
|
+
source: ThemeSource;
|
|
23
|
+
}
|