@kbach/react-native 1.0.0-beta.2 → 1.0.0-beta.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +253 -87
  2. package/android/build.gradle +36 -0
  3. package/android/src/main/AndroidManifest.xml +14 -0
  4. package/android/src/main/java/com/kbachnative/KbachModule.kt +83 -0
  5. package/android/src/main/java/com/kbachnative/KbachPackage.kt +54 -0
  6. package/android/src/main/jniLibs/arm64-v8a/libkbach_core_engine.so +0 -0
  7. package/android/src/main/jniLibs/x86_64/libkbach_core_engine.so +0 -0
  8. package/dist/chunk-AG7ZQ2K4.mjs +552 -0
  9. package/dist/chunk-OS7EPBH5.mjs +40 -0
  10. package/dist/chunk-TS2EEVOE.mjs +107 -0
  11. package/dist/darkModeStore.d.ts +58 -0
  12. package/dist/darkModeStore.js +112 -0
  13. package/dist/dynamicTokens.d.ts +36 -0
  14. package/dist/dynamicTokens.js +75 -0
  15. package/dist/index.d.ts +53 -102
  16. package/dist/index.js +1721 -385
  17. package/dist/index.web.d.ts +18 -0
  18. package/dist/index.web.js +436 -0
  19. package/dist/index.web.mjs +210 -0
  20. package/dist/jsx-dev-runtime.d.ts +3 -12
  21. package/dist/jsx-dev-runtime.js +1738 -343
  22. package/dist/jsx-dev-runtime.web.d.ts +10 -0
  23. package/dist/jsx-dev-runtime.web.js +270 -0
  24. package/dist/jsx-dev-runtime.web.mjs +29 -0
  25. package/dist/jsx-runtime.d.ts +3 -23
  26. package/dist/jsx-runtime.js +1729 -335
  27. package/dist/jsx-runtime.web.d.ts +7 -0
  28. package/dist/jsx-runtime.web.js +252 -0
  29. package/dist/{jsx-runtime.mjs → jsx-runtime.web.mjs} +2 -2
  30. package/dist/theme.d.ts +54 -0
  31. package/dist/{chunk-YHF3QLRL.mjs → theme.js} +94 -72
  32. package/dist/useDynamicToken-Dr15c88t.d.ts +160 -0
  33. package/package.json +62 -14
  34. package/react-native.config.js +29 -0
  35. package/specs/NativeKbachModule.ts +31 -0
  36. package/src/babel-plugin.js +74 -2
  37. package/dist/chunk-SVMYVXY6.mjs +0 -44
  38. package/dist/index.d.mts +0 -148
  39. package/dist/index.mjs +0 -80
  40. package/dist/jsx-dev-runtime.d.mts +0 -19
  41. package/dist/jsx-dev-runtime.mjs +0 -20
  42. package/dist/jsx-runtime.d.mts +0 -27
@@ -1,5 +1,9 @@
1
1
  'use strict';
2
2
 
