@oxyhq/bloom 0.44.0 → 0.45.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.
Files changed (32) hide show
  1. package/lib/commonjs/bottom-sheet/BottomSheetBase.js +9 -1
  2. package/lib/commonjs/bottom-sheet/BottomSheetBase.js.map +1 -1
  3. package/lib/commonjs/theme/BloomThemeProvider.js +34 -15
  4. package/lib/commonjs/theme/BloomThemeProvider.js.map +1 -1
  5. package/lib/commonjs/theme/ambient-store.js +153 -0
  6. package/lib/commonjs/theme/ambient-store.js.map +1 -0
  7. package/lib/commonjs/theme/index.js +13 -0
  8. package/lib/commonjs/theme/index.js.map +1 -1
  9. package/lib/module/bottom-sheet/BottomSheetBase.js +9 -1
  10. package/lib/module/bottom-sheet/BottomSheetBase.js.map +1 -1
  11. package/lib/module/theme/BloomThemeProvider.js +34 -15
  12. package/lib/module/theme/BloomThemeProvider.js.map +1 -1
  13. package/lib/module/theme/ambient-store.js +146 -0
  14. package/lib/module/theme/ambient-store.js.map +1 -0
  15. package/lib/module/theme/index.js +1 -0
  16. package/lib/module/theme/index.js.map +1 -1
  17. package/lib/typescript/commonjs/theme/BloomThemeProvider.d.ts.map +1 -1
  18. package/lib/typescript/commonjs/theme/ambient-store.d.ts +61 -0
  19. package/lib/typescript/commonjs/theme/ambient-store.d.ts.map +1 -0
  20. package/lib/typescript/commonjs/theme/index.d.ts +2 -0
  21. package/lib/typescript/commonjs/theme/index.d.ts.map +1 -1
  22. package/lib/typescript/module/theme/BloomThemeProvider.d.ts.map +1 -1
  23. package/lib/typescript/module/theme/ambient-store.d.ts +61 -0
  24. package/lib/typescript/module/theme/ambient-store.d.ts.map +1 -0
  25. package/lib/typescript/module/theme/index.d.ts +2 -0
  26. package/lib/typescript/module/theme/index.d.ts.map +1 -1
  27. package/package.json +1 -1
  28. package/src/__tests__/ambient-theme.test.tsx +136 -0
  29. package/src/bottom-sheet/BottomSheetBase.tsx +8 -0
  30. package/src/theme/BloomThemeProvider.tsx +52 -13
  31. package/src/theme/ambient-store.ts +186 -0
  32. package/src/theme/index.ts +7 -0
