@oxyhq/bloom 0.39.0 → 0.40.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 (38) hide show
  1. package/lib/commonjs/theme/build-theme-from-seed.js +90 -0
  2. package/lib/commonjs/theme/build-theme-from-seed.js.map +1 -0
  3. package/lib/commonjs/theme/index.js +94 -0
  4. package/lib/commonjs/theme/index.js.map +1 -1
  5. package/lib/commonjs/theme/seed-scope/index.js +87 -0
  6. package/lib/commonjs/theme/seed-scope/index.js.map +1 -0
  7. package/lib/commonjs/theme/seed-scope/index.web.js +86 -0
  8. package/lib/commonjs/theme/seed-scope/index.web.js.map +1 -0
  9. package/lib/module/theme/build-theme-from-seed.js +85 -0
  10. package/lib/module/theme/build-theme-from-seed.js.map +1 -0
  11. package/lib/module/theme/index.js +7 -0
  12. package/lib/module/theme/index.js.map +1 -1
  13. package/lib/module/theme/seed-scope/index.js +82 -0
  14. package/lib/module/theme/seed-scope/index.js.map +1 -0
  15. package/lib/module/theme/seed-scope/index.web.js +81 -0
  16. package/lib/module/theme/seed-scope/index.web.js.map +1 -0
  17. package/lib/typescript/commonjs/theme/build-theme-from-seed.d.ts +23 -0
  18. package/lib/typescript/commonjs/theme/build-theme-from-seed.d.ts.map +1 -0
  19. package/lib/typescript/commonjs/theme/index.d.ts +7 -0
  20. package/lib/typescript/commonjs/theme/index.d.ts.map +1 -1
  21. package/lib/typescript/commonjs/theme/seed-scope/index.d.ts +37 -0
  22. package/lib/typescript/commonjs/theme/seed-scope/index.d.ts.map +1 -0
  23. package/lib/typescript/commonjs/theme/seed-scope/index.web.d.ts +35 -0
  24. package/lib/typescript/commonjs/theme/seed-scope/index.web.d.ts.map +1 -0
  25. package/lib/typescript/module/theme/build-theme-from-seed.d.ts +23 -0
  26. package/lib/typescript/module/theme/build-theme-from-seed.d.ts.map +1 -0
  27. package/lib/typescript/module/theme/index.d.ts +7 -0
  28. package/lib/typescript/module/theme/index.d.ts.map +1 -1
  29. package/lib/typescript/module/theme/seed-scope/index.d.ts +37 -0
  30. package/lib/typescript/module/theme/seed-scope/index.d.ts.map +1 -0
  31. package/lib/typescript/module/theme/seed-scope/index.web.d.ts +35 -0
  32. package/lib/typescript/module/theme/seed-scope/index.web.d.ts.map +1 -0
  33. package/package.json +1 -1
  34. package/src/theme/__tests__/build-theme-from-seed-parity.test.ts +33 -0
  35. package/src/theme/build-theme-from-seed.ts +99 -0
  36. package/src/theme/index.ts +21 -0
  37. package/src/theme/seed-scope/index.tsx +100 -0
  38. package/src/theme/seed-scope/index.web.tsx +110 -0
