@oxyhq/bloom 0.6.12 → 0.6.14
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/lib/commonjs/hooks/useControllableState.js +35 -0
- package/lib/commonjs/hooks/useControllableState.js.map +1 -0
- package/lib/commonjs/theme/BloomThemeProvider.js +134 -112
- package/lib/commonjs/theme/BloomThemeProvider.js.map +1 -1
- package/lib/commonjs/theme/apply-dark-class.js +6 -1
- package/lib/commonjs/theme/apply-dark-class.js.map +1 -1
- package/lib/commonjs/theme/build-theme.js +110 -0
- package/lib/commonjs/theme/build-theme.js.map +1 -0
- package/lib/commonjs/theme/color-presets.js +15 -0
- package/lib/commonjs/theme/color-presets.js.map +1 -1
- package/lib/commonjs/theme/index.js +15 -1
- package/lib/commonjs/theme/index.js.map +1 -1
- package/lib/commonjs/theme/persistence.js +127 -0
- package/lib/commonjs/theme/persistence.js.map +1 -0
- package/lib/commonjs/theme/use-isomorphic-layout-effect.js +15 -0
- package/lib/commonjs/theme/use-isomorphic-layout-effect.js.map +1 -0
- package/lib/module/hooks/useControllableState.js +31 -0
- package/lib/module/hooks/useControllableState.js.map +1 -0
- package/lib/module/theme/BloomThemeProvider.js +135 -112
- package/lib/module/theme/BloomThemeProvider.js.map +1 -1
- package/lib/module/theme/apply-dark-class.js +6 -1
- package/lib/module/theme/apply-dark-class.js.map +1 -1
- package/lib/module/theme/build-theme.js +105 -0
- package/lib/module/theme/build-theme.js.map +1 -0
- package/lib/module/theme/color-presets.js +15 -0
- package/lib/module/theme/color-presets.js.map +1 -1
- package/lib/module/theme/index.js +3 -1
- package/lib/module/theme/index.js.map +1 -1
- package/lib/module/theme/persistence.js +120 -0
- package/lib/module/theme/persistence.js.map +1 -0
- package/lib/module/theme/use-isomorphic-layout-effect.js +12 -0
- package/lib/module/theme/use-isomorphic-layout-effect.js.map +1 -0
- package/lib/typescript/commonjs/hooks/useControllableState.d.ts +19 -0
- package/lib/typescript/commonjs/hooks/useControllableState.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/BloomThemeProvider.d.ts +29 -12
- package/lib/typescript/commonjs/theme/BloomThemeProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/apply-dark-class.d.ts +5 -0
- package/lib/typescript/commonjs/theme/apply-dark-class.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/build-theme.d.ts +20 -0
- package/lib/typescript/commonjs/theme/build-theme.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/color-presets.d.ts +18 -3
- package/lib/typescript/commonjs/theme/color-presets.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/index.d.ts +7 -4
- package/lib/typescript/commonjs/theme/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/persistence.d.ts +48 -0
- package/lib/typescript/commonjs/theme/persistence.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/use-isomorphic-layout-effect.d.ts +8 -0
- package/lib/typescript/commonjs/theme/use-isomorphic-layout-effect.d.ts.map +1 -0
- package/lib/typescript/module/hooks/useControllableState.d.ts +19 -0
- package/lib/typescript/module/hooks/useControllableState.d.ts.map +1 -0
- package/lib/typescript/module/theme/BloomThemeProvider.d.ts +29 -12
- package/lib/typescript/module/theme/BloomThemeProvider.d.ts.map +1 -1
- package/lib/typescript/module/theme/apply-dark-class.d.ts +5 -0
- package/lib/typescript/module/theme/apply-dark-class.d.ts.map +1 -1
- package/lib/typescript/module/theme/build-theme.d.ts +20 -0
- package/lib/typescript/module/theme/build-theme.d.ts.map +1 -0
- package/lib/typescript/module/theme/color-presets.d.ts +18 -3
- package/lib/typescript/module/theme/color-presets.d.ts.map +1 -1
- package/lib/typescript/module/theme/index.d.ts +7 -4
- package/lib/typescript/module/theme/index.d.ts.map +1 -1
- package/lib/typescript/module/theme/persistence.d.ts +48 -0
- package/lib/typescript/module/theme/persistence.d.ts.map +1 -0
- package/lib/typescript/module/theme/use-isomorphic-layout-effect.d.ts +8 -0
- package/lib/typescript/module/theme/use-isomorphic-layout-effect.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/BloomThemeProvider.modes.test.tsx +159 -0
- package/src/__tests__/BloomThemeProvider.persistence.test.tsx +217 -0
- package/src/__tests__/persistence.test.ts +155 -0
- package/src/__tests__/theme.test.ts +1 -1
- package/src/hooks/useControllableState.ts +44 -0
- package/src/theme/BloomThemeProvider.tsx +219 -157
- package/src/theme/apply-dark-class.ts +6 -1
- package/src/theme/build-theme.ts +128 -0
- package/src/theme/color-presets.ts +19 -3
- package/src/theme/index.ts +16 -4
- package/src/theme/persistence.ts +149 -0
- package/src/theme/use-isomorphic-layout-effect.ts +10 -0
|
@@ -3,107 +3,37 @@
|
|
|
3
3
|
// throw "Cannot manually set color scheme, as dark mode is type 'media'" the
|
|
4
4
|
// first time Bloom toggles the dark class on <html>. See ./init-css-interop.
|
|
5
5
|
import './init-css-interop';
|
|
6
|
-
import React, { createContext, useCallback, useContext, useMemo, useRef, useState } from 'react';
|
|
7
|
-
import { useColorScheme as useRNColorScheme, Platform } from 'react-native';
|
|
8
|
-
import { APP_COLOR_PRESETS, type AppColorName } from './color-presets';
|
|
9
|
-
import { getAdaptiveColors } from './adaptive-colors';
|
|
10
|
-
import { applyDarkClass, applyColorPresetVars } from './apply-dark-class';
|
|
11
|
-
import { setColorSchemeSafe } from './set-color-scheme-safe';
|
|
12
|
-
import { FontLoader } from '../fonts/FontLoader';
|
|
13
|
-
import type { Theme, ThemeColors, ThemeMode } from './types';
|
|
14
|
-
|
|
15
|
-
function hslVarToCSS(value: string): string {
|
|
16
|
-
const parts = value.split('/').map((s) => s.trim());
|
|
17
|
-
if (parts.length === 2) {
|
|
18
|
-
const alpha = parseFloat(parts[1] ?? '100') / 100;
|
|
19
|
-
return `hsla(${(parts[0] ?? '').replace(/ /g, ', ')}, ${alpha})`;
|
|
20
|
-
}
|
|
21
|
-
return `hsl(${value.replace(/ /g, ', ')})`;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function extractHue(hslVar: string): number {
|
|
25
|
-
return parseInt(hslVar.split(' ')[0] ?? '0', 10);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function hsl(h: number, s: number, l: number): string {
|
|
29
|
-
return `hsl(${h}, ${s}%, ${l}%)`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/** Build a Theme object from a color preset name and resolved light/dark mode. */
|
|
33
|
-
export function buildTheme(appColor: AppColorName, resolved: 'light' | 'dark', isAdaptive: boolean = false): Theme {
|
|
34
|
-
const isDark = resolved === 'dark';
|
|
35
|
-
|
|
36
|
-
let themeColors: ThemeColors | undefined;
|
|
37
6
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
7
|
+
import React, {
|
|
8
|
+
createContext,
|
|
9
|
+
useCallback,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useMemo,
|
|
13
|
+
useRef,
|
|
14
|
+
useState,
|
|
15
|
+
} from 'react';
|
|
16
|
+
import { useColorScheme as useRNColorScheme } from 'react-native';
|
|
17
|
+
|
|
18
|
+
import { useControllableState } from '../hooks/useControllableState';
|
|
19
|
+
import { FontLoader } from '../fonts/FontLoader';
|
|
44
20
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
themeColors = {
|
|
59
|
-
background,
|
|
60
|
-
backgroundSecondary: surface,
|
|
61
|
-
backgroundTertiary: hslVarToCSS(vars['--muted'] ?? '0 0% 0%'),
|
|
62
|
-
|
|
63
|
-
text: hslVarToCSS(vars['--foreground'] ?? '0 0% 100%'),
|
|
64
|
-
textSecondary: mutedForeground,
|
|
65
|
-
textTertiary: mutedForeground,
|
|
66
|
-
|
|
67
|
-
border: hslVarToCSS(vars['--border'] ?? '0 0% 20%'),
|
|
68
|
-
borderLight: hslVarToCSS(vars['--input'] ?? '0 0% 20%'),
|
|
69
|
-
|
|
70
|
-
primary: primaryColor,
|
|
71
|
-
primaryForeground,
|
|
72
|
-
primaryLight: surface,
|
|
73
|
-
primaryDark: background,
|
|
74
|
-
|
|
75
|
-
secondary: primaryColor,
|
|
76
|
-
|
|
77
|
-
tint: primaryColor,
|
|
78
|
-
icon: mutedForeground,
|
|
79
|
-
iconActive: primaryColor,
|
|
80
|
-
|
|
81
|
-
success: '#10B981',
|
|
82
|
-
error: '#EF4444',
|
|
83
|
-
warning: '#F59E0B',
|
|
84
|
-
info: '#3B82F6',
|
|
85
|
-
|
|
86
|
-
primarySubtle: isDark ? hsl(primaryHue, 50, 10) : hsl(primaryHue, 70, 93),
|
|
87
|
-
primarySubtleForeground: isDark ? hsl(primaryHue, 70, 65) : hsl(primaryHue, 90, 25),
|
|
88
|
-
negative: hsl(destructiveHue, 84, 45),
|
|
89
|
-
negativeForeground: '#FFFFFF',
|
|
90
|
-
negativeSubtle: isDark ? hsl(destructiveHue, 50, 10) : hsl(destructiveHue, 90, 95),
|
|
91
|
-
negativeSubtleForeground: isDark ? hsl(destructiveHue, 70, 65) : hsl(destructiveHue, 80, 40),
|
|
92
|
-
contrast50: isDark ? hsl(primaryHue, 15, 12) : hsl(primaryHue, 10, 93),
|
|
93
|
-
|
|
94
|
-
card: surface,
|
|
95
|
-
shadow: isDark ? 'rgba(0, 0, 0, 0.3)' : 'rgba(0, 0, 0, 0.1)',
|
|
96
|
-
overlay: 'rgba(0, 0, 0, 0.5)',
|
|
97
|
-
};
|
|
98
|
-
}
|
|
21
|
+
import { applyDarkClass, applyColorPresetVars } from './apply-dark-class';
|
|
22
|
+
import { buildTheme } from './build-theme';
|
|
23
|
+
import { type AppColorName } from './color-presets';
|
|
24
|
+
import {
|
|
25
|
+
readPersistedTheme,
|
|
26
|
+
readPersistedThemeSync,
|
|
27
|
+
writePersistedTheme,
|
|
28
|
+
type BloomThemeStorage,
|
|
29
|
+
type SyncReadResult,
|
|
30
|
+
} from './persistence';
|
|
31
|
+
import { setColorSchemeSafe } from './set-color-scheme-safe';
|
|
32
|
+
import { useIsomorphicLayoutEffect } from './use-isomorphic-layout-effect';
|
|
33
|
+
import type { Theme, ThemeMode } from './types';
|
|
99
34
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
colors: themeColors,
|
|
103
|
-
isDark,
|
|
104
|
-
isLight: !isDark,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
35
|
+
const DEFAULT_PRESET: AppColorName = 'oxy';
|
|
36
|
+
const DEFAULT_MODE: ThemeMode = 'system';
|
|
107
37
|
|
|
108
38
|
export interface BloomThemeContextValue {
|
|
109
39
|
theme: Theme;
|
|
@@ -116,97 +46,235 @@ export interface BloomThemeContextValue {
|
|
|
116
46
|
export const BloomThemeContext = createContext<BloomThemeContextValue | null>(null);
|
|
117
47
|
|
|
118
48
|
export interface BloomThemeProviderProps {
|
|
49
|
+
/** Controlled mode. Omit to use Bloom's internal state (with optional persistence). */
|
|
119
50
|
mode?: ThemeMode;
|
|
51
|
+
/** Controlled color preset. Omit to use Bloom's internal state. */
|
|
120
52
|
colorPreset?: AppColorName;
|
|
53
|
+
/** Initial mode when uncontrolled and nothing is persisted yet. */
|
|
54
|
+
defaultMode?: ThemeMode;
|
|
55
|
+
/** Initial color preset when uncontrolled and nothing is persisted yet. */
|
|
56
|
+
defaultColorPreset?: AppColorName;
|
|
57
|
+
|
|
121
58
|
onModeChange?: (mode: ThemeMode) => void;
|
|
122
59
|
onColorPresetChange?: (preset: AppColorName) => void;
|
|
60
|
+
|
|
123
61
|
/**
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
*
|
|
62
|
+
* Persist mode + preset under this storage key. Bloom becomes the single
|
|
63
|
+
* source of truth — apps don't need their own theme store. Has no effect
|
|
64
|
+
* without `storage`.
|
|
127
65
|
*/
|
|
128
|
-
|
|
66
|
+
persistKey?: string;
|
|
129
67
|
/**
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* before the bundled fonts resolve.
|
|
68
|
+
* Storage adapter paired with `persistKey`. Use `webLocalStorage` on web,
|
|
69
|
+
* or pass an `AsyncStorage`-compatible adapter on native.
|
|
133
70
|
*/
|
|
71
|
+
storage?: BloomThemeStorage;
|
|
72
|
+
/**
|
|
73
|
+
* Block rendering until persisted state has been read. Default `true` when
|
|
74
|
+
* both `persistKey` and `storage` are provided, ensuring native apps don't
|
|
75
|
+
* flash the default palette. Web hydrates synchronously so this is a no-op
|
|
76
|
+
* there.
|
|
77
|
+
*/
|
|
78
|
+
awaitHydration?: boolean;
|
|
79
|
+
/** Rendered while async hydration is pending. */
|
|
80
|
+
onHydrating?: React.ReactNode;
|
|
81
|
+
|
|
82
|
+
/** Load and inject Bloom's font system. Default `true`. */
|
|
83
|
+
fonts?: boolean;
|
|
84
|
+
/** Rendered while native fonts load. Ignored on web. */
|
|
134
85
|
onFontsLoading?: React.ReactNode;
|
|
86
|
+
|
|
135
87
|
children: React.ReactNode;
|
|
136
88
|
}
|
|
137
89
|
|
|
90
|
+
interface ThemeStateOptions {
|
|
91
|
+
controlledMode?: ThemeMode;
|
|
92
|
+
controlledPreset?: AppColorName;
|
|
93
|
+
defaultMode: ThemeMode;
|
|
94
|
+
defaultPreset: AppColorName;
|
|
95
|
+
persistKey?: string;
|
|
96
|
+
storage?: BloomThemeStorage;
|
|
97
|
+
onModeChange?: (mode: ThemeMode) => void;
|
|
98
|
+
onColorPresetChange?: (preset: AppColorName) => void;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
interface ThemeStateResult {
|
|
102
|
+
mode: ThemeMode;
|
|
103
|
+
colorPreset: AppColorName;
|
|
104
|
+
setMode: (mode: ThemeMode) => void;
|
|
105
|
+
setColorPreset: (preset: AppColorName) => void;
|
|
106
|
+
hydrated: boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function useThemeState({
|
|
110
|
+
controlledMode,
|
|
111
|
+
controlledPreset,
|
|
112
|
+
defaultMode,
|
|
113
|
+
defaultPreset,
|
|
114
|
+
persistKey,
|
|
115
|
+
storage,
|
|
116
|
+
onModeChange,
|
|
117
|
+
onColorPresetChange,
|
|
118
|
+
}: ThemeStateOptions): ThemeStateResult {
|
|
119
|
+
// Synchronous read happens once on first render. Succeeds on web with
|
|
120
|
+
// localStorage-backed adapters; async adapters rehydrate via the effect
|
|
121
|
+
// below. Lazy-initialized via `useState` so it runs exactly once per mount.
|
|
122
|
+
const [syncResult] = useState<SyncReadResult>(() =>
|
|
123
|
+
readPersistedThemeSync(persistKey, storage),
|
|
124
|
+
);
|
|
125
|
+
const syncState = syncResult.kind === 'sync' ? syncResult.state : null;
|
|
126
|
+
|
|
127
|
+
const initialMode = syncState?.mode ?? defaultMode;
|
|
128
|
+
const initialPreset = syncState?.colorPreset ?? defaultPreset;
|
|
129
|
+
|
|
130
|
+
// Hydrated immediately when persistence is off or the adapter resolved
|
|
131
|
+
// synchronously (including a null hit — that's a valid "no value" answer).
|
|
132
|
+
const [hydrated, setHydrated] = useState<boolean>(syncResult.kind !== 'async');
|
|
133
|
+
|
|
134
|
+
const [mode, setModeInternal] = useControllableState<ThemeMode>({
|
|
135
|
+
value: controlledMode,
|
|
136
|
+
defaultValue: initialMode,
|
|
137
|
+
});
|
|
138
|
+
const [colorPreset, setPresetInternal] = useControllableState<AppColorName>({
|
|
139
|
+
value: controlledPreset,
|
|
140
|
+
defaultValue: initialPreset,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Refs let setMode/setColorPreset stay referentially stable. Callbacks
|
|
144
|
+
// memoized only by setters and storage identity won't churn the context
|
|
145
|
+
// value on every theme change.
|
|
146
|
+
const modeRef = useRef(mode);
|
|
147
|
+
modeRef.current = mode;
|
|
148
|
+
const presetRef = useRef(colorPreset);
|
|
149
|
+
presetRef.current = colorPreset;
|
|
150
|
+
|
|
151
|
+
// Async hydration for adapters that can't be read synchronously
|
|
152
|
+
// (AsyncStorage, MMKV via JSI fallback, etc).
|
|
153
|
+
useEffect(() => {
|
|
154
|
+
if (hydrated) return;
|
|
155
|
+
if (!persistKey || !storage) {
|
|
156
|
+
setHydrated(true);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
let cancelled = false;
|
|
161
|
+
readPersistedTheme(persistKey, storage).then((state) => {
|
|
162
|
+
if (cancelled) return;
|
|
163
|
+
if (state?.mode && controlledMode === undefined) {
|
|
164
|
+
setModeInternal(state.mode);
|
|
165
|
+
onModeChange?.(state.mode);
|
|
166
|
+
}
|
|
167
|
+
if (state?.colorPreset && controlledPreset === undefined) {
|
|
168
|
+
setPresetInternal(state.colorPreset);
|
|
169
|
+
onColorPresetChange?.(state.colorPreset);
|
|
170
|
+
}
|
|
171
|
+
setHydrated(true);
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
return () => {
|
|
175
|
+
cancelled = true;
|
|
176
|
+
};
|
|
177
|
+
// Hydration runs once per storage instance. controlledMode/Preset are
|
|
178
|
+
// captured by closure intentionally — switching controlled-ness mid-flight
|
|
179
|
+
// is unsupported and would invalidate the in-flight hydration anyway.
|
|
180
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
181
|
+
}, [persistKey, storage]);
|
|
182
|
+
|
|
183
|
+
const setMode = useCallback(
|
|
184
|
+
(next: ThemeMode) => {
|
|
185
|
+
setModeInternal(next);
|
|
186
|
+
onModeChange?.(next);
|
|
187
|
+
void writePersistedTheme(persistKey, storage, {
|
|
188
|
+
mode: next,
|
|
189
|
+
colorPreset: presetRef.current,
|
|
190
|
+
});
|
|
191
|
+
},
|
|
192
|
+
[setModeInternal, onModeChange, persistKey, storage],
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const setColorPreset = useCallback(
|
|
196
|
+
(next: AppColorName) => {
|
|
197
|
+
setPresetInternal(next);
|
|
198
|
+
onColorPresetChange?.(next);
|
|
199
|
+
void writePersistedTheme(persistKey, storage, {
|
|
200
|
+
mode: modeRef.current,
|
|
201
|
+
colorPreset: next,
|
|
202
|
+
});
|
|
203
|
+
},
|
|
204
|
+
[setPresetInternal, onColorPresetChange, persistKey, storage],
|
|
205
|
+
);
|
|
206
|
+
|
|
207
|
+
return { mode, colorPreset, setMode, setColorPreset, hydrated };
|
|
208
|
+
}
|
|
209
|
+
|
|
138
210
|
export function BloomThemeProvider({
|
|
139
211
|
mode: controlledMode,
|
|
140
212
|
colorPreset: controlledPreset,
|
|
213
|
+
defaultMode = DEFAULT_MODE,
|
|
214
|
+
defaultColorPreset = DEFAULT_PRESET,
|
|
141
215
|
onModeChange,
|
|
142
216
|
onColorPresetChange,
|
|
217
|
+
persistKey,
|
|
218
|
+
storage,
|
|
219
|
+
awaitHydration,
|
|
220
|
+
onHydrating,
|
|
143
221
|
fonts = true,
|
|
144
222
|
onFontsLoading,
|
|
145
223
|
children,
|
|
146
224
|
}: BloomThemeProviderProps) {
|
|
147
225
|
const rnScheme = useRNColorScheme();
|
|
148
|
-
const [internalPreset, setInternalPreset] = useState<AppColorName>(controlledPreset ?? 'oxy');
|
|
149
|
-
|
|
150
|
-
if (controlledPreset !== undefined && controlledPreset !== internalPreset) {
|
|
151
|
-
setInternalPreset(controlledPreset);
|
|
152
|
-
}
|
|
153
226
|
|
|
154
|
-
const
|
|
155
|
-
|
|
227
|
+
const { mode, colorPreset, setMode, setColorPreset, hydrated } = useThemeState({
|
|
228
|
+
controlledMode,
|
|
229
|
+
controlledPreset,
|
|
230
|
+
defaultMode,
|
|
231
|
+
defaultPreset: defaultColorPreset,
|
|
232
|
+
persistKey,
|
|
233
|
+
storage,
|
|
234
|
+
onModeChange,
|
|
235
|
+
onColorPresetChange,
|
|
236
|
+
});
|
|
156
237
|
|
|
157
238
|
const isAdaptive = mode === 'adaptive';
|
|
158
|
-
const effectiveMode = isAdaptive ? 'system' : mode;
|
|
239
|
+
const effectiveMode: Exclude<ThemeMode, 'adaptive'> = isAdaptive ? 'system' : mode;
|
|
159
240
|
const resolved: 'light' | 'dark' =
|
|
160
|
-
effectiveMode === 'system'
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
//
|
|
165
|
-
|
|
166
|
-
// the last-applied values avoids calling side effects on every render
|
|
167
|
-
// while still applying them before the first paint.
|
|
168
|
-
const lastApplied = useRef<string>('');
|
|
169
|
-
const applyKey = `${resolved}:${appColor}`;
|
|
170
|
-
if (lastApplied.current !== applyKey) {
|
|
171
|
-
lastApplied.current = applyKey;
|
|
241
|
+
effectiveMode === 'system' ? (rnScheme === 'dark' ? 'dark' : 'light') : effectiveMode;
|
|
242
|
+
|
|
243
|
+
// Apply native color scheme, dark class, and CSS vars whenever the resolved
|
|
244
|
+
// mode or preset changes. `useIsomorphicLayoutEffect` runs before paint on
|
|
245
|
+
// both native and web, eliminating the previous render-time side effect.
|
|
246
|
+
useIsomorphicLayoutEffect(() => {
|
|
172
247
|
setColorSchemeSafe(effectiveMode);
|
|
173
248
|
applyDarkClass(resolved);
|
|
174
|
-
applyColorPresetVars(
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
const setMode = useCallback(
|
|
178
|
-
(newMode: ThemeMode) => {
|
|
179
|
-
setColorSchemeSafe(newMode);
|
|
180
|
-
onModeChange?.(newMode);
|
|
181
|
-
},
|
|
182
|
-
[onModeChange],
|
|
183
|
-
);
|
|
249
|
+
applyColorPresetVars(colorPreset, resolved);
|
|
250
|
+
}, [effectiveMode, resolved, colorPreset]);
|
|
184
251
|
|
|
185
|
-
const
|
|
186
|
-
(
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
252
|
+
const contextValue = useMemo<BloomThemeContextValue>(
|
|
253
|
+
() => ({
|
|
254
|
+
theme: buildTheme(colorPreset, resolved, isAdaptive),
|
|
255
|
+
mode,
|
|
256
|
+
colorPreset,
|
|
257
|
+
setMode,
|
|
258
|
+
setColorPreset,
|
|
259
|
+
}),
|
|
260
|
+
[colorPreset, resolved, isAdaptive, mode, setMode, setColorPreset],
|
|
191
261
|
);
|
|
192
262
|
|
|
193
|
-
const
|
|
194
|
-
|
|
195
|
-
return { theme, mode, colorPreset: appColor, setMode, setColorPreset };
|
|
196
|
-
}, [resolved, appColor, isAdaptive, mode, setMode, setColorPreset]);
|
|
263
|
+
const shouldAwait = awaitHydration ?? Boolean(persistKey && storage);
|
|
264
|
+
const isGated = shouldAwait && !hydrated;
|
|
197
265
|
|
|
198
266
|
return (
|
|
199
267
|
<BloomThemeContext.Provider value={contextValue}>
|
|
200
268
|
<FontLoader enabled={fonts} fallback={onFontsLoading}>
|
|
201
|
-
{children}
|
|
269
|
+
{isGated ? onHydrating ?? null : children}
|
|
202
270
|
</FontLoader>
|
|
203
271
|
</BloomThemeContext.Provider>
|
|
204
272
|
);
|
|
205
273
|
}
|
|
206
274
|
|
|
207
275
|
/**
|
|
208
|
-
* Scoped color override for a subtree.
|
|
209
|
-
*
|
|
276
|
+
* Scoped color override for a subtree. Inherits the resolved mode from the
|
|
277
|
+
* parent `BloomThemeProvider` but renders descendants with a different preset.
|
|
210
278
|
*/
|
|
211
279
|
export interface BloomColorScopeProps {
|
|
212
280
|
colorPreset: AppColorName;
|
|
@@ -220,17 +288,11 @@ export function BloomColorScope({ colorPreset, children }: BloomColorScopeProps)
|
|
|
220
288
|
}
|
|
221
289
|
|
|
222
290
|
const contextValue = useMemo<BloomThemeContextValue>(() => {
|
|
223
|
-
const theme = buildTheme(colorPreset, parent.theme.mode
|
|
224
|
-
return {
|
|
225
|
-
...parent,
|
|
226
|
-
theme,
|
|
227
|
-
colorPreset,
|
|
228
|
-
};
|
|
291
|
+
const theme = buildTheme(colorPreset, parent.theme.mode);
|
|
292
|
+
return { ...parent, theme, colorPreset };
|
|
229
293
|
}, [colorPreset, parent]);
|
|
230
294
|
|
|
231
295
|
return (
|
|
232
|
-
<BloomThemeContext.Provider value={contextValue}>
|
|
233
|
-
{children}
|
|
234
|
-
</BloomThemeContext.Provider>
|
|
296
|
+
<BloomThemeContext.Provider value={contextValue}>{children}</BloomThemeContext.Provider>
|
|
235
297
|
);
|
|
236
298
|
}
|
|
@@ -10,6 +10,11 @@ export function applyDarkClass(resolved: 'light' | 'dark') {
|
|
|
10
10
|
/**
|
|
11
11
|
* Apply a color preset's CSS custom properties to the document root.
|
|
12
12
|
* No-op on native — only affects web.
|
|
13
|
+
*
|
|
14
|
+
* Values are written as raw HSL triples (e.g. `185 100% 20%`), matching the
|
|
15
|
+
* shadcn/Tailwind convention where stylesheets wrap them themselves with
|
|
16
|
+
* `hsl(var(--primary))`. Writing pre-resolved `hsl(...)` values here would
|
|
17
|
+
* produce invalid `hsl(hsl(...))` in consuming stylesheets and break theming.
|
|
13
18
|
*/
|
|
14
19
|
export function applyColorPresetVars(preset: AppColorName, resolved: 'light' | 'dark') {
|
|
15
20
|
if (Platform.OS !== 'web' || typeof document === 'undefined') return;
|
|
@@ -21,6 +26,6 @@ export function applyColorPresetVars(preset: AppColorName, resolved: 'light' | '
|
|
|
21
26
|
const root = document.documentElement.style;
|
|
22
27
|
|
|
23
28
|
for (const [key, value] of Object.entries(vars)) {
|
|
24
|
-
root.setProperty(key,
|
|
29
|
+
root.setProperty(key, value);
|
|
25
30
|
}
|
|
26
31
|
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import { APP_COLOR_PRESETS, type AppColorName, type PresetTokens } from './color-presets';
|
|
3
|
+
import { getAdaptiveColors } from './adaptive-colors';
|
|
4
|
+
import type { Theme, ThemeColors } from './types';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Status colors used across the design system. Independent of the accent
|
|
8
|
+
* preset so semantic intent stays stable across themes.
|
|
9
|
+
*/
|
|
10
|
+
export const STATUS_COLORS = {
|
|
11
|
+
success: '#10B981',
|
|
12
|
+
error: '#EF4444',
|
|
13
|
+
warning: '#F59E0B',
|
|
14
|
+
info: '#3B82F6',
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Convert a shadcn-style HSL CSS variable (`'H S% L%'` or `'H S% L% / A'`)
|
|
19
|
+
* into a fully-resolved `hsl()` / `hsla()` color string consumable by both
|
|
20
|
+
* web `style` and React Native.
|
|
21
|
+
*/
|
|
22
|
+
function hslVarToColor(hslVar: string): string {
|
|
23
|
+
const parts = hslVar.split('/').map((p) => p.trim());
|
|
24
|
+
const triple = parts[0] ?? '0 0% 0%';
|
|
25
|
+
const components = triple.replace(/\s+/g, ', ');
|
|
26
|
+
|
|
27
|
+
if (parts.length === 2) {
|
|
28
|
+
const alpha = parseFloat(parts[1] ?? '100') / 100;
|
|
29
|
+
return `hsla(${components}, ${alpha})`;
|
|
30
|
+
}
|
|
31
|
+
return `hsl(${components})`;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function extractHue(hslVar: string): number {
|
|
35
|
+
const first = hslVar.split(/\s+/)[0] ?? '0';
|
|
36
|
+
const hue = parseInt(first, 10);
|
|
37
|
+
return Number.isFinite(hue) ? hue : 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function hsl(h: number, s: number, l: number): string {
|
|
41
|
+
return `hsl(${h}, ${s}%, ${l}%)`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function readToken(tokens: PresetTokens, key: string, fallback = '0 0% 0%'): string {
|
|
45
|
+
return hslVarToColor(tokens[key] ?? fallback);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function buildColorsFromPreset(
|
|
49
|
+
preset: AppColorName,
|
|
50
|
+
resolved: 'light' | 'dark',
|
|
51
|
+
): ThemeColors {
|
|
52
|
+
const config = APP_COLOR_PRESETS[preset];
|
|
53
|
+
const tokens = resolved === 'dark' ? config.dark : config.light;
|
|
54
|
+
const isDark = resolved === 'dark';
|
|
55
|
+
|
|
56
|
+
const primaryHue = extractHue(tokens['--primary'] ?? '0 0% 50%');
|
|
57
|
+
const destructiveHue = extractHue(tokens['--destructive'] ?? '0 0% 0%');
|
|
58
|
+
|
|
59
|
+
const background = readToken(tokens, '--background');
|
|
60
|
+
const surface = readToken(tokens, '--surface');
|
|
61
|
+
const mutedForeground = readToken(tokens, '--muted-foreground', '0 0% 50%');
|
|
62
|
+
const primaryColor = readToken(tokens, '--primary', '0 0% 50%');
|
|
63
|
+
const primaryForeground = readToken(tokens, '--primary-foreground', '0 0% 100%');
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
background,
|
|
67
|
+
backgroundSecondary: surface,
|
|
68
|
+
backgroundTertiary: readToken(tokens, '--muted'),
|
|
69
|
+
|
|
70
|
+
text: readToken(tokens, '--foreground', '0 0% 100%'),
|
|
71
|
+
textSecondary: mutedForeground,
|
|
72
|
+
textTertiary: mutedForeground,
|
|
73
|
+
|
|
74
|
+
border: readToken(tokens, '--border', '0 0% 20%'),
|
|
75
|
+
borderLight: readToken(tokens, '--input', '0 0% 20%'),
|
|
76
|
+
|
|
77
|
+
primary: primaryColor,
|
|
78
|
+
primaryForeground,
|
|
79
|
+
// Legacy aliases retained for backwards compatibility with downstream
|
|
80
|
+
// consumers. `primaryLight` should be a brand tint, not the surface, but
|
|
81
|
+
// changing this is a breaking change handled in a separate major.
|
|
82
|
+
primaryLight: surface,
|
|
83
|
+
primaryDark: background,
|
|
84
|
+
|
|
85
|
+
// `secondary` historically mirrored `primary`. Retained for compatibility.
|
|
86
|
+
secondary: primaryColor,
|
|
87
|
+
|
|
88
|
+
tint: primaryColor,
|
|
89
|
+
icon: mutedForeground,
|
|
90
|
+
iconActive: primaryColor,
|
|
91
|
+
|
|
92
|
+
...STATUS_COLORS,
|
|
93
|
+
|
|
94
|
+
primarySubtle: isDark ? hsl(primaryHue, 50, 10) : hsl(primaryHue, 70, 93),
|
|
95
|
+
primarySubtleForeground: isDark ? hsl(primaryHue, 70, 65) : hsl(primaryHue, 90, 25),
|
|
96
|
+
negative: hsl(destructiveHue, 84, 45),
|
|
97
|
+
negativeForeground: '#FFFFFF',
|
|
98
|
+
negativeSubtle: isDark ? hsl(destructiveHue, 50, 10) : hsl(destructiveHue, 90, 95),
|
|
99
|
+
negativeSubtleForeground: isDark ? hsl(destructiveHue, 70, 65) : hsl(destructiveHue, 80, 40),
|
|
100
|
+
contrast50: isDark ? hsl(primaryHue, 15, 12) : hsl(primaryHue, 10, 93),
|
|
101
|
+
|
|
102
|
+
card: surface,
|
|
103
|
+
shadow: isDark ? 'rgba(0, 0, 0, 0.3)' : 'rgba(0, 0, 0, 0.1)',
|
|
104
|
+
overlay: 'rgba(0, 0, 0, 0.5)',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Build a `Theme` from a color preset and a resolved light/dark mode.
|
|
110
|
+
*
|
|
111
|
+
* When `isAdaptive` is true and the platform exposes adaptive (Material You /
|
|
112
|
+
* iOS dynamic) colors, those override the preset-derived palette.
|
|
113
|
+
*/
|
|
114
|
+
export function buildTheme(
|
|
115
|
+
preset: AppColorName,
|
|
116
|
+
resolved: 'light' | 'dark',
|
|
117
|
+
isAdaptive: boolean = false,
|
|
118
|
+
): Theme {
|
|
119
|
+
const adaptive = isAdaptive && Platform.OS !== 'web' ? getAdaptiveColors() : undefined;
|
|
120
|
+
const colors = adaptive ?? buildColorsFromPreset(preset, resolved);
|
|
121
|
+
|
|
122
|
+
return {
|
|
123
|
+
mode: resolved,
|
|
124
|
+
colors,
|
|
125
|
+
isDark: resolved === 'dark',
|
|
126
|
+
isLight: resolved === 'light',
|
|
127
|
+
};
|
|
128
|
+
}
|
|
@@ -1,13 +1,29 @@
|
|
|
1
1
|
export type AppColorName = 'teal' | 'blue' | 'green' | 'amber' | 'yellow' | 'red' | 'purple' | 'pink' | 'sky' | 'orange' | 'mint' | 'oxy' | 'faircoin';
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* Palette tokens for a single mode (light or dark) of a color preset.
|
|
5
|
+
*
|
|
6
|
+
* Keys carry a leading `--` for historical reasons (shadcn-style CSS variable
|
|
7
|
+
* names) — keep in mind the values are platform-agnostic **raw HSL triples**
|
|
8
|
+
* (e.g. `'185 100% 20%'` or `'185 100% 20% / 0.5'`), not CSS-resolved colors.
|
|
9
|
+
* The same map drives both:
|
|
10
|
+
* - the web layer (written verbatim into `document.documentElement.style`
|
|
11
|
+
* so Tailwind's `hsl(var(--primary))` plumbing picks them up), and
|
|
12
|
+
* - the native layer (`buildTheme` resolves them into `hsl(...)` strings
|
|
13
|
+
* consumable by React Native styles).
|
|
14
|
+
*
|
|
15
|
+
* The `--` prefix is an implementation detail we will drop in a future major.
|
|
16
|
+
*/
|
|
17
|
+
export type PresetTokens = Record<string, string>;
|
|
18
|
+
|
|
3
19
|
export interface AppColorPreset {
|
|
4
20
|
name: AppColorName;
|
|
5
21
|
hex: string;
|
|
6
|
-
light:
|
|
7
|
-
dark:
|
|
22
|
+
light: PresetTokens;
|
|
23
|
+
dark: PresetTokens;
|
|
8
24
|
}
|
|
9
25
|
|
|
10
|
-
export const APP_COLOR_NAMES: AppColorName[] = ['teal', 'blue', 'green', 'amber', 'yellow', 'red', 'purple', 'pink', 'sky', 'orange', 'mint', 'oxy', 'faircoin'];
|
|
26
|
+
export const APP_COLOR_NAMES: readonly AppColorName[] = ['teal', 'blue', 'green', 'amber', 'yellow', 'red', 'purple', 'pink', 'sky', 'orange', 'mint', 'oxy', 'faircoin'];
|
|
11
27
|
|
|
12
28
|
export const HEX_TO_APP_COLOR: Record<string, AppColorName> = {
|
|
13
29
|
'#005c67': 'teal',
|
package/src/theme/index.ts
CHANGED
|
@@ -1,9 +1,21 @@
|
|
|
1
|
-
export { BloomThemeProvider, BloomColorScope
|
|
2
|
-
export type {
|
|
1
|
+
export { BloomThemeProvider, BloomColorScope } from './BloomThemeProvider';
|
|
2
|
+
export type {
|
|
3
|
+
BloomThemeProviderProps,
|
|
4
|
+
BloomThemeContextValue,
|
|
5
|
+
BloomColorScopeProps,
|
|
6
|
+
} from './BloomThemeProvider';
|
|
7
|
+
export { buildTheme, STATUS_COLORS } from './build-theme';
|
|
3
8
|
export { useTheme, useThemeColor, useBloomTheme } from './use-theme';
|
|
4
9
|
export type { Theme, ThemeColors, ThemeMode } from './types';
|
|
5
|
-
export type { AppColorName, AppColorPreset } from './color-presets';
|
|
6
|
-
export {
|
|
10
|
+
export type { AppColorName, AppColorPreset, PresetTokens } from './color-presets';
|
|
11
|
+
export {
|
|
12
|
+
APP_COLOR_NAMES,
|
|
13
|
+
APP_COLOR_PRESETS,
|
|
14
|
+
HEX_TO_APP_COLOR,
|
|
15
|
+
hexToAppColorName,
|
|
16
|
+
} from './color-presets';
|
|
7
17
|
export { applyDarkClass } from './apply-dark-class';
|
|
8
18
|
export { setColorSchemeSafe } from './set-color-scheme-safe';
|
|
9
19
|
export { initCssInteropDarkMode } from './init-css-interop';
|
|
20
|
+
export type { BloomThemeStorage, PersistedThemeState } from './persistence';
|
|
21
|
+
export { webLocalStorage } from './persistence';
|