@react-native-rethemed/core 0.0.0-stage → 0.1.1

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 (57) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -2
  3. package/dist/color-mode-store.cjs +57 -0
  4. package/dist/color-mode-store.d.cts +11 -0
  5. package/dist/color-mode-store.d.mts +11 -0
  6. package/dist/color-mode-store.mjs +56 -0
  7. package/dist/config.cjs +26 -0
  8. package/dist/config.d.cts +8 -0
  9. package/dist/config.d.mts +8 -0
  10. package/dist/config.mjs +7 -0
  11. package/dist/create-themed-styles.cjs +31 -0
  12. package/dist/create-themed-styles.d.cts +15 -0
  13. package/dist/create-themed-styles.d.mts +15 -0
  14. package/dist/create-themed-styles.mjs +31 -0
  15. package/dist/create-themed.cjs +93 -0
  16. package/dist/create-themed.d.cts +48 -0
  17. package/dist/create-themed.d.mts +48 -0
  18. package/dist/create-themed.mjs +93 -0
  19. package/dist/define-theme.cjs +15 -0
  20. package/dist/define-theme.d.cts +13 -0
  21. package/dist/define-theme.d.mts +13 -0
  22. package/dist/define-theme.mjs +15 -0
  23. package/dist/extend-theme.cjs +109 -0
  24. package/dist/extend-theme.d.cts +16 -0
  25. package/dist/extend-theme.d.mts +16 -0
  26. package/dist/extend-theme.mjs +109 -0
  27. package/dist/index.cjs +30 -0
  28. package/dist/index.d.cts +13 -0
  29. package/dist/index.d.mts +13 -0
  30. package/dist/index.mjs +9 -0
  31. package/dist/resolvers/color-resolver.cjs +23 -0
  32. package/dist/resolvers/color-resolver.d.cts +4 -0
  33. package/dist/resolvers/color-resolver.d.mts +4 -0
  34. package/dist/resolvers/color-resolver.mjs +23 -0
  35. package/dist/resolvers/line-height-resolver.cjs +17 -0
  36. package/dist/resolvers/line-height-resolver.d.cts +10 -0
  37. package/dist/resolvers/line-height-resolver.d.mts +10 -0
  38. package/dist/resolvers/line-height-resolver.mjs +16 -0
  39. package/dist/resolvers/style-resolver.cjs +86 -0
  40. package/dist/resolvers/style-resolver.mjs +86 -0
  41. package/dist/semantic-colors.cjs +29 -0
  42. package/dist/semantic-colors.d.cts +16 -0
  43. package/dist/semantic-colors.d.mts +16 -0
  44. package/dist/semantic-colors.mjs +28 -0
  45. package/dist/style-props.cjs +103 -0
  46. package/dist/style-props.d.cts +29 -0
  47. package/dist/style-props.d.mts +29 -0
  48. package/dist/style-props.mjs +95 -0
  49. package/dist/text-tree.cjs +70 -0
  50. package/dist/text-tree.d.cts +26 -0
  51. package/dist/text-tree.d.mts +26 -0
  52. package/dist/text-tree.mjs +66 -0
  53. package/dist/text-variants.cjs +25 -0
  54. package/dist/text-variants.mjs +25 -0
  55. package/dist/types.d.cts +280 -0
  56. package/dist/types.d.mts +280 -0
  57. package/package.json +66 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 manato
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.
package/README.md CHANGED
@@ -1,3 +1,88 @@
1
- # Temporary Holding Version
1
+ # @react-native-rethemed/core
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ [![npm](https://img.shields.io/npm/v/@react-native-rethemed/core.svg)](https://www.npmjs.com/package/@react-native-rethemed/core)
4
+
5
+ Type-safe design tokens for React Native style props — no wrapper components. Light/dark aware, with text presets and a `themed` style API. Part of [react-native-rethemed](https://github.com/manakuro/react-native-rethemed).
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @react-native-rethemed/core
11
+ npm install --save-dev @react-native-rethemed/cli
12
+ ```
13
+
14
+ `react` and `react-native` are peer dependencies.
15
+
16
+ ## Usage
17
+
18
+ Define a theme:
19
+
20
+ ```ts
21
+ // src/theme/theme.ts
22
+ import { defineTheme } from '@react-native-rethemed/core';
23
+
24
+ export const themeConfig = defineTheme({
25
+ tokens: {
26
+ radii: { md: 6, full: 9999 },
27
+ spacing: { 1: 4, 2: 8, 4: 16 },
28
+ fontSizes: { sm: 14, md: 16 },
29
+ fontWeights: { normal: '400', semibold: '600' },
30
+ lineHeights: { moderate: 1.5 },
31
+ },
32
+ semanticTokens: {
33
+ colors: {
34
+ bg: { default: { light: '#ffffff', dark: '#111111' } },
35
+ fg: { default: { light: '#111111', dark: '#fafafa' } },
36
+ },
37
+ text: {
38
+ title: { fontSize: 'md', lineHeight: 'moderate', fontWeight: 'semibold' },
39
+ },
40
+ },
41
+ defaults: { fontSize: 'md' },
42
+ });
43
+ ```
44
+
45
+ Generate the typed bindings with [`@react-native-rethemed/cli`](https://github.com/manakuro/react-native-rethemed/tree/main/packages/react-native-rethemed/cli):
46
+
47
+ ```sh
48
+ npx @react-native-rethemed/cli codegen src/theme/theme.ts --docs docs/themed.md
49
+ ```
50
+
51
+ Then wrap the app and style with tokens:
52
+
53
+ ```tsx
54
+ import { Text, View } from 'react-native';
55
+ import { ThemedProvider, useThemed } from './theme/themed.gen';
56
+
57
+ export default function App() {
58
+ return (
59
+ <ThemedProvider>
60
+ <Card />
61
+ </ThemedProvider>
62
+ );
63
+ }
64
+
65
+ function Card() {
66
+ const { themed } = useThemed();
67
+ return (
68
+ <View style={themed.view({ backgroundColor: 'bg.default', padding: 4, borderRadius: 'md' })}>
69
+ <Text style={themed.text.title({ color: 'fg.default' })}>Hello</Text>
70
+ </View>
71
+ );
72
+ }
73
+ ```
74
+
75
+ Start from a design system with a token package (`@react-native-rethemed/chakra-ui-tokens`, `material-ui-tokens`, `material-design-tokens`, `panda-css-tokens`, `tailwind-css-tokens`, `shadcn-ui-tokens`, `radix-ui-tokens`) and `extendTheme`.
76
+
77
+ ## Entry points
78
+
79
+ | Import | Contents |
80
+ | --- | --- |
81
+ | `@react-native-rethemed/core` | Everything: `defineTheme`, `extendTheme`, `createThemed`, `createThemedStyles` and the types |
82
+ | `@react-native-rethemed/core/config` | The React / React Native-free part (`defineTheme`, `extendTheme`, token helpers), for theme files and tooling |
83
+
84
+ See the [main README](https://github.com/manakuro/react-native-rethemed#readme) for color mode, `extendTheme` merge rules and the full API.
85
+
86
+ ## License
87
+
88
+ MIT
@@ -0,0 +1,57 @@
1
+ //#region src/color-mode-store.ts
2
+ const isColorMode = (value) => value === "light" || value === "dark" || value === "system";
3
+ /**
4
+ * External store holding the uncontrolled color mode. One is created per
5
+ * `createThemed()` call (never at module level) so independent instances
6
+ * don't share state.
7
+ */
8
+ function createColorModeStore() {
9
+ let mode = null;
10
+ let userSet = false;
11
+ let hydrationStarted = false;
12
+ const listeners = /* @__PURE__ */ new Set();
13
+ const emit = () => {
14
+ for (const listener of listeners) listener();
15
+ };
16
+ return {
17
+ /**
18
+ * Seeds the mode on first use only. Called during render so the very
19
+ * first render already sees `defaultMode`; later calls (e.g. a changed
20
+ * `defaultColorMode` prop) are ignored rather than overwriting the
21
+ * current mode.
22
+ */
23
+ init(defaultMode) {
24
+ if (mode === null) mode = defaultMode;
25
+ },
26
+ subscribe(listener) {
27
+ listeners.add(listener);
28
+ return () => {
29
+ listeners.delete(listener);
30
+ };
31
+ },
32
+ getSnapshot() {
33
+ return mode ?? "system";
34
+ },
35
+ setMode(next) {
36
+ userSet = true;
37
+ if (next === mode) return;
38
+ mode = next;
39
+ emit();
40
+ },
41
+ /** Returns `false` if rehydration already ran for this store. */
42
+ beginHydration() {
43
+ if (hydrationStarted) return false;
44
+ hydrationStarted = true;
45
+ return true;
46
+ },
47
+ /** Applies a stored value unless it's malformed or the user already chose. */
48
+ hydrate(saved) {
49
+ if (userSet || !isColorMode(saved) || saved === mode) return;
50
+ mode = saved;
51
+ emit();
52
+ }
53
+ };
54
+ }
55
+ //#endregion
56
+ exports.createColorModeStore = createColorModeStore;
57
+ exports.isColorMode = isColorMode;
@@ -0,0 +1,11 @@
1
+ //#region src/color-mode-store.d.ts
2
+ export type ColorMode = 'light' | 'dark' | 'system';
3
+ /**
4
+ * Structural subset of `expo-secure-store` / `AsyncStorage`, so either can be
5
+ * passed as-is without an adapter.
6
+ */
7
+ export type ColorModeStorage = {
8
+ getItem: (key: string) => string | null | Promise<string | null>;
9
+ setItem: (key: string, value: string) => void | Promise<void>;
10
+ };
11
+ //#endregion
@@ -0,0 +1,11 @@
1
+ //#region src/color-mode-store.d.ts
2
+ export type ColorMode = 'light' | 'dark' | 'system';
3
+ /**
4
+ * Structural subset of `expo-secure-store` / `AsyncStorage`, so either can be
5
+ * passed as-is without an adapter.
6
+ */
7
+ export type ColorModeStorage = {
8
+ getItem: (key: string) => string | null | Promise<string | null>;
9
+ setItem: (key: string, value: string) => void | Promise<void>;
10
+ };
11
+ //#endregion
@@ -0,0 +1,56 @@
1
+ //#region src/color-mode-store.ts
2
+ const isColorMode = (value) => value === "light" || value === "dark" || value === "system";
3
+ /**
4
+ * External store holding the uncontrolled color mode. One is created per
5
+ * `createThemed()` call (never at module level) so independent instances
6
+ * don't share state.
7
+ */
8
+ function createColorModeStore() {
9
+ let mode = null;
10
+ let userSet = false;
11
+ let hydrationStarted = false;
12
+ const listeners = /* @__PURE__ */ new Set();
13
+ const emit = () => {
14
+ for (const listener of listeners) listener();
15
+ };
16
+ return {
17
+ /**
18
+ * Seeds the mode on first use only. Called during render so the very
19
+ * first render already sees `defaultMode`; later calls (e.g. a changed
20
+ * `defaultColorMode` prop) are ignored rather than overwriting the
21
+ * current mode.
22
+ */
23
+ init(defaultMode) {
24
+ if (mode === null) mode = defaultMode;
25
+ },
26
+ subscribe(listener) {
27
+ listeners.add(listener);
28
+ return () => {
29
+ listeners.delete(listener);
30
+ };
31
+ },
32
+ getSnapshot() {
33
+ return mode ?? "system";
34
+ },
35
+ setMode(next) {
36
+ userSet = true;
37
+ if (next === mode) return;
38
+ mode = next;
39
+ emit();
40
+ },
41
+ /** Returns `false` if rehydration already ran for this store. */
42
+ beginHydration() {
43
+ if (hydrationStarted) return false;
44
+ hydrationStarted = true;
45
+ return true;
46
+ },
47
+ /** Applies a stored value unless it's malformed or the user already chose. */
48
+ hydrate(saved) {
49
+ if (userSet || !isColorMode(saved) || saved === mode) return;
50
+ mode = saved;
51
+ emit();
52
+ }
53
+ };
54
+ }
55
+ //#endregion
56
+ export { createColorModeStore, isColorMode };
@@ -0,0 +1,26 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_define_theme = require("./define-theme.cjs");
3
+ const require_semantic_colors = require("./semantic-colors.cjs");
4
+ const require_text_tree = require("./text-tree.cjs");
5
+ const require_extend_theme = require("./extend-theme.cjs");
6
+ const require_line_height_resolver = require("./resolvers/line-height-resolver.cjs");
7
+ const require_style_props = require("./style-props.cjs");
8
+ exports.COLOR_KEYS = require_style_props.COLOR_KEYS;
9
+ exports.FONT_SIZE_KEYS = require_style_props.FONT_SIZE_KEYS;
10
+ exports.FONT_WEIGHT_KEYS = require_style_props.FONT_WEIGHT_KEYS;
11
+ exports.LETTER_SPACING_KEYS = require_style_props.LETTER_SPACING_KEYS;
12
+ exports.LINE_HEIGHT_KEYS = require_style_props.LINE_HEIGHT_KEYS;
13
+ exports.RADIUS_KEYS = require_style_props.RADIUS_KEYS;
14
+ exports.RN_DEFAULT_FONT_SIZE = require_line_height_resolver.RN_DEFAULT_FONT_SIZE;
15
+ exports.SHADOW_STYLE_PROPS = require_style_props.SHADOW_STYLE_PROPS;
16
+ exports.SPACING_KEYS = require_style_props.SPACING_KEYS;
17
+ exports.TEXT_TOKEN_FIELDS = require_text_tree.TEXT_TOKEN_FIELDS;
18
+ exports.Z_INDEX_KEYS = require_style_props.Z_INDEX_KEYS;
19
+ exports.defineTheme = require_define_theme.defineTheme;
20
+ exports.extendTheme = require_extend_theme.extendTheme;
21
+ exports.isSchemeColor = require_semantic_colors.isSchemeColor;
22
+ exports.isTextPreset = require_text_tree.isTextPreset;
23
+ exports.resolveBaseFontSize = require_line_height_resolver.resolveBaseFontSize;
24
+ exports.resolveTextColor = require_text_tree.resolveTextColor;
25
+ exports.walkSemanticColors = require_semantic_colors.walkSemanticColors;
26
+ exports.walkTextPresets = require_text_tree.walkTextPresets;
@@ -0,0 +1,8 @@
1
+ import { COLOR_KEYS, FONT_SIZE_KEYS, FONT_WEIGHT_KEYS, LETTER_SPACING_KEYS, LINE_HEIGHT_KEYS, RADIUS_KEYS, SHADOW_STYLE_PROPS, SPACING_KEYS, Z_INDEX_KEYS } from "./style-props.cjs";
2
+ import { LooseSchema, SchemeColor, SemanticColors, ShadowToken, TextColor, TextToken, TextTokenTree, ThemeConfig, ThemedSchema, ThemedStyles, TokenizeStyle } from "./types.cjs";
3
+ import { defineTheme } from "./define-theme.cjs";
4
+ import { extendTheme } from "./extend-theme.cjs";
5
+ import { RN_DEFAULT_FONT_SIZE, resolveBaseFontSize } from "./resolvers/line-height-resolver.cjs";
6
+ import { isSchemeColor, walkSemanticColors } from "./semantic-colors.cjs";
7
+ import { TEXT_TOKEN_FIELDS, isTextPreset, resolveTextColor, walkTextPresets } from "./text-tree.cjs";
8
+ export { COLOR_KEYS, FONT_SIZE_KEYS, FONT_WEIGHT_KEYS, LETTER_SPACING_KEYS, LINE_HEIGHT_KEYS, type LooseSchema, RADIUS_KEYS, RN_DEFAULT_FONT_SIZE, SHADOW_STYLE_PROPS, SPACING_KEYS, type SchemeColor, type SemanticColors, type ShadowToken, TEXT_TOKEN_FIELDS, type TextColor, type TextToken, type TextTokenTree, type ThemeConfig, type ThemedSchema, type ThemedStyles, type TokenizeStyle, Z_INDEX_KEYS, defineTheme, extendTheme, isSchemeColor, isTextPreset, resolveBaseFontSize, resolveTextColor, walkSemanticColors, walkTextPresets };
@@ -0,0 +1,8 @@
1
+ import { COLOR_KEYS, FONT_SIZE_KEYS, FONT_WEIGHT_KEYS, LETTER_SPACING_KEYS, LINE_HEIGHT_KEYS, RADIUS_KEYS, SHADOW_STYLE_PROPS, SPACING_KEYS, Z_INDEX_KEYS } from "./style-props.mjs";
2
+ import { LooseSchema, SchemeColor, SemanticColors, ShadowToken, TextColor, TextToken, TextTokenTree, ThemeConfig, ThemedSchema, ThemedStyles, TokenizeStyle } from "./types.mjs";
3
+ import { defineTheme } from "./define-theme.mjs";
4
+ import { extendTheme } from "./extend-theme.mjs";
5
+ import { RN_DEFAULT_FONT_SIZE, resolveBaseFontSize } from "./resolvers/line-height-resolver.mjs";
6
+ import { isSchemeColor, walkSemanticColors } from "./semantic-colors.mjs";
7
+ import { TEXT_TOKEN_FIELDS, isTextPreset, resolveTextColor, walkTextPresets } from "./text-tree.mjs";
8
+ export { COLOR_KEYS, FONT_SIZE_KEYS, FONT_WEIGHT_KEYS, LETTER_SPACING_KEYS, LINE_HEIGHT_KEYS, type LooseSchema, RADIUS_KEYS, RN_DEFAULT_FONT_SIZE, SHADOW_STYLE_PROPS, SPACING_KEYS, type SchemeColor, type SemanticColors, type ShadowToken, TEXT_TOKEN_FIELDS, type TextColor, type TextToken, type TextTokenTree, type ThemeConfig, type ThemedSchema, type ThemedStyles, type TokenizeStyle, Z_INDEX_KEYS, defineTheme, extendTheme, isSchemeColor, isTextPreset, resolveBaseFontSize, resolveTextColor, walkSemanticColors, walkTextPresets };
@@ -0,0 +1,7 @@
1
+ import { defineTheme } from "./define-theme.mjs";
2
+ import { isSchemeColor, walkSemanticColors } from "./semantic-colors.mjs";
3
+ import { TEXT_TOKEN_FIELDS, isTextPreset, resolveTextColor, walkTextPresets } from "./text-tree.mjs";
4
+ import { extendTheme } from "./extend-theme.mjs";
5
+ import { RN_DEFAULT_FONT_SIZE, resolveBaseFontSize } from "./resolvers/line-height-resolver.mjs";
6
+ import { COLOR_KEYS, FONT_SIZE_KEYS, FONT_WEIGHT_KEYS, LETTER_SPACING_KEYS, LINE_HEIGHT_KEYS, RADIUS_KEYS, SHADOW_STYLE_PROPS, SPACING_KEYS, Z_INDEX_KEYS } from "./style-props.mjs";
7
+ export { COLOR_KEYS, FONT_SIZE_KEYS, FONT_WEIGHT_KEYS, LETTER_SPACING_KEYS, LINE_HEIGHT_KEYS, RADIUS_KEYS, RN_DEFAULT_FONT_SIZE, SHADOW_STYLE_PROPS, SPACING_KEYS, TEXT_TOKEN_FIELDS, Z_INDEX_KEYS, defineTheme, extendTheme, isSchemeColor, isTextPreset, resolveBaseFontSize, resolveTextColor, walkSemanticColors, walkTextPresets };
@@ -0,0 +1,31 @@
1
+ const require_text_tree = require("./text-tree.cjs");
2
+ const require_style_resolver = require("./resolvers/style-resolver.cjs");
3
+ const require_text_variants = require("./text-variants.cjs");
4
+ //#region src/create-themed-styles.ts
5
+ /**
6
+ * Builds `{ view, text, image }` from a fully resolved `ThemeConfig` for one
7
+ * fixed color scheme. No React here — plain data in, plain functions out.
8
+ * `createThemed` calls this once per scheme and hands the result out through
9
+ * `useThemed()`, so components re-render (and pick up new colors) when the
10
+ * scheme changes.
11
+ *
12
+ * `S` comes from the generated `themed.gen.ts`; without it every token
13
+ * prop is loosely typed (`LooseSchema`).
14
+ */
15
+ function createThemedStyles(config, scheme) {
16
+ const resolveStyle = require_style_resolver.createStyleResolver(config, scheme);
17
+ const text = (input = {}) => resolveStyle(input);
18
+ const variants = require_text_variants.createTextVariants(require_text_tree.resolveTextTree(config.semanticTokens?.text, scheme), (input) => resolveStyle(input));
19
+ for (const [role, sizes] of Object.entries(variants)) Object.defineProperty(text, role, {
20
+ value: sizes,
21
+ enumerable: true,
22
+ configurable: true
23
+ });
24
+ return {
25
+ view: resolveStyle,
26
+ image: resolveStyle,
27
+ text
28
+ };
29
+ }
30
+ //#endregion
31
+ exports.createThemedStyles = createThemedStyles;
@@ -0,0 +1,15 @@
1
+ import { LooseSchema, ThemeConfig, ThemedSchema, ThemedStyles } from "./types.cjs";
2
+ import { ColorScheme } from "./resolvers/color-resolver.cjs";
3
+ //#region src/create-themed-styles.d.ts
4
+ /**
5
+ * Builds `{ view, text, image }` from a fully resolved `ThemeConfig` for one
6
+ * fixed color scheme. No React here — plain data in, plain functions out.
7
+ * `createThemed` calls this once per scheme and hands the result out through
8
+ * `useThemed()`, so components re-render (and pick up new colors) when the
9
+ * scheme changes.
10
+ *
11
+ * `S` comes from the generated `themed.gen.ts`; without it every token
12
+ * prop is loosely typed (`LooseSchema`).
13
+ */
14
+ export declare function createThemedStyles<S extends ThemedSchema = LooseSchema>(config: ThemeConfig, scheme: ColorScheme): ThemedStyles<S>;
15
+ //#endregion
@@ -0,0 +1,15 @@
1
+ import { LooseSchema, ThemeConfig, ThemedSchema, ThemedStyles } from "./types.mjs";
2
+ import { ColorScheme } from "./resolvers/color-resolver.mjs";
3
+ //#region src/create-themed-styles.d.ts
4
+ /**
5
+ * Builds `{ view, text, image }` from a fully resolved `ThemeConfig` for one
6
+ * fixed color scheme. No React here — plain data in, plain functions out.
7
+ * `createThemed` calls this once per scheme and hands the result out through
8
+ * `useThemed()`, so components re-render (and pick up new colors) when the
9
+ * scheme changes.
10
+ *
11
+ * `S` comes from the generated `themed.gen.ts`; without it every token
12
+ * prop is loosely typed (`LooseSchema`).
13
+ */
14
+ export declare function createThemedStyles<S extends ThemedSchema = LooseSchema>(config: ThemeConfig, scheme: ColorScheme): ThemedStyles<S>;
15
+ //#endregion
@@ -0,0 +1,31 @@
1
+ import { resolveTextTree } from "./text-tree.mjs";
2
+ import { createStyleResolver } from "./resolvers/style-resolver.mjs";
3
+ import { createTextVariants } from "./text-variants.mjs";
4
+ //#region src/create-themed-styles.ts
5
+ /**
6
+ * Builds `{ view, text, image }` from a fully resolved `ThemeConfig` for one
7
+ * fixed color scheme. No React here — plain data in, plain functions out.
8
+ * `createThemed` calls this once per scheme and hands the result out through
9
+ * `useThemed()`, so components re-render (and pick up new colors) when the
10
+ * scheme changes.
11
+ *
12
+ * `S` comes from the generated `themed.gen.ts`; without it every token
13
+ * prop is loosely typed (`LooseSchema`).
14
+ */
15
+ function createThemedStyles(config, scheme) {
16
+ const resolveStyle = createStyleResolver(config, scheme);
17
+ const text = (input = {}) => resolveStyle(input);
18
+ const variants = createTextVariants(resolveTextTree(config.semanticTokens?.text, scheme), (input) => resolveStyle(input));
19
+ for (const [role, sizes] of Object.entries(variants)) Object.defineProperty(text, role, {
20
+ value: sizes,
21
+ enumerable: true,
22
+ configurable: true
23
+ });
24
+ return {
25
+ view: resolveStyle,
26
+ image: resolveStyle,
27
+ text
28
+ };
29
+ }
30
+ //#endregion
31
+ export { createThemedStyles };
@@ -0,0 +1,93 @@
1
+ const require_text_tree = require("./text-tree.cjs");
2
+ const require_color_mode_store = require("./color-mode-store.cjs");
3
+ const require_create_themed_styles = require("./create-themed-styles.cjs");
4
+ const require_color_resolver = require("./resolvers/color-resolver.cjs");
5
+ let react = require("react");
6
+ let react_native = require("react-native");
7
+ //#region src/create-themed.ts
8
+ const DEFAULT_STORAGE_KEY = "react-native-rethemed.color-mode";
9
+ /** RN 0.86 reports `'unspecified'` (and may report `null`); the theme is binary. */
10
+ function toColorScheme(value) {
11
+ return value === "dark" ? "dark" : "light";
12
+ }
13
+ /** `Appearance.setColorScheme` is RN 0.73+ only and absent on react-native-web. */
14
+ function syncNativeAppearance(mode) {
15
+ if (typeof react_native.Appearance?.setColorScheme !== "function") return;
16
+ react_native.Appearance.setColorScheme(mode === "system" ? "unspecified" : mode);
17
+ }
18
+ const warnControlledSetMode = () => {
19
+ if (__DEV__) console.warn("[react-native-rethemed] setMode() was called while <ThemedProvider> is controlled via the `colorScheme` prop. Update that prop instead.");
20
+ };
21
+ /**
22
+ * Binds a theme config to React: returns a provider plus hooks that read
23
+ * the current scheme from it. Everything (store, contexts, precomputed
24
+ * themes) is scoped to this call, so several instances can coexist.
25
+ *
26
+ * `S` is supplied by the generated `themed.gen.ts`
27
+ * (`@react-native-rethemed/cli codegen`); without it tokens are loosely typed.
28
+ */
29
+ function createThemed(config) {
30
+ const build = (colorScheme) => ({
31
+ themed: require_create_themed_styles.createThemedStyles(config, colorScheme),
32
+ tokens: config.tokens ?? {},
33
+ semanticTokens: {
34
+ colors: require_color_resolver.resolveSemanticColors(config, colorScheme),
35
+ text: require_text_tree.resolveTextTree(config.semanticTokens?.text, colorScheme)
36
+ },
37
+ colorScheme
38
+ });
39
+ const themes = {
40
+ light: build("light"),
41
+ dark: build("dark")
42
+ };
43
+ const store = require_color_mode_store.createColorModeStore();
44
+ const SchemeContext = (0, react.createContext)(null);
45
+ const ModeContext = (0, react.createContext)(null);
46
+ function ThemedProvider({ children, colorScheme, defaultColorMode = "system", storage, storageKey = DEFAULT_STORAGE_KEY }) {
47
+ const isControlled = colorScheme !== void 0;
48
+ store.init(defaultColorMode);
49
+ const storedMode = (0, react.useSyncExternalStore)(store.subscribe, store.getSnapshot, store.getSnapshot);
50
+ const systemScheme = toColorScheme((0, react_native.useColorScheme)());
51
+ (0, react.useEffect)(() => {
52
+ if (isControlled || !storage || !store.beginHydration()) return;
53
+ Promise.resolve().then(() => storage.getItem(storageKey)).then((saved) => store.hydrate(saved)).catch(() => {});
54
+ }, [
55
+ isControlled,
56
+ storage,
57
+ storageKey
58
+ ]);
59
+ (0, react.useEffect)(() => {
60
+ if (!isControlled) syncNativeAppearance(storedMode);
61
+ }, [isControlled, storedMode]);
62
+ const setUncontrolledMode = (0, react.useCallback)((next) => {
63
+ syncNativeAppearance(next);
64
+ store.setMode(next);
65
+ if (storage) Promise.resolve().then(() => storage.setItem(storageKey, next)).catch(() => {});
66
+ }, [storage, storageKey]);
67
+ const scheme = isControlled ? colorScheme : storedMode === "system" ? systemScheme : storedMode;
68
+ const mode = isControlled ? colorScheme : storedMode;
69
+ const setMode = isControlled ? warnControlledSetMode : setUncontrolledMode;
70
+ const modeValue = (0, react.useMemo)(() => ({
71
+ mode,
72
+ setMode
73
+ }), [mode, setMode]);
74
+ return (0, react.createElement)(SchemeContext, { value: scheme }, (0, react.createElement)(ModeContext, { value: modeValue }, children));
75
+ }
76
+ function useThemed() {
77
+ const scheme = (0, react.useContext)(SchemeContext);
78
+ if (scheme === null) throw new Error("useThemed() must be used within a <ThemedProvider> returned by the same createThemed() call.");
79
+ return themes[scheme];
80
+ }
81
+ function useColorMode() {
82
+ const value = (0, react.useContext)(ModeContext);
83
+ if (value === null) throw new Error("useColorMode() must be used within a <ThemedProvider> returned by the same createThemed() call.");
84
+ return value;
85
+ }
86
+ return {
87
+ ThemedProvider,
88
+ useThemed,
89
+ useColorMode
90
+ };
91
+ }
92
+ //#endregion
93
+ exports.createThemed = createThemed;
@@ -0,0 +1,48 @@
1
+ import { LooseSchema, ThemeConfig, ThemedSchema, ThemedStyles } from "./types.cjs";
2
+ import { ColorScheme } from "./resolvers/color-resolver.cjs";
3
+ import { ColorMode, ColorModeStorage } from "./color-mode-store.cjs";
4
+ import { ReactNode } from "react";
5
+ //#region src/create-themed.d.ts
6
+ export type ThemedProviderProps = {
7
+ children?: ReactNode;
8
+ /**
9
+ * Controlled mode: the app owns the scheme and this value is used as-is.
10
+ * When set, `defaultColorMode`/`storage`/`storageKey` are ignored.
11
+ */
12
+ colorScheme?: ColorScheme;
13
+ /** Uncontrolled initial mode. Only read the first time the store is used. */
14
+ defaultColorMode?: ColorMode;
15
+ /** Uncontrolled persistence, e.g. `expo-secure-store` or `AsyncStorage`. */
16
+ storage?: ColorModeStorage;
17
+ storageKey?: string;
18
+ };
19
+ export type UseThemedResult<S extends ThemedSchema = LooseSchema> = {
20
+ themed: ThemedStyles<S>;
21
+ /** Primitive, scheme-independent tokens exactly as in the config. */
22
+ tokens: S['tokens'];
23
+ /**
24
+ * Semantic tokens with colors (including text preset colors) resolved for
25
+ * the current scheme.
26
+ */
27
+ semanticTokens: S['semanticTokens'];
28
+ colorScheme: ColorScheme;
29
+ };
30
+ export type UseColorModeResult = {
31
+ mode: ColorMode;
32
+ setMode: (mode: ColorMode) => void;
33
+ };
34
+ /**
35
+ * Binds a theme config to React: returns a provider plus hooks that read
36
+ * the current scheme from it. Everything (store, contexts, precomputed
37
+ * themes) is scoped to this call, so several instances can coexist.
38
+ *
39
+ * `S` is supplied by the generated `themed.gen.ts`
40
+ * (`@react-native-rethemed/cli codegen`); without it tokens are loosely typed.
41
+ */
42
+ export declare function createThemed<S extends ThemedSchema = LooseSchema>(config: ThemeConfig): {
43
+ ThemedProvider: ({ children, colorScheme, defaultColorMode, storage, storageKey }: ThemedProviderProps) => import("react").FunctionComponentElement<import("react").ProviderProps<ColorScheme | null>>;
44
+ useThemed: () => UseThemedResult<S>;
45
+ useColorMode: () => UseColorModeResult;
46
+ };
47
+ //#endregion
48
+ export type { ColorMode, ColorModeStorage, ColorScheme };
@@ -0,0 +1,48 @@
1
+ import { LooseSchema, ThemeConfig, ThemedSchema, ThemedStyles } from "./types.mjs";
2
+ import { ColorScheme } from "./resolvers/color-resolver.mjs";
3
+ import { ColorMode, ColorModeStorage } from "./color-mode-store.mjs";
4
+ import { ReactNode } from "react";
5
+ //#region src/create-themed.d.ts
6
+ export type ThemedProviderProps = {
7
+ children?: ReactNode;
8
+ /**
9
+ * Controlled mode: the app owns the scheme and this value is used as-is.
10
+ * When set, `defaultColorMode`/`storage`/`storageKey` are ignored.
11
+ */
12
+ colorScheme?: ColorScheme;
13
+ /** Uncontrolled initial mode. Only read the first time the store is used. */
14
+ defaultColorMode?: ColorMode;
15
+ /** Uncontrolled persistence, e.g. `expo-secure-store` or `AsyncStorage`. */
16
+ storage?: ColorModeStorage;
17
+ storageKey?: string;
18
+ };
19
+ export type UseThemedResult<S extends ThemedSchema = LooseSchema> = {
20
+ themed: ThemedStyles<S>;
21
+ /** Primitive, scheme-independent tokens exactly as in the config. */
22
+ tokens: S['tokens'];
23
+ /**
24
+ * Semantic tokens with colors (including text preset colors) resolved for
25
+ * the current scheme.
26
+ */
27
+ semanticTokens: S['semanticTokens'];
28
+ colorScheme: ColorScheme;
29
+ };
30
+ export type UseColorModeResult = {
31
+ mode: ColorMode;
32
+ setMode: (mode: ColorMode) => void;
33
+ };
34
+ /**
35
+ * Binds a theme config to React: returns a provider plus hooks that read
36
+ * the current scheme from it. Everything (store, contexts, precomputed
37
+ * themes) is scoped to this call, so several instances can coexist.
38
+ *
39
+ * `S` is supplied by the generated `themed.gen.ts`
40
+ * (`@react-native-rethemed/cli codegen`); without it tokens are loosely typed.
41
+ */
42
+ export declare function createThemed<S extends ThemedSchema = LooseSchema>(config: ThemeConfig): {
43
+ ThemedProvider: ({ children, colorScheme, defaultColorMode, storage, storageKey }: ThemedProviderProps) => import("react").FunctionComponentElement<import("react").ProviderProps<ColorScheme | null>>;
44
+ useThemed: () => UseThemedResult<S>;
45
+ useColorMode: () => UseColorModeResult;
46
+ };
47
+ //#endregion
48
+ export type { ColorMode, ColorModeStorage, ColorScheme };