@oxyhq/bloom 0.6.13 → 0.6.15

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.
Files changed (68) hide show
  1. package/lib/commonjs/hooks/useControllableState.js +35 -0
  2. package/lib/commonjs/hooks/useControllableState.js.map +1 -0
  3. package/lib/commonjs/theme/BloomThemeProvider.js +148 -112
  4. package/lib/commonjs/theme/BloomThemeProvider.js.map +1 -1
  5. package/lib/commonjs/theme/build-theme.js +110 -0
  6. package/lib/commonjs/theme/build-theme.js.map +1 -0
  7. package/lib/commonjs/theme/color-presets.js +15 -0
  8. package/lib/commonjs/theme/color-presets.js.map +1 -1
  9. package/lib/commonjs/theme/index.js +15 -1
  10. package/lib/commonjs/theme/index.js.map +1 -1
  11. package/lib/commonjs/theme/persistence.js +147 -0
  12. package/lib/commonjs/theme/persistence.js.map +1 -0
  13. package/lib/commonjs/theme/use-isomorphic-layout-effect.js +15 -0
  14. package/lib/commonjs/theme/use-isomorphic-layout-effect.js.map +1 -0
  15. package/lib/module/hooks/useControllableState.js +31 -0
  16. package/lib/module/hooks/useControllableState.js.map +1 -0
  17. package/lib/module/theme/BloomThemeProvider.js +149 -112
  18. package/lib/module/theme/BloomThemeProvider.js.map +1 -1
  19. package/lib/module/theme/build-theme.js +105 -0
  20. package/lib/module/theme/build-theme.js.map +1 -0
  21. package/lib/module/theme/color-presets.js +15 -0
  22. package/lib/module/theme/color-presets.js.map +1 -1
  23. package/lib/module/theme/index.js +3 -1
  24. package/lib/module/theme/index.js.map +1 -1
  25. package/lib/module/theme/persistence.js +139 -0
  26. package/lib/module/theme/persistence.js.map +1 -0
  27. package/lib/module/theme/use-isomorphic-layout-effect.js +12 -0
  28. package/lib/module/theme/use-isomorphic-layout-effect.js.map +1 -0
  29. package/lib/typescript/commonjs/hooks/useControllableState.d.ts +19 -0
  30. package/lib/typescript/commonjs/hooks/useControllableState.d.ts.map +1 -0
  31. package/lib/typescript/commonjs/theme/BloomThemeProvider.d.ts +35 -12
  32. package/lib/typescript/commonjs/theme/BloomThemeProvider.d.ts.map +1 -1
  33. package/lib/typescript/commonjs/theme/build-theme.d.ts +20 -0
  34. package/lib/typescript/commonjs/theme/build-theme.d.ts.map +1 -0
  35. package/lib/typescript/commonjs/theme/color-presets.d.ts +18 -3
  36. package/lib/typescript/commonjs/theme/color-presets.d.ts.map +1 -1
  37. package/lib/typescript/commonjs/theme/index.d.ts +7 -4
  38. package/lib/typescript/commonjs/theme/index.d.ts.map +1 -1
  39. package/lib/typescript/commonjs/theme/persistence.d.ts +50 -0
  40. package/lib/typescript/commonjs/theme/persistence.d.ts.map +1 -0
  41. package/lib/typescript/commonjs/theme/use-isomorphic-layout-effect.d.ts +8 -0
  42. package/lib/typescript/commonjs/theme/use-isomorphic-layout-effect.d.ts.map +1 -0
  43. package/lib/typescript/module/hooks/useControllableState.d.ts +19 -0
  44. package/lib/typescript/module/hooks/useControllableState.d.ts.map +1 -0
  45. package/lib/typescript/module/theme/BloomThemeProvider.d.ts +35 -12
  46. package/lib/typescript/module/theme/BloomThemeProvider.d.ts.map +1 -1
  47. package/lib/typescript/module/theme/build-theme.d.ts +20 -0
  48. package/lib/typescript/module/theme/build-theme.d.ts.map +1 -0
  49. package/lib/typescript/module/theme/color-presets.d.ts +18 -3
  50. package/lib/typescript/module/theme/color-presets.d.ts.map +1 -1
  51. package/lib/typescript/module/theme/index.d.ts +7 -4
  52. package/lib/typescript/module/theme/index.d.ts.map +1 -1
  53. package/lib/typescript/module/theme/persistence.d.ts +50 -0
  54. package/lib/typescript/module/theme/persistence.d.ts.map +1 -0
  55. package/lib/typescript/module/theme/use-isomorphic-layout-effect.d.ts +8 -0
  56. package/lib/typescript/module/theme/use-isomorphic-layout-effect.d.ts.map +1 -0
  57. package/package.json +1 -1
  58. package/src/__tests__/BloomThemeProvider.modes.test.tsx +159 -0
  59. package/src/__tests__/BloomThemeProvider.persistence.test.tsx +321 -0
  60. package/src/__tests__/persistence.test.ts +155 -0
  61. package/src/__tests__/theme.test.ts +1 -1
  62. package/src/hooks/useControllableState.ts +44 -0
  63. package/src/theme/BloomThemeProvider.tsx +251 -157
  64. package/src/theme/build-theme.ts +128 -0
  65. package/src/theme/color-presets.ts +19 -3
  66. package/src/theme/index.ts +16 -4
  67. package/src/theme/persistence.ts +174 -0
  68. package/src/theme/use-isomorphic-layout-effect.ts +10 -0