@@ -0,0 +1,110 @@
1
+ import React, { Children, cloneElement, isValidElement, useContext, useMemo } from 'react';
2
+
3
+ import { BloomThemeContext, type BloomThemeContextValue } from '../BloomThemeProvider';
4
+ import { buildSeedScopeVars } from '../color-scope/seed-scope';
5
+ import { buildThemeFromSeed } from '../build-theme-from-seed';
6
+ import type { SchemeVariant } from '../color-engine';
7
+
8
+ export interface BloomSeedScopeProps {
9
+ /**
10
+ * The seed colour (`#rrggbb`) to derive this subtree's palette from. When
11
+ * `undefined`, the scope is a no-op and children inherit the parent scope
12
+ * unchanged — pass `undefined` to fall back to the app preset (e.g. on
13
+ * mouse-leave/blur when restoring the default theme).
14
+ */
15
+ seed: string | undefined;
16
+ /** Tonal scheme variant. Defaults to `'vibrant'` (matches Bloom presets). */
17
+ variant?: SchemeVariant;
18
+ /** −1 (low) … 0 (normal) … 1 (high). Defaults to 0. */
19
+ contrastLevel?: number;
20
+ /**
21
+ * When `true`, do not render a wrapping `<div>`. The single child is cloned
22
+ * with the scope's CSS vars merged into its `style` prop (Radix-style).
23
+ */
24
+ asChild?: boolean;
25
+ /** Additional style applied to the wrapping `<div>` (or merged into the cloned child with `asChild`). */
26
+ style?: React.CSSProperties;
27
+ children: React.ReactNode;
28
+ }
29
+
30
+ /**
31
+ * On web the single `asChild` child is frequently a react-native-web component
32
+ * whose `style` prop is a style ARRAY (or a numeric registered-style id), not a
33
+ * plain object. Spreading such an array into an object literal would copy its
34
+ * numeric indices as keys and crash RNW; merge as an array instead.
35
+ */
36
+ type WebStyle =
37
+ | React.CSSProperties
38
+ | number
39
+ | null
40
+ | undefined
41
+ | false
42
+ | ReadonlyArray<WebStyle>;
43
+
44
+ interface StyleableProps {
45
+ style?: WebStyle;
46
+ }
47
+
48
+ /**
49
+ * Web `<BloomSeedScope>`: scopes a subtree to an ARBITRARY seed colour's palette
50
+ * (the dynamic counterpart of `<BloomColorScope>`). It (1) writes every canonical
51
+ * `--x` token plus the Tailwind v4 `--color-x` aliases (already resolved to
52
+ * `rgb(...)`) onto the wrapping element's inline `style`, so NativeWind classes
53
+ * inside resolve against the seed palette instead of the document root, and
54
+ * (2) publishes a matching JS `theme` on `BloomThemeContext` so
55
+ * `useTheme()`/`useBloomTheme()` consumers inside the subtree re-theme too. The
56
+ * `colorPreset` field is left as the parent's (a seed is not a named preset).
57
+ */
58
+ export function BloomSeedScope({
59
+ seed,
60
+ variant,
61
+ contrastLevel,
62
+ asChild = false,
63
+ style,
64
+ children,
65
+ }: BloomSeedScopeProps) {
66
+ const parent = useContext(BloomThemeContext);
67
+ const resolvedMode = parent?.theme.mode ?? 'light';
68
+
69
+ const varsStyle = useMemo<React.CSSProperties | null>(
70
+ () =>
71
+ seed
72
+ ? (buildSeedScopeVars({
73
+ seed,
74
+ mode: resolvedMode,
75
+ variant,
76
+ contrastLevel,
77
+ }) as React.CSSProperties)
78
+ : null,
79
+ [seed, resolvedMode, variant, contrastLevel],
80
+ );
81
+
82
+ const contextValue = useMemo<BloomThemeContextValue | null>(() => {
83
+ if (!parent || !seed) return null;
84
+ const theme = buildThemeFromSeed(seed, resolvedMode, variant, contrastLevel);
85
+ return { ...parent, theme };
86
+ }, [parent, seed, resolvedMode, variant, contrastLevel]);
87
+
88
+ if (!parent) {
89
+ throw new Error('BloomSeedScope must be used within a <BloomThemeProvider>');
90
+ }
91
+ if (!varsStyle || !contextValue) return <>{children}</>;
92
+
93
+ let content: React.ReactNode;
94
+ if (asChild) {
95
+ const child = Children.only(children);
96
+ if (!isValidElement<StyleableProps>(child)) {
97
+ throw new Error(
98
+ 'BloomSeedScope with `asChild` requires a single React element child that accepts a `style` prop.',
99
+ );
100
+ }
101
+ const childStyle = child.props.style;
102
+ const mergedStyle: WebStyle = [varsStyle, style, childStyle];
103
+ content = cloneElement(child, { style: mergedStyle });
104
+ } else {
105
+ const mergedStyle: React.CSSProperties = { ...varsStyle, ...style };
106
+ content = <div style={mergedStyle}>{children}</div>;
107
+ }
108
+
109
+ return <BloomThemeContext.Provider value={contextValue}>{content}</BloomThemeContext.Provider>;
110
+ }