@umber-style/react-native 0.0.0 → 0.1.0-alpha.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 +11 -1
  3. package/dist/CacheProvider.cjs +7 -0
  4. package/dist/CacheProvider.d.cts +4 -0
  5. package/dist/CacheProvider.d.mts +4 -0
  6. package/dist/CacheProvider.mjs +7 -0
  7. package/dist/ThemeValue.cjs +68 -0
  8. package/dist/ThemeValue.mjs +68 -0
  9. package/dist/alpha.cjs +16 -0
  10. package/dist/alpha.d.cts +8 -0
  11. package/dist/alpha.d.mts +8 -0
  12. package/dist/alpha.mjs +16 -0
  13. package/dist/calc.cjs +90 -0
  14. package/dist/calc.d.cts +24 -0
  15. package/dist/calc.d.mts +24 -0
  16. package/dist/calc.mjs +86 -0
  17. package/dist/conditions.cjs +29 -0
  18. package/dist/conditions.mjs +27 -0
  19. package/dist/createStyle.cjs +183 -0
  20. package/dist/createStyle.d.cts +8 -0
  21. package/dist/createStyle.d.mts +8 -0
  22. package/dist/createStyle.mjs +182 -0
  23. package/dist/defaultStyle.cjs +69 -0
  24. package/dist/defaultStyle.d.cts +34 -0
  25. package/dist/defaultStyle.d.mts +34 -0
  26. package/dist/defaultStyle.mjs +69 -0
  27. package/dist/index.cjs +41 -0
  28. package/dist/index.d.cts +21 -0
  29. package/dist/index.d.mts +21 -0
  30. package/dist/index.mjs +8 -0
  31. package/dist/mediaQuery.cjs +179 -0
  32. package/dist/mediaQuery.d.cts +1 -0
  33. package/dist/mediaQuery.mjs +178 -0
  34. package/dist/nativeStyle.d.cts +32 -0
  35. package/dist/nativeStyle.d.mts +32 -0
  36. package/dist/nativeTheme.cjs +51 -0
  37. package/dist/nativeTheme.d.cts +15 -0
  38. package/dist/nativeTheme.d.mts +15 -0
  39. package/dist/nativeTheme.mjs +50 -0
  40. package/dist/platform.d.cts +2 -0
  41. package/dist/platform.d.mts +2 -0
  42. package/dist/platform.mjs +1 -0
  43. package/dist/styled.cjs +120 -0
  44. package/dist/styled.mjs +120 -0
  45. package/dist/toNativeStyle.cjs +88 -0
  46. package/dist/toNativeStyle.mjs +87 -0
  47. package/dist/types.d.cts +130 -0
  48. package/dist/types.d.mts +130 -0
  49. package/dist/units.cjs +137 -0
  50. package/dist/units.mjs +133 -0
  51. package/dist/warnStyle.cjs +97 -0
  52. package/dist/warnStyle.mjs +95 -0
  53. package/dist/windowState.cjs +89 -0
  54. package/dist/windowState.d.cts +6 -0
  55. package/dist/windowState.d.mts +5 -0
  56. package/dist/windowState.mjs +85 -0
  57. package/package.json +77 -5
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 soroush.tech
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 +1,11 @@
1
- Placeholder. See https://github.com/soroush-tech/umber-style
1
+ # @umber-style/react-native
2
+
3
+ The React Native binding of UmberStyle: the same names as the React binding, over the real theme values.
4
+
5
+ Install: `pnpm add @umber-style/react-native`.
6
+
7
+ ```ts
8
+ import { styled, ThemeProvider } from '@umber-style/react-native'
9
+ ```
10
+
11
+ Documentation: [docs](../../docs/README.md), and [React Native](../../docs/react-native.md) for this package.
@@ -0,0 +1,7 @@
1
+ let react = require("react");
2
+ // src/CacheProvider.ts
3
+ /** `CacheProvider` on React Native: there is no CSS to write, so it renders its children and reads no cache. */
4
+ function CacheProvider({ children }) {
5
+ return (0, react.createElement)(react.Fragment, null, children);
6
+ }
7
+ exports.CacheProvider = CacheProvider;
@@ -0,0 +1,4 @@
1
+ import { ReactElement } from "react";
2
+ import { CacheProviderProps } from "./types.cjs";
3
+ /** `CacheProvider` on React Native: there is no CSS to write, so it renders its children and reads no cache. */
4
+ export declare function CacheProvider({ children }: CacheProviderProps): ReactElement;
@@ -0,0 +1,4 @@
1
+ import { ReactElement } from "react";
2
+ import { CacheProviderProps } from "./types.mjs";
3
+ /** `CacheProvider` on React Native: there is no CSS to write, so it renders its children and reads no cache. */
4
+ export declare function CacheProvider({ children }: CacheProviderProps): ReactElement;
@@ -0,0 +1,7 @@
1
+ import { Fragment, createElement } from "react";
2
+ // src/CacheProvider.ts
3
+ /** `CacheProvider` on React Native: there is no CSS to write, so it renders its children and reads no cache. */
4
+ function CacheProvider({ children }) {
5
+ return createElement(Fragment, null, children);
6
+ }
7
+ export { CacheProvider };
@@ -0,0 +1,68 @@
1
+ "use client";
2
+ let react = require("react");
3
+ let _umber_style_core = require("@umber-style/core");
4
+ const require_conditions = require("./conditions.cjs");
5
+ let react_jsx_runtime = require("react/jsx-runtime");
6
+ // src/ThemeValue.tsx
7
+ /**
8
+ * The contexts of the engine: the real theme values (`ThemeContext`, made here unless given; the base
9
+ * theme of `holder`, else an empty object, where there is no provider), and what a condition is matched
10
+ * against. A theme is held as React Native takes it: `convert` makes that of each theme object once, and
11
+ * the result is remembered for that object. A provider merges, and a function theme receives, the theme
12
+ * as it was given, not the converted one, so that one source does the same on both platforms.
13
+ */
14
+ function createThemeValue(holder, convert, ThemeContext = (0, react.createContext)(void 0)) {
15
+ const ConditionContext = (0, react.createContext)(void 0);
16
+ const nativeByTheme = /* @__PURE__ */ new WeakMap();
17
+ const themeByNative = /* @__PURE__ */ new WeakMap();
18
+ /** `theme` converted, once for each theme object. */
19
+ const toNative = (theme) => {
20
+ let native = nativeByTheme.get(theme);
21
+ if (native === void 0) {
22
+ native = convert(theme);
23
+ nativeByTheme.set(theme, native);
24
+ themeByNative.set(native, theme);
25
+ }
26
+ return native;
27
+ };
28
+ /** The theme where no provider is above: the base theme, or the empty theme. */
29
+ const readOwnTheme = () => holder.baseTheme ?? _umber_style_core.EMPTY_THEME;
30
+ function ThemeValueProvider({ theme, colorScheme, children }) {
31
+ const above = (0, react.useContext)(ThemeContext);
32
+ const outer = above === void 0 ? holder.baseTheme : themeByNative.get(above) ?? above;
33
+ const outerScheme = (0, react.useContext)(ConditionContext)?.colorScheme;
34
+ const given = (0, react.useMemo)(() => (0, _umber_style_core.resolveThemeInput)(theme, outer ?? _umber_style_core.EMPTY_THEME), [theme, outer]);
35
+ holder.baseTheme ??= given;
36
+ const value = (0, react.useMemo)(() => toNative((0, _umber_style_core.mergeTheme)(outer, given)), [outer, given]);
37
+ const scheme = require_conditions.readSchemeName(colorScheme) ?? outerScheme;
38
+ const condition = (0, react.useMemo)(() => ({
39
+ theme: given,
40
+ colorScheme: scheme
41
+ }), [given, scheme]);
42
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ThemeContext.Provider, {
43
+ value,
44
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ConditionContext.Provider, {
45
+ value: condition,
46
+ children
47
+ })
48
+ });
49
+ }
50
+ /** The theme of the nearest provider, the base theme, or an empty object, converted. */
51
+ function useTheme() {
52
+ return (0, react.useContext)(ThemeContext) ?? toNative(readOwnTheme());
53
+ }
54
+ /** What `when()` and `whenScheme()` keys are matched against: the nearest provider, or the base theme with no color scheme. */
55
+ function useCondition() {
56
+ return (0, react.useContext)(ConditionContext) ?? {
57
+ theme: holder.baseTheme,
58
+ colorScheme: void 0
59
+ };
60
+ }
61
+ return {
62
+ ThemeContext,
63
+ ThemeValueProvider,
64
+ useTheme,
65
+ useCondition
66
+ };
67
+ }
68
+ exports.createThemeValue = createThemeValue;
@@ -0,0 +1,68 @@
1
+ "use client";
2
+ import { createContext, useContext, useMemo } from "react";
3
+ import { EMPTY_THEME, mergeTheme, resolveThemeInput } from "@umber-style/core";
4
+ import { readSchemeName } from "./conditions.mjs";
5
+ import { jsx } from "react/jsx-runtime";
6
+ // src/ThemeValue.tsx
7
+ /**
8
+ * The contexts of the engine: the real theme values (`ThemeContext`, made here unless given; the base
9
+ * theme of `holder`, else an empty object, where there is no provider), and what a condition is matched
10
+ * against. A theme is held as React Native takes it: `convert` makes that of each theme object once, and
11
+ * the result is remembered for that object. A provider merges, and a function theme receives, the theme
12
+ * as it was given, not the converted one, so that one source does the same on both platforms.
13
+ */
14
+ function createThemeValue(holder, convert, ThemeContext = createContext(void 0)) {
15
+ const ConditionContext = createContext(void 0);
16
+ const nativeByTheme = /* @__PURE__ */ new WeakMap();
17
+ const themeByNative = /* @__PURE__ */ new WeakMap();
18
+ /** `theme` converted, once for each theme object. */
19
+ const toNative = (theme) => {
20
+ let native = nativeByTheme.get(theme);
21
+ if (native === void 0) {
22
+ native = convert(theme);
23
+ nativeByTheme.set(theme, native);
24
+ themeByNative.set(native, theme);
25
+ }
26
+ return native;
27
+ };
28
+ /** The theme where no provider is above: the base theme, or the empty theme. */
29
+ const readOwnTheme = () => holder.baseTheme ?? EMPTY_THEME;
30
+ function ThemeValueProvider({ theme, colorScheme, children }) {
31
+ const above = useContext(ThemeContext);
32
+ const outer = above === void 0 ? holder.baseTheme : themeByNative.get(above) ?? above;
33
+ const outerScheme = useContext(ConditionContext)?.colorScheme;
34
+ const given = useMemo(() => resolveThemeInput(theme, outer ?? EMPTY_THEME), [theme, outer]);
35
+ holder.baseTheme ??= given;
36
+ const value = useMemo(() => toNative(mergeTheme(outer, given)), [outer, given]);
37
+ const scheme = readSchemeName(colorScheme) ?? outerScheme;
38
+ const condition = useMemo(() => ({
39
+ theme: given,
40
+ colorScheme: scheme
41
+ }), [given, scheme]);
42
+ return /* @__PURE__ */ jsx(ThemeContext.Provider, {
43
+ value,
44
+ children: /* @__PURE__ */ jsx(ConditionContext.Provider, {
45
+ value: condition,
46
+ children
47
+ })
48
+ });
49
+ }
50
+ /** The theme of the nearest provider, the base theme, or an empty object, converted. */
51
+ function useTheme() {
52
+ return useContext(ThemeContext) ?? toNative(readOwnTheme());
53
+ }
54
+ /** What `when()` and `whenScheme()` keys are matched against: the nearest provider, or the base theme with no color scheme. */
55
+ function useCondition() {
56
+ return useContext(ConditionContext) ?? {
57
+ theme: holder.baseTheme,
58
+ colorScheme: void 0
59
+ };
60
+ }
61
+ return {
62
+ ThemeContext,
63
+ ThemeValueProvider,
64
+ useTheme,
65
+ useCondition
66
+ };
67
+ }
68
+ export { createThemeValue };
package/dist/alpha.cjs ADDED
@@ -0,0 +1,16 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ // src/alpha.ts
3
+ /**
4
+ * `color` at `opacity` (0 to 1), as one color string: `rgba(r, g, b, a)` for a literal the core can
5
+ * read (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()` with numbers), any other color as
6
+ * it is. 1 returns `color` and 0 returns `transparent`. Throws what the web `alpha` throws: a `TypeError`
7
+ * for a color that is not a string or an opacity that is not a number, a `RangeError` for an opacity
8
+ * outside 0 to 1.
9
+ */
10
+ function alpha(color, opacity) {
11
+ (0, _umber_style_core.assertAlphaInput)(color, opacity);
12
+ if (opacity === 1) return color;
13
+ if (opacity === 0) return "transparent";
14
+ return (0, _umber_style_core.toRgba)(color, opacity) ?? color;
15
+ }
16
+ exports.alpha = alpha;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * `color` at `opacity` (0 to 1), as one color string: `rgba(r, g, b, a)` for a literal the core can
3
+ * read (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()` with numbers), any other color as
4
+ * it is. 1 returns `color` and 0 returns `transparent`. Throws what the web `alpha` throws: a `TypeError`
5
+ * for a color that is not a string or an opacity that is not a number, a `RangeError` for an opacity
6
+ * outside 0 to 1.
7
+ */
8
+ export declare function alpha(color: string, opacity: number): string;
@@ -0,0 +1,8 @@
1
+ /**
2
+ * `color` at `opacity` (0 to 1), as one color string: `rgba(r, g, b, a)` for a literal the core can
3
+ * read (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()` with numbers), any other color as
4
+ * it is. 1 returns `color` and 0 returns `transparent`. Throws what the web `alpha` throws: a `TypeError`
5
+ * for a color that is not a string or an opacity that is not a number, a `RangeError` for an opacity
6
+ * outside 0 to 1.
7
+ */
8
+ export declare function alpha(color: string, opacity: number): string;
package/dist/alpha.mjs ADDED
@@ -0,0 +1,16 @@
1
+ import { assertAlphaInput, toRgba } from "@umber-style/core";
2
+ // src/alpha.ts
3
+ /**
4
+ * `color` at `opacity` (0 to 1), as one color string: `rgba(r, g, b, a)` for a literal the core can
5
+ * read (`#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()` with numbers), any other color as
6
+ * it is. 1 returns `color` and 0 returns `transparent`. Throws what the web `alpha` throws: a `TypeError`
7
+ * for a color that is not a string or an opacity that is not a number, a `RangeError` for an opacity
8
+ * outside 0 to 1.
9
+ */
10
+ function alpha(color, opacity) {
11
+ assertAlphaInput(color, opacity);
12
+ if (opacity === 1) return color;
13
+ if (opacity === 0) return "transparent";
14
+ return toRgba(color, opacity) ?? color;
15
+ }
16
+ export { alpha };
package/dist/calc.cjs ADDED
@@ -0,0 +1,90 @@
1
+ const require_units = require("./units.cjs");
2
+ // src/calc.ts
3
+ /** `value` rounded so that float noise (0.1 * 3) does not show; `-0` is `0`. */
4
+ function tidy(value) {
5
+ return Math.round(value * 1e6) / 1e6 + 0;
6
+ }
7
+ /** The error for a `value` that React Native cannot compute: the helper and the value are in the message. */
8
+ function createUnsupportedError(name, value) {
9
+ return /* @__PURE__ */ new TypeError(`@umber-style/react-native: ${name}() cannot compute ${JSON.stringify(value)}; it takes a number, a numeric string ("8", "8px") or a percentage ("50%"), and a percentage cannot be mixed with a number`);
10
+ }
11
+ /** Throws unless `value` is a finite number. */
12
+ function assertFinite(name, what, value) {
13
+ if (typeof value !== "number") throw new TypeError(`@umber-style/react-native: ${name}(): ${what} must be a number, got ${typeof value}`);
14
+ if (!Number.isFinite(value)) throw new RangeError(`@umber-style/react-native: ${name}(): ${what} must be finite, got ${value}`);
15
+ }
16
+ /**
17
+ * The number of `operand`, and whether it is a percentage; anything else cannot be computed. A string is
18
+ * read as a style value is converted (`readNumber`), but `rem` is refused: a helper has no base for it.
19
+ */
20
+ function read(name, operand) {
21
+ if (typeof operand !== "string") {
22
+ assertFinite(name, "a value", operand);
23
+ return {
24
+ value: operand,
25
+ isPercent: false
26
+ };
27
+ }
28
+ const reading = require_units.readNumber(operand);
29
+ if (reading === void 0 || reading.unit === "rem") throw createUnsupportedError(name, operand);
30
+ return {
31
+ value: reading.value,
32
+ isPercent: reading.unit === "%"
33
+ };
34
+ }
35
+ /** `value` as the percentage that a style takes. */
36
+ function toPercent(value) {
37
+ return `${tidy(value)}%`;
38
+ }
39
+ /** `operand` multiplied or divided by `factor`; a percentage stays a percentage. */
40
+ function scale(name, operand, factor, isDivision) {
41
+ const { value, isPercent } = read(name, operand);
42
+ assertFinite(name, isDivision ? "the divisor" : "the factor", factor);
43
+ if (isDivision && factor === 0) throw new RangeError(`@umber-style/react-native: ${name}(): the divisor must not be 0`);
44
+ const result = isDivision ? value / factor : value * factor;
45
+ return isPercent ? toPercent(result) : tidy(result);
46
+ }
47
+ /** The sum of `terms`: all numbers, or all percentages (a number 0 is both). */
48
+ function sum(name, terms) {
49
+ const readings = terms.map(({ operand, sign }) => ({
50
+ operand,
51
+ sign,
52
+ ...read(name, operand)
53
+ }));
54
+ const isPercent = readings.some((reading) => reading.isPercent);
55
+ let total = 0;
56
+ for (const { operand, sign, value, isPercent: isPercentValue } of readings) {
57
+ if (isPercentValue !== isPercent && operand !== 0) throw createUnsupportedError(name, operand);
58
+ total += sign * value;
59
+ }
60
+ return isPercent ? toPercent(total) : tidy(total);
61
+ }
62
+ function multiply(value, factor) {
63
+ return scale("multiply", value, factor, false);
64
+ }
65
+ function divide(value, divisor) {
66
+ return scale("divide", value, divisor, true);
67
+ }
68
+ function add(...values) {
69
+ return sum("add", values.map((operand) => ({
70
+ operand,
71
+ sign: 1
72
+ })));
73
+ }
74
+ function subtract(value, ...values) {
75
+ return sum("subtract", [{
76
+ operand: value,
77
+ sign: 1
78
+ }, ...values.map((operand) => ({
79
+ operand,
80
+ sign: -1
81
+ }))]);
82
+ }
83
+ function negate(value) {
84
+ return scale("negate", value, -1, false);
85
+ }
86
+ exports.add = add;
87
+ exports.divide = divide;
88
+ exports.multiply = multiply;
89
+ exports.negate = negate;
90
+ exports.subtract = subtract;
@@ -0,0 +1,24 @@
1
+ /** What arithmetic on tokens takes on React Native: a number, a numeric string, or a percentage string. */
2
+ export type Operand = string | number;
3
+ /** A percentage, as the platform takes it: `'50%'`. */
4
+ export type Percent = `${number}%`;
5
+ /** `value` times `factor`: a number for a number, a percentage for a percentage. */
6
+ export declare function multiply(value: number, factor: number): number;
7
+ export declare function multiply(value: Percent, factor: number): Percent;
8
+ export declare function multiply(value: Operand, factor: number): number | Percent;
9
+ /** `value` divided by `divisor`; a `divisor` of 0 throws a `RangeError`. */
10
+ export declare function divide(value: number, divisor: number): number;
11
+ export declare function divide(value: Percent, divisor: number): Percent;
12
+ export declare function divide(value: Operand, divisor: number): number | Percent;
13
+ /** The sum of `values`: all numbers, or all percentages. */
14
+ export declare function add(...values: number[]): number;
15
+ export declare function add(...values: Percent[]): Percent;
16
+ export declare function add(...values: Operand[]): number | Percent;
17
+ /** `value` minus each of `values`: all numbers, or all percentages. */
18
+ export declare function subtract(value: number, ...values: number[]): number;
19
+ export declare function subtract(value: Percent, ...values: Percent[]): Percent;
20
+ export declare function subtract(value: Operand, ...values: Operand[]): number | Percent;
21
+ /** `value` times -1: a negative margin from a token. */
22
+ export declare function negate(value: number): number;
23
+ export declare function negate(value: Percent): Percent;
24
+ export declare function negate(value: Operand): number | Percent;
@@ -0,0 +1,24 @@
1
+ /** What arithmetic on tokens takes on React Native: a number, a numeric string, or a percentage string. */
2
+ export type Operand = string | number;
3
+ /** A percentage, as the platform takes it: `'50%'`. */
4
+ export type Percent = `${number}%`;
5
+ /** `value` times `factor`: a number for a number, a percentage for a percentage. */
6
+ export declare function multiply(value: number, factor: number): number;
7
+ export declare function multiply(value: Percent, factor: number): Percent;
8
+ export declare function multiply(value: Operand, factor: number): number | Percent;
9
+ /** `value` divided by `divisor`; a `divisor` of 0 throws a `RangeError`. */
10
+ export declare function divide(value: number, divisor: number): number;
11
+ export declare function divide(value: Percent, divisor: number): Percent;
12
+ export declare function divide(value: Operand, divisor: number): number | Percent;
13
+ /** The sum of `values`: all numbers, or all percentages. */
14
+ export declare function add(...values: number[]): number;
15
+ export declare function add(...values: Percent[]): Percent;
16
+ export declare function add(...values: Operand[]): number | Percent;
17
+ /** `value` minus each of `values`: all numbers, or all percentages. */
18
+ export declare function subtract(value: number, ...values: number[]): number;
19
+ export declare function subtract(value: Percent, ...values: Percent[]): Percent;
20
+ export declare function subtract(value: Operand, ...values: Operand[]): number | Percent;
21
+ /** `value` times -1: a negative margin from a token. */
22
+ export declare function negate(value: number): number;
23
+ export declare function negate(value: Percent): Percent;
24
+ export declare function negate(value: Operand): number | Percent;
package/dist/calc.mjs ADDED
@@ -0,0 +1,86 @@
1
+ import { readNumber } from "./units.mjs";
2
+ // src/calc.ts
3
+ /** `value` rounded so that float noise (0.1 * 3) does not show; `-0` is `0`. */
4
+ function tidy(value) {
5
+ return Math.round(value * 1e6) / 1e6 + 0;
6
+ }
7
+ /** The error for a `value` that React Native cannot compute: the helper and the value are in the message. */
8
+ function createUnsupportedError(name, value) {
9
+ return /* @__PURE__ */ new TypeError(`@umber-style/react-native: ${name}() cannot compute ${JSON.stringify(value)}; it takes a number, a numeric string ("8", "8px") or a percentage ("50%"), and a percentage cannot be mixed with a number`);
10
+ }
11
+ /** Throws unless `value` is a finite number. */
12
+ function assertFinite(name, what, value) {
13
+ if (typeof value !== "number") throw new TypeError(`@umber-style/react-native: ${name}(): ${what} must be a number, got ${typeof value}`);
14
+ if (!Number.isFinite(value)) throw new RangeError(`@umber-style/react-native: ${name}(): ${what} must be finite, got ${value}`);
15
+ }
16
+ /**
17
+ * The number of `operand`, and whether it is a percentage; anything else cannot be computed. A string is
18
+ * read as a style value is converted (`readNumber`), but `rem` is refused: a helper has no base for it.
19
+ */
20
+ function read(name, operand) {
21
+ if (typeof operand !== "string") {
22
+ assertFinite(name, "a value", operand);
23
+ return {
24
+ value: operand,
25
+ isPercent: false
26
+ };
27
+ }
28
+ const reading = readNumber(operand);
29
+ if (reading === void 0 || reading.unit === "rem") throw createUnsupportedError(name, operand);
30
+ return {
31
+ value: reading.value,
32
+ isPercent: reading.unit === "%"
33
+ };
34
+ }
35
+ /** `value` as the percentage that a style takes. */
36
+ function toPercent(value) {
37
+ return `${tidy(value)}%`;
38
+ }
39
+ /** `operand` multiplied or divided by `factor`; a percentage stays a percentage. */
40
+ function scale(name, operand, factor, isDivision) {
41
+ const { value, isPercent } = read(name, operand);
42
+ assertFinite(name, isDivision ? "the divisor" : "the factor", factor);
43
+ if (isDivision && factor === 0) throw new RangeError(`@umber-style/react-native: ${name}(): the divisor must not be 0`);
44
+ const result = isDivision ? value / factor : value * factor;
45
+ return isPercent ? toPercent(result) : tidy(result);
46
+ }
47
+ /** The sum of `terms`: all numbers, or all percentages (a number 0 is both). */
48
+ function sum(name, terms) {
49
+ const readings = terms.map(({ operand, sign }) => ({
50
+ operand,
51
+ sign,
52
+ ...read(name, operand)
53
+ }));
54
+ const isPercent = readings.some((reading) => reading.isPercent);
55
+ let total = 0;
56
+ for (const { operand, sign, value, isPercent: isPercentValue } of readings) {
57
+ if (isPercentValue !== isPercent && operand !== 0) throw createUnsupportedError(name, operand);
58
+ total += sign * value;
59
+ }
60
+ return isPercent ? toPercent(total) : tidy(total);
61
+ }
62
+ function multiply(value, factor) {
63
+ return scale("multiply", value, factor, false);
64
+ }
65
+ function divide(value, divisor) {
66
+ return scale("divide", value, divisor, true);
67
+ }
68
+ function add(...values) {
69
+ return sum("add", values.map((operand) => ({
70
+ operand,
71
+ sign: 1
72
+ })));
73
+ }
74
+ function subtract(value, ...values) {
75
+ return sum("subtract", [{
76
+ operand: value,
77
+ sign: 1
78
+ }, ...values.map((operand) => ({
79
+ operand,
80
+ sign: -1
81
+ }))]);
82
+ }
83
+ function negate(value) {
84
+ return scale("negate", value, -1, false);
85
+ }
86
+ export { add, divide, multiply, negate, subtract };
@@ -0,0 +1,29 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ let react_native = require("react-native");
3
+ // src/conditions.ts
4
+ const themesById = /* @__PURE__ */ new Map();
5
+ const idsByTheme = /* @__PURE__ */ new WeakMap();
6
+ /** The key of `when(theme)`: it names the theme object itself, which `matchesCondition` compares by identity. */
7
+ function createNativeThemeKey(theme) {
8
+ let id = idsByTheme.get(theme);
9
+ if (id === void 0) {
10
+ id = `n${themesById.size}`;
11
+ idsByTheme.set(theme, id);
12
+ themesById.set(id, theme);
13
+ }
14
+ return (0, _umber_style_core.createThemeKey)(id);
15
+ }
16
+ /** `light` or `dark` as given to a provider; `light dark` and nothing say no scheme of their own. */
17
+ function readSchemeName(colorScheme) {
18
+ return colorScheme === "light" || colorScheme === "dark" ? colorScheme : void 0;
19
+ }
20
+ /** Whether the condition key `key` holds for `current`: it is a condition key, and the nearest theme or color scheme is the one it names. */
21
+ function matchesCondition(key, current) {
22
+ const condition = (0, _umber_style_core.readCondition)(key);
23
+ if (condition === void 0 || current === void 0) return false;
24
+ if (condition.kind === "theme") return themesById.get(condition.id) === current.theme;
25
+ return (current.colorScheme ?? react_native.Appearance.getColorScheme()) === condition.scheme;
26
+ }
27
+ exports.createNativeThemeKey = createNativeThemeKey;
28
+ exports.matchesCondition = matchesCondition;
29
+ exports.readSchemeName = readSchemeName;
@@ -0,0 +1,27 @@
1
+ import { createThemeKey, readCondition } from "@umber-style/core";
2
+ import { platformAppearance } from "./platform.mjs";
3
+ // src/conditions.ts
4
+ const themesById = /* @__PURE__ */ new Map();
5
+ const idsByTheme = /* @__PURE__ */ new WeakMap();
6
+ /** The key of `when(theme)`: it names the theme object itself, which `matchesCondition` compares by identity. */
7
+ function createNativeThemeKey(theme) {
8
+ let id = idsByTheme.get(theme);
9
+ if (id === void 0) {
10
+ id = `n${themesById.size}`;
11
+ idsByTheme.set(theme, id);
12
+ themesById.set(id, theme);
13
+ }
14
+ return createThemeKey(id);
15
+ }
16
+ /** `light` or `dark` as given to a provider; `light dark` and nothing say no scheme of their own. */
17
+ function readSchemeName(colorScheme) {
18
+ return colorScheme === "light" || colorScheme === "dark" ? colorScheme : void 0;
19
+ }
20
+ /** Whether the condition key `key` holds for `current`: it is a condition key, and the nearest theme or color scheme is the one it names. */
21
+ function matchesCondition(key, current) {
22
+ const condition = readCondition(key);
23
+ if (condition === void 0 || current === void 0) return false;
24
+ if (condition.kind === "theme") return themesById.get(condition.id) === current.theme;
25
+ return (current.colorScheme ?? platformAppearance.getColorScheme()) === condition.scheme;
26
+ }
27
+ export { createNativeThemeKey, matchesCondition, readSchemeName };