@kbach/react-native 1.0.0-beta.9 → 1.0.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/AGENTS.md +495 -0
- package/LICENSE +21 -0
- package/README.md +198 -70
- package/THIRD_PARTY_NOTICES.md +41 -0
- package/android/src/main/jniLibs/arm64-v8a/libkbach_core_engine.so +0 -0
- package/android/src/main/jniLibs/x86_64/libkbach_core_engine.so +0 -0
- package/dist/chunk-7CNURQSH.mjs +112 -0
- package/dist/{chunk-YOC3SHF5.mjs → chunk-HYQFT32E.mjs} +2 -2
- package/dist/chunk-MUPEQJRT.mjs +623 -0
- package/dist/darkModeStore.d.ts +73 -0
- package/dist/darkModeStore.js +118 -0
- package/dist/dynamicTokens.d.ts +53 -0
- package/dist/dynamicTokens.js +75 -0
- package/dist/index.d.ts +90 -70
- package/dist/index.js +1300 -610
- package/dist/index.web.d.ts +91 -1
- package/dist/index.web.js +204 -441
- package/dist/index.web.mjs +294 -12
- package/dist/jsx-dev-runtime.d.ts +2 -2
- package/dist/jsx-dev-runtime.js +1532 -603
- package/dist/jsx-dev-runtime.web.d.ts +2 -2
- package/dist/jsx-dev-runtime.web.js +20 -355
- package/dist/jsx-dev-runtime.web.mjs +18 -6
- package/dist/jsx-runtime.d.ts +3 -3
- package/dist/jsx-runtime.js +1522 -593
- package/dist/jsx-runtime.web.d.ts +3 -3
- package/dist/jsx-runtime.web.js +17 -355
- package/dist/jsx-runtime.web.mjs +2 -3
- package/dist/semanticElements-Bk3TB-vt.d.ts +334 -0
- package/dist/theme.d.ts +79 -0
- package/dist/{chunk-HK6PDJJK.mjs → theme.js} +99 -11
- package/llms.txt +38 -0
- package/package.json +15 -13
- package/src/babel-plugin.js +95 -25
- package/dist/chunk-DLIXI5OW.mjs +0 -53
- package/dist/chunk-DOWW44F7.mjs +0 -14
- package/dist/chunk-H5Z24XX5.mjs +0 -192
- package/dist/chunk-SY5OAY2C.mjs +0 -1211
- package/dist/chunk-X4UQCEOE.mjs +0 -186
- package/dist/chunk-Z664AKW7.mjs +0 -54
- package/dist/index.d.mts +0 -83
- package/dist/index.mjs +0 -38
- package/dist/index.web.d.mts +0 -15
- package/dist/jsx-dev-runtime.d.mts +0 -10
- package/dist/jsx-dev-runtime.mjs +0 -18
- package/dist/jsx-dev-runtime.web.d.mts +0 -10
- package/dist/jsx-runtime.d.mts +0 -7
- package/dist/jsx-runtime.mjs +0 -15
- package/dist/jsx-runtime.web.d.mts +0 -7
- package/dist/useColors-CeP5_HvM.d.mts +0 -230
- package/dist/useColors-CeP5_HvM.d.ts +0 -230
package/AGENTS.md
ADDED
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
# `@kbach/react-native` — reference for AI assistants
|
|
2
|
+
|
|
3
|
+
The React Native binding of [Kbach](https://github.com/semsakadanupol/kbach)
|
|
4
|
+
— the same Tailwind-v4-shaped class-name vocabulary as `@kbach/react`,
|
|
5
|
+
resolved to a **`StyleSheet`-ready style object at render time** via a
|
|
6
|
+
shared Rust core (JNI on Android, WASM on Expo Web, a pure-JS fallback on
|
|
7
|
+
Expo Go and iOS). This file covers `@kbach/react-native` only; for the
|
|
8
|
+
cross-package overview, `kbach.config.js` reference, the color palette,
|
|
9
|
+
and how the underlying engine is built, see the repo root's
|
|
10
|
+
[AGENTS.md](https://github.com/semsakadanupol/kbach/blob/main/AGENTS.md).
|
|
11
|
+
|
|
12
|
+
> **Before writing a single class here, know these three things —
|
|
13
|
+
> Tailwind familiarity actively misleads on all three:**
|
|
14
|
+
>
|
|
15
|
+
> 1. Color shades are `1`–`12` (lightest→darkest), not Tailwind's
|
|
16
|
+
> `50`–`950` — `bg-blue-500` resolves to nothing; the real class is
|
|
17
|
+
> `bg-blue-6`. Full palette: root AGENTS.md §7.
|
|
18
|
+
> 2. `group-*`, `peer-*`, `has-[…]`, container queries, grid, and pseudo-
|
|
19
|
+
> elements (`before:`/`after:`) are real Tailwind syntax that PARSES
|
|
20
|
+
> here but applies **zero style, with no warning** — native has no
|
|
21
|
+
> cascade/DOM to support them. See §4's "Not supported" list. A typo
|
|
22
|
+
> DOES warn; an unsupported-but-valid utility does not — don't assume
|
|
23
|
+
> silence means it worked.
|
|
24
|
+
> 3. The babel plugin (§1 below) is required on **every** target,
|
|
25
|
+
> including Expo Go — without it `className` does literally nothing,
|
|
26
|
+
> silently, no error. This is the single most common real setup bug
|
|
27
|
+
> reported against this package.
|
|
28
|
+
|
|
29
|
+
---
|
|
30
|
+
|
|
31
|
+
## 1. Install + setup
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npx @kbach/cli init
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Automates everything below — installs `@kbach/react-native`, patches (or
|
|
38
|
+
creates) `babel.config.js` via a format-preserving codemod (handles both
|
|
39
|
+
Expo's function-export form and React Native CLI's plain object-export
|
|
40
|
+
form), and on React Native CLI offers to run the native rebuild right
|
|
41
|
+
there. Safe to re-run at any time (idempotent — reports "already has the
|
|
42
|
+
kbach babel plugin" rather than duplicating the entry). See
|
|
43
|
+
[`packages/cli/AGENTS.md`](../cli/AGENTS.md) for exactly what it detects
|
|
44
|
+
and changes, and what it does when the codemod can't safely apply
|
|
45
|
+
(prints the same manual snippet shown below, rather than guessing).
|
|
46
|
+
|
|
47
|
+
The manual steps below are both the CLI's own fallback and the accurate
|
|
48
|
+
reference for what it actually does — start here if you're wiring this
|
|
49
|
+
up by hand, or the CLI reported it couldn't:
|
|
50
|
+
|
|
51
|
+
```sh
|
|
52
|
+
npm install @kbach/react-native@beta
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The babel plugin is **required on every target, including Expo Go** — it
|
|
56
|
+
injects the per-file `@jsxImportSource @kbach/react-native` pragma
|
|
57
|
+
(babel-plugin.js) that routes `className` through this package's
|
|
58
|
+
jsx-runtime instead of `react/jsx-runtime`; nothing else makes
|
|
59
|
+
interception happen. Confirmed as a real, reported doc bug: the table
|
|
60
|
+
below used to say "None" for Expo Go/Expo Web, and a from-scratch repro
|
|
61
|
+
(fresh Expo project, README's own example, zero babel config, Expo Go on
|
|
62
|
+
a real emulator) showed `className` doing nothing at all — no styling, no
|
|
63
|
+
warning, no error — exactly as this mechanism predicts. What differs per
|
|
64
|
+
target is what happens *after* the plugin is in place:
|
|
65
|
+
|
|
66
|
+
| Target | Setup |
|
|
67
|
+
| :-- | :-- |
|
|
68
|
+
| **Expo Go** | Babel plugin. A pure-JS fallback engine loads automatically after that (reduced utility coverage) — no other setup. |
|
|
69
|
+
| **Expo Web** / `react-native-web` | Babel plugin. Uses the same WASM CSS engine as `@kbach/react` after that — no other setup. |
|
|
70
|
+
| **Expo dev client / prebuild / EAS** | Babel plugin, then `npx expo prebuild && npx expo run:android`. Full native engine. |
|
|
71
|
+
| **React Native CLI** | Babel plugin, then restart Metro with `--reset-cache` and rebuild the Android app. |
|
|
72
|
+
|
|
73
|
+
Babel plugin — added to the existing `babel.config.js` (keep the function
|
|
74
|
+
form Expo generates; only add the `plugins` entry):
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
module.exports = function (api) {
|
|
78
|
+
api.cache(true);
|
|
79
|
+
return {
|
|
80
|
+
presets: ['babel-preset-expo'],
|
|
81
|
+
plugins: ['@kbach/react-native/babel-plugin'],
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
React Native CLI uses `presets: ['module:@react-native/babel-preset']` and
|
|
87
|
+
a plain object export.
|
|
88
|
+
|
|
89
|
+
**Native is Android-only** (no iOS bridge yet) — on iOS, every build
|
|
90
|
+
(dev client, CLI, EAS) runs the same reduced-coverage JS-fallback engine
|
|
91
|
+
as Expo Go, not the full native engine (see §3's engine table).
|
|
92
|
+
|
|
93
|
+
## 2. What the babel plugin does
|
|
94
|
+
|
|
95
|
+
Per user source file (never `node_modules`, never Metro/Expo virtual
|
|
96
|
+
modules, never `kbach.config.js` itself):
|
|
97
|
+
|
|
98
|
+
1. Prepends a `// @jsxImportSource @kbach/react-native` comment so the JSX
|
|
99
|
+
transform routes `className` through this package's jsx-runtime.
|
|
100
|
+
2. Prepends `require('@kbach/react-native').applyKbachConfig(require('<abs
|
|
101
|
+
path>/kbach.config.js'))` if that file exists at the project root — so
|
|
102
|
+
the config is applied before the app's first render with **no import or
|
|
103
|
+
call in app code**.
|
|
104
|
+
|
|
105
|
+
A dev-server restart is required after changing `kbach.config.js` or the
|
|
106
|
+
babel config — for the babel config specifically, a plain restart isn't
|
|
107
|
+
reliably enough (Metro's transform cache doesn't always invalidate on a
|
|
108
|
+
babel config change on its own); use `--reset-cache` (`npx react-native
|
|
109
|
+
start --reset-cache` / `npx expo start --clear`).
|
|
110
|
+
|
|
111
|
+
## 3. How resolution works
|
|
112
|
+
|
|
113
|
+
`className` on a host element (`View`, `Text`, `Pressable`, …) is
|
|
114
|
+
intercepted by the jsx-runtime and resolved to a style object. One of three
|
|
115
|
+
engines does the work, chosen automatically:
|
|
116
|
+
|
|
117
|
+
| Runtime | Engine | Mechanism |
|
|
118
|
+
| :-- | :-- | :-- |
|
|
119
|
+
| Android dev-client / CLI build | **Rust via JNI** (`KbachModule` TurboModule, prebuilt `.so`) | `resolveStyle(classString, themeJson, colorScheme, pressed, width)` → JSON style object. |
|
|
120
|
+
| Expo Web / `react-native-web` | **Rust via WASM** (`generate_css_attr`) | Emits real CSS rules into a `<style>` tag; element gets `dataSet={{ kb }}` and rules target `[data-kb~="…"]`. Full modifier set. |
|
|
121
|
+
| Expo Go | **Pure-JS fallback** (`jsEngine/`) | A partial TS port of the native resolver. No custom native module can load in Expo Go. |
|
|
122
|
+
| iOS — ANY build (dev-client, CLI, EAS) | **Pure-JS fallback** (`jsEngine/`) | Same reduced-coverage path as Expo Go, since there's no iOS native module at all yet (`TurboModuleRegistry.get('KbachModule')` returns `null` on iOS, so `resolveStyle` falls through to the JS engine — see nativeBridge.ts). Not specific to Expo Go; a real iOS dev-client/CLI build is on this row too. |
|
|
123
|
+
|
|
124
|
+
Resolution is memoized per `(classString, pressed, colorScheme, width,
|
|
125
|
+
theme)`.
|
|
126
|
+
|
|
127
|
+
## 4. Modifiers on native
|
|
128
|
+
|
|
129
|
+
Native has no selector/cascade engine. Two groups:
|
|
130
|
+
|
|
131
|
+
**Resolved in the engine from live parameters:**
|
|
132
|
+
- `dark:` — current OS/app color scheme; `not-dark:` is the other one.
|
|
133
|
+
- `sm:` `md:` `lg:` `xl:` `2xl:` — window width vs `theme.screens`, plus
|
|
134
|
+
any breakpoint a config adds (`3xl:`, `tablet:`), and `max-<name>:`
|
|
135
|
+
(strictly below that width). `not-<name>:` / `not-max-<name>:` flip
|
|
136
|
+
either one.
|
|
137
|
+
- `min-[500px]:` / `max-[30rem]:` — arbitrary width breakpoints (constant
|
|
138
|
+
px/rem only). `max-[…]` is strictly below its width, like `max-<name>:`;
|
|
139
|
+
through core-engine 1.0.0-beta.18 it also held at exactly that width.
|
|
140
|
+
- `active:` — `Pressable` press state (via its style-callback).
|
|
141
|
+
|
|
142
|
+
**Resolved in `jsxRuntimeCore.ts` from React props, before the engine sees
|
|
143
|
+
the string:**
|
|
144
|
+
- `hover:` — `onHoverIn`/`onHoverOut` — only fires for pointer-capable
|
|
145
|
+
input (mouse/trackpad); inert on pure touch.
|
|
146
|
+
- `focus:` — `onFocus`/`onBlur`.
|
|
147
|
+
- `disabled:` — the element's own `disabled` prop.
|
|
148
|
+
- `data-[key=value]:` / `data-[key]:` / `aria-[…]:` / static `aria-*`
|
|
149
|
+
shortcuts — the element's own `data-*` / `aria-*` props.
|
|
150
|
+
|
|
151
|
+
A satisfied prop-based modifier is rewritten to an internal `_kbon` marker
|
|
152
|
+
(one per satisfied modifier) so it still contributes to specificity.
|
|
153
|
+
|
|
154
|
+
**Prop-based modifiers never inherit from an ancestor.** `disabled:` /
|
|
155
|
+
`aria-[…]:` / `data-[…]:` read `hostRest` off THAT exact element's own
|
|
156
|
+
props (`propBasedModifierState` in jsxRuntimeCore.ts) — there is no
|
|
157
|
+
`group-disabled:`-style cascade on native to inherit one through. Confirmed
|
|
158
|
+
as a real footgun (reported after two independent hits building a demo
|
|
159
|
+
app): `<Pressable disabled><Text className="disabled:text-gray-5">` does
|
|
160
|
+
nothing at all, silently — `Text` never received a `disabled` prop, so
|
|
161
|
+
`propBasedModifierState` returns `null` for it (no opinion), the modifier
|
|
162
|
+
is treated as never-satisfied, and nothing warns. The prop has to be
|
|
163
|
+
repeated on whichever element actually carries the modifier:
|
|
164
|
+
`<Pressable disabled><Text disabled className="disabled:text-gray-5">`.
|
|
165
|
+
|
|
166
|
+
**Not supported** (parse without error, never apply): `group-*`, `peer-*`,
|
|
167
|
+
`has-[…]`, container queries, `*:` / `**:`, `before:` / `after:` and other
|
|
168
|
+
pseudo-elements.
|
|
169
|
+
|
|
170
|
+
**Specificity / class order.** When two classes set the same property, the
|
|
171
|
+
one with more satisfied modifiers wins **regardless of source order** —
|
|
172
|
+
`dark:bg-black bg-white` and `bg-white dark:bg-black` both resolve to black
|
|
173
|
+
in dark mode, matching web. Equal specificity falls back to last-token-wins.
|
|
174
|
+
`transform-none` is an exception: it clears all transforms above it in
|
|
175
|
+
source order regardless of specificity.
|
|
176
|
+
|
|
177
|
+
Chaining composes and is order-independent (`dark:sm:` ≡ `sm:dark:`); every
|
|
178
|
+
named condition must hold.
|
|
179
|
+
|
|
180
|
+
## 5. Expo Go coverage gap
|
|
181
|
+
|
|
182
|
+
The JS fallback covers: layout, spacing, border/radius, plain colors,
|
|
183
|
+
font-weight, text-transform/decoration, line-height, letter-spacing,
|
|
184
|
+
shadows, and the RN-representable transforms (`translate-x/y`,
|
|
185
|
+
`scale`/`scale-x/y`, `rotate`/`rotate-x/y/z`, `skew-x/y`, `transform-none`,
|
|
186
|
+
`backface-*`).
|
|
187
|
+
|
|
188
|
+
Not covered: grid, filters, gradients, DOM-only typography, and transforms
|
|
189
|
+
with no RN equivalent (`translate-z`, `scale-z`, `perspective*`, `origin`,
|
|
190
|
+
`transform-gpu/cpu`). Same scope native itself excludes. Typo warnings are
|
|
191
|
+
also unavailable in Expo Go (no full-vocabulary resolver to check against).
|
|
192
|
+
|
|
193
|
+
This is also exactly what iOS gets in every build type (§3) — the coverage
|
|
194
|
+
gap isn't Expo-Go-specific, it's "whatever the JS-fallback engine covers."
|
|
195
|
+
|
|
196
|
+
Measured 2026-11-17 against every class in the docs' reference tables
|
|
197
|
+
(1,048), the JS fallback matches the Rust engine except: the `object-*`
|
|
198
|
+
classes (no `objectFit`), the small container sizes `3xs`/`2xs`/`xs` on
|
|
199
|
+
`w-`/`min-w-`/`max-w-`/`basis-` (no value), and `container`. The other
|
|
200
|
+
differences are properties React Native ignores anyway (`float`, `columns`,
|
|
201
|
+
`break-*`, `clear`, `visibility`, `isolation`, logical `rounded-s`/`border-s`
|
|
202
|
+
with `"0.5rem"`/`"1px"` strings), which the Rust engine returns and the JS
|
|
203
|
+
one doesn't.
|
|
204
|
+
|
|
205
|
+
Within that scope it must match the Rust native engine exactly.
|
|
206
|
+
`src/jsEngine/parity.test.ts` runs class strings through both (Rust via
|
|
207
|
+
`packages/core-engine/dist-node`, so core-engine must be built first) and
|
|
208
|
+
fails on any difference. Extend its `CASES` whenever a native-reaching
|
|
209
|
+
resolver changes in either engine; the 2026-11 Tailwind v4 pass (exact-px
|
|
210
|
+
border widths, `px`, two-axis `translate-*`/`skew-*`, `rotate-none`/
|
|
211
|
+
`scale-none`, `flex-N`, formula `leading-N`, `rounded-xs`/`rounded-4xl`)
|
|
212
|
+
and the Tailwind v4.3 values (`rounded-sm`, the `shadow-*` scale, outlines)
|
|
213
|
+
are covered there. See root AGENTS.md §8.1 for what's web-only by design.
|
|
214
|
+
|
|
215
|
+
**Rings (`ring-*`, `ring-offset-*`) apply no style on native**, on purpose:
|
|
216
|
+
they're a `var()`-built CSS `box-shadow`. Both engines also drop every CSS
|
|
217
|
+
custom property and `var()`-containing value before building the RN style
|
|
218
|
+
(it used to leak through as keys like `KbRingWidth`). Use `border-*` or
|
|
219
|
+
`shadow-*` for a native focus/selection outline. Text sizes on native set
|
|
220
|
+
`fontSize` only (no Tailwind default line-height); add `leading-*` for an
|
|
221
|
+
explicit native line height.
|
|
222
|
+
|
|
223
|
+
## 6. Dark mode
|
|
224
|
+
|
|
225
|
+
Zero setup on all targets. On native `dark:` is resolved from a live
|
|
226
|
+
parameter every render — no build step to keep in sync. Expo Web defaults
|
|
227
|
+
to the `'attribute'` strategy.
|
|
228
|
+
|
|
229
|
+
```tsx
|
|
230
|
+
import { useTheme } from '@kbach/react-native';
|
|
231
|
+
const { mode, isDark, setMode, toggle } = useTheme();
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
No provider required. `<ThemeProvider>` optionally seeds a startup
|
|
235
|
+
`defaultMode` and enables persistence:
|
|
236
|
+
|
|
237
|
+
```tsx
|
|
238
|
+
import AsyncStorage from '@react-native-async-storage/async-storage';
|
|
239
|
+
<ThemeProvider persist={AsyncStorage}><App /></ThemeProvider>;
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
Mount at most one — dark mode is one global store, not scoped per
|
|
243
|
+
provider, so a second mounted instance logs a dev-only warning (they'd
|
|
244
|
+
fight over the same `defaultMode`/`persist` seed).
|
|
245
|
+
|
|
246
|
+
`persist` takes any `{ getItem, setItem }` async store. The persisted mode
|
|
247
|
+
is read once on mount and wins over `defaultMode`; later changes are
|
|
248
|
+
written back. This package never imports a storage library itself.
|
|
249
|
+
|
|
250
|
+
Outside React: `getGlobalDarkMode()` / `toggleGlobalDarkMode()` /
|
|
251
|
+
`setGlobalThemeMode()`.
|
|
252
|
+
|
|
253
|
+
## 7. `calc()` / `min()` / `max()` / `clamp()`
|
|
254
|
+
|
|
255
|
+
- **Constant** px/rem/bare-number expressions are reduced at resolve time:
|
|
256
|
+
`p-[calc(16px+8px)]` → `padding: 24`. No spaces or underscores are
|
|
257
|
+
required around operators; `calc(16px+8px)` is fine.
|
|
258
|
+
- **Viewport-relative** (`calc(10vw+8px)`): no such unit on native — the
|
|
259
|
+
declaration is dropped and a dev-only `console.warn` explains why.
|
|
260
|
+
- **Percentage-relative `width`/`height`** (`w-[calc(100%-3rem)]`,
|
|
261
|
+
`w-[min(50%,20rem)]`, `w-[clamp(16rem,50%,32rem)]`): supported via a
|
|
262
|
+
one-frame measure-then-snap — renders at `100%`, measures via
|
|
263
|
+
`onLayout`, then corrects to the computed number; re-measures on layout
|
|
264
|
+
changes. Narrow shapes only: `calc()` = one percent term + one constant
|
|
265
|
+
term; `min`/`max` args each a plain percent OR a constant expression (not
|
|
266
|
+
mixed); `clamp` exactly three args; `width`/`height` only.
|
|
267
|
+
- **Literal spaces are illegal inside `[...]`** on any Tailwind-family
|
|
268
|
+
engine (a space splits the class token). Use underscores if you want them
|
|
269
|
+
for readability: `w-[calc(100%_-_3rem)]`.
|
|
270
|
+
|
|
271
|
+
### 7.1 Arbitrary values elsewhere — same px/rem-only rule
|
|
272
|
+
|
|
273
|
+
Not just `calc()`: any arbitrary value RN needs as a plain number (font
|
|
274
|
+
size, spacing, radii, ...) follows the identical rule. `text-[20px]` and
|
|
275
|
+
`text-[1.5rem]` work (`rem` × 16, same as everywhere else); `text-[2em]`,
|
|
276
|
+
`text-[50%]`, `text-[10vh]`/`text-[10vw]` don't — `em` needs the parent's
|
|
277
|
+
own cascaded font-size and `%`/viewport units need real layout, neither
|
|
278
|
+
of which exists at native resolve time — dropped with the same dev-only
|
|
279
|
+
`console.warn`, never a crash. Identical on Expo Go's JS-fallback engine
|
|
280
|
+
(`jsEngine/resolveStyle.ts` mirrors `resolve_style.rs`'s `rn_style_value`
|
|
281
|
+
exactly). Web (including Expo Web) has none of this restriction — real
|
|
282
|
+
CSS resolves every unit natively.
|
|
283
|
+
|
|
284
|
+
**`text-[…]` specifically needs `< 1.0.0-beta.52` excluded** — every
|
|
285
|
+
arbitrary `text-[…]` value, including a plain `text-[10px]`, silently
|
|
286
|
+
resolved nothing at all on native/Expo Go before that version (see §13).
|
|
287
|
+
|
|
288
|
+
## 8. Dynamic tokens (native counterpart to CSS custom properties)
|
|
289
|
+
|
|
290
|
+
```tsx
|
|
291
|
+
import { setDynamicToken, useDynamicToken } from '@kbach/react-native';
|
|
292
|
+
|
|
293
|
+
setDynamicToken('sidebar-width', '240px');
|
|
294
|
+
<View className="w-[var(--sidebar-width)]" />;
|
|
295
|
+
const w = useDynamicToken('sidebar-width'); // '240px' | undefined, reactive
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
On web `setDynamicToken` writes a real CSS custom property (browser
|
|
299
|
+
repaints, no re-render). On native the token value is substituted into the
|
|
300
|
+
class string before each resolve, and subscribed elements re-render on
|
|
301
|
+
change. `var(--x)` in a `className` already works unaided on Expo Web.
|
|
302
|
+
|
|
303
|
+
**Call `setDynamicToken` synchronously from a render body, not from module
|
|
304
|
+
top-level.** It calls `notify()` synchronously (dynamicTokens.ts), which
|
|
305
|
+
synchronously re-renders every already-mounted `ReactiveElement` subscribed
|
|
306
|
+
via `useSyncExternalStore` — safe when nothing is subscribed yet, a real
|
|
307
|
+
hazard once something is. Module-top-level code runs whenever Metro's
|
|
308
|
+
inline-requires first evaluates that module, which is NOT a fixed point in
|
|
309
|
+
the app's lifecycle — it can land in the middle of React rendering some
|
|
310
|
+
unrelated component elsewhere in the tree, which is exactly the "Cannot
|
|
311
|
+
update a component while rendering a different component" class of bug:
|
|
312
|
+
|
|
313
|
+
```tsx
|
|
314
|
+
// Bad — races Metro inline-requires; can fire mid-render of an unrelated component.
|
|
315
|
+
setDynamicToken('accent', '#8b5cf6');
|
|
316
|
+
|
|
317
|
+
// Good — call synchronously in your ROOT component's own render, before any
|
|
318
|
+
// consumer mounts; guard so it only ever fires once (unguarded, every
|
|
319
|
+
// re-render would call notify() again, wastefully re-rendering every
|
|
320
|
+
// subscribed element for no value change).
|
|
321
|
+
function RootLayout({ children }) {
|
|
322
|
+
if (getDynamicToken('accent') === undefined) {
|
|
323
|
+
setDynamicToken('accent', '#8b5cf6');
|
|
324
|
+
}
|
|
325
|
+
return <ThemeProvider>{children}</ThemeProvider>;
|
|
326
|
+
}
|
|
327
|
+
```
|
|
328
|
+
|
|
329
|
+
## 9. Reading colors as values
|
|
330
|
+
|
|
331
|
+
```tsx
|
|
332
|
+
import { useColors } from '@kbach/react-native';
|
|
333
|
+
|
|
334
|
+
const colors = useColors();
|
|
335
|
+
colors.blue[6]; // '#2563eb'
|
|
336
|
+
colors.blue['6/50']; // 50% opacity
|
|
337
|
+
colors.alpha(colors.blue[6], 30);
|
|
338
|
+
colors.get('brand'); // a custom color, typed as plain `string`
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
`colors.blue[6]` returns the same value in both light and dark mode
|
|
342
|
+
unless `blue-6` itself is defined in the theme as a `{ light, dark }` pair
|
|
343
|
+
(§6.1 of the root AGENTS.md) — define a color as mode-aware in
|
|
344
|
+
`kbach.config.js` if you want it to differ by mode.
|
|
345
|
+
|
|
346
|
+
`colors.brand` (a custom flat color) is typed `string | ColorScale`
|
|
347
|
+
because the same object also serves shade families and TS can't tell them
|
|
348
|
+
apart statically — use `colors.get(name)` when you need a guaranteed
|
|
349
|
+
`string`.
|
|
350
|
+
|
|
351
|
+
## 10. Unknown-class warnings
|
|
352
|
+
|
|
353
|
+
On a native dev-client/CLI build, a class matching no Kbach utility on any
|
|
354
|
+
platform emits a one-time `console.warn` (dev only), with a "did you
|
|
355
|
+
mean" when there is one: a Tailwind shade mapped to Kbach's
|
|
356
|
+
(`bg-blue-500` → `bg-blue-6`) or a one-character typo fix (core-engine
|
|
357
|
+
suggest.rs). A real utility native
|
|
358
|
+
just doesn't support yet (`grid-cols-3`, `blur`, …) stays silent — that's a
|
|
359
|
+
documented platform gap, not a typo. Not available in Expo Go.
|
|
360
|
+
|
|
361
|
+
A malformed or misused value fails the same safe way — a deduped, dev-only
|
|
362
|
+
`console.warn` naming the offending class and the fix, never a crash or
|
|
363
|
+
silently-wrong output. Confirmed across CSS-injection attempts in an
|
|
364
|
+
arbitrary value, invalid `calc()` operands, and double-mounted providers.
|
|
365
|
+
|
|
366
|
+
## 11. Monorepo / workspace installs
|
|
367
|
+
|
|
368
|
+
If `@kbach/react-native` is a workspace/hoisted dependency, Metro can end
|
|
369
|
+
up with **two physical copies of `react-native` / `react`** — different
|
|
370
|
+
files resolve to different module instances, and a dark-mode toggle can
|
|
371
|
+
silently do nothing. Force a single instance in your app's
|
|
372
|
+
`metro.config.js` via `resolver.resolveRequest`, pointing `react-native`
|
|
373
|
+
and `react` at `projectRoot/node_modules/...`. A plain non-monorepo
|
|
374
|
+
`npm install` never hits this.
|
|
375
|
+
|
|
376
|
+
## 12. Exports
|
|
377
|
+
|
|
378
|
+
`setTheme`, `getTheme`, `defaultTheme`, `resolveStyle`, `resolveKbachConfig`,
|
|
379
|
+
`applyKbachConfig`, `useTheme`, `ThemeProvider`, `useColors`,
|
|
380
|
+
`setDynamicToken`, `getDynamicToken`, `deleteDynamicToken`,
|
|
381
|
+
`subscribeDynamicTokens`, `useDynamicToken`, `clsx`, `getGlobalDarkMode`,
|
|
382
|
+
`getGlobalThemeMode`, `setGlobalThemeMode`, `toggleGlobalDarkMode`,
|
|
383
|
+
`subscribeGlobalDarkMode`, `Button`, `Paragraph`, `H1`, `H2`, `H3`, `H4`,
|
|
384
|
+
`H5`, `H6` (§14). Types: `ThemeConfig`, `ColorEntry`, `ContainerConfig`,
|
|
385
|
+
`StyleObject`, `KbachConfig`, `ThemeState`, `ThemeProviderProps`,
|
|
386
|
+
`ThemeStorageAdapter`, `ThemeMode`, `ColorsAPI`, `ColorScale`,
|
|
387
|
+
`ClassValue`, `ClassDictionary`, `ClassArray`, `ButtonProps`, `ParagraphProps`,
|
|
388
|
+
`HeadingProps`. The docs site's `tests/e2e/api-reference.mjs` fails when an
|
|
389
|
+
export here is missing from the React Native API page.
|
|
390
|
+
|
|
391
|
+
Subpath exports: `@kbach/react-native/babel-plugin`,
|
|
392
|
+
`@kbach/react-native/jsx-runtime`, `@kbach/react-native/jsx-dev-runtime`.
|
|
393
|
+
|
|
394
|
+
## 13. Common failure modes
|
|
395
|
+
|
|
396
|
+
- **`bg-<custom>` renders nothing** — the color is mode-aware
|
|
397
|
+
(`{light,dark}` / `dark:` block) and the app is on a stale
|
|
398
|
+
`@kbach/react-native` (`< 1.0.0-beta.36`). Update.
|
|
399
|
+
- **A `dark:` variant loses to a plain class** — stale
|
|
400
|
+
`@kbach/react-native` (`< 1.0.0-beta.37`); class-order independence
|
|
401
|
+
landed there. Update.
|
|
402
|
+
- **`useColors().brand` type error** — assign via `colors.get('brand')`
|
|
403
|
+
(`< beta.36` has no `.get`).
|
|
404
|
+
- **Dark toggle does nothing in a monorepo** — duplicate `react-native` /
|
|
405
|
+
`react` instances; add the `metro.config.js` `resolveRequest` override
|
|
406
|
+
(§11).
|
|
407
|
+
- **Rust change not reflected on a real Android build** — stale prebuilt
|
|
408
|
+
`.so`; the maintainer must re-run `build:android` and republish.
|
|
409
|
+
- **`kbach.config.js` "does nothing" / Metro bundling error** — an
|
|
410
|
+
unquoted hyphenated key (`surface-dim:` instead of `'surface-dim':`).
|
|
411
|
+
Quote it.
|
|
412
|
+
- **TypeScript error on `className`** (`Property 'className' does not
|
|
413
|
+
exist on type ... ViewProps ...`) — fixed in `>= 1.0.0-beta.46`; update.
|
|
414
|
+
- **`className` does absolutely nothing — no styling, no warning, no
|
|
415
|
+
error, on any target including Expo Go** — the babel plugin (§1) is
|
|
416
|
+
missing. It's required on every target, not just Android/dev-client
|
|
417
|
+
builds; without it nothing routes `className` through this package's
|
|
418
|
+
jsx-runtime at all, so RN just silently ignores the unrecognized prop.
|
|
419
|
+
- **`useColors().get('yourCustomColor')` returns the literal name, not
|
|
420
|
+
the color, on Expo Web — icons/fills using it render invisible** —
|
|
421
|
+
stale `@kbach/react-native` (`< 1.0.0-beta.53`). Root cause: on web,
|
|
422
|
+
this package loads as two separate module instances in the same
|
|
423
|
+
bundle — a bare `import` resolves through package.json's `import`
|
|
424
|
+
condition, but the babel plugin's injected
|
|
425
|
+
`require('@kbach/react-native').applyKbachConfig(...)` always resolves
|
|
426
|
+
through `require` — so `kbach.config.js`'s custom colors landed in one
|
|
427
|
+
instance's theme state while every component's own `useColors()` read
|
|
428
|
+
the other, untouched one. Fixed by routing `theme.ts`'s active-theme
|
|
429
|
+
state through a `globalThis`-backed slot instead of a plain
|
|
430
|
+
module-scope variable, so both instances share it regardless of which
|
|
431
|
+
one set it. Update.
|
|
432
|
+
- **`ERROR [Error: Unable to find node on an unmounted component.]` on
|
|
433
|
+
Expo Fast Refresh, especially right after editing `kbach.config.js`** —
|
|
434
|
+
stale `@kbach/react-native` (`< 1.0.0-beta.51`). Root cause: the babel
|
|
435
|
+
plugin (§1) injects `applyKbachConfig(require('kbach.config.js'))` into
|
|
436
|
+
*every* user file, so editing that config forces Metro to re-execute the
|
|
437
|
+
entire app (every file requires it), not just the screen you're on;
|
|
438
|
+
before beta.51, `setTheme` (`theme.ts`) handed every one of those
|
|
439
|
+
redundant re-applications a brand-new theme object even when the config
|
|
440
|
+
content hadn't changed, invalidating every `useColors()` memo and
|
|
441
|
+
reactive-element check across the whole tree simultaneously, right as
|
|
442
|
+
Fast Refresh was already mid-teardown — worsening this normally-rare
|
|
443
|
+
React Native Fast Refresh race. `setTheme` is idempotent by content
|
|
444
|
+
since beta.51 (compares against the theme's own JSON serialization
|
|
445
|
+
before replacing it); update.
|
|
446
|
+
- **`text-[10px]` (or any other arbitrary `text-[…]` value) does nothing
|
|
447
|
+
on native or Expo Go** — stale `@kbach/react-native` (`< 1.0.0-beta.52`).
|
|
448
|
+
The native dispatcher's `resolve_text_native` (`resolvers/mod.rs`) never
|
|
449
|
+
got the arbitrary-value length check `color::resolve_text` (web) already
|
|
450
|
+
had — an arbitrary value skipped the font-size/align lookup entirely and
|
|
451
|
+
fell straight through to color resolution, which correctly rejected
|
|
452
|
+
"10px" as not a real color and silently dropped the whole declaration.
|
|
453
|
+
Fixed identically in both the Rust native dispatcher and its Expo Go JS-
|
|
454
|
+
fallback mirror (`jsEngine/resolveUtilityNative.ts`) in beta.52; update.
|
|
455
|
+
Web (including Expo Web) was never affected.
|
|
456
|
+
|
|
457
|
+
## 14. Semantic HTML on Expo Web
|
|
458
|
+
|
|
459
|
+
On Expo Web, `react-native-web` renders a bare `<View>`/`<Text>` as a
|
|
460
|
+
plain `<div>`/`<span>` by default — confirmed directly in its own source.
|
|
461
|
+
That's **not** a Kbach limitation: this package's jsx-runtime never reads
|
|
462
|
+
or substitutes the JSX element type at all, so whatever tag shows up is
|
|
463
|
+
exactly whatever component the app wrote.
|
|
464
|
+
|
|
465
|
+
`react-native-web` already has a real fix for this that this package
|
|
466
|
+
never gets in the way of: an `accessibilityRole`/`role` prop on ANY host
|
|
467
|
+
component makes it render the matching real HTML element instead —
|
|
468
|
+
`accessibilityRole="button"` → `<button>`, `accessibilityRole="header"` +
|
|
469
|
+
`aria-level` (web-only; real native has no heading-LEVEL concept, only
|
|
470
|
+
`header` itself) → `<h1>`-`<h6>`, `role="paragraph"` → `<p>`, and so on
|
|
471
|
+
(`react-native-web`'s own `propsToAccessibilityComponent`). `role` covers
|
|
472
|
+
every value in React Native's own typed `Role` union (`button`,
|
|
473
|
+
`heading`, `list`, `listitem`, `navigation`, `main`, `form`, `article`,
|
|
474
|
+
`banner`, `contentinfo`, `complementary`, `region`, ...); `"paragraph"`
|
|
475
|
+
is the one react-native-web additionally supports that isn't in that
|
|
476
|
+
union at all.
|
|
477
|
+
|
|
478
|
+
This package exports a handful of convenience components that are just
|
|
479
|
+
that prop, pre-set on the right underlying component — nothing beyond
|
|
480
|
+
that, and writing `<Pressable accessibilityRole="button">` directly
|
|
481
|
+
instead of `<Button>` behaves identically:
|
|
482
|
+
|
|
483
|
+
```tsx
|
|
484
|
+
import { Button, Paragraph, H1, H2, H3, H4, H5, H6 } from '@kbach/react-native';
|
|
485
|
+
|
|
486
|
+
<Button className="px-4 py-2 rounded bg-blue-6" onPress={...}>Save</Button>
|
|
487
|
+
<H1 className="text-2xl font-bold">Page title</H1>
|
|
488
|
+
<Paragraph className="text-sm text-gray-7">Body copy.</Paragraph>
|
|
489
|
+
```
|
|
490
|
+
|
|
491
|
+
`className` keeps working on every one of these exactly like it does on
|
|
492
|
+
a bare `Pressable`/`Text`. No `Div` or `Input` here on purpose: a plain
|
|
493
|
+
`<View>` already is the right generic container, and `<TextInput>`
|
|
494
|
+
already renders a real `<input>` (or `<textarea>` when `multiline`) by
|
|
495
|
+
default, with no role needed at all.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 semsakadanupol
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|