@@ -0,0 +1,136 @@
1
+ import React from 'react';
2
+ import { Text, Pressable } from 'react-native';
3
+ import { act, render, fireEvent } from '@testing-library/react-native';
4
+
5
+ import { BloomThemeProvider } from '../theme/BloomThemeProvider';
6
+ import { useTheme } from '../theme/use-theme';
7
+ import {
8
+ useAmbientTheme,
9
+ ambientTheme,
10
+ __flushAmbientForTests,
11
+ __resetAmbientForTests,
12
+ } from '../theme/ambient-store';
13
+ import { buildThemeFromSeed } from '../theme/build-theme-from-seed';
14
+ import { buildTheme } from '../theme/build-theme';
15
+
16
+ const OXY_PRESET_PRIMARY = buildTheme('oxy', 'light', false).colors.primary;
17
+ const RED_SEED = '#ff0000';
18
+ const RED_SEED_PRIMARY = buildThemeFromSeed(RED_SEED, 'light').colors.primary;
19
+
20
+ function PrimaryProbe() {
21
+ const theme = useTheme();
22
+ return <Text testID="primary">{theme.colors.primary}</Text>;
23
+ }
24
+
25
+ function AmbientDriver({ debounceMs }: { debounceMs?: number }) {
26
+ const { seed, setAmbient, clearAmbient } = useAmbientTheme({ debounceMs });
27
+ return (
28
+ <>
29
+ <Text testID="ambient-seed">{seed ?? 'null'}</Text>
30
+ <Pressable testID="set" onPress={() => setAmbient(RED_SEED)} />
31
+ <Pressable testID="clear" onPress={() => clearAmbient()} />
32
+ </>
33
+ );
34
+ }
35
+
36
+ describe('ambient theme store', () => {
37
+ beforeEach(() => {
38
+ __resetAmbientForTests();
39
+ });
40
+ afterEach(() => {
41
+ __resetAmbientForTests();
42
+ });
43
+
44
+ it('starts empty and returns a stable snapshot ref while unchanged', () => {
45
+ const { getByTestId } = render(
46
+ <BloomThemeProvider>
47
+ <AmbientDriver debounceMs={0} />
48
+ </BloomThemeProvider>,
49
+ );
50
+ expect(getByTestId('ambient-seed').props.children).toBe('null');
51
+ });
52
+
53
+ it('debounces setAmbient and coalesces rapid calls', () => {
54
+ ambientTheme.setAmbient('#111111');
55
+ ambientTheme.setAmbient('#222222');
56
+ ambientTheme.setAmbient(RED_SEED);
57
+ // Nothing committed yet — still debouncing.
58
+ expect(ambientTheme.getState().seed).toBeNull();
59
+ act(() => {
60
+ __flushAmbientForTests();
61
+ });
62
+ // Only the LAST call wins.
63
+ expect(ambientTheme.getState().seed).toBe(RED_SEED);
64
+ });
65
+
66
+ it('applies debounceMs: 0 synchronously', () => {
67
+ const { getByTestId } = render(
68
+ <BloomThemeProvider>
69
+ <AmbientDriver debounceMs={0} />
70
+ </BloomThemeProvider>,
71
+ );
72
+ act(() => {
73
+ fireEvent.press(getByTestId('set'));
74
+ });
75
+ expect(getByTestId('ambient-seed').props.children).toBe(RED_SEED);
76
+ });
77
+ });
78
+
79
+ describe('BloomThemeProvider ambient override / restore', () => {
80
+ beforeEach(() => {
81
+ __resetAmbientForTests();
82
+ });
83
+ afterEach(() => {
84
+ __resetAmbientForTests();
85
+ });
86
+
87
+ it('overrides the preset while ambient is set and restores it on clear', () => {
88
+ const { getByTestId } = render(
89
+ <BloomThemeProvider>
90
+ <PrimaryProbe />
91
+ <AmbientDriver debounceMs={0} />
92
+ </BloomThemeProvider>,
93
+ );
94
+ // Baseline: the oxy preset.
95
+ expect(getByTestId('primary').props.children).toBe(OXY_PRESET_PRIMARY);
96
+
97
+ act(() => {
98
+ fireEvent.press(getByTestId('set'));
99
+ });
100
+ // Ambient seed themes the whole app from the red seed.
101
+ expect(getByTestId('primary').props.children).toBe(RED_SEED_PRIMARY);
102
+ expect(RED_SEED_PRIMARY).not.toBe(OXY_PRESET_PRIMARY);
103
+
104
+ act(() => {
105
+ fireEvent.press(getByTestId('clear'));
106
+ });
107
+ // Cleared → back to the preset.
108
+ expect(getByTestId('primary').props.children).toBe(OXY_PRESET_PRIMARY);
109
+ });
110
+
111
+ it('ambient overrides even a static seed prop, then restores it on clear', () => {
112
+ const staticSeed = '#00ff00';
113
+ const staticPrimary = buildThemeFromSeed(staticSeed, 'light').colors.primary;
114
+
115
+ const { getByTestId } = render(
116
+ <BloomThemeProvider seed={staticSeed}>
117
+ <PrimaryProbe />
118
+ <AmbientDriver debounceMs={0} />
119
+ </BloomThemeProvider>,
120
+ );
121
+ // Static seed prop is active.
122
+ expect(getByTestId('primary').props.children).toBe(staticPrimary);
123
+
124
+ act(() => {
125
+ fireEvent.press(getByTestId('set'));
126
+ });
127
+ // Ambient wins over the static seed prop.
128
+ expect(getByTestId('primary').props.children).toBe(RED_SEED_PRIMARY);
129
+
130
+ act(() => {
131
+ fireEvent.press(getByTestId('clear'));
132
+ });
133
+ // Restored to the static seed prop, NOT the preset.
134
+ expect(getByTestId('primary').props.children).toBe(staticPrimary);
135
+ });
136
+ });
@@ -818,6 +818,14 @@ const styles = StyleSheet.create({
818
818
  },
819
819
  nonScrollableContent: {
820
820
  flex: 1,
821
+ // A `scrollable={false}` sheet hands its own scrolling to a child
822
+ // VirtualizedList, which needs a BOUNDED height to scroll. The sheet is
823
+ // clamped by `maxHeight`, but on web a flex child defaults to
824
+ // `min-height: auto` and grows to its content instead of shrinking into
825
+ // that clamp — so the list overflows and is clipped (no scroll). Yoga
826
+ // already defaults min to 0 on native; making it explicit here fixes the
827
+ // web output so the bounded height propagates down to the list.
828
+ minHeight: 0,
821
829
  },
822
830
  });
