@oxyhq/bloom 0.7.0 → 0.7.2
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/react-native-css.d.js +2 -0
- package/lib/commonjs/react-native-css.d.js.map +1 -0
- package/lib/commonjs/theme/BloomThemeProvider.js +39 -7
- package/lib/commonjs/theme/BloomThemeProvider.js.map +1 -1
- package/lib/commonjs/theme/apply-dark-class.js +6 -3
- package/lib/commonjs/theme/apply-dark-class.js.map +1 -1
- package/lib/commonjs/theme/color-presets.js +118 -118
- package/lib/commonjs/theme/color-presets.js.map +1 -1
- package/lib/commonjs/theme/color-scope/index.js +28 -6
- package/lib/commonjs/theme/color-scope/index.js.map +1 -1
- package/lib/commonjs/theme/color-scope/style-builder.js +35 -0
- package/lib/commonjs/theme/color-scope/style-builder.js.map +1 -1
- package/lib/commonjs/theme/native-root-vars.js +110 -0
- package/lib/commonjs/theme/native-root-vars.js.map +1 -0
- package/lib/commonjs/theme/native-root-vars.web.js +27 -0
- package/lib/commonjs/theme/native-root-vars.web.js.map +1 -0
- package/lib/commonjs/theme/preset-vars.js +71 -2
- package/lib/commonjs/theme/preset-vars.js.map +1 -1
- package/lib/module/react-native-css.d.js +2 -0
- package/lib/module/react-native-css.d.js.map +1 -0
- package/lib/module/theme/BloomThemeProvider.js +40 -8
- package/lib/module/theme/BloomThemeProvider.js.map +1 -1
- package/lib/module/theme/apply-dark-class.js +6 -3
- package/lib/module/theme/apply-dark-class.js.map +1 -1
- package/lib/module/theme/color-presets.js +118 -118
- package/lib/module/theme/color-presets.js.map +1 -1
- package/lib/module/theme/color-scope/index.js +29 -7
- package/lib/module/theme/color-scope/index.js.map +1 -1
- package/lib/module/theme/color-scope/style-builder.js +35 -0
- package/lib/module/theme/color-scope/style-builder.js.map +1 -1
- package/lib/module/theme/native-root-vars.js +106 -0
- package/lib/module/theme/native-root-vars.js.map +1 -0
- package/lib/module/theme/native-root-vars.web.js +23 -0
- package/lib/module/theme/native-root-vars.web.js.map +1 -0
- package/lib/module/theme/preset-vars.js +70 -2
- package/lib/module/theme/preset-vars.js.map +1 -1
- package/lib/typescript/commonjs/icons/common.d.ts +8 -8
- package/lib/typescript/commonjs/theme/BloomThemeProvider.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/apply-dark-class.d.ts +6 -3
- package/lib/typescript/commonjs/theme/apply-dark-class.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/color-scope/index.d.ts +9 -2
- package/lib/typescript/commonjs/theme/color-scope/index.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/color-scope/style-builder.d.ts +32 -0
- package/lib/typescript/commonjs/theme/color-scope/style-builder.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/native-root-vars.d.ts +94 -0
- package/lib/typescript/commonjs/theme/native-root-vars.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/native-root-vars.web.d.ts +20 -0
- package/lib/typescript/commonjs/theme/native-root-vars.web.d.ts.map +1 -0
- package/lib/typescript/commonjs/theme/preset-vars.d.ts +35 -2
- package/lib/typescript/commonjs/theme/preset-vars.d.ts.map +1 -1
- package/lib/typescript/module/icons/common.d.ts +8 -8
- package/lib/typescript/module/theme/BloomThemeProvider.d.ts.map +1 -1
- package/lib/typescript/module/theme/apply-dark-class.d.ts +6 -3
- package/lib/typescript/module/theme/apply-dark-class.d.ts.map +1 -1
- package/lib/typescript/module/theme/color-scope/index.d.ts +9 -2
- package/lib/typescript/module/theme/color-scope/index.d.ts.map +1 -1
- package/lib/typescript/module/theme/color-scope/style-builder.d.ts +32 -0
- package/lib/typescript/module/theme/color-scope/style-builder.d.ts.map +1 -1
- package/lib/typescript/module/theme/native-root-vars.d.ts +94 -0
- package/lib/typescript/module/theme/native-root-vars.d.ts.map +1 -0
- package/lib/typescript/module/theme/native-root-vars.web.d.ts +20 -0
- package/lib/typescript/module/theme/native-root-vars.web.d.ts.map +1 -0
- package/lib/typescript/module/theme/preset-vars.d.ts +35 -2
- package/lib/typescript/module/theme/preset-vars.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/__tests__/native-root-vars.test.ts +127 -0
- package/src/__tests__/preset-vars.test.ts +45 -4
- package/src/react-native-css.d.ts +70 -0
- package/src/theme/BloomThemeProvider.tsx +32 -6
- package/src/theme/apply-dark-class.ts +6 -3
- package/src/theme/color-presets.ts +118 -118
- package/src/theme/color-scope/index.tsx +30 -8
- package/src/theme/color-scope/style-builder.ts +40 -0
- package/src/theme/native-root-vars.ts +107 -0
- package/src/theme/native-root-vars.web.ts +22 -0
- package/src/theme/preset-vars.ts +88 -4
|
@@ -4,7 +4,7 @@ import { View, type StyleProp, type ViewStyle } from 'react-native';
|
|
|
4
4
|
import { BloomThemeContext, type BloomThemeContextValue } from '../BloomThemeProvider';
|
|
5
5
|
import { buildTheme } from '../build-theme';
|
|
6
6
|
import type { AppColorName } from '../color-presets';
|
|
7
|
-
import { buildNativePresetStyle } from './style-builder';
|
|
7
|
+
import { buildNativePresetStyle, buildScopeVars, getVariableContextProvider } from './style-builder';
|
|
8
8
|
|
|
9
9
|
export interface BloomColorScopeProps {
|
|
10
10
|
/**
|
|
@@ -46,10 +46,15 @@ export function BloomColorScope({
|
|
|
46
46
|
return { ...parent, theme, colorPreset };
|
|
47
47
|
}, [colorPreset, resolvedMode, parent]);
|
|
48
48
|
|
|
49
|
-
|
|
50
|
-
|
|
49
|
+
// Preset vars flow to the subtree through react-native-css's real
|
|
50
|
+
// VariableContext (`VariableContextProvider`), NOT via an inline `vars()`
|
|
51
|
+
// style — under react-native-css@3 inline vars applied to a plain `<View>`
|
|
52
|
+
// (no matched className rules) are dropped silently. See `style-builder.ts`.
|
|
53
|
+
const nativeVars = useMemo(
|
|
54
|
+
() => buildScopeVars(colorPreset, resolvedMode),
|
|
51
55
|
[colorPreset, resolvedMode],
|
|
52
56
|
);
|
|
57
|
+
const VariableProvider = getVariableContextProvider();
|
|
53
58
|
|
|
54
59
|
let content: React.ReactNode;
|
|
55
60
|
if (asChild) {
|
|
@@ -59,20 +64,37 @@ export function BloomColorScope({
|
|
|
59
64
|
'BloomColorScope with `asChild` requires a single React element child that accepts a `style` prop.',
|
|
60
65
|
);
|
|
61
66
|
}
|
|
67
|
+
// The vars no longer ride on the child's `style`; merge only the caller's
|
|
68
|
+
// `style` and the child's own `style` (child last so its styles win).
|
|
62
69
|
const childStyle = child.props.style;
|
|
63
|
-
const mergedStyle: StyleProp<ViewStyle> = [
|
|
70
|
+
const mergedStyle: StyleProp<ViewStyle> = [style, childStyle];
|
|
64
71
|
content = cloneElement(child, { style: mergedStyle });
|
|
65
72
|
} else {
|
|
66
|
-
content = <View style={[{ flex: 1 },
|
|
73
|
+
content = <View style={[{ flex: 1 }, style]}>{children}</View>;
|
|
67
74
|
}
|
|
68
75
|
|
|
69
|
-
|
|
76
|
+
// Wrap the subtree in the variable provider when NativeWind is available;
|
|
77
|
+
// otherwise render as-is so we never crash on web/non-NativeWind hosts.
|
|
78
|
+
const scoped = VariableProvider ? (
|
|
79
|
+
<VariableProvider value={nativeVars}>{content}</VariableProvider>
|
|
80
|
+
) : (
|
|
81
|
+
content
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
return <BloomThemeContext.Provider value={contextValue}>{scoped}</BloomThemeContext.Provider>;
|
|
70
85
|
}
|
|
71
86
|
|
|
72
87
|
/**
|
|
73
88
|
* Escape hatch for advanced cases where the wrapping element is owned by the
|
|
74
|
-
* caller. Returns a stable native style object carrying the preset's CSS vars
|
|
75
|
-
* Returns `undefined` on web or when `nativewind`
|
|
89
|
+
* caller. Returns a stable native style object carrying the preset's CSS vars
|
|
90
|
+
* via NativeWind's `vars()`. Returns `undefined` on web or when `nativewind`
|
|
91
|
+
* is not installed.
|
|
92
|
+
*
|
|
93
|
+
* NOTE: subject to the react-native-css@3 limitation — the returned style only
|
|
94
|
+
* propagates vars to descendants when applied to an interop className component
|
|
95
|
+
* (the runtime gates propagation on matched className style rules). It is a
|
|
96
|
+
* no-op on a plain `<View>` with no `className`. For the common case prefer
|
|
97
|
+
* `<BloomColorScope>`, which wraps children in `VariableContextProvider`.
|
|
76
98
|
*/
|
|
77
99
|
export function useColorScopeStyle(colorPreset: AppColorName): StyleProp<ViewStyle> {
|
|
78
100
|
const parent = useContext(BloomThemeContext);
|
|
@@ -1,11 +1,24 @@
|
|
|
1
|
+
import type React from 'react';
|
|
1
2
|
import { Platform, type StyleProp, type ViewStyle } from 'react-native';
|
|
2
3
|
|
|
3
4
|
import { getPresetVars } from '../preset-vars';
|
|
4
5
|
import type { AppColorName } from '../color-presets';
|
|
5
6
|
import { lazyRequire } from '../../utils/lazy-require';
|
|
6
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Component that provides inline CSS variables to a subtree via NativeWind's
|
|
10
|
+
* (react-native-css's) real `VariableContext`. The runtime strips the leading
|
|
11
|
+
* `--` from each key internally, so we feed it the full `--name -> value`
|
|
12
|
+
* record produced by `buildScopeVars`.
|
|
13
|
+
*/
|
|
14
|
+
export type VariableContextProviderComponent = React.ComponentType<{
|
|
15
|
+
value: Record<string, string>;
|
|
16
|
+
children: React.ReactNode;
|
|
17
|
+
}>;
|
|
18
|
+
|
|
7
19
|
interface NativeWindVarsModule {
|
|
8
20
|
vars: (record: Record<string, string>) => StyleProp<ViewStyle>;
|
|
21
|
+
VariableContextProvider?: VariableContextProviderComponent;
|
|
9
22
|
}
|
|
10
23
|
|
|
11
24
|
const getNativeWindVars = lazyRequire<NativeWindVarsModule>('nativewind');
|
|
@@ -22,11 +35,38 @@ export function buildScopeVars(
|
|
|
22
35
|
return getPresetVars(colorPreset, mode, { includeResolvedColorVars: true });
|
|
23
36
|
}
|
|
24
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Resolve NativeWind's `VariableContextProvider` — the canonical, non-deprecated
|
|
40
|
+
* react-native-css v3 API for providing inline CSS variables to a subtree. It
|
|
41
|
+
* reads the same `VariableContext` that interop className components consume, so
|
|
42
|
+
* descendants resolve `var(--primary)` etc. correctly. Returns `null` on web or
|
|
43
|
+
* when `nativewind` is not installed.
|
|
44
|
+
*
|
|
45
|
+
* This replaces the deprecated `vars()`-on-a-plain-`<View>`-style mechanism:
|
|
46
|
+
* under react-native-css@3, `vars()` returns an inline-vars marker that is only
|
|
47
|
+
* propagated to children when the host element matches className style rules
|
|
48
|
+
* (`usesVariables`/`variables`). A plain `<View>` with no `className` drops the
|
|
49
|
+
* vars silently, so the subtree renders without the preset palette.
|
|
50
|
+
*/
|
|
51
|
+
export function getVariableContextProvider(): VariableContextProviderComponent | null {
|
|
52
|
+
if (Platform.OS === 'web') return null;
|
|
53
|
+
const module = getNativeWindVars();
|
|
54
|
+
if (!module || typeof module.VariableContextProvider !== 'function') return null;
|
|
55
|
+
return module.VariableContextProvider;
|
|
56
|
+
}
|
|
57
|
+
|
|
25
58
|
/**
|
|
26
59
|
* Build a native style object carrying every CSS var of the preset, using
|
|
27
60
|
* NativeWind's `vars()` when available. Returns `undefined` on web (where the
|
|
28
61
|
* provider writes vars to `documentElement` instead) or when `nativewind` is
|
|
29
62
|
* not installed.
|
|
63
|
+
*
|
|
64
|
+
* @deprecated Under react-native-css@3, `vars()` only propagates to a subtree
|
|
65
|
+
* when applied to an interop className component (the runtime gates propagation
|
|
66
|
+
* on matched className style rules). Applied to a plain `<View>` the vars are
|
|
67
|
+
* dropped silently. Prefer wrapping children in `getVariableContextProvider()`.
|
|
68
|
+
* Retained only as the escape-hatch contract of `useColorScopeStyle`, where the
|
|
69
|
+
* caller owns the host element and is expected to be a className component.
|
|
30
70
|
*/
|
|
31
71
|
export function buildNativePresetStyle(
|
|
32
72
|
colorPreset: AppColorName,
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { rootVariables } from 'react-native-css/native-internal';
|
|
2
|
+
|
|
3
|
+
import { buildScopeVars } from './color-scope/style-builder';
|
|
4
|
+
import { type AppColorName } from './color-presets';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Publish the active color preset's CSS custom properties into react-native-css's
|
|
8
|
+
* GLOBAL root-variable family so they reach the ENTIRE native app tree — including
|
|
9
|
+
* content rendered outside the React subtree of `BloomThemeProvider`: expo-router
|
|
10
|
+
* navigator screens, `Portal`s, `Dialog`/`BottomSheet` overlays, and any other host
|
|
11
|
+
* that paints through a separate React root.
|
|
12
|
+
*
|
|
13
|
+
* Why a global write is required (native only)
|
|
14
|
+
* --------------------------------------------
|
|
15
|
+
* react-native-css resolves a `var(--x)` reference (its `varResolver`) in this order:
|
|
16
|
+
* 1. `name in inheritedVariables` — the React-context-inherited vars. This is what
|
|
17
|
+
* `VariableContextProvider` populates, but it is React-context-scoped, so it only
|
|
18
|
+
* reaches *direct descendants* of the provider in the React tree.
|
|
19
|
+
* 2. `inlineVariables[name]` — same-component inline vars.
|
|
20
|
+
* 3. `variables[name]` — resolved inherited vars.
|
|
21
|
+
* 4. `universalVariables(name)` — a GLOBAL observable family.
|
|
22
|
+
* 5. `rootVariables(name)` — a GLOBAL observable family.
|
|
23
|
+
* 6. the declared fallback.
|
|
24
|
+
*
|
|
25
|
+
* `BloomThemeProvider`'s `VariableContextProvider` only feeds path #1, so the navigator
|
|
26
|
+
* host and every portal/modal — which render outside that subtree — never see the
|
|
27
|
+
* preset vars and fall through to `rootVariables`/`universalVariables`. By default those
|
|
28
|
+
* families hold only react-native-css's own `__rn-css-rem`/`__rn-css-color` entries
|
|
29
|
+
* (Bloom's `global.css` defines the `hsl(var(--primary))` indirection but not the HSL
|
|
30
|
+
* triples themselves — those are injected at runtime), so the whole tree below the
|
|
31
|
+
* navigator renders monochrome. Writing the vars into `rootVariables` here is the
|
|
32
|
+
* exact same mechanism react-native-css uses for compiled `:root {}` / `@theme` vars
|
|
33
|
+
* (`StyleCollection.inject` does `rootVariables(name).set(valueArray)`), so portaled
|
|
34
|
+
* content resolves the palette identically regardless of React-tree position.
|
|
35
|
+
*
|
|
36
|
+
* The raw tokens are written as HSL triples (`primary: '205 87% 53%'`) and the
|
|
37
|
+
* resolved Tailwind v4 `--color-*` vars as sRGB `rgb(...)` (`color-primary:
|
|
38
|
+
* 'rgb(31 153 239)'`) — the latter so `color-mix`-based alpha utilities resolve
|
|
39
|
+
* on native (see `getPresetVars` / `hslTripletToRgb`).
|
|
40
|
+
*
|
|
41
|
+
* Why a STATIC `import`, not a runtime `require` (critical)
|
|
42
|
+
* ---------------------------------------------------------
|
|
43
|
+
* react-native-css@3.0.7 ships both a `dist/commonjs` and a `dist/module` build, and
|
|
44
|
+
* Metro bundles BOTH. In `native-internal/root.js` the variable families are plain
|
|
45
|
+
* module-local consts — NOT `globalThis`-guarded the way `StyleCollection` is — so the
|
|
46
|
+
* commonjs `root.js` and the module `root.js` each instantiate their OWN separate
|
|
47
|
+
* `rootVariables` family. The react-native-css renderer (`react-native-css/src/components/View.tsx`
|
|
48
|
+
* → `varResolver`) imports its `root.js` via ESM `import`, i.e. the MODULE build. A
|
|
49
|
+
* runtime `require('react-native-css/native-internal')` resolves the package's `require`
|
|
50
|
+
* export condition → the COMMONJS build → a DIFFERENT family the renderer never
|
|
51
|
+
* subscribes to, so the writes are a silent no-op on device. A static top-level
|
|
52
|
+
* `import` resolves the `import` condition → the MODULE build → the renderer's instance.
|
|
53
|
+
*
|
|
54
|
+
* This plain file is the NATIVE/default variant (Metro selects it on iOS/Android,
|
|
55
|
+
* which have no `.native` override here); the web variant lives in the sibling
|
|
56
|
+
* `native-root-vars.web.ts` no-op, which web bundlers pick over this one. So the
|
|
57
|
+
* static `react-native-css/native-internal` import below is never pulled into a web
|
|
58
|
+
* bundle — only into native. This mirrors Bloom's existing platform-split convention
|
|
59
|
+
* (`color-scope/index.tsx` + `color-scope/index.web.tsx`, `FontLoader.native.tsx`).
|
|
60
|
+
*
|
|
61
|
+
* Keying contract (verified against react-native-css@3.0.7)
|
|
62
|
+
* ---------------------------------------------------------
|
|
63
|
+
* The `rootVariables` family is keyed by the BARE variable name, WITHOUT the leading
|
|
64
|
+
* `--`. The compiler strips it uniformly: a `:root { --primary: ... }` declaration is
|
|
65
|
+
* stored via `rule.v.push([property.slice(2), value])` then routed into
|
|
66
|
+
* `shared.rootVariables[name]`, and `@property --x` goes through
|
|
67
|
+
* `name.startsWith("--") ? name.slice(2) : name`. The lookup side matches: a `var(--x)`
|
|
68
|
+
* reference compiles to `[{}, "var", ident.slice(2)]`, so `varResolver` looks up the
|
|
69
|
+
* bare name. `buildScopeVars` returns keys WITH the `--` prefix, so we strip it before
|
|
70
|
+
* writing each entry.
|
|
71
|
+
*
|
|
72
|
+
* Value shape
|
|
73
|
+
* -----------
|
|
74
|
+
* Each entry is a `VariableValue[]` — an array of `[value, mediaCondition?]` tuples.
|
|
75
|
+
* Bloom's preset vars are unconditional, so each is a single-element `[[value]]`, e.g.
|
|
76
|
+
* `rootVariables('primary').set([['205 87% 53%']])`. This is the same shape the compiler
|
|
77
|
+
* emits for unconditional `:root` vars.
|
|
78
|
+
*
|
|
79
|
+
* This is native-only and additive: the `VariableContextProvider` wrapper stays (it is
|
|
80
|
+
* the correct, scoped mechanism for `BloomColorScope` subtree overrides and harmlessly
|
|
81
|
+
* covers direct descendants). The web fork (`native-root-vars.web.ts`) no-ops — web
|
|
82
|
+
* writes the same vars to `document.documentElement` via `applyColorPresetVars`.
|
|
83
|
+
*/
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Write the preset's CSS vars to react-native-css's global `rootVariables` family
|
|
87
|
+
* so the whole native tree (navigator screens + portals/modals/bottom-sheets)
|
|
88
|
+
* resolves the active palette.
|
|
89
|
+
*
|
|
90
|
+
* Each `.set` notifies its own observers synchronously (react-native-css only
|
|
91
|
+
* batches notifications inside its own `StyleCollection.inject`; outside it,
|
|
92
|
+
* `observableBatch.current` is unset so every `.set` runs observers immediately),
|
|
93
|
+
* so sequential writes correctly trigger a re-render when the preset/mode changes.
|
|
94
|
+
*
|
|
95
|
+
* Defensive guard: if a host ships a react-native-css build that doesn't expose
|
|
96
|
+
* `rootVariables` as a callable (it always does on @3), we bail rather than throw.
|
|
97
|
+
*/
|
|
98
|
+
export function applyNativeRootVars(colorPreset: AppColorName, mode: 'light' | 'dark'): void {
|
|
99
|
+
if (typeof rootVariables !== 'function') return;
|
|
100
|
+
|
|
101
|
+
const vars = buildScopeVars(colorPreset, mode);
|
|
102
|
+
for (const [key, value] of Object.entries(vars)) {
|
|
103
|
+
// The family is keyed by the bare name; `buildScopeVars` keys include `--`.
|
|
104
|
+
const name = key.startsWith('--') ? key.slice(2) : key;
|
|
105
|
+
rootVariables(name).set([[value]]);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type AppColorName } from './color-presets';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Web fork of `applyNativeRootVars` — a no-op.
|
|
5
|
+
*
|
|
6
|
+
* On web, `BloomThemeProvider` already writes the active preset's CSS vars to
|
|
7
|
+
* `document.documentElement` via `applyColorPresetVars`, which the browser /
|
|
8
|
+
* react-native-web resolve natively. The native/default variant — the plain
|
|
9
|
+
* sibling `native-root-vars.ts` — instead publishes those vars into
|
|
10
|
+
* react-native-css's GLOBAL `rootVariables` family.
|
|
11
|
+
*
|
|
12
|
+
* Keeping the native variant's `react-native-css/native-internal` import out of
|
|
13
|
+
* the web bundle is the whole reason this is a platform-split module: web (and
|
|
14
|
+
* any non-NativeWind host) picks this `.web` fork over the plain file and so
|
|
15
|
+
* never statically pulls react-native-css internals.
|
|
16
|
+
*
|
|
17
|
+
* The signature is intentionally identical to the plain variant so the import
|
|
18
|
+
* site in `BloomThemeProvider` is platform-agnostic.
|
|
19
|
+
*/
|
|
20
|
+
export function applyNativeRootVars(_colorPreset: AppColorName, _mode: 'light' | 'dark'): void {
|
|
21
|
+
// Intentionally empty — web applies preset vars to `document.documentElement`.
|
|
22
|
+
}
|
package/src/theme/preset-vars.ts
CHANGED
|
@@ -9,6 +9,80 @@ function extractSat(hslVar: string): number {
|
|
|
9
9
|
return parseInt(hslVar.split(' ')[1] ?? '0', 10);
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
/**
|
|
13
|
+
* Convert a shadcn-style HSL triple (`'H S% L%'`, optionally with an alpha tail
|
|
14
|
+
* `'H S% L% / A'`) into an sRGB `rgb(...)` string.
|
|
15
|
+
*
|
|
16
|
+
* Why rgb and not hsl (the native alpha-utility bug this fixes)
|
|
17
|
+
* -------------------------------------------------------------
|
|
18
|
+
* Tailwind v4 compiles an alpha-on-theme-color utility (`bg-primary/10`) into a
|
|
19
|
+
* `color-mix(... var(--color-primary) 10%, transparent)` declaration. On native,
|
|
20
|
+
* react-native-css resolves that `color-mix` at runtime via `colorjs.io/fn`, but
|
|
21
|
+
* its `color-mix` implementation registers ONLY the `sRGB`, `P3`, and `OKLab`
|
|
22
|
+
* color spaces — never `HSL`. So if `--color-primary` resolves to an `hsl(...)`
|
|
23
|
+
* string, `parse('hsl(...)')` throws, the mix is swallowed, and the utility
|
|
24
|
+
* silently produces no color. Emitting the resolved var as `rgb(...)` (a space
|
|
25
|
+
* colorjs.io always has registered) makes the alpha utilities resolve on native,
|
|
26
|
+
* matching web. Full (non-alpha) utilities already worked because React Native's
|
|
27
|
+
* native color parser handles `hsl()` directly; only `color-mix` was affected.
|
|
28
|
+
*
|
|
29
|
+
* Output is space-separated channels (`rgb(31 153 239)`), the modern CSS syntax
|
|
30
|
+
* parsed by both `colorjs.io/fn` (with only sRGB/P3/OKLab registered) and
|
|
31
|
+
* react-native-web / browsers. An alpha tail emits `rgb(r g b / a)`, likewise
|
|
32
|
+
* parsed by both.
|
|
33
|
+
*
|
|
34
|
+
* Pure function — standard HSL→sRGB conversion, channels rounded to integers.
|
|
35
|
+
* Tolerates a `deg` suffix on the hue and `%` suffixes on saturation/lightness.
|
|
36
|
+
*/
|
|
37
|
+
export function hslTripletToRgb(triplet: string): string {
|
|
38
|
+
const [colorPart, alphaPart] = triplet.split('/').map((part) => part.trim());
|
|
39
|
+
const channels = (colorPart ?? '').split(/\s+/).filter(Boolean);
|
|
40
|
+
|
|
41
|
+
const hue = parseFloat((channels[0] ?? '0').replace(/deg$/i, ''));
|
|
42
|
+
const sat = parseFloat((channels[1] ?? '0').replace('%', '')) / 100;
|
|
43
|
+
const light = parseFloat((channels[2] ?? '0').replace('%', '')) / 100;
|
|
44
|
+
|
|
45
|
+
const h = Number.isFinite(hue) ? hue : 0;
|
|
46
|
+
const s = Number.isFinite(sat) ? sat : 0;
|
|
47
|
+
const l = Number.isFinite(light) ? light : 0;
|
|
48
|
+
|
|
49
|
+
const chroma = (1 - Math.abs(2 * l - 1)) * s;
|
|
50
|
+
const huePrime = ((((h % 360) + 360) % 360) / 60);
|
|
51
|
+
const second = chroma * (1 - Math.abs((huePrime % 2) - 1));
|
|
52
|
+
|
|
53
|
+
let r = 0;
|
|
54
|
+
let g = 0;
|
|
55
|
+
let b = 0;
|
|
56
|
+
if (huePrime < 1) {
|
|
57
|
+
[r, g, b] = [chroma, second, 0];
|
|
58
|
+
} else if (huePrime < 2) {
|
|
59
|
+
[r, g, b] = [second, chroma, 0];
|
|
60
|
+
} else if (huePrime < 3) {
|
|
61
|
+
[r, g, b] = [0, chroma, second];
|
|
62
|
+
} else if (huePrime < 4) {
|
|
63
|
+
[r, g, b] = [0, second, chroma];
|
|
64
|
+
} else if (huePrime < 5) {
|
|
65
|
+
[r, g, b] = [second, 0, chroma];
|
|
66
|
+
} else {
|
|
67
|
+
[r, g, b] = [chroma, 0, second];
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const match = l - chroma / 2;
|
|
71
|
+
const red = Math.round((r + match) * 255);
|
|
72
|
+
const green = Math.round((g + match) * 255);
|
|
73
|
+
const blue = Math.round((b + match) * 255);
|
|
74
|
+
|
|
75
|
+
if (alphaPart !== undefined && alphaPart !== '') {
|
|
76
|
+
const alpha = alphaPart.endsWith('%')
|
|
77
|
+
? parseFloat(alphaPart) / 100
|
|
78
|
+
: parseFloat(alphaPart);
|
|
79
|
+
const safeAlpha = Number.isFinite(alpha) ? alpha : 1;
|
|
80
|
+
return `rgb(${red} ${green} ${blue} / ${safeAlpha})`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return `rgb(${red} ${green} ${blue})`;
|
|
84
|
+
}
|
|
85
|
+
|
|
12
86
|
const RESOLVED_COLOR_MAP: Record<string, string> = {
|
|
13
87
|
'--background': '--color-background',
|
|
14
88
|
'--foreground': '--color-foreground',
|
|
@@ -34,11 +108,18 @@ const RESOLVED_COLOR_MAP: Record<string, string> = {
|
|
|
34
108
|
|
|
35
109
|
export interface PresetVarsOptions {
|
|
36
110
|
/**
|
|
37
|
-
* Also emit Tailwind v4 resolved `--color-*` vars (
|
|
111
|
+
* Also emit Tailwind v4 resolved `--color-*` vars (as sRGB `rgb(...)` strings)
|
|
38
112
|
* alongside the raw HSL triples. Needed when scoping a subtree where
|
|
39
113
|
* Tailwind's `@theme` block has already precomputed `--color-*` at `:root`,
|
|
40
114
|
* so overriding `--background` alone wouldn't cascade to `bg-background`.
|
|
41
|
-
*
|
|
115
|
+
*
|
|
116
|
+
* The resolved vars are emitted as `rgb(...)` rather than `hsl(...)` so the
|
|
117
|
+
* alpha-on-theme-color utilities (`bg-primary/10`, `text-foreground/50`)
|
|
118
|
+
* resolve on native: Tailwind compiles those to a runtime `color-mix(...)` that
|
|
119
|
+
* react-native-css evaluates via `colorjs.io/fn`, which only registers the
|
|
120
|
+
* sRGB/P3/OKLab spaces (not HSL) — so an `hsl(...)` var would throw and the
|
|
121
|
+
* utility would silently render no color. See `hslTripletToRgb`. Default
|
|
122
|
+
* `false`.
|
|
42
123
|
*/
|
|
43
124
|
includeResolvedColorVars?: boolean;
|
|
44
125
|
}
|
|
@@ -67,7 +148,10 @@ export function getPresetVars(
|
|
|
67
148
|
|
|
68
149
|
const extended: PresetTokens = {
|
|
69
150
|
...base,
|
|
70
|
-
|
|
151
|
+
// Light cards carry the same faint preset tint as `--popover` (H 12% 99%)
|
|
152
|
+
// so they read as part of the preset family rather than a pure-white slab —
|
|
153
|
+
// mirroring how dark cards (H 30% 10%) carry the accent hue into near-black.
|
|
154
|
+
'--card': isDark ? `${hue} 30% 10%` : `${hue} 12% 99%`,
|
|
71
155
|
'--card-foreground': foreground,
|
|
72
156
|
'--chart-1': `${hue} ${sat}% 85%`,
|
|
73
157
|
'--chart-2': `${hue} ${sat}% 75%`,
|
|
@@ -89,7 +173,7 @@ export function getPresetVars(
|
|
|
89
173
|
const resolved: PresetTokens = { ...extended };
|
|
90
174
|
for (const [rawKey, colorKey] of Object.entries(RESOLVED_COLOR_MAP)) {
|
|
91
175
|
const value = extended[rawKey];
|
|
92
|
-
if (value) resolved[colorKey] =
|
|
176
|
+
if (value) resolved[colorKey] = hslTripletToRgb(value);
|
|
93
177
|
}
|
|
94
178
|
return resolved;
|
|
95
179
|
}
|