rnwind 0.0.1 → 0.0.3
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/cjs/core/parser/animation.cjs +427 -0
- package/lib/cjs/core/parser/animation.cjs.map +1 -0
- package/lib/cjs/core/parser/animation.d.ts +126 -0
- package/lib/cjs/core/parser/border-dispatcher.cjs +180 -0
- package/lib/cjs/core/parser/border-dispatcher.cjs.map +1 -0
- package/lib/cjs/core/parser/border-dispatcher.d.ts +15 -0
- package/lib/cjs/core/parser/case-convert.cjs +15 -0
- package/lib/cjs/core/parser/case-convert.cjs.map +1 -0
- package/lib/cjs/core/parser/case-convert.d.ts +6 -0
- package/lib/cjs/core/parser/color-properties-dispatcher.cjs +84 -0
- package/lib/cjs/core/parser/color-properties-dispatcher.cjs.map +1 -0
- package/lib/cjs/core/parser/color-properties-dispatcher.d.ts +19 -0
- package/lib/cjs/core/parser/color.cjs +193 -0
- package/lib/cjs/core/parser/color.cjs.map +1 -0
- package/lib/cjs/core/parser/color.d.ts +12 -0
- package/lib/cjs/core/parser/constants.cjs +21 -0
- package/lib/cjs/core/parser/constants.cjs.map +1 -0
- package/lib/cjs/core/parser/constants.d.ts +8 -0
- package/lib/cjs/core/parser/declaration.cjs +347 -0
- package/lib/cjs/core/parser/declaration.cjs.map +1 -0
- package/lib/cjs/core/parser/declaration.d.ts +15 -0
- package/lib/cjs/core/parser/gradient.cjs +132 -0
- package/lib/cjs/core/parser/gradient.cjs.map +1 -0
- package/lib/cjs/core/parser/gradient.d.ts +59 -0
- package/lib/cjs/core/parser/haptics.cjs +73 -0
- package/lib/cjs/core/parser/haptics.cjs.map +1 -0
- package/lib/cjs/core/parser/haptics.d.ts +47 -0
- package/lib/cjs/core/parser/index.d.ts +8 -0
- package/lib/cjs/core/parser/keyframes.cjs +95 -0
- package/lib/cjs/core/parser/keyframes.cjs.map +1 -0
- package/lib/cjs/core/parser/keyframes.d.ts +26 -0
- package/lib/cjs/core/parser/layout-dispatcher.cjs +120 -0
- package/lib/cjs/core/parser/layout-dispatcher.cjs.map +1 -0
- package/lib/cjs/core/parser/layout-dispatcher.d.ts +14 -0
- package/lib/cjs/core/parser/length.cjs +110 -0
- package/lib/cjs/core/parser/length.cjs.map +1 -0
- package/lib/cjs/core/parser/length.d.ts +51 -0
- package/lib/cjs/core/parser/motion-dispatcher.cjs +77 -0
- package/lib/cjs/core/parser/motion-dispatcher.cjs.map +1 -0
- package/lib/cjs/core/parser/motion-dispatcher.d.ts +11 -0
- package/lib/cjs/core/parser/property.cjs +22 -0
- package/lib/cjs/core/parser/property.cjs.map +1 -0
- package/lib/cjs/core/parser/property.d.ts +8 -0
- package/lib/cjs/core/parser/safe-area.cjs +404 -0
- package/lib/cjs/core/parser/safe-area.cjs.map +1 -0
- package/lib/cjs/core/parser/safe-area.d.ts +39 -0
- package/lib/cjs/core/parser/selector.cjs +22 -0
- package/lib/cjs/core/parser/selector.cjs.map +1 -0
- package/lib/cjs/core/parser/selector.d.ts +11 -0
- package/lib/cjs/core/parser/shorthand.cjs +188 -0
- package/lib/cjs/core/parser/shorthand.cjs.map +1 -0
- package/lib/cjs/core/parser/shorthand.d.ts +67 -0
- package/lib/cjs/core/parser/text-truncate.cjs +78 -0
- package/lib/cjs/core/parser/text-truncate.cjs.map +1 -0
- package/lib/cjs/core/parser/text-truncate.d.ts +44 -0
- package/lib/cjs/core/parser/theme-vars.cjs +467 -0
- package/lib/cjs/core/parser/theme-vars.cjs.map +1 -0
- package/lib/cjs/core/parser/theme-vars.d.ts +82 -0
- package/lib/cjs/core/parser/tokens.cjs +486 -0
- package/lib/cjs/core/parser/tokens.cjs.map +1 -0
- package/lib/cjs/core/parser/tokens.d.ts +45 -0
- package/lib/cjs/core/parser/transform.cjs +198 -0
- package/lib/cjs/core/parser/transform.cjs.map +1 -0
- package/lib/cjs/core/parser/transform.d.ts +36 -0
- package/lib/cjs/core/parser/tw-parser.cjs +1680 -0
- package/lib/cjs/core/parser/tw-parser.cjs.map +1 -0
- package/lib/cjs/core/parser/tw-parser.d.ts +210 -0
- package/lib/cjs/core/parser/types.d.ts +37 -0
- package/lib/cjs/core/parser/typography-dispatcher.cjs +108 -0
- package/lib/cjs/core/parser/typography-dispatcher.cjs.map +1 -0
- package/lib/cjs/core/parser/typography-dispatcher.d.ts +11 -0
- package/lib/cjs/core/parser/typography.cjs +97 -0
- package/lib/cjs/core/parser/typography.cjs.map +1 -0
- package/lib/cjs/core/parser/typography.d.ts +43 -0
- package/lib/cjs/core/style-builder/build-style.cjs +444 -0
- package/lib/cjs/core/style-builder/build-style.cjs.map +1 -0
- package/lib/cjs/core/style-builder/build-style.d.ts +54 -0
- package/lib/cjs/core/style-builder/index.d.ts +3 -0
- package/lib/cjs/core/style-builder/union-builder.cjs +326 -0
- package/lib/cjs/core/style-builder/union-builder.cjs.map +1 -0
- package/lib/cjs/core/style-builder/union-builder.d.ts +128 -0
- package/lib/cjs/core/types.d.ts +14 -0
- package/lib/cjs/metro/dts.cjs +127 -0
- package/lib/cjs/metro/dts.cjs.map +1 -0
- package/lib/cjs/metro/dts.d.ts +16 -0
- package/lib/cjs/metro/index.cjs +19 -0
- package/lib/cjs/metro/index.cjs.map +1 -0
- package/lib/cjs/metro/index.d.ts +9 -0
- package/lib/cjs/metro/resolver.cjs +47 -0
- package/lib/cjs/metro/resolver.cjs.map +1 -0
- package/lib/cjs/metro/resolver.d.ts +22 -0
- package/lib/cjs/metro/state.cjs +301 -0
- package/lib/cjs/metro/state.cjs.map +1 -0
- package/lib/cjs/metro/state.d.ts +88 -0
- package/lib/cjs/metro/transform-ast.cjs +1472 -0
- package/lib/cjs/metro/transform-ast.cjs.map +1 -0
- package/lib/cjs/metro/transform-ast.d.ts +88 -0
- package/lib/cjs/metro/transformer.cjs +372 -0
- package/lib/cjs/metro/transformer.cjs.map +1 -0
- package/lib/cjs/metro/transformer.d.ts +47 -0
- package/lib/cjs/metro/warn-unknown-classes.cjs +86 -0
- package/lib/cjs/metro/warn-unknown-classes.cjs.map +1 -0
- package/lib/cjs/metro/warn-unknown-classes.d.ts +21 -0
- package/lib/cjs/metro/with-config.cjs +196 -0
- package/lib/cjs/metro/with-config.cjs.map +1 -0
- package/lib/cjs/metro/with-config.d.ts +79 -0
- package/lib/cjs/runtime/chain-handlers.cjs +37 -0
- package/lib/cjs/runtime/chain-handlers.cjs.map +1 -0
- package/lib/cjs/runtime/chain-handlers.d.ts +33 -0
- package/lib/cjs/runtime/components/rnwind-provider.cjs +98 -0
- package/lib/cjs/runtime/components/rnwind-provider.cjs.map +1 -0
- package/lib/cjs/runtime/components/rnwind-provider.d.ts +84 -0
- package/lib/cjs/runtime/gradient-types.d.ts +58 -0
- package/lib/cjs/runtime/haptics.cjs +113 -0
- package/lib/cjs/runtime/haptics.cjs.map +1 -0
- package/lib/cjs/runtime/haptics.d.ts +48 -0
- package/lib/cjs/runtime/hooks/use-css.cjs +21 -0
- package/lib/cjs/runtime/hooks/use-css.cjs.map +1 -0
- package/lib/cjs/runtime/hooks/use-css.d.ts +11 -0
- package/lib/cjs/runtime/hooks/use-interact.cjs +46 -0
- package/lib/cjs/runtime/hooks/use-interact.cjs.map +1 -0
- package/lib/cjs/runtime/hooks/use-interact.d.ts +42 -0
- package/lib/cjs/runtime/hooks/use-scheme.cjs +68 -0
- package/lib/cjs/runtime/hooks/use-scheme.cjs.map +1 -0
- package/lib/cjs/runtime/hooks/use-scheme.d.ts +34 -0
- package/lib/cjs/runtime/index.cjs +45 -0
- package/lib/cjs/runtime/index.cjs.map +1 -0
- package/lib/cjs/runtime/index.d.ts +27 -0
- package/lib/cjs/runtime/interactive-box.cjs +35 -0
- package/lib/cjs/runtime/interactive-box.cjs.map +1 -0
- package/lib/cjs/runtime/interactive-box.d.ts +40 -0
- package/lib/cjs/runtime/lookup-css.cjs +542 -0
- package/lib/cjs/runtime/lookup-css.cjs.map +1 -0
- package/lib/cjs/runtime/lookup-css.d.ts +164 -0
- package/lib/cjs/runtime/types.d.ts +29 -0
- package/lib/cjs/testing/index.cjs +367 -0
- package/lib/cjs/testing/index.cjs.map +1 -0
- package/lib/cjs/testing/index.d.ts +145 -0
- package/lib/esm/core/parser/animation.d.ts +126 -0
- package/lib/esm/core/parser/animation.mjs +408 -0
- package/lib/esm/core/parser/animation.mjs.map +1 -0
- package/lib/esm/core/parser/border-dispatcher.d.ts +15 -0
- package/lib/esm/core/parser/border-dispatcher.mjs +178 -0
- package/lib/esm/core/parser/border-dispatcher.mjs.map +1 -0
- package/lib/esm/core/parser/case-convert.d.ts +6 -0
- package/lib/esm/core/parser/case-convert.mjs +13 -0
- package/lib/esm/core/parser/case-convert.mjs.map +1 -0
- package/lib/esm/core/parser/color-properties-dispatcher.d.ts +19 -0
- package/lib/esm/core/parser/color-properties-dispatcher.mjs +82 -0
- package/lib/esm/core/parser/color-properties-dispatcher.mjs.map +1 -0
- package/lib/esm/core/parser/color.d.ts +12 -0
- package/lib/esm/core/parser/color.mjs +191 -0
- package/lib/esm/core/parser/color.mjs.map +1 -0
- package/lib/esm/core/parser/constants.d.ts +8 -0
- package/lib/esm/core/parser/constants.mjs +13 -0
- package/lib/esm/core/parser/constants.mjs.map +1 -0
- package/lib/esm/core/parser/declaration.d.ts +15 -0
- package/lib/esm/core/parser/declaration.mjs +345 -0
- package/lib/esm/core/parser/declaration.mjs.map +1 -0
- package/lib/esm/core/parser/gradient.d.ts +59 -0
- package/lib/esm/core/parser/gradient.mjs +130 -0
- package/lib/esm/core/parser/gradient.mjs.map +1 -0
- package/lib/esm/core/parser/haptics.d.ts +47 -0
- package/lib/esm/core/parser/haptics.mjs +71 -0
- package/lib/esm/core/parser/haptics.mjs.map +1 -0
- package/lib/esm/core/parser/index.d.ts +8 -0
- package/lib/esm/core/parser/keyframes.d.ts +26 -0
- package/lib/esm/core/parser/keyframes.mjs +91 -0
- package/lib/esm/core/parser/keyframes.mjs.map +1 -0
- package/lib/esm/core/parser/layout-dispatcher.d.ts +14 -0
- package/lib/esm/core/parser/layout-dispatcher.mjs +118 -0
- package/lib/esm/core/parser/layout-dispatcher.mjs.map +1 -0
- package/lib/esm/core/parser/length.d.ts +51 -0
- package/lib/esm/core/parser/length.mjs +104 -0
- package/lib/esm/core/parser/length.mjs.map +1 -0
- package/lib/esm/core/parser/motion-dispatcher.d.ts +11 -0
- package/lib/esm/core/parser/motion-dispatcher.mjs +75 -0
- package/lib/esm/core/parser/motion-dispatcher.mjs.map +1 -0
- package/lib/esm/core/parser/property.d.ts +8 -0
- package/lib/esm/core/parser/property.mjs +20 -0
- package/lib/esm/core/parser/property.mjs.map +1 -0
- package/lib/esm/core/parser/safe-area.d.ts +39 -0
- package/lib/esm/core/parser/safe-area.mjs +402 -0
- package/lib/esm/core/parser/safe-area.mjs.map +1 -0
- package/lib/esm/core/parser/selector.d.ts +11 -0
- package/lib/esm/core/parser/selector.mjs +20 -0
- package/lib/esm/core/parser/selector.mjs.map +1 -0
- package/lib/esm/core/parser/shorthand.d.ts +67 -0
- package/lib/esm/core/parser/shorthand.mjs +180 -0
- package/lib/esm/core/parser/shorthand.mjs.map +1 -0
- package/lib/esm/core/parser/text-truncate.d.ts +44 -0
- package/lib/esm/core/parser/text-truncate.mjs +75 -0
- package/lib/esm/core/parser/text-truncate.mjs.map +1 -0
- package/lib/esm/core/parser/theme-vars.d.ts +82 -0
- package/lib/esm/core/parser/theme-vars.mjs +461 -0
- package/lib/esm/core/parser/theme-vars.mjs.map +1 -0
- package/lib/esm/core/parser/tokens.d.ts +45 -0
- package/lib/esm/core/parser/tokens.mjs +480 -0
- package/lib/esm/core/parser/tokens.mjs.map +1 -0
- package/lib/esm/core/parser/transform.d.ts +36 -0
- package/lib/esm/core/parser/transform.mjs +193 -0
- package/lib/esm/core/parser/transform.mjs.map +1 -0
- package/lib/esm/core/parser/tw-parser.d.ts +210 -0
- package/lib/esm/core/parser/tw-parser.mjs +1678 -0
- package/lib/esm/core/parser/tw-parser.mjs.map +1 -0
- package/lib/esm/core/parser/types.d.ts +37 -0
- package/lib/esm/core/parser/typography-dispatcher.d.ts +11 -0
- package/lib/esm/core/parser/typography-dispatcher.mjs +106 -0
- package/lib/esm/core/parser/typography-dispatcher.mjs.map +1 -0
- package/lib/esm/core/parser/typography.d.ts +43 -0
- package/lib/esm/core/parser/typography.mjs +91 -0
- package/lib/esm/core/parser/typography.mjs.map +1 -0
- package/lib/esm/core/style-builder/build-style.d.ts +54 -0
- package/lib/esm/core/style-builder/build-style.mjs +442 -0
- package/lib/esm/core/style-builder/build-style.mjs.map +1 -0
- package/lib/esm/core/style-builder/index.d.ts +3 -0
- package/lib/esm/core/style-builder/union-builder.d.ts +128 -0
- package/lib/esm/core/style-builder/union-builder.mjs +324 -0
- package/lib/esm/core/style-builder/union-builder.mjs.map +1 -0
- package/lib/esm/core/types.d.ts +14 -0
- package/lib/esm/metro/dts.d.ts +16 -0
- package/lib/esm/metro/dts.mjs +125 -0
- package/lib/esm/metro/dts.mjs.map +1 -0
- package/lib/esm/metro/index.d.ts +9 -0
- package/lib/esm/metro/index.mjs +6 -0
- package/lib/esm/metro/index.mjs.map +1 -0
- package/lib/esm/metro/resolver.d.ts +22 -0
- package/lib/esm/metro/resolver.mjs +43 -0
- package/lib/esm/metro/resolver.mjs.map +1 -0
- package/lib/esm/metro/state.d.ts +88 -0
- package/lib/esm/metro/state.mjs +291 -0
- package/lib/esm/metro/state.mjs.map +1 -0
- package/lib/esm/metro/transform-ast.d.ts +88 -0
- package/lib/esm/metro/transform-ast.mjs +1451 -0
- package/lib/esm/metro/transform-ast.mjs.map +1 -0
- package/lib/esm/metro/transformer.d.ts +47 -0
- package/lib/esm/metro/transformer.mjs +349 -0
- package/lib/esm/metro/transformer.mjs.map +1 -0
- package/lib/esm/metro/warn-unknown-classes.d.ts +21 -0
- package/lib/esm/metro/warn-unknown-classes.mjs +84 -0
- package/lib/esm/metro/warn-unknown-classes.mjs.map +1 -0
- package/lib/esm/metro/with-config.d.ts +79 -0
- package/lib/esm/metro/with-config.mjs +194 -0
- package/lib/esm/metro/with-config.mjs.map +1 -0
- package/lib/esm/runtime/chain-handlers.d.ts +33 -0
- package/lib/esm/runtime/chain-handlers.mjs +34 -0
- package/lib/esm/runtime/chain-handlers.mjs.map +1 -0
- package/lib/esm/runtime/components/rnwind-provider.d.ts +84 -0
- package/lib/esm/runtime/components/rnwind-provider.mjs +94 -0
- package/lib/esm/runtime/components/rnwind-provider.mjs.map +1 -0
- package/lib/esm/runtime/gradient-types.d.ts +58 -0
- package/lib/esm/runtime/haptics.d.ts +48 -0
- package/lib/esm/runtime/haptics.mjs +110 -0
- package/lib/esm/runtime/haptics.mjs.map +1 -0
- package/lib/esm/runtime/hooks/use-css.d.ts +11 -0
- package/lib/esm/runtime/hooks/use-css.mjs +19 -0
- package/lib/esm/runtime/hooks/use-css.mjs.map +1 -0
- package/lib/esm/runtime/hooks/use-interact.d.ts +42 -0
- package/lib/esm/runtime/hooks/use-interact.mjs +44 -0
- package/lib/esm/runtime/hooks/use-interact.mjs.map +1 -0
- package/lib/esm/runtime/hooks/use-scheme.d.ts +34 -0
- package/lib/esm/runtime/hooks/use-scheme.mjs +63 -0
- package/lib/esm/runtime/hooks/use-scheme.mjs.map +1 -0
- package/lib/esm/runtime/index.d.ts +27 -0
- package/lib/esm/runtime/index.mjs +18 -0
- package/lib/esm/runtime/index.mjs.map +1 -0
- package/lib/esm/runtime/interactive-box.d.ts +40 -0
- package/lib/esm/runtime/interactive-box.mjs +33 -0
- package/lib/esm/runtime/interactive-box.mjs.map +1 -0
- package/lib/esm/runtime/lookup-css.d.ts +164 -0
- package/lib/esm/runtime/lookup-css.mjs +531 -0
- package/lib/esm/runtime/lookup-css.mjs.map +1 -0
- package/lib/esm/runtime/types.d.ts +29 -0
- package/lib/esm/testing/index.d.ts +145 -0
- package/lib/esm/testing/index.mjs +344 -0
- package/lib/esm/testing/index.mjs.map +1 -0
- package/package.json +80 -13
- package/preset.css +1171 -0
- package/src/core/parser/animation.ts +404 -0
- package/src/core/parser/border-dispatcher.ts +176 -0
- package/src/core/parser/case-convert.ts +10 -0
- package/src/core/parser/color-properties-dispatcher.ts +78 -0
- package/src/core/parser/color.ts +191 -0
- package/src/core/parser/constants.ts +11 -0
- package/src/core/parser/declaration.ts +340 -0
- package/src/core/parser/gradient.ts +148 -0
- package/src/core/parser/haptics.ts +88 -0
- package/src/core/parser/index.ts +8 -0
- package/src/core/parser/keyframes.ts +84 -0
- package/src/core/parser/layout-dispatcher.ts +111 -0
- package/src/core/parser/length.ts +114 -0
- package/src/core/parser/motion-dispatcher.ts +89 -0
- package/src/core/parser/property.ts +15 -0
- package/src/core/parser/safe-area.ts +404 -0
- package/src/core/parser/selector.ts +17 -0
- package/src/core/parser/shorthand.ts +182 -0
- package/src/core/parser/text-truncate.ts +79 -0
- package/src/core/parser/theme-vars.ts +465 -0
- package/src/core/parser/tokens.ts +456 -0
- package/src/core/parser/transform.ts +195 -0
- package/src/core/parser/tw-parser.ts +1828 -0
- package/src/core/parser/types.ts +45 -0
- package/src/core/parser/typography-dispatcher.ts +97 -0
- package/src/core/parser/typography.ts +83 -0
- package/src/core/style-builder/build-style.ts +500 -0
- package/src/core/style-builder/index.ts +3 -0
- package/src/core/style-builder/union-builder.ts +328 -0
- package/src/core/types.ts +15 -0
- package/src/metro/dts.ts +128 -0
- package/src/metro/index.ts +9 -0
- package/src/metro/resolver.ts +42 -0
- package/src/metro/state.ts +305 -0
- package/src/metro/transform-ast.ts +1729 -0
- package/src/metro/transformer.ts +372 -0
- package/src/metro/warn-unknown-classes.ts +79 -0
- package/src/metro/with-config.ts +251 -0
- package/src/runtime/chain-handlers.ts +47 -0
- package/src/runtime/components/rnwind-provider.tsx +144 -0
- package/src/runtime/gradient-types.ts +60 -0
- package/src/runtime/haptics.ts +120 -0
- package/src/runtime/hooks/use-css.ts +16 -0
- package/src/runtime/hooks/use-interact.ts +65 -0
- package/src/runtime/hooks/use-scheme.ts +63 -0
- package/src/runtime/index.ts +54 -0
- package/src/runtime/interactive-box.tsx +57 -0
- package/src/runtime/lookup-css.ts +628 -0
- package/src/runtime/types.ts +32 -0
- package/src/testing/index.ts +507 -0
- package/src/types/tailwindcss-node.d.ts +33 -0
- package/src/index.ts +0 -1
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, utimesSync, watch as watchFile } from 'node:fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
import { writeDtsFile } from './dts'
|
|
4
|
+
import { createRnwindResolver, type ResolveRequestFn } from './resolver'
|
|
5
|
+
import { configureRnwindState, getRnwindState, onThemeChange } from './state'
|
|
6
|
+
|
|
7
|
+
/** Default cache directory at the project root. Visible for debugging. */
|
|
8
|
+
const DEFAULT_CACHE_DIR = '.rnwind'
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Active CSS watcher — replaced (and the prior one closed) when
|
|
12
|
+
* `withRnwindConfig` is called again (Metro restart, repeated init).
|
|
13
|
+
* Only one watcher per process; no stacking.
|
|
14
|
+
*/
|
|
15
|
+
let activeCssWatcher: { cssPath: string; close: () => void } | null = null
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Watch the theme CSS for edits. On change, rewrite the per-scheme
|
|
19
|
+
* files with the fresh theme AND bump `mtime` on every source file
|
|
20
|
+
* rnwind has transformed so far — Metro's own watcher sees those
|
|
21
|
+
* mtime changes, invalidates the modules, and re-transforms them
|
|
22
|
+
* against the new CSS on the next request. `getCacheKey()` alone is
|
|
23
|
+
* NOT enough: Metro samples the cache key once per worker lifetime,
|
|
24
|
+
* so edits during an already-running dev server don't propagate
|
|
25
|
+
* without this explicit nudge.
|
|
26
|
+
* @param cssPath Absolute path to the theme CSS to watch.
|
|
27
|
+
* @param projectRoot Metro's project root (for `getRnwindState`).
|
|
28
|
+
*/
|
|
29
|
+
function watchThemeCss(cssPath: string, projectRoot: string): void {
|
|
30
|
+
if (activeCssWatcher?.cssPath === cssPath) return
|
|
31
|
+
activeCssWatcher?.close()
|
|
32
|
+
if (!existsSync(cssPath)) return
|
|
33
|
+
let pending = false
|
|
34
|
+
const watcher = watchFile(cssPath, { persistent: false }, () => {
|
|
35
|
+
// Debounce: editors often emit 2-3 change events per save (atomic
|
|
36
|
+
// rename dance, tmp files). Coalesce to ONE rebuild per microtask.
|
|
37
|
+
if (pending) return
|
|
38
|
+
pending = true
|
|
39
|
+
queueMicrotask(async () => {
|
|
40
|
+
pending = false
|
|
41
|
+
try {
|
|
42
|
+
await onThemeChange(projectRoot)
|
|
43
|
+
touchRecordedFiles(projectRoot)
|
|
44
|
+
} catch {
|
|
45
|
+
// Invalidation is best-effort — never crash the dev server.
|
|
46
|
+
}
|
|
47
|
+
})
|
|
48
|
+
})
|
|
49
|
+
activeCssWatcher = { cssPath, close: () => watcher.close() }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Bump the mtime on every file the builder has transformed. Metro's
|
|
54
|
+
* file watcher keys on `mtime`, so this is what makes it invalidate
|
|
55
|
+
* those modules and re-transform them.
|
|
56
|
+
* @param projectRoot Metro's project root.
|
|
57
|
+
*/
|
|
58
|
+
function touchRecordedFiles(projectRoot: string): void {
|
|
59
|
+
const state = getRnwindState(projectRoot)
|
|
60
|
+
const files = state.builder.recordedFiles()
|
|
61
|
+
const now = new Date()
|
|
62
|
+
for (const file of files) {
|
|
63
|
+
try {
|
|
64
|
+
if (existsSync(file)) utimesSync(file, now, now)
|
|
65
|
+
} catch {
|
|
66
|
+
// One file's stat failure shouldn't stop the others.
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Where the rnwind babel transformer lives — resolved relative to this
|
|
73
|
+
* module so the path works from both the `src/` tree (tests) and the
|
|
74
|
+
* built `lib/` output. Tries a few extensions because `require.resolve`
|
|
75
|
+
* doesn't auto-find `.cjs` / `.mjs` from a bare specifier.
|
|
76
|
+
* @returns Absolute path to the rnwind transformer module.
|
|
77
|
+
*/
|
|
78
|
+
function transformerPath(): string {
|
|
79
|
+
for (const candidate of ['./transformer.cjs', './transformer.mjs', './transformer.js', './transformer.ts', './transformer']) {
|
|
80
|
+
try {
|
|
81
|
+
return require.resolve(candidate)
|
|
82
|
+
} catch {
|
|
83
|
+
// try the next extension
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
throw new Error('rnwind: could not resolve the metro transformer path')
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Resolve the effective cache directory, honoring a user override and
|
|
91
|
+
* falling back to `<projectRoot>/.rnwind`.
|
|
92
|
+
* @param projectRoot Anchor for relative paths.
|
|
93
|
+
* @param override User-supplied option.
|
|
94
|
+
* @returns Absolute cache directory.
|
|
95
|
+
*/
|
|
96
|
+
function resolveCacheDir(projectRoot: string, override: string | undefined): string {
|
|
97
|
+
if (!override || override.length === 0) return path.resolve(projectRoot, DEFAULT_CACHE_DIR)
|
|
98
|
+
return path.isAbsolute(override) ? override : path.resolve(projectRoot, override)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Read the theme CSS and extract `@variant <name>` blocks for the .d.ts
|
|
103
|
+
* generator. Forces construction of `getRnwindState`, then reads
|
|
104
|
+
* `parser.declaredSchemes` (populated synchronously at construction).
|
|
105
|
+
* @param cssEntry Absolute path to theme CSS.
|
|
106
|
+
* @param projectRoot
|
|
107
|
+
* @returns Scheme names (empty when the theme has no variants; `'base'` is filtered).
|
|
108
|
+
*/
|
|
109
|
+
function discoverSchemes(cssEntry: string, projectRoot: string): readonly string[] {
|
|
110
|
+
if (!existsSync(cssEntry)) return []
|
|
111
|
+
try {
|
|
112
|
+
const { parser } = getRnwindState(projectRoot)
|
|
113
|
+
return parser.declaredSchemes.filter((name) => name !== 'base')
|
|
114
|
+
} catch {
|
|
115
|
+
return []
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** User-facing options for `withRnwindConfig`. */
|
|
120
|
+
export interface RnwindMetroOptions {
|
|
121
|
+
/** Path to the theme CSS (absolute or relative to `projectRoot`). Required. */
|
|
122
|
+
cssEntryFile: string
|
|
123
|
+
/** Where rnwind writes the `.d.ts` file. Set `false` to disable. Defaults to `<projectRoot>/rnwind-types.d.ts`. */
|
|
124
|
+
dtsFile?: string | false
|
|
125
|
+
/** Optional project-root override — defaults to `metroConfig.projectRoot` then `process.cwd()`. */
|
|
126
|
+
projectRoot?: string
|
|
127
|
+
/** Cache directory. Absolute, or relative to `projectRoot`. Default: `.rnwind` at project root. */
|
|
128
|
+
cacheDir?: string
|
|
129
|
+
/**
|
|
130
|
+
* Extra JSX prop-name prefixes that rnwind should rewrite. Each
|
|
131
|
+
* prefix `P` turns `<Tag PClassName="…">` into `<Tag
|
|
132
|
+
* PStyle={lookupCss(…)}>`. The built-in `'contentContainer'` prefix
|
|
133
|
+
* is always on (covers ScrollView / FlatList / SectionList); user
|
|
134
|
+
* entries merge on top.
|
|
135
|
+
*/
|
|
136
|
+
classNamePrefixes?: readonly string[]
|
|
137
|
+
/**
|
|
138
|
+
* Extra module specifiers whose JSX exports rnwind should treat as
|
|
139
|
+
* "host components" — i.e. tags whose `className="…"` attribute is
|
|
140
|
+
* rewritten to `style={lookupCss(…)}` at build time (zero runtime
|
|
141
|
+
* cost). Merged with the built-in defaults: `react-native`,
|
|
142
|
+
* `react-native-reanimated`, `react-native-svg`,
|
|
143
|
+
* `react-native-gesture-handler`, `expo-linear-gradient`, `expo-image`.
|
|
144
|
+
*
|
|
145
|
+
* Anything NOT marked as a host has its `className` left untouched —
|
|
146
|
+
* the importing component receives the raw string and decides what
|
|
147
|
+
* to do with it. Use this option to opt your design-system / UI
|
|
148
|
+
* primitive packages into the zero-runtime path.
|
|
149
|
+
*/
|
|
150
|
+
hostSources?: readonly string[]
|
|
151
|
+
/**
|
|
152
|
+
* Extra JSX tag names (verbatim — may include `.` for member access
|
|
153
|
+
* like `'Animated.View'`) rnwind should treat as host components,
|
|
154
|
+
* regardless of where they're imported from. Useful for one-off
|
|
155
|
+
* escape-hatches: `import { View as MyBox } from 'react-native'`
|
|
156
|
+
* doesn't change the local name → `'MyBox'` here picks it up.
|
|
157
|
+
*/
|
|
158
|
+
hostComponents?: readonly string[]
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** Shape we mutate on Metro's config. Loose so we don't pin Metro's internal types. */
|
|
162
|
+
export interface MetroConfigLike {
|
|
163
|
+
projectRoot?: string
|
|
164
|
+
watchFolders?: string[]
|
|
165
|
+
transformer?: {
|
|
166
|
+
babelTransformerPath?: string
|
|
167
|
+
[key: string]: unknown
|
|
168
|
+
}
|
|
169
|
+
resolver?: {
|
|
170
|
+
resolveRequest?: ResolveRequestFn | null
|
|
171
|
+
[key: string]: unknown
|
|
172
|
+
}
|
|
173
|
+
[key: string]: unknown
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* Wrap a Metro config with rnwind's pipeline:
|
|
178
|
+
* - Install the rnwind babel transformer.
|
|
179
|
+
* - Chain a `resolveRequest` hook that serves
|
|
180
|
+
* `rnwind/__generated/schemes` from `<cacheDir>/schemes.js`.
|
|
181
|
+
* - Write the `.d.ts` so TypeScript accepts `className=` on RN components.
|
|
182
|
+
* - Publish the theme CSS path + cache dir via env so Metro workers
|
|
183
|
+
* can rebuild their local state.
|
|
184
|
+
* - Ensure the cache dir is a watched folder Metro's haste-map indexes.
|
|
185
|
+
*
|
|
186
|
+
* Theme-edit hot reload happens implicitly: every transformed file
|
|
187
|
+
* imports `rnwind/__generated/schemes`, and that module eager-imports
|
|
188
|
+
* `common.style.js`. When the theme changes, the per-scheme files
|
|
189
|
+
* regenerate with new bytes; Metro's content SHA1 dedup detects the
|
|
190
|
+
* change and invalidates every importer automatically.
|
|
191
|
+
* `getCacheKey()` on the transformer covers the per-file transform
|
|
192
|
+
* cache. No file watcher / source-padding hack needed — the dep graph
|
|
193
|
+
* carries the signal.
|
|
194
|
+
* @param metroConfig Config from `getDefaultConfig(__dirname)` or equivalent.
|
|
195
|
+
* @param options rnwind options.
|
|
196
|
+
* @returns The same config, mutated.
|
|
197
|
+
*/
|
|
198
|
+
export function withRnwindConfig<C extends MetroConfigLike>(metroConfig: C, options: RnwindMetroOptions): C {
|
|
199
|
+
const projectRoot = options.projectRoot ?? metroConfig.projectRoot ?? process.cwd()
|
|
200
|
+
const cacheDir = resolveCacheDir(projectRoot, options.cacheDir)
|
|
201
|
+
const cssEntry = path.isAbsolute(options.cssEntryFile) ? options.cssEntryFile : path.resolve(projectRoot, options.cssEntryFile)
|
|
202
|
+
|
|
203
|
+
mkdirSync(cacheDir, { recursive: true })
|
|
204
|
+
const watchFolders = (metroConfig.watchFolders ?? []).filter((p) => typeof p === 'string' && p.length > 0)
|
|
205
|
+
configureRnwindState(cssEntry, cacheDir, watchFolders, options.classNamePrefixes, options.hostSources, options.hostComponents)
|
|
206
|
+
|
|
207
|
+
// Warm the state eagerly (in the Metro master process) so oxide's
|
|
208
|
+
// Scanner walks every project source (and every monorepo
|
|
209
|
+
// watch-folder) ONCE and the manifest + scheme files hold the
|
|
210
|
+
// complete union before Metro's resolver tries to SHA1 them on the
|
|
211
|
+
// first transform. Each worker lazy-repeats this scan on its first
|
|
212
|
+
// transform to converge on identical state.
|
|
213
|
+
try {
|
|
214
|
+
void getRnwindState(projectRoot).builder.ensureFilesExist()
|
|
215
|
+
} catch {
|
|
216
|
+
// Any init error surfaces again at the first transform; don't crash Metro boot.
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// Install transformer + resolver. Capture the existing
|
|
220
|
+
// babelTransformerPath BEFORE we override it — our worker chains to
|
|
221
|
+
// it (env-passed) so Flow / expo-router / babel-preset-expo etc. all
|
|
222
|
+
// continue to run.
|
|
223
|
+
const existingTransformerPath = metroConfig.transformer?.babelTransformerPath
|
|
224
|
+
if (typeof existingTransformerPath === 'string' && existingTransformerPath.length > 0) {
|
|
225
|
+
process.env.RNWIND_UPSTREAM_TRANSFORMER = existingTransformerPath
|
|
226
|
+
}
|
|
227
|
+
const upstream = metroConfig.resolver?.resolveRequest ?? null
|
|
228
|
+
metroConfig.transformer = { ...metroConfig.transformer, babelTransformerPath: transformerPath() }
|
|
229
|
+
metroConfig.resolver = { ...metroConfig.resolver, resolveRequest: createRnwindResolver(upstream) }
|
|
230
|
+
|
|
231
|
+
// Metro's haste-map indexes `watchFolders` at startup. Adding the
|
|
232
|
+
// cache dir guarantees scheme style files + manifest get SHA1'd
|
|
233
|
+
// without a "Failed to get the SHA-1" race when the first transform
|
|
234
|
+
// writes them.
|
|
235
|
+
const existingWatch = metroConfig.watchFolders ?? []
|
|
236
|
+
metroConfig.watchFolders = existingWatch.includes(cacheDir) ? existingWatch : [...existingWatch, cacheDir]
|
|
237
|
+
|
|
238
|
+
if (options.dtsFile !== false) {
|
|
239
|
+
const dtsPath = options.dtsFile ?? path.resolve(projectRoot, 'rnwind-types.d.ts')
|
|
240
|
+
const schemes = discoverSchemes(cssEntry, projectRoot)
|
|
241
|
+
writeDtsFile(dtsPath, schemes, options.classNamePrefixes)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Watch the theme CSS. On edit, we rewrite scheme files AND touch
|
|
245
|
+
// mtime on every transformed source file so Metro invalidates them
|
|
246
|
+
// and re-transforms — the only reliable way to propagate theme
|
|
247
|
+
// changes to an already-running dev server.
|
|
248
|
+
watchThemeCss(cssEntry, projectRoot)
|
|
249
|
+
|
|
250
|
+
return metroConfig
|
|
251
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tiny helpers that compose rnwind's press / focus handlers with any
|
|
3
|
+
* user-provided ones the JSX site already has. The transformer uses
|
|
4
|
+
* these when it rewrites a `<Pressable onPressIn={user}>` into
|
|
5
|
+
* `<Pressable onPressIn={chainPress(user, _i.onPressIn)}>` so the user's
|
|
6
|
+
* callback keeps firing.
|
|
7
|
+
*
|
|
8
|
+
* Both helpers bail out when the user's handler is `null` / `undefined`,
|
|
9
|
+
* returning rnwind's handler directly — that gives the transformer a
|
|
10
|
+
* cheap uniform rewrite ("always chain") without allocating a new
|
|
11
|
+
* function per render when the user didn't opt in to the handler.
|
|
12
|
+
*/
|
|
13
|
+
import type { GestureResponderEvent, NativeSyntheticEvent, TargetedEvent } from 'react-native'
|
|
14
|
+
|
|
15
|
+
type PressHandler = (event: GestureResponderEvent) => void
|
|
16
|
+
type FocusHandler = (event: NativeSyntheticEvent<TargetedEvent>) => void
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Compose a user-supplied press handler with rnwind's internal one.
|
|
20
|
+
* User fires first (its return / side-effects happen pre-gate flip); the
|
|
21
|
+
* rnwind handler then updates the active flag.
|
|
22
|
+
* @param user User-supplied handler from the original JSX (optional).
|
|
23
|
+
* @param ours rnwind's internal active-toggling handler.
|
|
24
|
+
* @returns Combined handler, or `ours` directly when the user didn't provide one.
|
|
25
|
+
*/
|
|
26
|
+
export function chainPress(user: PressHandler | null | undefined, ours: PressHandler): PressHandler {
|
|
27
|
+
if (user == null) return ours
|
|
28
|
+
return (event: GestureResponderEvent): void => {
|
|
29
|
+
user(event)
|
|
30
|
+
ours(event)
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Compose a user-supplied focus/blur handler with rnwind's internal one.
|
|
36
|
+
* Same ordering rule as {@link chainPress}: user first, rnwind second.
|
|
37
|
+
* @param user User-supplied handler (optional).
|
|
38
|
+
* @param ours rnwind's internal focus-toggling handler.
|
|
39
|
+
* @returns Combined handler, or `ours` directly when the user didn't provide one.
|
|
40
|
+
*/
|
|
41
|
+
export function chainFocus(user: FocusHandler | null | undefined, ours: FocusHandler): FocusHandler {
|
|
42
|
+
if (user == null) return ours
|
|
43
|
+
return (event: NativeSyntheticEvent<TargetedEvent>): void => {
|
|
44
|
+
user(event)
|
|
45
|
+
ours(event)
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { createContext, useContext, useMemo, type ReactNode } from 'react'
|
|
2
|
+
import { useWindowDimensions } from 'react-native'
|
|
3
|
+
import type { ThemeTables } from '../../core/types'
|
|
4
|
+
import type { Scheme } from '../types'
|
|
5
|
+
import type { OnHaptics } from '../../core/parser/haptics'
|
|
6
|
+
import { activeBreakpointFor, BASE_BREAKPOINT, loadScheme } from '../lookup-css'
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Per-render safe-area insets snapshot. Bridge from any source
|
|
10
|
+
* (`useSafeAreaInsets()`, expo-router insets, a manually computed
|
|
11
|
+
* value) into the {@link RnwindProvider} — rnwind stays
|
|
12
|
+
* library-agnostic.
|
|
13
|
+
*/
|
|
14
|
+
export type Insets = Readonly<{
|
|
15
|
+
top: number
|
|
16
|
+
right: number
|
|
17
|
+
bottom: number
|
|
18
|
+
left: number
|
|
19
|
+
}>
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Single value carried by the rnwind context. Every piece of runtime
|
|
23
|
+
* state rnwind needs — scheme, theme tables, insets, font scale,
|
|
24
|
+
* window width, active responsive breakpoint, optional haptic
|
|
25
|
+
* dispatcher — lives on this one bag. Consumers read it via
|
|
26
|
+
* {@link useRnwind} and either destructure or forward straight to
|
|
27
|
+
* `lookupCss` / `useCss`.
|
|
28
|
+
*
|
|
29
|
+
* `activeBreakpoint` is the highest-threshold registered breakpoint
|
|
30
|
+
* whose min-width is `<= windowWidth`, or `'base'` when below the
|
|
31
|
+
* smallest one (mobile-first tier) or when no breakpoints are
|
|
32
|
+
* registered yet (tests, bundles without rnwind-transformed sources).
|
|
33
|
+
* Always a string — never null. Reactive: it updates with
|
|
34
|
+
* `useWindowDimensions().width`, so consumers can branch on it
|
|
35
|
+
* without a separate hook.
|
|
36
|
+
*/
|
|
37
|
+
export type RnwindState = Readonly<{
|
|
38
|
+
scheme: Scheme
|
|
39
|
+
tables: ThemeTables
|
|
40
|
+
insets: Insets
|
|
41
|
+
onHaptics: OnHaptics | undefined
|
|
42
|
+
fontScale: number
|
|
43
|
+
windowWidth: number
|
|
44
|
+
activeBreakpoint: string
|
|
45
|
+
}>
|
|
46
|
+
|
|
47
|
+
/** Props accepted by {@link RnwindProvider}. */
|
|
48
|
+
export type RnwindProviderProps = Readonly<{
|
|
49
|
+
scheme: Scheme
|
|
50
|
+
tables?: ThemeTables
|
|
51
|
+
insets?: Partial<Insets>
|
|
52
|
+
onHaptics?: OnHaptics
|
|
53
|
+
children?: ReactNode
|
|
54
|
+
}>
|
|
55
|
+
|
|
56
|
+
const EMPTY_TABLES: ThemeTables = {}
|
|
57
|
+
const ZERO_INSETS: Insets = { top: 0, right: 0, bottom: 0, left: 0 }
|
|
58
|
+
const DEFAULT_STATE: RnwindState = {
|
|
59
|
+
scheme: 'light' as Scheme,
|
|
60
|
+
tables: EMPTY_TABLES,
|
|
61
|
+
insets: ZERO_INSETS,
|
|
62
|
+
onHaptics: undefined,
|
|
63
|
+
fontScale: 1,
|
|
64
|
+
windowWidth: 0,
|
|
65
|
+
activeBreakpoint: BASE_BREAKPOINT,
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Normalise a `Partial<Insets>` into a complete {@link Insets}, returning
|
|
70
|
+
* the shared {@link ZERO_INSETS} reference when nothing is supplied so
|
|
71
|
+
* downstream memoisation stays stable.
|
|
72
|
+
* @param partial Caller-supplied insets (or undefined).
|
|
73
|
+
* @returns Complete insets record.
|
|
74
|
+
*/
|
|
75
|
+
function normaliseInsets(partial: Partial<Insets> | undefined): Insets {
|
|
76
|
+
if (!partial) return ZERO_INSETS
|
|
77
|
+
const top = partial.top ?? 0
|
|
78
|
+
const right = partial.right ?? 0
|
|
79
|
+
const bottom = partial.bottom ?? 0
|
|
80
|
+
const left = partial.left ?? 0
|
|
81
|
+
if (top === 0 && right === 0 && bottom === 0 && left === 0) return ZERO_INSETS
|
|
82
|
+
return { top, right, bottom, left }
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Single internal context the runtime reads from. */
|
|
86
|
+
const RnwindContext = createContext<RnwindState>(DEFAULT_STATE)
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Read rnwind's full runtime state — scheme, theme tables, insets,
|
|
90
|
+
* fontScale, windowWidth, onHaptics — in one go. Pass the returned
|
|
91
|
+
* value straight to `lookupCss` / `useCss`, or destructure what you
|
|
92
|
+
* need.
|
|
93
|
+
* @returns Active rnwind state under the nearest {@link RnwindProvider}.
|
|
94
|
+
*/
|
|
95
|
+
export function useRnwind(): RnwindState {
|
|
96
|
+
return useContext(RnwindContext)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Internal context hook the babel transformer injects at the top of
|
|
101
|
+
* every rewritten component as `const _t = useR_()`. Same body as the
|
|
102
|
+
* public {@link useRnwind} — exposed under a `use*`-prefixed name so
|
|
103
|
+
* react-refresh's babel plugin (which only tracks call-sites whose
|
|
104
|
+
* identifier matches `^use[A-Z]`) folds it into each component's
|
|
105
|
+
* fast-refresh signature. Without that prefix the signature stayed
|
|
106
|
+
* stable across transformer changes; HMR then preserved fiber state
|
|
107
|
+
* while the rendered hook list shifted, surfacing as "change in the
|
|
108
|
+
* order of Hooks" runtime errors. Trailing underscore keeps it
|
|
109
|
+
* visually distinct from the user-facing `useRnwind`.
|
|
110
|
+
* @returns Active rnwind state.
|
|
111
|
+
*/
|
|
112
|
+
export function useR_(): RnwindState {
|
|
113
|
+
return useContext(RnwindContext)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Provider for rnwind's full runtime state. fontScale + windowWidth
|
|
118
|
+
* come from `useWindowDimensions()` so they react to OS-level
|
|
119
|
+
* orientation / accessibility-text-size changes automatically.
|
|
120
|
+
* @param props Provider props.
|
|
121
|
+
* @param props.scheme Active scheme name.
|
|
122
|
+
* @param props.tables Optional pre-resolved token tables.
|
|
123
|
+
* @param props.insets Optional safe-area insets.
|
|
124
|
+
* @param props.onHaptics Optional haptic dispatcher.
|
|
125
|
+
* @param props.children React subtree.
|
|
126
|
+
* @returns Provider element.
|
|
127
|
+
*/
|
|
128
|
+
export function RnwindProvider({ scheme, tables, insets, onHaptics, children }: RnwindProviderProps): ReactNode {
|
|
129
|
+
const normalized = normaliseInsets(insets)
|
|
130
|
+
const { fontScale, width } = useWindowDimensions()
|
|
131
|
+
const value = useMemo<RnwindState>(() => {
|
|
132
|
+
loadScheme(scheme)
|
|
133
|
+
return {
|
|
134
|
+
scheme,
|
|
135
|
+
tables: tables ?? EMPTY_TABLES,
|
|
136
|
+
insets: normalized,
|
|
137
|
+
onHaptics,
|
|
138
|
+
fontScale,
|
|
139
|
+
windowWidth: width,
|
|
140
|
+
activeBreakpoint: activeBreakpointFor(width),
|
|
141
|
+
}
|
|
142
|
+
}, [scheme, tables, normalized, onHaptics, fontScale, width])
|
|
143
|
+
return <RnwindContext.Provider value={value}>{children}</RnwindContext.Provider>
|
|
144
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Typed contract for the component rnwind's transformer calls with a
|
|
3
|
+
* gradient-class `className`. Matches `expo-linear-gradient` verbatim
|
|
4
|
+
* so users can plug any gradient library that mirrors that API.
|
|
5
|
+
*
|
|
6
|
+
* A typical wiring:
|
|
7
|
+
*
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { LinearGradient } from 'expo-linear-gradient'
|
|
10
|
+
* import type { AsLinearGradientProps } from 'rnwind'
|
|
11
|
+
*
|
|
12
|
+
* // Assert at the type level — CI catches mismatches before runtime.
|
|
13
|
+
* const _typeCheck: AsLinearGradientProps = {} as Parameters<typeof LinearGradient>[0]
|
|
14
|
+
*
|
|
15
|
+
* // Then use it in real JSX. rnwind fills in colors / start / end.
|
|
16
|
+
* <LinearGradient className="bg-gradient-to-r from-red-500 to-blue-500" />
|
|
17
|
+
* ```
|
|
18
|
+
*
|
|
19
|
+
* The rnwind transformer emits `colors` as a frozen `readonly string[]`,
|
|
20
|
+
* and `start` / `end` as frozen `{x: number, y: number}` records.
|
|
21
|
+
* Users pass the component; rnwind fills the props. Unused props
|
|
22
|
+
* (`locations`, `dither`, …) are forwarded verbatim from the JSX site.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/** A unit-square point. Matches the `LinearGradientPoint` object form. */
|
|
26
|
+
export interface LinearGradientPoint {
|
|
27
|
+
/** Horizontal component, 0..1. */
|
|
28
|
+
readonly x: number
|
|
29
|
+
/** Vertical component, 0..1. */
|
|
30
|
+
readonly y: number
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The exact prop shape rnwind fills onto a gradient component at
|
|
35
|
+
* build time. A user-supplied `<LinearGradient>` (or any other
|
|
36
|
+
* component) must accept these props — no wrapper, no adapter.
|
|
37
|
+
*/
|
|
38
|
+
export interface AsLinearGradientProps {
|
|
39
|
+
/**
|
|
40
|
+
* Gradient colour stops in source order. rnwind emits a frozen
|
|
41
|
+
* `readonly string[]` with 2+ entries (at least `from` + `to`).
|
|
42
|
+
* Compatible with expo-linear-gradient's `colors: readonly [ColorValue, ColorValue, ...ColorValue[]]`.
|
|
43
|
+
*/
|
|
44
|
+
readonly colors: readonly string[]
|
|
45
|
+
/**
|
|
46
|
+
* Start point, in the unit square (0,0)=top-left → (1,1)=bottom-right.
|
|
47
|
+
* rnwind emits a frozen `{x, y}` record matching the Tailwind
|
|
48
|
+
* direction utility (`to-r` → `{x: 0, y: 0.5}`).
|
|
49
|
+
*/
|
|
50
|
+
readonly start: LinearGradientPoint
|
|
51
|
+
/** End point — same unit-square coordinates as `start`. */
|
|
52
|
+
readonly end: LinearGradientPoint
|
|
53
|
+
/**
|
|
54
|
+
* Optional colour-stop positions matching `colors` length. rnwind
|
|
55
|
+
* doesn't emit this today (Tailwind v4's stop-position atoms aren't
|
|
56
|
+
* wired yet); listed here so the prop surface still matches
|
|
57
|
+
* expo-linear-gradient when a consumer passes their own.
|
|
58
|
+
*/
|
|
59
|
+
readonly locations?: readonly number[] | null
|
|
60
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime helpers the transformer injects for haptic utilities.
|
|
3
|
+
*
|
|
4
|
+
* Two entry points:
|
|
5
|
+
*
|
|
6
|
+
* useMountHaptic(requests) — fires each request on mount via
|
|
7
|
+
* `useEffect(() => { ... }, [])`. Used by the transformer for bare
|
|
8
|
+
* `haptic-*` atoms (no variant prefix).
|
|
9
|
+
*
|
|
10
|
+
* triggerHaptic(onHaptics, request, trigger) — thin forwarding
|
|
11
|
+
* helper for event-driven variants (`active:haptic-*`, `focus:...`,
|
|
12
|
+
* `hover:...`). The transformer emits an inline arrow that calls
|
|
13
|
+
* this from `onPressIn` / `onFocus` / etc.
|
|
14
|
+
*
|
|
15
|
+
* Both emit a one-shot dev-mode warning when `onHaptics` is missing —
|
|
16
|
+
* so developers get a clear "you forgot to wire <SchemeProvider
|
|
17
|
+
* onHaptics=...>" signal instead of silently dropping the haptic.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
import { useEffect } from 'react'
|
|
21
|
+
import type { HapticRequest, HapticTrigger, OnHaptics } from '../core/parser/haptics'
|
|
22
|
+
import { useRnwind } from './components/rnwind-provider'
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Module-scope set tracking haptic trigger strings we've already
|
|
26
|
+
* warned about. Prevents the "missing onHaptics" warning from
|
|
27
|
+
* spamming the console when the same class appears on many elements.
|
|
28
|
+
*/
|
|
29
|
+
const WARNED_MISSING_HAPTICS = new Set<string>()
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Render a `HapticRequest` as a short descriptive tag for log lines.
|
|
33
|
+
* @param request Haptic request.
|
|
34
|
+
* @returns `impact/Light`, `notification/Success`, or `selection`.
|
|
35
|
+
*/
|
|
36
|
+
function hapticTag(request: HapticRequest): string {
|
|
37
|
+
if (request.kind === 'impact') return `impact/${request.style}`
|
|
38
|
+
if (request.kind === 'notification') return `notification/${request.type}`
|
|
39
|
+
return 'selection'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Warn once per haptic trigger kind when a `haptic-*` atom tries to
|
|
44
|
+
* dispatch without an `onHaptics` provider. Dev-mode only — `__DEV__`
|
|
45
|
+
* is a Metro / Expo / RN global that compiles to `false` in release
|
|
46
|
+
* bundles, so no warn code ships to production.
|
|
47
|
+
* @param request The haptic request that had no provider.
|
|
48
|
+
* @param trigger The lifecycle trigger that tried to fire.
|
|
49
|
+
*/
|
|
50
|
+
function warnMissingOnHaptics(request: HapticRequest, trigger: HapticTrigger): void {
|
|
51
|
+
// `__DEV__` is a RN / Metro global — `false` in release, strips the
|
|
52
|
+
// branch entirely. Guarded in case we're evaluated outside of Metro
|
|
53
|
+
// (tests, node scripts) where the global isn't defined.
|
|
54
|
+
const isDevelopment = typeof __DEV__ === 'undefined' || __DEV__
|
|
55
|
+
if (!isDevelopment) return
|
|
56
|
+
const tag = hapticTag(request)
|
|
57
|
+
const key = `${tag}@${trigger}`
|
|
58
|
+
if (WARNED_MISSING_HAPTICS.has(key)) return
|
|
59
|
+
WARNED_MISSING_HAPTICS.add(key)
|
|
60
|
+
// eslint-disable-next-line no-console
|
|
61
|
+
console.warn(
|
|
62
|
+
`rnwind: a haptic utility fired (${tag}, trigger=${trigger}) but no onHaptics callback is wired on <SchemeProvider>. ` +
|
|
63
|
+
`Pass \`onHaptics\` on the provider to forward this to expo-haptics (or any library of your choice).`,
|
|
64
|
+
)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Test-only hook — clears the warned-haptics set so successive test
|
|
69
|
+
* runs don't silently swallow their own warnings.
|
|
70
|
+
*/
|
|
71
|
+
function __resetHapticWarnings(): void {
|
|
72
|
+
WARNED_MISSING_HAPTICS.clear()
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Invoke every request in `requests` on mount (once per component
|
|
77
|
+
* mount), using the `onHaptics` dispatcher from the nearest
|
|
78
|
+
* `<SchemeProvider>`. Missing-provider dev warnings fire via
|
|
79
|
+
* {@link warnMissingOnHaptics}.
|
|
80
|
+
*
|
|
81
|
+
* Uses `useEffect(..., [])` — the requests array is identity-stable
|
|
82
|
+
* (hoisted at module scope by the transformer), so re-firing on
|
|
83
|
+
* re-renders isn't a concern.
|
|
84
|
+
* @param requests Hoisted request list for this component.
|
|
85
|
+
*/
|
|
86
|
+
function useMountHaptic(requests: readonly HapticRequest[]): void {
|
|
87
|
+
const { onHaptics } = useRnwind()
|
|
88
|
+
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
for (const request of requests) {
|
|
91
|
+
if (onHaptics) onHaptics(request, 'mount')
|
|
92
|
+
else warnMissingOnHaptics(request, 'mount')
|
|
93
|
+
}
|
|
94
|
+
// requests is a hoisted stable reference — depending on onHaptics
|
|
95
|
+
// identity keeps the effect fresh if the provider remounts with a
|
|
96
|
+
// different dispatcher, while the hoisted const prevents a remount
|
|
97
|
+
// from an inline `onHaptics={(r) => ...}`.
|
|
98
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
99
|
+
}, [onHaptics])
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Fire one haptic request through the provider dispatcher. Emits the
|
|
104
|
+
* missing-provider dev warning when no dispatcher is wired. Designed
|
|
105
|
+
* for the inline arrows the transformer synthesises:
|
|
106
|
+
*
|
|
107
|
+
* onPressIn={(e) => { triggerHaptic(_h, _HR_xxx, 'pressIn'); user?.(e) }}
|
|
108
|
+
* @param onHaptics Provider dispatcher (may be undefined).
|
|
109
|
+
* @param request Pre-hoisted request object.
|
|
110
|
+
* @param trigger Lifecycle trigger.
|
|
111
|
+
*/
|
|
112
|
+
function triggerHaptic(onHaptics: OnHaptics | undefined, request: HapticRequest, trigger: HapticTrigger): void {
|
|
113
|
+
if (onHaptics) {
|
|
114
|
+
onHaptics(request, trigger)
|
|
115
|
+
return
|
|
116
|
+
}
|
|
117
|
+
warnMissingOnHaptics(request, trigger)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export { useMountHaptic, triggerHaptic, __resetHapticWarnings }
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useRnwind } from '../components/rnwind-provider'
|
|
2
|
+
import { lookupCss } from '../lookup-css'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Convenience hook: `useRnwind()` + `lookupCss()` rolled into one. Use
|
|
6
|
+
* inside any component that wants the resolved style array without
|
|
7
|
+
* threading the rnwind context manually. JSX-heavy components should
|
|
8
|
+
* still call `useRnwind()` once and pass it to `lookupCss(...)` per
|
|
9
|
+
* element so React only does a single context read per render.
|
|
10
|
+
* @param className Raw className string or transformer-hoisted atom-name array.
|
|
11
|
+
* @param userStyle Optional caller-supplied style appended last.
|
|
12
|
+
* @returns Frozen style array for React Native's `style` prop.
|
|
13
|
+
*/
|
|
14
|
+
export function useCss(className?: string | readonly string[] | null, userStyle?: unknown): readonly unknown[] {
|
|
15
|
+
return lookupCss(className, useRnwind(), userStyle)
|
|
16
|
+
}
|