823
831
 
@@ -24,6 +24,8 @@ import {
24
24
  import { useControllableState } from '../hooks/useControllableState';
25
25
  import { FontLoader } from '../fonts/FontLoader';
26
26
 
27
+ import { useAmbientThemeState } from './ambient-store';
28
+
27
29
  import { applyDarkClass, applyVarsToDocument } from './apply-dark-class';
28
30
  import { buildTheme } from './build-theme';
29
31
  import { buildThemeFromSeed } from './build-theme-from-seed';
@@ -332,16 +334,40 @@ export function BloomThemeProvider({
332
334
  }: BloomThemeProviderProps) {
333
335
  const rnScheme = useRNColorScheme();
334
336
 
337
+ // The app-wide ambient override, driven imperatively via `useAmbientTheme()`
338
+ // from anywhere in the app. Subscribed through the module-level store's
339
+ // `useSyncExternalStore` (stable snapshot ref while unchanged → React-Compiler
340
+ // safe). When an ambient seed is set it OVERRIDES the static `seed` prop and
341
+ // the active preset; clearing it restores them.
342
+ const ambient = useAmbientThemeState();
343
+
344
+ // The EFFECTIVE dynamic seed + accents for this render: ambient wins over the
345
+ // static props. `null` ambient seed means "no override" → fall back to the
346
+ // `seed` prop (which itself may be undefined → preset path). When ambient is
347
+ // active, ambient accents replace the static accent props entirely (a null
348
+ // ambient accent = "no pin for this artwork colour").
349
+ const ambientActive = ambient.seed !== null;
350
+ const effectiveSeed: string | undefined = ambientActive
351
+ ? ambient.seed ?? undefined
352
+ : seed;
353
+ const effectiveSecondary: string | undefined = ambientActive
354
+ ? ambient.secondary ?? undefined
355
+ : secondaryColor;
356
+ const effectiveTertiary: string | undefined = ambientActive
357
+ ? ambient.tertiary ?? undefined
358
+ : tertiaryColor;
359
+
335
360
  // The app-wide pinned accents, if any. Memoized so its identity only changes
336
- // when a prop changes (keeps the effect/memo deps below stable). Left
361
+ // when an accent changes (keeps the effect/memo deps below stable). Left
337
362
  // `undefined` when neither is set so every downstream call is byte-identical
338
- // to the no-accent path.
363
+ // to the no-accent path. Uses the EFFECTIVE accents so ambient accents apply
364
+ // to the preset path too when no ambient seed is present.
339
365
  const explicitAccents = useMemo<ExplicitAccents | undefined>(
340
366
  () =>
341
- secondaryColor === undefined && tertiaryColor === undefined
367
+ effectiveSecondary === undefined && effectiveTertiary === undefined
342
368
  ? undefined
343
- : { secondaryHex: secondaryColor, tertiaryHex: tertiaryColor },
344
- [secondaryColor, tertiaryColor],
369
+ : { secondaryHex: effectiveSecondary, tertiaryHex: effectiveTertiary },
370
+ [effectiveSecondary, effectiveTertiary],
345
371
  );
346
372
 
347
373
  const { mode, colorPreset, setMode, setColorPreset, resetTheme, hydrated } = useThemeState({
@@ -371,20 +397,33 @@ export function BloomThemeProvider({
371
397
  // colour engine, so there is one code path — no seed/preset duplication.
372
398
  const themeVars = useMemo(
373
399
  () =>
374
- seed
375
- ? buildSeedScopeVars({ seed, mode: resolved, secondarySeed: secondaryColor, tertiarySeed: tertiaryColor })
400
+ effectiveSeed
401
+ ? buildSeedScopeVars({
402
+ seed: effectiveSeed,
403
+ mode: resolved,
404
+ secondarySeed: effectiveSecondary,
405
+ tertiarySeed: effectiveTertiary,
406
+ })
376
407
  : buildScopeVars(colorPreset, resolved, explicitAccents),
377
- [seed, secondaryColor, tertiaryColor, colorPreset, resolved, explicitAccents],
408
+ [effectiveSeed, effectiveSecondary, effectiveTertiary, colorPreset, resolved, explicitAccents],
378
409
  );
379
410
  const themeColors = useMemo(
380
411
  () =>
381
- seed
382
- ? buildThemeFromSeed(seed, resolved, undefined, undefined, {
383
- secondarySeed: secondaryColor,
384
- tertiarySeed: tertiaryColor,
412
+ effectiveSeed
413
+ ? buildThemeFromSeed(effectiveSeed, resolved, undefined, undefined, {
414
+ secondarySeed: effectiveSecondary,
415
+ tertiarySeed: effectiveTertiary,
385
416
  })
386
417
  : buildTheme(colorPreset, resolved, isAdaptive, explicitAccents),
387
- [seed, secondaryColor, tertiaryColor, colorPreset, resolved, isAdaptive, explicitAccents],
418
+ [
419
+ effectiveSeed,
420
+ effectiveSecondary,
421
+ effectiveTertiary,
422
+ colorPreset,
423
+ resolved,
424
+ isAdaptive,
425
+ explicitAccents,
426
+ ],
388
427
  );
389
428
 
390
429
  useIsomorphicLayoutEffect(() => {
@@ -0,0 +1,186 @@
1
+ import { useSyncExternalStore } from 'react';
2
+
3
+ /**
4
+ * The current ambient theme override. When `seed` is non-null the whole app is
5
+ * themed from this dynamic seed (same colour engine the `seed` prop uses),
6
+ * overriding the active preset / static `seed` prop. `secondary`/`tertiary` pin
7
+ * this seed's accent families (e.g. the 2nd/3rd colours extracted from artwork).
8
+ */
9
+ export interface AmbientThemeState {
10
+ readonly seed: string | null;
11
+ readonly secondary: string | null;
12
+ readonly tertiary: string | null;
13
+ }
14
+
15
+ export interface AmbientAccents {
16
+ secondary?: string | null;
17
+ tertiary?: string | null;
18
+ }
19
+
20
+ export interface AmbientThemeApi {
21
+ /**
22
+ * Set the ambient seed (and optional accents). Coalesced through an internal
23
+ * debounce so rapid hover/scroll doesn't thrash the theme. The debounce lives
24
+ * HERE — apps never own one.
25
+ */
26
+ setAmbient: (seed: string, accents?: AmbientAccents) => void;
27
+ /** Clear the ambient override, restoring the preset (or the static `seed` prop). */
28
+ clearAmbient: () => void;
29
+ }
30
+
31
+ /** Options for {@link useAmbientTheme}. */
32
+ export interface UseAmbientThemeOptions {
33
+ /**
34
+ * Debounce, in ms, applied to `setAmbient`/`clearAmbient` before the store
35
+ * commits. Default `120`. Pass `0` to apply synchronously.
36
+ */
37
+ debounceMs?: number;
38
+ }
39
+
40
+ const DEFAULT_DEBOUNCE_MS = 120;
41
+
42
+ // A single frozen "no override" snapshot. `getSnapshot` returns this EXACT
43
+ // reference whenever nothing is set, so `useSyncExternalStore` sees a stable
44
+ // identity across renders (React-Compiler-safe: the memoized value never
45
+ // silently goes stale because the snapshot ref only changes on a real commit).
46
+ const EMPTY_STATE: AmbientThemeState = Object.freeze({
47
+ seed: null,
48
+ secondary: null,
49
+ tertiary: null,
50
+ });
51
+
52
+ let state: AmbientThemeState = EMPTY_STATE;
53
+ const listeners = new Set<() => void>();
54
+
55
+ // Pending debounced commit. `null` operation means "clear".
56
+ let pendingTimer: ReturnType<typeof setTimeout> | null = null;
57
+ let pendingCommit: (() => void) | null = null;
58
+
59
+ function emit(): void {
60
+ for (const listener of listeners) listener();
61
+ }
62
+
63
+ function commit(next: AmbientThemeState): void {
64
+ // Skip a no-op commit so subscribers don't re-render for an identical value.
65
+ if (
66
+ next.seed === state.seed &&
67
+ next.secondary === state.secondary &&
68
+ next.tertiary === state.tertiary
69
+ ) {
70
+ return;
71
+ }
72
+ state = next;
73
+ emit();
74
+ }
75
+
76
+ function schedule(next: AmbientThemeState, debounceMs: number): void {
77
+ if (pendingTimer !== null) {
78
+ clearTimeout(pendingTimer);
79
+ pendingTimer = null;
80
+ }
81
+ const run = () => {
82
+ pendingTimer = null;
83
+ pendingCommit = null;
84
+ commit(next);
85
+ };
86
+ if (debounceMs <= 0) {
87
+ run();
88
+ return;
89
+ }
90
+ pendingCommit = run;
91
+ pendingTimer = setTimeout(run, debounceMs);
92
+ pendingTimer.unref?.();
93
+ }
94
+
95
+ function subscribe(listener: () => void): () => void {
96
+ listeners.add(listener);
97
+ return () => {
98
+ listeners.delete(listener);
99
+ };
100
+ }
101
+
102
+ function getSnapshot(): AmbientThemeState {
103
+ return state;
104
+ }
105
+
106
+ /** Internal setter used by the debounced hook API. */
107
+ function setAmbientInternal(seed: string, accents: AmbientAccents | undefined, debounceMs: number): void {
108
+ schedule(
109
+ {
110
+ seed,
111
+ secondary: accents?.secondary ?? null,
112
+ tertiary: accents?.tertiary ?? null,
113
+ },
114
+ debounceMs,
115
+ );
116
+ }
117
+
118
+ /** Internal clear used by the debounced hook API. */
119
+ function clearAmbientInternal(debounceMs: number): void {
120
+ schedule(EMPTY_STATE, debounceMs);
121
+ }
122
+
123
+ /**
124
+ * Read + drive the app-wide ambient theme. Reading returns the current override
125
+ * (via `useSyncExternalStore` — stable snapshot ref while unchanged); the
126
+ * returned `setAmbient`/`clearAmbient` are debounced imperative controls.
127
+ *
128
+ * `BloomThemeProvider` consumes the SAME store internally, so calling
129
+ * `setAmbient(...)` from anywhere themes the whole app through the provider's
130
+ * single apply path — no `seed` prop threading, no app-owned theming store.
131
+ */
132
+ export function useAmbientTheme(
133
+ options?: UseAmbientThemeOptions,
134
+ ): AmbientThemeState & AmbientThemeApi {
135
+ const debounceMs = options?.debounceMs ?? DEFAULT_DEBOUNCE_MS;
136
+ const current = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
137
+ return {
138
+ seed: current.seed,
139
+ secondary: current.secondary,
140
+ tertiary: current.tertiary,
141
+ setAmbient: (seed, accents) => setAmbientInternal(seed, accents, debounceMs),
142
+ clearAmbient: () => clearAmbientInternal(debounceMs),
143
+ };
144
+ }
145
+
146
+ /**
147
+ * Provider-side subscription. Returns the current ambient state with a stable
148
+ * ref while unchanged. Uses the same store, so it stays in lockstep with any
149
+ * `useAmbientTheme()` caller.
150
+ */
151
+ export function useAmbientThemeState(): AmbientThemeState {
152
+ return useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
153
+ }
154
+
155
+ /**
156
+ * Non-hook imperative escape hatch (e.g. gesture handlers / worklets bridges /
157
+ * non-React callers). Same debounced store as {@link useAmbientTheme}.
158
+ */
159
+ export const ambientTheme: AmbientThemeApi & { getState: () => AmbientThemeState } = {
160
+ setAmbient: (seed, accents) => setAmbientInternal(seed, accents, DEFAULT_DEBOUNCE_MS),
161
+ clearAmbient: () => clearAmbientInternal(DEFAULT_DEBOUNCE_MS),
162
+ getState: getSnapshot,
163
+ };
164
+
165
+ /** Test-only: flush any pending debounced commit synchronously. */
166
+ export function __flushAmbientForTests(): void {
167
+ if (pendingCommit) {
168
+ if (pendingTimer !== null) {
169
+ clearTimeout(pendingTimer);
170
+ pendingTimer = null;
171
+ }
172
+ const run = pendingCommit;
173
+ pendingCommit = null;
174
+ run();
175
+ }
176
+ }
177
+
178
+ /** Test-only: reset the store to the empty state without notifying via debounce. */
179
+ export function __resetAmbientForTests(): void {
180
+ if (pendingTimer !== null) {
181
+ clearTimeout(pendingTimer);
182
+ pendingTimer = null;
183
+ }
184
+ pendingCommit = null;
185
+ commit(EMPTY_STATE);
186
+ }
@@ -30,6 +30,13 @@ export { buildThemeFromSeed, buildColorsFromSeed } from './build-theme-from-seed
30
30
  export type { SeedAccents } from './build-theme-from-seed';
31
31
  export { THEME_GRADIENTS } from './gradients';
32
32
  export { useTheme, useThemeColor, useBloomTheme } from './use-theme';
33
+ export { useAmbientTheme, ambientTheme } from './ambient-store';
34
+ export type {
35
+ AmbientThemeState,
36
+ AmbientThemeApi,
37
+ AmbientAccents,
38
+ UseAmbientThemeOptions,
39
+ } from './ambient-store';
33
40
  export { useNavigationTheme } from './use-navigation-theme';
34
41
  export type { NavigationTheme, NavigationThemeFont } from './use-navigation-theme';
35
42
  export type { Theme, ThemeColors, ThemeMode, ThemeGradient, ThemeGradients } from './types';