3
+ const path = require('path');
4
+ const fs = require('fs');
5
+ const template = require('@babel/template').default;
6
+
3
7
  /**
4
8
  * Injects a per-file `@jsxImportSource @kbach/react-native` pragma comment
5
9
  * so Metro's JSX transform routes className resolution through
@@ -19,9 +23,67 @@
19
23
  * Deliberately NOT porting old-kbach's build-time class-resolution half
20
24
  * (the __kbachStyles fast path) — resolution happens at the (already
21
25
  * synchronous) native-bridge call inside the jsx-runtime instead (see
22
- * nativeBridge.ts), so this plugin's only job is the pragma injection.
26
+ * nativeBridge.ts), so this plugin's only job is the pragma injection —
27
+ * PLUS one more, added later: auto-applying `kbach.config.js` if one
28
+ * exists (see `getConfigStatement`'s own doc comment), so a project never
29
+ * needs its own explicit `applyKbachConfig(kbachConfig)` call at all. That
30
+ * addition deliberately still stops short of old-kbach's own version of
31
+ * this (an mtime-polling live-reload of the config, re-checked on every
32
+ * file) — a Babel-plugin-level change needs a dev-server restart to take
33
+ * effect either way, so a one-time check per (root, process) pair is
34
+ * enough, and much simpler to reason about.
35
+ */
36
+
37
+ // Keyed by the root directory checked — undefined = not checked yet for
38
+ // that root, null = checked, no kbach.config.js found there. A Babel/Metro
39
+ // worker process transforms many files over its lifetime — this makes the
40
+ // existsSync + template-build work happen ONCE per root, not on every
41
+ // single file `pre()` runs for.
42
+ const _configStatementCache = new Map();
43
+
44
+ /**
45
+ * Builds the (uncompiled) AST statement that applies `kbach.config.js`,
46
+ * cached per `root` after the first call — `null` if no such file exists
47
+ * there. `root` defaults to `process.cwd()` (how `metro start`/`expo
48
+ * start` are conventionally invoked) at the plugin's own call site below;
49
+ * pass `{ configRoot: '...' }` as this plugin's Babel options if your
50
+ * `kbach.config.js` lives somewhere else (a monorepo where Metro's actual
51
+ * working directory isn't the app's own root, for instance).
52
+ *
53
+ * The injected statement is a plain `require(...).applyKbachConfig(require(...))`
54
+ * — no try/catch around it. `kbach.config.js`'s own path is only ever
55
+ * baked in here after `existsSync` already confirmed it exists, so the
56
+ * only way this could still throw is a genuine bug in that file (a syntax
57
+ * error, a bad nested require) — letting that surface as a real, visible
58
+ * Metro bundling error is far better than silently swallowing it: a
59
+ * developer's real customization would otherwise go missing with zero
60
+ * indication why, exactly the kind of headache a good default should avoid.
61
+ *
62
+ * `@babel/template` builds this from a plain string with the absolute path
63
+ * JSON-escaped directly into it (correct on Windows too, backslashes and
64
+ * all) — simpler than building the equivalent `@babel/types` node graph by
65
+ * hand for one straightforward statement.
23
66
  */
