@oxyhq/bloom 0.38.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.
- package/lib/commonjs/design-tokens/theme-css.js +3 -0
- package/lib/commonjs/design-tokens/theme-css.js.map +1 -1
- package/lib/commonjs/design-tokens/theme.css +3 -0
- package/lib/commonjs/prompt-input/Attachments.js +2 -2
- package/lib/commonjs/prompt-input/Attachments.js.map +1 -1
- package/lib/commonjs/theme/adaptive-colors.js +6 -0
- package/lib/commonjs/theme/adaptive-colors.js.map +1 -1
- package/lib/commonjs/theme/build-theme-from-seed.js +90 -0
- package/lib/commonjs/theme/build-theme-from-seed.js.map +1 -0
- package/lib/commonjs/theme/build-theme.js +5 -1
- package/lib/commonjs/theme/build-theme.js.map +1 -1
- package/lib/commonjs/theme/color-scope/seed-scope.js +99 -0
- package/lib/commonjs/theme/color-scope/seed-scope.js.map +1 -0
- package/lib/commonjs/theme/index.js +94 -0
- package/lib/commonjs/theme/index.js.map +1 -1
- package/lib/commonjs/theme/preset-vars.js +4 -0
- package/lib/commonjs/theme/preset-vars.js.map +1 -1
- package/lib/commonjs/theme/seed-scope/index.js +87 -0
- package/lib/commonjs/theme/seed-scope/index.js.map +1 -0
- package/lib/commonjs/theme/seed-scope/index.web.js +86 -0
- package/lib/commonjs/theme/seed-scope/index.web.js.map +1 -0
- package/lib/commonjs/theme/token-registry.js +1 -1
- package/lib/commonjs/theme/token-registry.js.map +1 -1
- package/lib/module/design-tokens/theme-css.js +3 -0
- package/lib/module/design-tokens/theme-css.js.map +1 -1
- package/lib/module/design-tokens/theme.css +3 -0
- package/lib/module/prompt-input/Attachments.js +2 -2
- package/lib/module/prompt-input/Attachments.js.map +1 -1
- package/lib/module/theme/adaptive-colors.js +6 -0
- package/lib/module/theme/adaptive-colors.js.map +1 -1
- package/lib/module/theme/build-theme-from-seed.js +85 -0
- package/lib/module/theme/build-theme-from-seed.js.map +1 -0
- package/lib/module/theme/build-theme.js +5 -1
- package/lib/module/theme/build-theme.js.map +1 -1
- package/lib/module/theme/color-scope/seed-scope.js +95 -0
- package/lib/module/theme/color-scope/seed-scope.js.map +1 -0
- package/lib/module/theme/index.js +7 -0
- package/lib/module/theme/index.js.map +1 -1
- package/lib/module/theme/preset-vars.js +4 -0
- package/lib/module/theme/preset-vars.js.map +1 -1
- package/lib/module/theme/seed-scope/index.js +82 -0
- package/lib/module/theme/seed-scope/index.js.map +1 -0
- package/lib/module/theme/seed-scope/index.web.js +81 -0
- package/lib/module/theme/seed-scope/index.web.js.map +1 -0
- package/lib/module/theme/token-registry.js +1 -1
- package/lib/module/theme/token-registry.js.map +1 -1
- package/lib/typescript/commonjs/design-tokens/theme-css.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/adaptive-colors.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/build-theme-from-seed.d.ts +23 -0
- package/lib/typescript/commonjs/theme/build-theme-from-seed.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/build-theme.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/color-scope/seed-scope.d.ts +43 -0
- package/lib/typescript/commonjs/theme/color-scope/seed-scope.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/index.d.ts +7 -0
- package/lib/typescript/commonjs/theme/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/preset-vars.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/seed-scope/index.d.ts +37 -0
- package/lib/typescript/commonjs/theme/seed-scope/index.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/seed-scope/index.web.d.ts +35 -0
- package/lib/typescript/commonjs/theme/seed-scope/index.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/token-registry.d.ts +1 -1
- package/lib/typescript/commonjs/theme/token-registry.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/types.d.ts +5 -1
- package/lib/typescript/commonjs/theme/types.d.ts.map +1 -1
- package/lib/typescript/module/design-tokens/theme-css.d.ts.map +1 -1
- package/lib/typescript/module/theme/adaptive-colors.d.ts.map +1 -1
- package/lib/typescript/module/theme/build-theme-from-seed.d.ts +23 -0
- package/lib/typescript/module/theme/build-theme-from-seed.d.ts.map +1 -0
- package/lib/typescript/module/theme/build-theme.d.ts.map +1 -1
- package/lib/typescript/module/theme/color-scope/seed-scope.d.ts +43 -0
- package/lib/typescript/module/theme/color-scope/seed-scope.d.ts.map +1 -0
- package/lib/typescript/module/theme/index.d.ts +7 -0
- package/lib/typescript/module/theme/index.d.ts.map +1 -1
- package/lib/typescript/module/theme/preset-vars.d.ts.map +1 -1
- package/lib/typescript/module/theme/seed-scope/index.d.ts +37 -0
- package/lib/typescript/module/theme/seed-scope/index.d.ts.map +1 -0
- package/lib/typescript/module/theme/seed-scope/index.web.d.ts +35 -0
- package/lib/typescript/module/theme/seed-scope/index.web.d.ts.map +1 -0
- package/lib/typescript/module/theme/token-registry.d.ts +1 -1
- package/lib/typescript/module/theme/token-registry.d.ts.map +1 -1
- package/lib/typescript/module/theme/types.d.ts +5 -1
- package/lib/typescript/module/theme/types.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/design-tokens/theme-css.ts +3 -0
- package/src/design-tokens/theme.css +3 -0
- package/src/prompt-input/Attachments.tsx +2 -2
- package/src/theme/__tests__/__snapshots__/visual-gallery.test.tsx.snap +78 -0
- package/src/theme/__tests__/build-theme-from-seed-parity.test.ts +33 -0
- package/src/theme/__tests__/seed-scope-parity.test.ts +27 -0
- package/src/theme/adaptive-colors.ts +6 -0
- package/src/theme/build-theme-from-seed.ts +99 -0
- package/src/theme/build-theme.ts +5 -1
- package/src/theme/color-scope/seed-scope.ts +105 -0
- package/src/theme/index.ts +21 -0
- package/src/theme/preset-vars.ts +4 -0
- package/src/theme/seed-scope/index.tsx +100 -0
- package/src/theme/seed-scope/index.web.tsx +110 -0
- package/src/theme/token-registry.ts +2 -0
- package/src/theme/types.ts +5 -1
|
@@ -23,6 +23,9 @@ function getAndroidColors(): ThemeColors | null {
|
|
|
23
23
|
primaryLight: c(d.primaryContainer),
|
|
24
24
|
primaryDark: c(d.onPrimaryContainer),
|
|
25
25
|
secondary: c(d.secondary),
|
|
26
|
+
secondaryForeground: c(d.onSecondary),
|
|
27
|
+
tertiary: c(d.tertiary),
|
|
28
|
+
tertiaryForeground: c(d.onTertiary),
|
|
26
29
|
tint: c(d.primary),
|
|
27
30
|
icon: c(d.onSurfaceVariant),
|
|
28
31
|
iconActive: c(d.primary),
|
|
@@ -66,6 +69,9 @@ function getIOSColors(): ThemeColors | null {
|
|
|
66
69
|
primaryLight: c(i.systemGray6),
|
|
67
70
|
primaryDark: c(i.systemBlue),
|
|
68
71
|
secondary: c(i.systemPurple),
|
|
72
|
+
secondaryForeground: '#FFFFFF',
|
|
73
|
+
tertiary: c(i.systemTeal),
|
|
74
|
+
tertiaryForeground: '#FFFFFF',
|
|
69
75
|
tint: c(i.systemBlue),
|
|
70
76
|
icon: c(i.secondaryLabel),
|
|
71
77
|
iconActive: c(i.systemBlue),
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { generateRoleColors, type RoleColors, type SchemeVariant } from './color-engine';
|
|
2
|
+
import { buildSeedScopeVars } from './color-scope/seed-scope';
|
|
3
|
+
import { STATUS_COLORS } from './build-theme';
|
|
4
|
+
import { THEME_GRADIENTS } from './gradients';
|
|
5
|
+
import type { Theme, ThemeColors } from './types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Build the JS `theme.colors` object from an ARBITRARY seed colour — the dynamic
|
|
9
|
+
* counterpart of `buildTheme(preset, …)`.
|
|
10
|
+
*
|
|
11
|
+
* This mirrors `buildColorsFromPreset` (`build-theme.ts`) field-for-field, but
|
|
12
|
+
* sources its tokens from `buildSeedScopeVars({ seed })` instead of a named
|
|
13
|
+
* preset, so a preset's seed reproduces that preset's JS theme exactly. Keeping
|
|
14
|
+
* the two in lockstep is enforced by `__tests__/build-theme-from-seed-parity.test.ts`.
|
|
15
|
+
*
|
|
16
|
+
* Token-backed fields read the resolved `rgb(...)` token map; the brand-tint
|
|
17
|
+
* family (primarySubtle / negative / …) reads the engine's container/error roles
|
|
18
|
+
* directly — identical to the preset path.
|
|
19
|
+
*/
|
|
20
|
+
export function buildColorsFromSeed(
|
|
21
|
+
seed: string,
|
|
22
|
+
resolved: 'light' | 'dark',
|
|
23
|
+
variant?: SchemeVariant,
|
|
24
|
+
contrastLevel?: number,
|
|
25
|
+
): ThemeColors {
|
|
26
|
+
const t = buildSeedScopeVars({ seed, mode: resolved, variant, contrastLevel });
|
|
27
|
+
const isDark = resolved === 'dark';
|
|
28
|
+
|
|
29
|
+
// Read a resolved `rgb(...)` token by its bare name (no leading `--`).
|
|
30
|
+
const g = (k: string): string => t[`--${k}`] ?? 'rgb(0 0 0)';
|
|
31
|
+
|
|
32
|
+
const r: RoleColors = generateRoleColors({
|
|
33
|
+
seed,
|
|
34
|
+
variant,
|
|
35
|
+
isDark,
|
|
36
|
+
contrastLevel,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
background: g('background'),
|
|
41
|
+
backgroundSecondary: g('surface'),
|
|
42
|
+
backgroundTertiary: g('popover'),
|
|
43
|
+
|
|
44
|
+
text: g('foreground'),
|
|
45
|
+
textSecondary: g('muted-foreground'),
|
|
46
|
+
textTertiary: r.outline,
|
|
47
|
+
|
|
48
|
+
border: g('border'),
|
|
49
|
+
borderLight: g('input'),
|
|
50
|
+
|
|
51
|
+
primary: g('primary'),
|
|
52
|
+
primaryForeground: g('primary-foreground'),
|
|
53
|
+
primaryLight: g('surface'),
|
|
54
|
+
primaryDark: g('background'),
|
|
55
|
+
|
|
56
|
+
secondary: g('secondary'),
|
|
57
|
+
secondaryForeground: g('secondary-foreground'),
|
|
58
|
+
tertiary: g('tertiary'),
|
|
59
|
+
tertiaryForeground: g('tertiary-foreground'),
|
|
60
|
+
|
|
61
|
+
tint: g('primary'),
|
|
62
|
+
icon: g('muted-foreground'),
|
|
63
|
+
iconActive: g('primary'),
|
|
64
|
+
|
|
65
|
+
...STATUS_COLORS,
|
|
66
|
+
|
|
67
|
+
primarySubtle: r.primaryContainer,
|
|
68
|
+
primarySubtleForeground: r.onPrimaryContainer,
|
|
69
|
+
negative: r.error,
|
|
70
|
+
negativeForeground: r.onError,
|
|
71
|
+
negativeSubtle: r.errorContainer,
|
|
72
|
+
negativeSubtleForeground: r.onErrorContainer,
|
|
73
|
+
contrast50: g('muted'),
|
|
74
|
+
|
|
75
|
+
card: g('card'),
|
|
76
|
+
shadow: isDark ? 'rgba(0, 0, 0, 0.3)' : 'rgba(0, 0, 0, 0.1)',
|
|
77
|
+
overlay: 'rgba(0, 0, 0, 0.5)',
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Build a full `Theme` from a seed colour and a resolved light/dark mode. Unlike
|
|
83
|
+
* `buildTheme`, there is no adaptive (Material You / iOS dynamic) override — a
|
|
84
|
+
* seed IS the dynamic source here.
|
|
85
|
+
*/
|
|
86
|
+
export function buildThemeFromSeed(
|
|
87
|
+
seed: string,
|
|
88
|
+
resolved: 'light' | 'dark',
|
|
89
|
+
variant?: SchemeVariant,
|
|
90
|
+
contrastLevel?: number,
|
|
91
|
+
): Theme {
|
|
92
|
+
return {
|
|
93
|
+
mode: resolved,
|
|
94
|
+
colors: buildColorsFromSeed(seed, resolved, variant, contrastLevel),
|
|
95
|
+
gradients: THEME_GRADIENTS,
|
|
96
|
+
isDark: resolved === 'dark',
|
|
97
|
+
isLight: resolved === 'light',
|
|
98
|
+
};
|
|
99
|
+
}
|
package/src/theme/build-theme.ts
CHANGED
|
@@ -67,8 +67,12 @@ function buildColorsFromPreset(
|
|
|
67
67
|
primaryLight: g('surface'),
|
|
68
68
|
primaryDark: g('background'),
|
|
69
69
|
|
|
70
|
-
// The
|
|
70
|
+
// The M3 accent trio: secondary is a real contrast colour (engine `secondary`),
|
|
71
|
+
// and tertiary completes primary/secondary/tertiary.
|
|
71
72
|
secondary: g('secondary'),
|
|
73
|
+
secondaryForeground: g('secondary-foreground'),
|
|
74
|
+
tertiary: g('tertiary'),
|
|
75
|
+
tertiaryForeground: g('tertiary-foreground'),
|
|
72
76
|
|
|
73
77
|
tint: g('primary'),
|
|
74
78
|
icon: g('muted-foreground'),
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { generateRoleColors, type RoleColors, type SchemeVariant } from '../color-engine';
|
|
2
|
+
import { CANONICAL_TOKENS } from '../token-registry';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The canonical role → token assignment, sourced from an arbitrary seed's role
|
|
6
|
+
* set instead of a named preset.
|
|
7
|
+
*
|
|
8
|
+
* This MUST stay byte-for-byte identical to the mapping in `getPresetVars`
|
|
9
|
+
* (`preset-vars.ts`): a named preset is just a fixed seed, so scoping a subtree
|
|
10
|
+
* to a preset via `buildScopeVars` and scoping it to that preset's seed hex via
|
|
11
|
+
* `buildSeedScopeVars` are required to produce the same tokens. The parity test
|
|
12
|
+
* (`__tests__/seed-scope-parity.test.ts`) fails CI if the two ever drift.
|
|
13
|
+
*
|
|
14
|
+
* Values are full `rgb(r g b)` strings straight from the engine — no HSL-triple
|
|
15
|
+
* intermediate, so they flow through the scope pipeline unchanged (same as the
|
|
16
|
+
* preset path after `getResolvedTokens`).
|
|
17
|
+
*/
|
|
18
|
+
export function roleColorsToPresetTokens(r: RoleColors): Record<string, string> {
|
|
19
|
+
return {
|
|
20
|
+
// --- base shadcn palette ---
|
|
21
|
+
'--background': r.background,
|
|
22
|
+
'--foreground': r.onBackground,
|
|
23
|
+
'--surface': r.surfaceContainerLow,
|
|
24
|
+
'--surface-foreground': r.onSurface,
|
|
25
|
+
'--popover': r.surfaceContainer,
|
|
26
|
+
'--popover-foreground': r.onSurface,
|
|
27
|
+
'--primary': r.primary,
|
|
28
|
+
'--primary-foreground': r.onPrimary,
|
|
29
|
+
'--secondary': r.secondary,
|
|
30
|
+
'--secondary-foreground': r.onSecondary,
|
|
31
|
+
'--tertiary': r.tertiary,
|
|
32
|
+
'--tertiary-foreground': r.onTertiary,
|
|
33
|
+
'--muted': r.surfaceContainerHigh,
|
|
34
|
+
'--muted-foreground': r.onSurfaceVariant,
|
|
35
|
+
'--accent': r.secondaryContainer,
|
|
36
|
+
'--accent-foreground': r.onSecondaryContainer,
|
|
37
|
+
'--destructive': r.error,
|
|
38
|
+
'--border': r.outlineVariant,
|
|
39
|
+
'--input': r.outlineVariant,
|
|
40
|
+
'--ring': r.primary,
|
|
41
|
+
'--sidebar': r.surfaceContainerLow,
|
|
42
|
+
|
|
43
|
+
// --- extended tokens ---
|
|
44
|
+
'--card': r.surfaceContainerLowest,
|
|
45
|
+
'--card-foreground': r.onSurface,
|
|
46
|
+
'--chart-1': r.primary,
|
|
47
|
+
'--chart-2': r.secondary,
|
|
48
|
+
'--chart-3': r.tertiary,
|
|
49
|
+
'--chart-4': r.primaryContainer,
|
|
50
|
+
'--chart-5': r.tertiaryContainer,
|
|
51
|
+
'--content-area': r.background,
|
|
52
|
+
'--sidebar-foreground': r.onSurface,
|
|
53
|
+
'--sidebar-primary': r.primary,
|
|
54
|
+
'--sidebar-primary-foreground': r.onPrimary,
|
|
55
|
+
'--sidebar-accent': r.secondaryContainer,
|
|
56
|
+
'--sidebar-accent-foreground': r.onSecondaryContainer,
|
|
57
|
+
'--sidebar-border': r.outlineVariant,
|
|
58
|
+
'--sidebar-ring': r.primary,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Options for deriving scope vars directly from a seed colour. */
|
|
63
|
+
export interface SeedScopeOptions {
|
|
64
|
+
/** The seed colour, `#rrggbb`. */
|
|
65
|
+
seed: string;
|
|
66
|
+
mode: 'light' | 'dark';
|
|
67
|
+
/**
|
|
68
|
+
* The tonal scheme variant. Defaults to `'vibrant'` — the same variant every
|
|
69
|
+
* named Bloom preset uses, so a preset's seed reproduces that preset exactly.
|
|
70
|
+
*/
|
|
71
|
+
variant?: SchemeVariant;
|
|
72
|
+
/** −1 (low) … 0 (normal) … 1 (high). Defaults to 0. */
|
|
73
|
+
contrastLevel?: number;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Build the CSS custom-property map for an ARBITRARY seed colour — the dynamic
|
|
78
|
+
* (non-preset) counterpart of `buildScopeVars`. Feeds the seed through the
|
|
79
|
+
* colour engine's `generateRoleColors` and assigns the roles to Bloom's
|
|
80
|
+
* canonical token names, then adds the Tailwind v4 `--color-x` aliases (required
|
|
81
|
+
* on web so scoped utilities resolve against the subtree instead of the document
|
|
82
|
+
* root).
|
|
83
|
+
*
|
|
84
|
+
* Scope the returned record onto an element (web: inline `style`; native:
|
|
85
|
+
* NativeWind `VariableContextProvider`) to tint just that subtree — e.g. the
|
|
86
|
+
* ambient region behind album/artist artwork — without re-theming the whole app.
|
|
87
|
+
*/
|
|
88
|
+
export function buildSeedScopeVars(options: SeedScopeOptions): Record<string, string> {
|
|
89
|
+
const roles = generateRoleColors({
|
|
90
|
+
seed: options.seed,
|
|
91
|
+
isDark: options.mode === 'dark',
|
|
92
|
+
contrastLevel: options.contrastLevel ?? 0,
|
|
93
|
+
});
|
|
94
|
+
const tokens = roleColorsToPresetTokens(roles);
|
|
95
|
+
const vars: Record<string, string> = { ...tokens };
|
|
96
|
+
|
|
97
|
+
for (const token of CANONICAL_TOKENS) {
|
|
98
|
+
const value = tokens[`--${token}`];
|
|
99
|
+
if (value !== undefined) {
|
|
100
|
+
vars[`--color-${token}`] = value;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return vars;
|
|
105
|
+
}
|
package/src/theme/index.ts
CHANGED
|
@@ -5,7 +5,28 @@ export type {
|
|
|
5
5
|
} from './BloomThemeProvider';
|
|
6
6
|
export { BloomColorScope, useColorScopeStyle } from './color-scope';
|
|
7
7
|
export type { BloomColorScopeProps } from './color-scope';
|
|
8
|
+
export { BloomSeedScope } from './seed-scope';
|
|
9
|
+
export type { BloomSeedScopeProps } from './seed-scope';
|
|
10
|
+
export { buildSeedScopeVars, roleColorsToPresetTokens } from './color-scope/seed-scope';
|
|
11
|
+
export type { SeedScopeOptions } from './color-scope/seed-scope';
|
|
12
|
+
// Colour engine — the dependency-free tonal system that turns ANY seed colour
|
|
13
|
+
// into a full role set. Surfaced here so consumers can derive dynamic themes
|
|
14
|
+
// (e.g. from artwork) without reaching into the internal color-engine path.
|
|
15
|
+
export {
|
|
16
|
+
generateRoleColors,
|
|
17
|
+
seedHexFromImagePixels,
|
|
18
|
+
seedsFromImagePixels,
|
|
19
|
+
quantizeImage,
|
|
20
|
+
argbFromHex,
|
|
21
|
+
hexFromArgb,
|
|
22
|
+
argbFromRgb,
|
|
23
|
+
redFromArgb,
|
|
24
|
+
greenFromArgb,
|
|
25
|
+
blueFromArgb,
|
|
26
|
+
} from './color-engine';
|
|
27
|
+
export type { RoleColors, GenerateOptions, RoleName, SchemeVariant } from './color-engine';
|
|
8
28
|
export { buildTheme, STATUS_COLORS } from './build-theme';
|
|
29
|
+
export { buildThemeFromSeed, buildColorsFromSeed } from './build-theme-from-seed';
|
|
9
30
|
export { THEME_GRADIENTS } from './gradients';
|
|
10
31
|
export { useTheme, useThemeColor, useBloomTheme } from './use-theme';
|
|
11
32
|
export { useNavigationTheme } from './use-navigation-theme';
|
package/src/theme/preset-vars.ts
CHANGED
|
@@ -48,6 +48,10 @@ export function getPresetVars(
|
|
|
48
48
|
// role), not a mirror of `primary`.
|
|
49
49
|
'--secondary': r.secondary,
|
|
50
50
|
'--secondary-foreground': r.onSecondary,
|
|
51
|
+
// The third accent of the M3 trio, exposed so `bg-tertiary` / `colors.tertiary`
|
|
52
|
+
// work alongside primary + secondary.
|
|
53
|
+
'--tertiary': r.tertiary,
|
|
54
|
+
'--tertiary-foreground': r.onTertiary,
|
|
51
55
|
'--muted': r.surfaceContainerHigh,
|
|
52
56
|
'--muted-foreground': r.onSurfaceVariant,
|
|
53
57
|
'--accent': r.secondaryContainer,
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import React, { Children, cloneElement, isValidElement, useContext, useMemo } from 'react';
|
|
2
|
+
import { View, type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
|
|
4
|
+
import { BloomThemeContext, type BloomThemeContextValue } from '../BloomThemeProvider';
|
|
5
|
+
import { buildSeedScopeVars } from '../color-scope/seed-scope';
|
|
6
|
+
import { getVariableContextProvider } from '../color-scope/style-builder';
|
|
7
|
+
import { buildThemeFromSeed } from '../build-theme-from-seed';
|
|
8
|
+
import type { SchemeVariant } from '../color-engine';
|
|
9
|
+
|
|
10
|
+
export interface BloomSeedScopeProps {
|
|
11
|
+
/**
|
|
12
|
+
* The seed colour (`#rrggbb`) to derive this subtree's palette from. When
|
|
13
|
+
* `undefined`, the scope is a no-op and children inherit the parent scope
|
|
14
|
+
* unchanged — pass `undefined` to fall back to the app preset (e.g. on
|
|
15
|
+
* mouse-leave/blur when restoring the default theme).
|
|
16
|
+
*/
|
|
17
|
+
seed: string | undefined;
|
|
18
|
+
/** Tonal scheme variant. Defaults to `'vibrant'` (matches Bloom presets). */
|
|
19
|
+
variant?: SchemeVariant;
|
|
20
|
+
/** −1 (low) … 0 (normal) … 1 (high). Defaults to 0. */
|
|
21
|
+
contrastLevel?: number;
|
|
22
|
+
/**
|
|
23
|
+
* When `true`, do not render a wrapping `<View>`. The single child is cloned
|
|
24
|
+
* with the caller's `style` merged into its `style` prop (Radix-style).
|
|
25
|
+
*/
|
|
26
|
+
asChild?: boolean;
|
|
27
|
+
/** Additional style applied to the wrapping `<View>` (or merged into the cloned child with `asChild`). */
|
|
28
|
+
style?: StyleProp<ViewStyle>;
|
|
29
|
+
children: React.ReactNode;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
interface StyleableProps {
|
|
33
|
+
style?: StyleProp<ViewStyle>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Native `<BloomSeedScope>`: scopes a subtree to an ARBITRARY seed colour's
|
|
38
|
+
* palette (the dynamic counterpart of `<BloomColorScope>`, which takes a named
|
|
39
|
+
* preset). Feeds the seed through the colour engine and (1) provides the
|
|
40
|
+
* resulting canonical CSS vars to descendants via react-native-css's
|
|
41
|
+
* `VariableContext` — so `bg-background` / `bg-card` / `var(--primary)` resolve
|
|
42
|
+
* against the seed inside this subtree — and (2) publishes a matching JS `theme`
|
|
43
|
+
* on `BloomThemeContext`, so `useTheme()`/`useBloomTheme()` consumers inside the
|
|
44
|
+
* subtree also re-theme. The `colorPreset` field is left as the parent's (a seed
|
|
45
|
+
* is not a named preset).
|
|
46
|
+
*/
|
|
47
|
+
export function BloomSeedScope({
|
|
48
|
+
seed,
|
|
49
|
+
variant,
|
|
50
|
+
contrastLevel,
|
|
51
|
+
asChild = false,
|
|
52
|
+
style,
|
|
53
|
+
children,
|
|
54
|
+
}: BloomSeedScopeProps) {
|
|
55
|
+
// Hooks run unconditionally; conditional no-op/throw is applied afterwards.
|
|
56
|
+
const parent = useContext(BloomThemeContext);
|
|
57
|
+
const resolvedMode = parent?.theme.mode ?? 'light';
|
|
58
|
+
|
|
59
|
+
const nativeVars = useMemo<Record<string, string> | null>(
|
|
60
|
+
() => (seed ? buildSeedScopeVars({ seed, mode: resolvedMode, variant, contrastLevel }) : null),
|
|
61
|
+
[seed, resolvedMode, variant, contrastLevel],
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
const contextValue = useMemo<BloomThemeContextValue | null>(() => {
|
|
65
|
+
if (!parent || !seed) return null;
|
|
66
|
+
const theme = buildThemeFromSeed(seed, resolvedMode, variant, contrastLevel);
|
|
67
|
+
return { ...parent, theme };
|
|
68
|
+
}, [parent, seed, resolvedMode, variant, contrastLevel]);
|
|
69
|
+
|
|
70
|
+
if (!parent) {
|
|
71
|
+
throw new Error('BloomSeedScope must be used within a <BloomThemeProvider>');
|
|
72
|
+
}
|
|
73
|
+
// `seed` undefined => no-op; children inherit the parent scope.
|
|
74
|
+
if (!nativeVars || !contextValue) return <>{children}</>;
|
|
75
|
+
|
|
76
|
+
const VariableProvider = getVariableContextProvider();
|
|
77
|
+
|
|
78
|
+
let content: React.ReactNode;
|
|
79
|
+
if (asChild) {
|
|
80
|
+
const child = Children.only(children);
|
|
81
|
+
if (!isValidElement<StyleableProps>(child)) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
'BloomSeedScope with `asChild` requires a single React element child that accepts a `style` prop.',
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
const childStyle = child.props.style;
|
|
87
|
+
const mergedStyle: StyleProp<ViewStyle> = [style, childStyle];
|
|
88
|
+
content = cloneElement(child, { style: mergedStyle });
|
|
89
|
+
} else {
|
|
90
|
+
content = <View style={[{ flex: 1 }, style]}>{children}</View>;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const scoped = VariableProvider ? (
|
|
94
|
+
<VariableProvider value={nativeVars}>{content}</VariableProvider>
|
|
95
|
+
) : (
|
|
96
|
+
content
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
return <BloomThemeContext.Provider value={contextValue}>{scoped}</BloomThemeContext.Provider>;
|
|
100
|
+
}
|
|
@@ -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
|
+
}
|
package/src/theme/types.ts
CHANGED
|
@@ -19,8 +19,12 @@ export interface ThemeColors {
|
|
|
19
19
|
/** Legacy alias: the page background (`--background`), NOT the focus ring. */
|
|
20
20
|
primaryDark: string;
|
|
21
21
|
|
|
22
|
-
/**
|
|
22
|
+
/** The M3 secondary accent — a real contrast colour (no longer a mirror of primary). */
|
|
23
23
|
secondary: string;
|
|
24
|
+
secondaryForeground: string;
|
|
25
|
+
/** The M3 tertiary accent, completing the primary/secondary/tertiary trio. */
|
|
26
|
+
tertiary: string;
|
|
27
|
+
tertiaryForeground: string;
|
|
24
28
|
|
|
25
29
|
tint: string;
|
|
26
30
|
icon: string;
|