@@ -0,0 +1,174 @@
1
+ import { Platform } from 'react-native';
2
+ import { APP_COLOR_NAMES, type AppColorName } from './color-presets';
3
+ import type { ThemeMode } from './types';
4
+
5
+ const VALID_PRESETS = new Set<string>(APP_COLOR_NAMES);
6
+ const VALID_MODES = new Set<string>(['light', 'dark', 'system', 'adaptive']);
7
+
8
+ /**
9
+ * Storage adapter for persisting Bloom theme state.
10
+ *
11
+ * Methods may be synchronous or asynchronous. The provider awaits both, so
12
+ * `AsyncStorage`-compatible adapters on native and `localStorage`-backed
13
+ * adapters on web both work without consumer-side branching.
14
+ *
15
+ * On web, a synchronous adapter lets Bloom hydrate before the first paint —
16
+ * preventing a flash of the default palette. On native, the provider can
17
+ * gate `children` rendering until hydration completes (see
18
+ * `awaitHydration` on `BloomThemeProvider`).
19
+ */
20
+ export interface BloomThemeStorage {
21
+ getItem(key: string): string | null | Promise<string | null>;
22
+ setItem(key: string, value: string): void | Promise<void>;
23
+ removeItem?(key: string): void | Promise<void>;
24
+ }
25
+
26
+ export interface PersistedThemeState {
27
+ mode?: ThemeMode;
28
+ colorPreset?: AppColorName;
29
+ }
30
+
31
+ const isValidMode = (value: unknown): value is ThemeMode =>
32
+ typeof value === 'string' && VALID_MODES.has(value);
33
+
34
+ const isValidPreset = (value: unknown): value is AppColorName =>
35
+ typeof value === 'string' && VALID_PRESETS.has(value);
36
+
37
+ function parsePersistedTheme(raw: unknown): PersistedThemeState | null {
38
+ if (typeof raw !== 'string') return null;
39
+
40
+ let parsed: unknown;
41
+ try {
42
+ parsed = JSON.parse(raw);
43
+ } catch {
44
+ return null;
45
+ }
46
+
47
+ if (!parsed || typeof parsed !== 'object') return null;
48
+ const obj = parsed as { mode?: unknown; colorPreset?: unknown };
49
+
50
+ const mode = isValidMode(obj.mode) ? obj.mode : undefined;
51
+ const colorPreset = isValidPreset(obj.colorPreset) ? obj.colorPreset : undefined;
52
+
53
+ if (!mode && !colorPreset) return null;
54
+ return { mode, colorPreset };
55
+ }
56
+
57
+ export type SyncReadResult =
58
+ | { kind: 'sync'; state: PersistedThemeState | null }
59
+ | { kind: 'async' }
60
+ | { kind: 'none' };
61
+
62
+ const isThenable = (value: unknown): value is Promise<unknown> =>
63
+ !!value && typeof (value as { then?: unknown }).then === 'function';
64
+
65
+ /**
66
+ * Read persisted state during the first render. Returns a discriminated union
67
+ * so the provider can tell apart:
68
+ * - `none`: persistence not configured.
69
+ * - `sync`: adapter returned a value synchronously (web `localStorage`).
70
+ * Provider can render immediately with the resolved state.
71
+ * - `async`: adapter returned a Promise (native `AsyncStorage`). Provider
72
+ * must await `readPersistedTheme` before painting if gating is enabled.
73
+ */
74
+ export function readPersistedThemeSync(
75
+ persistKey: string | undefined,
76
+ storage: BloomThemeStorage | undefined,
77
+ ): SyncReadResult {
78
+ if (!persistKey || !storage) return { kind: 'none' };
79
+
80
+ let raw: string | null | Promise<string | null>;
81
+ try {
82
+ raw = storage.getItem(persistKey);
83
+ } catch {
84
+ return { kind: 'sync', state: null };
85
+ }
86
+
87
+ if (isThenable(raw)) return { kind: 'async' };
88
+ return { kind: 'sync', state: parsePersistedTheme(raw) };
89
+ }
90
+
91
+ export async function readPersistedTheme(
92
+ persistKey: string | undefined,
93
+ storage: BloomThemeStorage | undefined,
94
+ ): Promise<PersistedThemeState | null> {
95
+ if (!persistKey || !storage) return null;
96
+
97
+ try {
98
+ const raw = await storage.getItem(persistKey);
99
+ return parsePersistedTheme(raw);
100
+ } catch {
101
+ return null;
102
+ }
103
+ }
104
+
105
+ export async function writePersistedTheme(
106
+ persistKey: string | undefined,
107
+ storage: BloomThemeStorage | undefined,
108
+ state: PersistedThemeState,
109
+ ): Promise<void> {
110
+ if (!persistKey || !storage) return;
111
+
112
+ const payload: PersistedThemeState = {};
113
+ if (state.mode !== undefined) payload.mode = state.mode;
114
+ if (state.colorPreset !== undefined) payload.colorPreset = state.colorPreset;
115
+
116
+ try {
117
+ await storage.setItem(persistKey, JSON.stringify(payload));
118
+ } catch {
119
+ // Persistence is best-effort: ignore quota and privacy-mode errors.
120
+ }
121
+ }
122
+
123
+ export async function removePersistedTheme(
124
+ persistKey: string | undefined,
125
+ storage: BloomThemeStorage | undefined,
126
+ ): Promise<void> {
127
+ if (!persistKey || !storage) return;
128
+
129
+ try {
130
+ if (typeof storage.removeItem === 'function') {
131
+ await storage.removeItem(persistKey);
132
+ return;
133
+ }
134
+ await storage.setItem(persistKey, '');
135
+ } catch {
136
+ // Best-effort cleanup.
137
+ }
138
+ }
139
+
140
+ /**
141
+ * `localStorage`-backed storage adapter. Only defined on web; on native the
142
+ * export is `undefined` so consumers pass an `AsyncStorage` adapter explicitly.
143
+ */
144
+ export const webLocalStorage: BloomThemeStorage | undefined = (() => {
145
+ if (Platform.OS !== 'web') return undefined;
146
+ if (typeof globalThis === 'undefined') return undefined;
147
+ if (!('localStorage' in globalThis)) return undefined;
148
+
149
+ const ls = (globalThis as { localStorage: Storage }).localStorage;
150
+
151
+ return {
152
+ getItem: (key) => {
153
+ try {
154
+ return ls.getItem(key);
155
+ } catch {
156
+ return null;
157
+ }
158
+ },
159
+ setItem: (key, value) => {
160
+ try {
161
+ ls.setItem(key, value);
162
+ } catch {
163
+ // Swallow quota / privacy-mode errors.
164
+ }
165
+ },
166
+ removeItem: (key) => {
167
+ try {
168
+ ls.removeItem(key);
169
+ } catch {
170
+ // Swallow privacy-mode errors.
171
+ }
172
+ },
173
+ };
174
+ })();
@@ -0,0 +1,10 @@
1
+ import { useEffect, useLayoutEffect } from 'react';
2
+ import { Platform } from 'react-native';
3
+
4
+ /**
5
+ * `useLayoutEffect` on web (and native — react-native polyfills it) but falls
6
+ * back to `useEffect` during SSR to avoid the React warning. Bloom apps are
7
+ * primarily Expo (no SSR), but Next.js consumers of the web bundle need this.
8
+ */
9
+ export const useIsomorphicLayoutEffect =
10
+ Platform.OS === 'web' && typeof document === 'undefined' ? useEffect : useLayoutEffect;