24
- module.exports = function kbachReactNativeBabelPlugin() {
67
+ function getConfigStatement(root) {
68
+ if (_configStatementCache.has(root)) return _configStatementCache.get(root);
69
+
70
+ const configPath = path.join(root, 'kbach.config.js');
71
+ if (!fs.existsSync(configPath)) {
72
+ _configStatementCache.set(root, null);
73
+ return null;
74
+ }
75
+
76
+ const buildStatement = template(
77
+ `require('@kbach/react-native').applyKbachConfig(require(${JSON.stringify(configPath)}));`,
78
+ );
79
+ const statement = buildStatement();
80
+ _configStatementCache.set(root, statement);
81
+ return statement;
82
+ }
83
+
84
+ module.exports = function kbachReactNativeBabelPlugin(_babel, options) {
85
+ const configRoot = (options && options.configRoot) || process.cwd();
86
+
25
87
  return {
26
88
  name: 'kbach-react-native',
27
89
  pre(file) {
@@ -33,7 +95,17 @@ module.exports = function kbachReactNativeBabelPlugin() {
33
95
  if (!alreadySet) {
34
96
  comments.unshift({ type: 'CommentLine', value: ' @jsxImportSource @kbach/react-native' });
35
97
  }
98
+
99
+ const configStatement = getConfigStatement(configRoot);
100
+ if (configStatement) {
101
+ file.ast.program.body.unshift(configStatement);
102
+ }
36
103
  },
37
104
  visitor: {},
38
105
  };
39
106
  };
107
+
108
+ /** Exported for tests only — clears the per-root config-file cache between test cases. */
109
+ module.exports._resetForTests = function resetForTests() {
110
+ _configStatementCache.clear();
111
+ };
@@ -1,44 +0,0 @@
1
- import {
2
- resolveStyle
3
- } from "./chunk-YHF3QLRL.mjs";
4
-
5
- // src/jsx-runtime.tsx
6
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
7
- import { Fragment } from "react";
8
- import { Pressable } from "react-native";
9
- function makeElement(isStaticChildren, type, props, key) {
10
- return (isStaticChildren ? _jsxs : _jsx)(type, props, key);
11
- }
12
- function resolvedStyleFor(classStrRaw, userStyle, pressed) {
13
- const resolved = resolveStyle(classStrRaw, pressed);
14
- if (userStyle === void 0) {
15
- return resolved;
16
- }
17
- if (typeof userStyle === "function") {
18
- return [resolved, userStyle({ pressed })];
19
- }
20
- return [resolved, userStyle];
21
- }
22
- function processElement(type, rawProps, key, isStaticChildren) {
23
- if (type === null || typeof type === "symbol" || !rawProps) {
24
- return makeElement(isStaticChildren, type, rawProps ?? {}, key);
25
- }
26
- const { className: classStrRaw, style: userStyle, ...rest } = rawProps;
27
- if (typeof classStrRaw !== "string") {
28
- return makeElement(isStaticChildren, type, rawProps, key);
29
- }
30
- const finalStyle = type === Pressable ? (state) => resolvedStyleFor(classStrRaw, userStyle, state.pressed) : resolvedStyleFor(classStrRaw, userStyle, false);
31
- return makeElement(isStaticChildren, type, { ...rest, style: finalStyle }, key);
32
- }
33
- function jsx(type, props, key) {
34
- return processElement(type, props, key, false);
35
- }
36
- function jsxs(type, props, key) {
37
- return processElement(type, props, key, true);
38
- }
39
-
40
- export {
41
- Fragment,
42
- jsx,
43
- jsxs
44
- };
package/dist/index.d.mts DELETED
@@ -1,148 +0,0 @@
1
- import * as react from 'react';
2
- import { ReactNode } from 'react';
3
-
4
- interface ThemeConfig {
5
- /** Plain hex only, this pass — e.g. "blue-6" -> "#2563eb" */
6
- colors: Record<string, string>;
7
- spacing: Record<string, number>;
8
- screens: Record<string, number>;
9
- darkMode: 'attribute' | 'class' | 'media';
10
- }
11
- declare const defaultTheme: ThemeConfig;
12
- declare function setTheme(theme: ThemeConfig): void;
13
- declare function getTheme(): ThemeConfig;
14
-
15
- interface StyleObject {
16
- [key: string]: string | number;
17
- }
18
- /**
19
- * Calls the Rust engine via the KbachModule TurboModule (see
20
- * apps/native-sandbox/android/.../KbachModule.kt and
21
- * packages/core-engine/src/resolve_style.rs) and returns a plain style
22
- * object ready for RN's `style` prop. Synchronous — no async init step,
23
- * unlike the web/WASM path, since JSI-backed TurboModule methods are
24
- * available as soon as the module is loaded.
25
- *
26
- * Reads `getGlobalDarkMode()` (darkModeStore.ts) fresh on every call (no
27
- * caching) so `dark:` resolves correctly whenever this runs. That store —
28
- * not a direct `Appearance.getColorScheme()` read, which this function
29
- * used prior to `<ThemeProvider>` support — is what makes an explicit
30
- * `setGlobalThemeMode('dark')`/`toggle()` override actually affect
31
- * rendering; the store itself falls back to tracking `Appearance` live
32
- * whenever the mode is `'system'` (its own default), so nothing regresses
33
- * for apps that never touch the override API at all. This function itself
34
- * has no subscription of its own, so nothing re-renders just because the
35
- * store's value changed — call `useGlobalDarkMode()` (or consume
36
- * `useTheme()`'s `isDark`) somewhere in the tree for that, NOT React
37
- * Native's own `useColorScheme()`, which only reflects the raw OS setting
38
- * and would miss an explicit override entirely.
39
- *
40
- * Phase 11 originally shipped with live in-place theme switching unverified
41
- * (a `uiMode` toggle triggered a re-render but `Appearance.getColorScheme()`
42
- * kept returning a stale value). Root-caused in Phase 12 while debugging an
43
- * unrelated `active:` failure: `apps/native-sandbox` has its own separate
44
- * `node_modules/react-native` (excluded from the npm workspace since Phase
45
- * 5), while `packages/react-native` resolved `react-native` hierarchically
46
- * up to a *different* copy at the repo root — two physical module
47
- * instances, so a `useColorScheme()` subscription and an
48
- * `Appearance.getColorScheme()` read were silently talking to two separate
49
- * copies of the Appearance module. Fixed by forcing a single instance via
50
- * `resolver.resolveRequest` in apps/native-sandbox/metro.config.js; live
51
- * theme switching (and `active:`, the bug that surfaced this) both verified
52
- * working after the fix — see Phase 12. Still relevant here: darkModeStore.ts
53
- * itself calls `Appearance.addChangeListener`, so the same dual-instance
54
- * failure mode would silently break the 'system' fallback path too if that
55
- * metro.config.js fix ever regressed.
56
- *
57
- * `pressed` gates the `active:` modifier — unlike `colorScheme`, there's no
58
- * system-level getter for "is this element currently pressed"; only RN's
59
- * `Pressable` ever knows this, so it's passed in directly by the caller
60
- * (jsx-runtime, from Pressable's own style-function callback) rather than
61
- * queried here. Defaults to `false` for every other element type.
62
- */
63
- declare function resolveStyle(classString: string, pressed?: boolean): StyleObject;
64
-
65
- /**
66
- * Native port of @kbach/react's darkModeStore.ts — same architecture (a
67
- * single module-level store is the real source of truth;
68
- * ThemeProvider/useTheme are a thin, OPTIONAL wrapper around it; every
69
- * function here works standalone with no provider mounted anywhere), with
70
- * two real platform differences from the web version:
71
- *
72
- * 1. No persistence. There's no React-Native-builtin equivalent of
73
- * localStorage — adding one would mean forcing a real native dependency
74
- * (e.g. @react-native-async-storage/async-storage) on every consumer,
75
- * even ones that don't want it. The mode always starts at 'system' on a
76
- * fresh app launch. If you want persistence, read your own storage at
77
- * startup (before your first render) and call setGlobalThemeMode() with
78
- * the result — same "seed once, no built-in write-back" shape as
79
- * seedDefaultMode() below, just driven by your own storage instead of
80
- * one this package picks for you.
81
- * 2. No DOM to "apply" the resolved mode to. On web, applyToDom() flips a
82
- * class/attribute the generated CSS's `dark:` selectors match against.
83
- * On native there's no CSS at all — nativeBridge.ts's resolveStyle()
84
- * reads getGlobalDarkMode() directly on every call instead of querying
85
- * Appearance.getColorScheme() itself, which is what actually makes an
86
- * explicit override affect rendering (previously that function always
87
- * read the raw OS setting, so it had no way to reflect a user choice —
88
- * see nativeBridge.ts's own comment on this for why that's the one
89
- * genuinely fixed bug this change introduces, not just a new feature).
90
- */
91
- type ThemeMode = 'light' | 'dark' | 'system';
92
- declare function getGlobalDarkMode(): boolean;
93
- declare function getGlobalThemeMode(): ThemeMode;
94
- /** Explicit user choice — takes priority over any later `<ThemeProvider defaultMode>` seed (see `seedDefaultMode` below). Not persisted; see this module's own doc comment. */
95
- declare function setGlobalThemeMode(nextMode: ThemeMode): void;
96
- declare function toggleGlobalDarkMode(): void;
97
- declare function subscribeGlobalDarkMode(listener: () => void): () => void;
98
-
99
- /**
100
- * Reactive read of the global dark-mode store — works with or without a
101
- * `<ThemeProvider>` mounted anywhere in the tree, since it subscribes to
102
- * `darkModeStore.ts` directly. No server-snapshot argument (unlike
103
- * @kbach/react's web version) — React Native has no SSR concept at all,
104
- * so there's no third argument for `useSyncExternalStore` to need.
105
- */
106
- declare function useGlobalDarkMode(): boolean;
107
-
108
- interface ThemeProviderProps {
109
- children: ReactNode;
110
- /**
111
- * Applied once, on this provider's first mount, but ONLY if nothing has
112
- * made an explicit mode choice yet (see
113
- * `darkModeStore.ts::seedDefaultMode`) — an explicit prior choice always
114
- * wins over a component's own default. Unlike @kbach/react's web
115
- * version, there's no persisted value to check here — see
116
- * darkModeStore.ts's own doc comment on why this package doesn't persist
117
- * the mode across app launches.
118
- */
119
- defaultMode?: ThemeMode;
120
- }
121
- /**
122
- * Native port of @kbach/react's `ThemeProvider` — a thin, OPTIONAL React
123
- * Context wrapper around `darkModeStore.ts`'s single global store, not the
124
- * source of truth itself. Because the store is global, MULTIPLE mounted
125
- * providers share the exact same `mode`/`isDark` state rather than each
126
- * getting their own scoped value — mounting more than one is very likely a
127
- * mistake (each fighting over the same state via a different
128
- * `defaultMode`), so this warns once in dev, matching the web version's
129
- * identical guard.
130
- */
131
- declare function ThemeProvider({ children, defaultMode }: ThemeProviderProps): react.JSX.Element;
132
-
133
- interface ThemeContextValue {
134
- mode: ThemeMode;
135
- isDark: boolean;
136
- setMode: (mode: ThemeMode) => void;
137
- toggle: () => void;
138
- }
139
- /**
140
- * Requires a `<ThemeProvider>` ancestor — throws otherwise, since a
141
- * component that destructures `{ mode, setMode }` genuinely needs Context
142
- * (unlike a component that just wants the dark/light boolean, which should
143
- * reach for `useGlobalDarkMode()` instead and never needs a provider at
144
- * all).
145
- */
146
- declare function useTheme(): ThemeContextValue;
147
-
148
- export { type StyleObject, type ThemeConfig, type ThemeContextValue, type ThemeMode, ThemeProvider, type ThemeProviderProps, defaultTheme, getGlobalDarkMode, getGlobalThemeMode, getTheme, resolveStyle, setGlobalThemeMode, setTheme, subscribeGlobalDarkMode, toggleGlobalDarkMode, useGlobalDarkMode, useTheme };
package/dist/index.mjs DELETED
@@ -1,80 +0,0 @@
1
- import {
2
- defaultTheme,
3
- getGlobalDarkMode,
4
- getGlobalThemeMode,
5
- getTheme,
6
- resolveStyle,
7
- seedDefaultMode,
8
- setGlobalThemeMode,
9
- setTheme,
10
- subscribeGlobalDarkMode,
11
- toggleGlobalDarkMode
12
- } from "./chunk-YHF3QLRL.mjs";
13
-
14
- // src/useGlobalDarkMode.ts
15
- import { useSyncExternalStore } from "react";
16
- function useGlobalDarkMode() {
17
- return useSyncExternalStore(subscribeGlobalDarkMode, getGlobalDarkMode);
18
- }
19
-
20
- // src/ThemeProvider.tsx
21
- import { useEffect, useMemo, useRef, useSyncExternalStore as useSyncExternalStore2 } from "react";
22
-
23
- // src/ThemeContext.tsx
24
- import { createContext, useContext } from "react";
25
- var ThemeContext = createContext(null);
26
- function useTheme() {
27
- const ctx = useContext(ThemeContext);
28
- if (!ctx) {
29
- throw new Error(
30
- 'useTheme() must be called within a <ThemeProvider>. For dark-mode state without a provider, use useGlobalDarkMode() (read) or toggleGlobalDarkMode()/setGlobalThemeMode() (write) from "@kbach/react-native" instead.'
31
- );
32
- }
33
- return ctx;
34
- }
35
-
36
- // src/ThemeProvider.tsx
37
- import { jsx } from "react/jsx-runtime";
38
- var mountedProviderCount = 0;
39
- var warnedMultipleProviders = false;
40
- function ThemeProvider({ children, defaultMode = "system" }) {
41
- useEffect(() => {
42
- mountedProviderCount++;
43
- if (mountedProviderCount > 1 && !warnedMultipleProviders) {
44
- warnedMultipleProviders = true;
45
- console.warn(
46
- "[kbach] Multiple <ThemeProvider> instances are mounted at once. Dark-mode state is one global store (the same one useGlobalDarkMode()/toggleGlobalDarkMode() read and write), not scoped per-provider, so they will fight over the same state instead of each getting their own."
47
- );
48
- }
49
- return () => {
50
- mountedProviderCount--;
51
- };
52
- }, []);
53
- const seeded = useRef(false);
54
- useEffect(() => {
55
- if (seeded.current) return;
56
- seeded.current = true;
57
- seedDefaultMode(defaultMode);
58
- }, []);
59
- const isDark = useSyncExternalStore2(subscribeGlobalDarkMode, getGlobalDarkMode);
60
- const mode = useSyncExternalStore2(subscribeGlobalDarkMode, getGlobalThemeMode);
61
- const value = useMemo(
62
- () => ({ mode, isDark, setMode: setGlobalThemeMode, toggle: toggleGlobalDarkMode }),
63
- [mode, isDark]
64
- );
65
- return /* @__PURE__ */ jsx(ThemeContext.Provider, { value, children });
66
- }
67
- export {
68
- ThemeProvider,
69
- defaultTheme,
70
- getGlobalDarkMode,
71
- getGlobalThemeMode,
72
- getTheme,
73
- resolveStyle,
74
- setGlobalThemeMode,
75
- setTheme,
76
- subscribeGlobalDarkMode,
77
- toggleGlobalDarkMode,
78
- useGlobalDarkMode,
79
- useTheme
80
- };
@@ -1,19 +0,0 @@
1
- import { ReactElement } from 'react';
2
- export { Fragment } from 'react';
3
-
4
- /**
5
- * @kbach/react-native/jsx-dev-runtime
6
- *
7
- * Development variant — Metro calls jsxDEV instead of jsx/jsxs in dev
8
- * builds. Same pattern as @kbach/react/jsx-dev-runtime (Phase 3): reuses
9
- * jsx/jsxs for the actual interception, then patches _source/_self onto
10
- * the result so React DevTools shows the original file/line.
11
- */
12
-
13
- declare function jsxDEV(type: unknown, props: Record<string, unknown> | null, key?: string, isStaticChildren?: boolean, source?: {
14
- fileName: string;
15
- lineNumber: number;
16
- columnNumber: number;
17
- }, self?: unknown): ReactElement;
18
-
19
- export { jsxDEV };
@@ -1,20 +0,0 @@
1
- import {
2
- Fragment,
3
- jsx,
4
- jsxs
5
- } from "./chunk-SVMYVXY6.mjs";
6
- import "./chunk-YHF3QLRL.mjs";
7
-
8
- // src/jsx-dev-runtime.tsx
9
- function jsxDEV(type, props, key, isStaticChildren, source, self) {
10
- const element = (isStaticChildren ? jsxs : jsx)(type, props, key);
11
- if (source && element && typeof element === "object" && Object.isExtensible(element)) {
12
- element._source = source;
13
- element._self = self;
14
- }
15
- return element;
16
- }
17
- export {
18
- Fragment,
19
- jsxDEV
20
- };
@@ -1,27 +0,0 @@
1
- import { ReactElement } from 'react';
2
- export { Fragment, JSX } from 'react';
3
-
4
- /**
5
- * @kbach/react-native/jsx-runtime
6
- *
7
- * Drop-in replacement for react/jsx-runtime — same interception shape as
8
- * @kbach/react/jsx-runtime (Phase 3), but the resolved value becomes the
9
- * `style` prop instead of `className`: View/Text have no `className` prop
10
- * at all. Covers layout, color, spacing, border, typography, dark:, and
11
- * active: (Pressable only) — see nativeBridge.ts / resolve_style.rs for
12
- * exactly what's resolved; every other modifier (hover:/responsive/...)
13
- * parses but isn't applied yet.
14
- *
15
- * `active:` needs different handling from every other modifier: whether an
16
- * element is "pressed" isn't a value that can be read once and reused like
17
- * `dark:`'s color scheme — only RN's `Pressable` ever knows it, and only
18
- * via a style FUNCTION it calls on every press/release
19
- * (`style={({pressed}) => ...}`). So for `type === Pressable` specifically,
20
- * `style` is built as a function instead of a static value; every other
21
- * element type is completely unaffected by this — same code path as before.
22
- */
23
-
24
- declare function jsx(type: unknown, props: Record<string, unknown> | null, key?: string): ReactElement;
25
- declare function jsxs(type: unknown, props: Record<string, unknown> | null, key?: string): ReactElement;
26
-
27
- export { jsx, jsxs };