@oxyhq/bloom 0.7.5 → 0.7.6
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/theme/native-root-vars.js +32 -117
- package/lib/commonjs/theme/native-root-vars.js.map +1 -1
- package/lib/commonjs/theme/native-root-vars.native.js +127 -0
- package/lib/commonjs/theme/native-root-vars.native.js.map +1 -0
- package/lib/module/theme/native-root-vars.js +32 -116
- package/lib/module/theme/native-root-vars.js.map +1 -1
- package/lib/module/theme/native-root-vars.native.js +122 -0
- package/lib/module/theme/native-root-vars.native.js.map +1 -0
- package/lib/typescript/commonjs/theme/native-root-vars.d.ts +30 -90
- package/lib/typescript/commonjs/theme/native-root-vars.d.ts.map +1 -1
- package/lib/typescript/commonjs/theme/native-root-vars.native.d.ts +94 -0
- package/lib/typescript/commonjs/theme/native-root-vars.native.d.ts.map +1 -0
- package/lib/typescript/module/theme/native-root-vars.d.ts +30 -90
- package/lib/typescript/module/theme/native-root-vars.d.ts.map +1 -1
- package/lib/typescript/module/theme/native-root-vars.native.d.ts +94 -0
- package/lib/typescript/module/theme/native-root-vars.native.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/native-root-vars.test.ts +30 -4
- package/src/theme/native-root-vars.native.ts +123 -0
- package/src/theme/native-root-vars.ts +32 -118
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/// <reference path="../react-native-css.d.ts" />
|
|
2
|
+
// The triple-slash reference above pins Bloom's ambient declaration for
|
|
3
|
+
// `react-native-css/native-internal` (`src/react-native-css.d.ts`) into the
|
|
4
|
+
// compilation graph of EVERY program that compiles THIS file — including
|
|
5
|
+
// downstream consumers that resolve Bloom's `"react-native"` source export
|
|
6
|
+
// condition (`./src/theme/index.ts` → `BloomThemeProvider` → this file). Without
|
|
7
|
+
// it the ambient decl is only auto-loaded inside Bloom's own tsconfig
|
|
8
|
+
// (`include: ["src"]`); a consumer's tsconfig never includes
|
|
9
|
+
// `node_modules/@oxyhq/bloom/src/**`, and TypeScript does NOT auto-pick-up
|
|
10
|
+
// ambient `.d.ts` files that live under `node_modules`, so
|
|
11
|
+
// `react-native-css/native-internal` is unresolved (TS2307) unless the reference
|
|
12
|
+
// travels with the source. react-native-css is intentionally NOT a Bloom
|
|
13
|
+
// dependency — it arrives transitively via a host's NativeWind 5 install, or is
|
|
14
|
+
// absent entirely for NativeWind 4 consumers — so the ambient declaration is the
|
|
15
|
+
// only thing that can type this import, and it MUST reach the consumer. The
|
|
16
|
+
// directive ships raw in `src/` (Bloom publishes `files: ["src", "lib"]`).
|
|
17
|
+
import { rootVariables } from 'react-native-css/native-internal';
|
|
18
|
+
|
|
19
|
+
import { buildScopeVars } from './color-scope/style-builder';
|
|
20
|
+
import { type AppColorName } from './color-presets';
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Publish the active color preset's CSS custom properties into react-native-css's
|
|
24
|
+
* GLOBAL root-variable family so they reach the ENTIRE native app tree — including
|
|
25
|
+
* content rendered outside the React subtree of `BloomThemeProvider`: expo-router
|
|
26
|
+
* navigator screens, `Portal`s, `Dialog`/`BottomSheet` overlays, and any other host
|
|
27
|
+
* that paints through a separate React root.
|
|
28
|
+
*
|
|
29
|
+
* Why a global write is required (native only)
|
|
30
|
+
* --------------------------------------------
|
|
31
|
+
* react-native-css resolves a `var(--x)` reference (its `varResolver`) in this order:
|
|
32
|
+
* 1. `name in inheritedVariables` — the React-context-inherited vars. This is what
|
|
33
|
+
* `VariableContextProvider` populates, but it is React-context-scoped, so it only
|
|
34
|
+
* reaches *direct descendants* of the provider in the React tree.
|
|
35
|
+
* 2. `inlineVariables[name]` — same-component inline vars.
|
|
36
|
+
* 3. `variables[name]` — resolved inherited vars.
|
|
37
|
+
* 4. `universalVariables(name)` — a GLOBAL observable family.
|
|
38
|
+
* 5. `rootVariables(name)` — a GLOBAL observable family.
|
|
39
|
+
* 6. the declared fallback.
|
|
40
|
+
*
|
|
41
|
+
* `BloomThemeProvider`'s `VariableContextProvider` only feeds path #1, so the navigator
|
|
42
|
+
* host and every portal/modal — which render outside that subtree — never see the
|
|
43
|
+
* preset vars and fall through to `rootVariables`/`universalVariables`. By default those
|
|
44
|
+
* families hold only react-native-css's own `__rn-css-rem`/`__rn-css-color` entries
|
|
45
|
+
* (Bloom's `global.css` defines the `hsl(var(--primary))` indirection but not the HSL
|
|
46
|
+
* triples themselves — those are injected at runtime), so the whole tree below the
|
|
47
|
+
* navigator renders monochrome. Writing the vars into `rootVariables` here is the
|
|
48
|
+
* exact same mechanism react-native-css uses for compiled `:root {}` / `@theme` vars
|
|
49
|
+
* (`StyleCollection.inject` does `rootVariables(name).set(valueArray)`), so portaled
|
|
50
|
+
* content resolves the palette identically regardless of React-tree position.
|
|
51
|
+
*
|
|
52
|
+
* The raw tokens are written as HSL triples (`primary: '205 87% 53%'`) and the
|
|
53
|
+
* resolved Tailwind v4 `--color-*` vars as sRGB `rgb(...)` (`color-primary:
|
|
54
|
+
* 'rgb(31 153 239)'`) — the latter so `color-mix`-based alpha utilities resolve
|
|
55
|
+
* on native (see `getPresetVars` / `hslTripletToRgb`).
|
|
56
|
+
*
|
|
57
|
+
* Why a STATIC `import`, not a runtime `require` (critical)
|
|
58
|
+
* ---------------------------------------------------------
|
|
59
|
+
* react-native-css@3.0.7 ships both a `dist/commonjs` and a `dist/module` build, and
|
|
60
|
+
* Metro bundles BOTH. In `native-internal/root.js` the variable families are plain
|
|
61
|
+
* module-local consts — NOT `globalThis`-guarded the way `StyleCollection` is — so the
|
|
62
|
+
* commonjs `root.js` and the module `root.js` each instantiate their OWN separate
|
|
63
|
+
* `rootVariables` family. The react-native-css renderer (`react-native-css/src/components/View.tsx`
|
|
64
|
+
* → `varResolver`) imports its `root.js` via ESM `import`, i.e. the MODULE build. A
|
|
65
|
+
* runtime `require('react-native-css/native-internal')` resolves the package's `require`
|
|
66
|
+
* export condition → the COMMONJS build → a DIFFERENT family the renderer never
|
|
67
|
+
* subscribes to, so the writes are a silent no-op on device. A static top-level
|
|
68
|
+
* `import` resolves the `import` condition → the MODULE build → the renderer's instance.
|
|
69
|
+
*
|
|
70
|
+
* This plain file is the NATIVE/default variant (Metro selects it on iOS/Android,
|
|
71
|
+
* which have no `.native` override here); the web variant lives in the sibling
|
|
72
|
+
* `native-root-vars.web.ts` no-op, which web bundlers pick over this one. So the
|
|
73
|
+
* static `react-native-css/native-internal` import below is never pulled into a web
|
|
74
|
+
* bundle — only into native. This mirrors Bloom's existing platform-split convention
|
|
75
|
+
* (`color-scope/index.tsx` + `color-scope/index.web.tsx`, `FontLoader.native.tsx`).
|
|
76
|
+
*
|
|
77
|
+
* Keying contract (verified against react-native-css@3.0.7)
|
|
78
|
+
* ---------------------------------------------------------
|
|
79
|
+
* The `rootVariables` family is keyed by the BARE variable name, WITHOUT the leading
|
|
80
|
+
* `--`. The compiler strips it uniformly: a `:root { --primary: ... }` declaration is
|
|
81
|
+
* stored via `rule.v.push([property.slice(2), value])` then routed into
|
|
82
|
+
* `shared.rootVariables[name]`, and `@property --x` goes through
|
|
83
|
+
* `name.startsWith("--") ? name.slice(2) : name`. The lookup side matches: a `var(--x)`
|
|
84
|
+
* reference compiles to `[{}, "var", ident.slice(2)]`, so `varResolver` looks up the
|
|
85
|
+
* bare name. `buildScopeVars` returns keys WITH the `--` prefix, so we strip it before
|
|
86
|
+
* writing each entry.
|
|
87
|
+
*
|
|
88
|
+
* Value shape
|
|
89
|
+
* -----------
|
|
90
|
+
* Each entry is a `VariableValue[]` — an array of `[value, mediaCondition?]` tuples.
|
|
91
|
+
* Bloom's preset vars are unconditional, so each is a single-element `[[value]]`, e.g.
|
|
92
|
+
* `rootVariables('primary').set([['205 87% 53%']])`. This is the same shape the compiler
|
|
93
|
+
* emits for unconditional `:root` vars.
|
|
94
|
+
*
|
|
95
|
+
* This is native-only and additive: the `VariableContextProvider` wrapper stays (it is
|
|
96
|
+
* the correct, scoped mechanism for `BloomColorScope` subtree overrides and harmlessly
|
|
97
|
+
* covers direct descendants). The web fork (`native-root-vars.web.ts`) no-ops — web
|
|
98
|
+
* writes the same vars to `document.documentElement` via `applyColorPresetVars`.
|
|
99
|
+
*/
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Write the preset's CSS vars to react-native-css's global `rootVariables` family
|
|
103
|
+
* so the whole native tree (navigator screens + portals/modals/bottom-sheets)
|
|
104
|
+
* resolves the active palette.
|
|
105
|
+
*
|
|
106
|
+
* Each `.set` notifies its own observers synchronously (react-native-css only
|
|
107
|
+
* batches notifications inside its own `StyleCollection.inject`; outside it,
|
|
108
|
+
* `observableBatch.current` is unset so every `.set` runs observers immediately),
|
|
109
|
+
* so sequential writes correctly trigger a re-render when the preset/mode changes.
|
|
110
|
+
*
|
|
111
|
+
* Defensive guard: if a host ships a react-native-css build that doesn't expose
|
|
112
|
+
* `rootVariables` as a callable (it always does on @3), we bail rather than throw.
|
|
113
|
+
*/
|
|
114
|
+
export function applyNativeRootVars(colorPreset: AppColorName, mode: 'light' | 'dark'): void {
|
|
115
|
+
if (typeof rootVariables !== 'function') return;
|
|
116
|
+
|
|
117
|
+
const vars = buildScopeVars(colorPreset, mode);
|
|
118
|
+
for (const [key, value] of Object.entries(vars)) {
|
|
119
|
+
// The family is keyed by the bare name; `buildScopeVars` keys include `--`.
|
|
120
|
+
const name = key.startsWith('--') ? key.slice(2) : key;
|
|
121
|
+
rootVariables(name).set([[value]]);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -1,123 +1,37 @@
|
|
|
1
|
-
/// <reference path="../react-native-css.d.ts" />
|
|
2
|
-
// The triple-slash reference above pins Bloom's ambient declaration for
|
|
3
|
-
// `react-native-css/native-internal` (`src/react-native-css.d.ts`) into the
|
|
4
|
-
// compilation graph of EVERY program that compiles THIS file — including
|
|
5
|
-
// downstream consumers that resolve Bloom's `"react-native"` source export
|
|
6
|
-
// condition (`./src/theme/index.ts` → `BloomThemeProvider` → this file). Without
|
|
7
|
-
// it the ambient decl is only auto-loaded inside Bloom's own tsconfig
|
|
8
|
-
// (`include: ["src"]`); a consumer's tsconfig never includes
|
|
9
|
-
// `node_modules/@oxyhq/bloom/src/**`, and TypeScript does NOT auto-pick-up
|
|
10
|
-
// ambient `.d.ts` files that live under `node_modules`, so
|
|
11
|
-
// `react-native-css/native-internal` is unresolved (TS2307) unless the reference
|
|
12
|
-
// travels with the source. react-native-css is intentionally NOT a Bloom
|
|
13
|
-
// dependency — it arrives transitively via a host's NativeWind 5 install, or is
|
|
14
|
-
// absent entirely for NativeWind 4 consumers — so the ambient declaration is the
|
|
15
|
-
// only thing that can type this import, and it MUST reach the consumer. The
|
|
16
|
-
// directive ships raw in `src/` (Bloom publishes `files: ["src", "lib"]`).
|
|
17
|
-
import { rootVariables } from 'react-native-css/native-internal';
|
|
18
|
-
|
|
19
|
-
import { buildScopeVars } from './color-scope/style-builder';
|
|
20
1
|
import { type AppColorName } from './color-presets';
|
|
21
2
|
|
|
22
3
|
/**
|
|
23
|
-
*
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
* The raw tokens are written as HSL triples (`primary: '205 87% 53%'`) and the
|
|
53
|
-
* resolved Tailwind v4 `--color-*` vars as sRGB `rgb(...)` (`color-primary:
|
|
54
|
-
* 'rgb(31 153 239)'`) — the latter so `color-mix`-based alpha utilities resolve
|
|
55
|
-
* on native (see `getPresetVars` / `hslTripletToRgb`).
|
|
56
|
-
*
|
|
57
|
-
* Why a STATIC `import`, not a runtime `require` (critical)
|
|
58
|
-
* ---------------------------------------------------------
|
|
59
|
-
* react-native-css@3.0.7 ships both a `dist/commonjs` and a `dist/module` build, and
|
|
60
|
-
* Metro bundles BOTH. In `native-internal/root.js` the variable families are plain
|
|
61
|
-
* module-local consts — NOT `globalThis`-guarded the way `StyleCollection` is — so the
|
|
62
|
-
* commonjs `root.js` and the module `root.js` each instantiate their OWN separate
|
|
63
|
-
* `rootVariables` family. The react-native-css renderer (`react-native-css/src/components/View.tsx`
|
|
64
|
-
* → `varResolver`) imports its `root.js` via ESM `import`, i.e. the MODULE build. A
|
|
65
|
-
* runtime `require('react-native-css/native-internal')` resolves the package's `require`
|
|
66
|
-
* export condition → the COMMONJS build → a DIFFERENT family the renderer never
|
|
67
|
-
* subscribes to, so the writes are a silent no-op on device. A static top-level
|
|
68
|
-
* `import` resolves the `import` condition → the MODULE build → the renderer's instance.
|
|
69
|
-
*
|
|
70
|
-
* This plain file is the NATIVE/default variant (Metro selects it on iOS/Android,
|
|
71
|
-
* which have no `.native` override here); the web variant lives in the sibling
|
|
72
|
-
* `native-root-vars.web.ts` no-op, which web bundlers pick over this one. So the
|
|
73
|
-
* static `react-native-css/native-internal` import below is never pulled into a web
|
|
74
|
-
* bundle — only into native. This mirrors Bloom's existing platform-split convention
|
|
75
|
-
* (`color-scope/index.tsx` + `color-scope/index.web.tsx`, `FontLoader.native.tsx`).
|
|
76
|
-
*
|
|
77
|
-
* Keying contract (verified against react-native-css@3.0.7)
|
|
78
|
-
* ---------------------------------------------------------
|
|
79
|
-
* The `rootVariables` family is keyed by the BARE variable name, WITHOUT the leading
|
|
80
|
-
* `--`. The compiler strips it uniformly: a `:root { --primary: ... }` declaration is
|
|
81
|
-
* stored via `rule.v.push([property.slice(2), value])` then routed into
|
|
82
|
-
* `shared.rootVariables[name]`, and `@property --x` goes through
|
|
83
|
-
* `name.startsWith("--") ? name.slice(2) : name`. The lookup side matches: a `var(--x)`
|
|
84
|
-
* reference compiles to `[{}, "var", ident.slice(2)]`, so `varResolver` looks up the
|
|
85
|
-
* bare name. `buildScopeVars` returns keys WITH the `--` prefix, so we strip it before
|
|
86
|
-
* writing each entry.
|
|
87
|
-
*
|
|
88
|
-
* Value shape
|
|
89
|
-
* -----------
|
|
90
|
-
* Each entry is a `VariableValue[]` — an array of `[value, mediaCondition?]` tuples.
|
|
91
|
-
* Bloom's preset vars are unconditional, so each is a single-element `[[value]]`, e.g.
|
|
92
|
-
* `rootVariables('primary').set([['205 87% 53%']])`. This is the same shape the compiler
|
|
93
|
-
* emits for unconditional `:root` vars.
|
|
94
|
-
*
|
|
95
|
-
* This is native-only and additive: the `VariableContextProvider` wrapper stays (it is
|
|
96
|
-
* the correct, scoped mechanism for `BloomColorScope` subtree overrides and harmlessly
|
|
97
|
-
* covers direct descendants). The web fork (`native-root-vars.web.ts`) no-ops — web
|
|
98
|
-
* writes the same vars to `document.documentElement` via `applyColorPresetVars`.
|
|
4
|
+
* Platform-neutral DEFAULT variant of `applyNativeRootVars` — a no-op.
|
|
5
|
+
*
|
|
6
|
+
* This is the file resolved by every bundler / type-checker that does NOT do
|
|
7
|
+
* React Native's platform-extension resolution:
|
|
8
|
+
* - a consumer's `tsc` (resolving Bloom's `"react-native"` -> `./src/...` source
|
|
9
|
+
* export condition: `theme/index.ts` -> `BloomThemeProvider` -> this import);
|
|
10
|
+
* - non-Metro web bundlers (Vite/Rolldown, webpack) resolving the package
|
|
11
|
+
* `exports` `import`/`require` conditions to `lib/.../native-root-vars.js`.
|
|
12
|
+
*
|
|
13
|
+
* It deliberately does NOT import `react-native-css/native-internal`. That subpath
|
|
14
|
+
* is native-only: it has no web build, and react-native-css is not even a Bloom
|
|
15
|
+
* dependency. Statically importing it from a file a web bundler resolves breaks
|
|
16
|
+
* module resolution outright (the original report: `Rolldown failed to resolve
|
|
17
|
+
* import "react-native-css/native-internal" from .../native-root-vars.js`).
|
|
18
|
+
* Importing it from a file a consumer's `tsc` resolves breaks type-check (TS2307).
|
|
19
|
+
*
|
|
20
|
+
* Confining the real implementation (and that import) to the `.native` sibling —
|
|
21
|
+
* `native-root-vars.native.ts`, which ONLY Metro selects — keeps both web bundling
|
|
22
|
+
* AND downstream `tsc` green, while native still publishes the preset vars into
|
|
23
|
+
* react-native-css's GLOBAL `rootVariables` family so the whole native tree
|
|
24
|
+
* (navigator screens + portals/modals) resolves the palette.
|
|
25
|
+
*
|
|
26
|
+
* The other variants:
|
|
27
|
+
* - `native-root-vars.native.ts` — iOS/Android (Metro), the real implementation;
|
|
28
|
+
* - `native-root-vars.web.ts` — web, also a no-op (web writes the same vars to
|
|
29
|
+
* `document.documentElement` via `applyColorPresetVars` instead).
|
|
30
|
+
*
|
|
31
|
+
* The signature is intentionally identical to those variants so the import site in
|
|
32
|
+
* `BloomThemeProvider` is platform-agnostic.
|
|
99
33
|
*/
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
* so the whole native tree (navigator screens + portals/modals/bottom-sheets)
|
|
104
|
-
* resolves the active palette.
|
|
105
|
-
*
|
|
106
|
-
* Each `.set` notifies its own observers synchronously (react-native-css only
|
|
107
|
-
* batches notifications inside its own `StyleCollection.inject`; outside it,
|
|
108
|
-
* `observableBatch.current` is unset so every `.set` runs observers immediately),
|
|
109
|
-
* so sequential writes correctly trigger a re-render when the preset/mode changes.
|
|
110
|
-
*
|
|
111
|
-
* Defensive guard: if a host ships a react-native-css build that doesn't expose
|
|
112
|
-
* `rootVariables` as a callable (it always does on @3), we bail rather than throw.
|
|
113
|
-
*/
|
|
114
|
-
export function applyNativeRootVars(colorPreset: AppColorName, mode: 'light' | 'dark'): void {
|
|
115
|
-
if (typeof rootVariables !== 'function') return;
|
|
116
|
-
|
|
117
|
-
const vars = buildScopeVars(colorPreset, mode);
|
|
118
|
-
for (const [key, value] of Object.entries(vars)) {
|
|
119
|
-
// The family is keyed by the bare name; `buildScopeVars` keys include `--`.
|
|
120
|
-
const name = key.startsWith('--') ? key.slice(2) : key;
|
|
121
|
-
rootVariables(name).set([[value]]);
|
|
122
|
-
}
|
|
34
|
+
export function applyNativeRootVars(_colorPreset: AppColorName, _mode: 'light' | 'dark'): void {
|
|
35
|
+
// Intentionally empty — the native variant (`native-root-vars.native.ts`) does
|
|
36
|
+
// the real work; this neutral default and the web fork are inert.
|
|
123
37
|
}
|