@umber-style/react-native 0.0.0-stage → 0.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +10 -2
- package/dist/ThemeValue.cjs +51 -0
- package/dist/ThemeValue.mjs +51 -0
- package/dist/alpha.cjs +16 -0
- package/dist/alpha.d.cts +8 -0
- package/dist/alpha.d.mts +8 -0
- package/dist/alpha.mjs +16 -0
- package/dist/calc.cjs +87 -0
- package/dist/calc.d.cts +24 -0
- package/dist/calc.d.mts +24 -0
- package/dist/calc.mjs +83 -0
- package/dist/conditions.cjs +29 -0
- package/dist/conditions.mjs +27 -0
- package/dist/createStyle.cjs +79 -0
- package/dist/createStyle.d.cts +10 -0
- package/dist/createStyle.d.mts +10 -0
- package/dist/createStyle.mjs +78 -0
- package/dist/defaultStyle.cjs +43 -0
- package/dist/defaultStyle.d.cts +23 -0
- package/dist/defaultStyle.d.mts +23 -0
- package/dist/defaultStyle.mjs +43 -0
- package/dist/index.cjs +32 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.mjs +7 -0
- package/dist/nativeStyle.d.cts +32 -0
- package/dist/nativeStyle.d.mts +32 -0
- package/dist/platform.d.cts +2 -0
- package/dist/platform.d.mts +2 -0
- package/dist/platform.mjs +1 -0
- package/dist/styled.cjs +44 -0
- package/dist/styled.mjs +44 -0
- package/dist/toNativeStyle.cjs +39 -0
- package/dist/toNativeStyle.mjs +38 -0
- package/dist/types.d.cts +62 -0
- package/dist/types.d.mts +62 -0
- package/dist/warnStyle.cjs +53 -0
- package/dist/warnStyle.mjs +53 -0
- package/package.json +79 -3
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,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @umber-style/react-native
|
|
2
2
|
|
|
3
|
-
|
|
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,51 @@
|
|
|
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
|
+
/** The contexts of the engine: the real theme values (the base theme of `holder` is the default), and what a condition is matched against. */
|
|
8
|
+
function createThemeValue(holder) {
|
|
9
|
+
const ThemeContext = (0, react.createContext)(void 0);
|
|
10
|
+
const ConditionContext = (0, react.createContext)(void 0);
|
|
11
|
+
function ThemeValueProvider({ theme, colorScheme, children }) {
|
|
12
|
+
const outer = (0, react.useContext)(ThemeContext) ?? holder.baseTheme;
|
|
13
|
+
const outerScheme = (0, react.useContext)(ConditionContext)?.colorScheme;
|
|
14
|
+
const value = (0, react.useMemo)(() => outer === void 0 ? theme : (0, _umber_style_core.mergeDeep)(outer, theme), [outer, theme]);
|
|
15
|
+
const scheme = require_conditions.readSchemeName(colorScheme) ?? outerScheme;
|
|
16
|
+
const condition = (0, react.useMemo)(() => ({
|
|
17
|
+
theme,
|
|
18
|
+
colorScheme: scheme
|
|
19
|
+
}), [theme, scheme]);
|
|
20
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ThemeContext.Provider, {
|
|
21
|
+
value,
|
|
22
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ConditionContext.Provider, {
|
|
23
|
+
value: condition,
|
|
24
|
+
children
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** The theme above, or the base theme, or undefined when there is none. */
|
|
29
|
+
function useThemeValue() {
|
|
30
|
+
return (0, react.useContext)(ThemeContext) ?? holder.baseTheme;
|
|
31
|
+
}
|
|
32
|
+
function useTheme() {
|
|
33
|
+
const value = useThemeValue();
|
|
34
|
+
if (value === void 0) throw new Error(_umber_style_core.NO_THEME_MESSAGE);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
/** What `when()` and `whenScheme()` keys are matched against: the nearest provider, or the base theme with no color scheme. */
|
|
38
|
+
function useCondition() {
|
|
39
|
+
return (0, react.useContext)(ConditionContext) ?? {
|
|
40
|
+
theme: holder.baseTheme,
|
|
41
|
+
colorScheme: void 0
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
ThemeValueProvider,
|
|
46
|
+
useTheme,
|
|
47
|
+
useThemeValue,
|
|
48
|
+
useCondition
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
exports.createThemeValue = createThemeValue;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext, useMemo } from "react";
|
|
3
|
+
import { NO_THEME_MESSAGE, mergeDeep } from "@umber-style/core";
|
|
4
|
+
import { readSchemeName } from "./conditions.mjs";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
// src/ThemeValue.tsx
|
|
7
|
+
/** The contexts of the engine: the real theme values (the base theme of `holder` is the default), and what a condition is matched against. */
|
|
8
|
+
function createThemeValue(holder) {
|
|
9
|
+
const ThemeContext = createContext(void 0);
|
|
10
|
+
const ConditionContext = createContext(void 0);
|
|
11
|
+
function ThemeValueProvider({ theme, colorScheme, children }) {
|
|
12
|
+
const outer = useContext(ThemeContext) ?? holder.baseTheme;
|
|
13
|
+
const outerScheme = useContext(ConditionContext)?.colorScheme;
|
|
14
|
+
const value = useMemo(() => outer === void 0 ? theme : mergeDeep(outer, theme), [outer, theme]);
|
|
15
|
+
const scheme = readSchemeName(colorScheme) ?? outerScheme;
|
|
16
|
+
const condition = useMemo(() => ({
|
|
17
|
+
theme,
|
|
18
|
+
colorScheme: scheme
|
|
19
|
+
}), [theme, scheme]);
|
|
20
|
+
return /* @__PURE__ */ jsx(ThemeContext.Provider, {
|
|
21
|
+
value,
|
|
22
|
+
children: /* @__PURE__ */ jsx(ConditionContext.Provider, {
|
|
23
|
+
value: condition,
|
|
24
|
+
children
|
|
25
|
+
})
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
/** The theme above, or the base theme, or undefined when there is none. */
|
|
29
|
+
function useThemeValue() {
|
|
30
|
+
return useContext(ThemeContext) ?? holder.baseTheme;
|
|
31
|
+
}
|
|
32
|
+
function useTheme() {
|
|
33
|
+
const value = useThemeValue();
|
|
34
|
+
if (value === void 0) throw new Error(NO_THEME_MESSAGE);
|
|
35
|
+
return value;
|
|
36
|
+
}
|
|
37
|
+
/** What `when()` and `whenScheme()` keys are matched against: the nearest provider, or the base theme with no color scheme. */
|
|
38
|
+
function useCondition() {
|
|
39
|
+
return useContext(ConditionContext) ?? {
|
|
40
|
+
theme: holder.baseTheme,
|
|
41
|
+
colorScheme: void 0
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
return {
|
|
45
|
+
ThemeValueProvider,
|
|
46
|
+
useTheme,
|
|
47
|
+
useThemeValue,
|
|
48
|
+
useCondition
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
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;
|
package/dist/alpha.d.cts
ADDED
|
@@ -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.d.mts
ADDED
|
@@ -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,87 @@
|
|
|
1
|
+
// src/calc.ts
|
|
2
|
+
const NUMBER_PATTERN = /^(-?(?:\d+(?:\.\d*)?|\.\d+))(px|%)?$/i;
|
|
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 unsupported(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
|
+
/** The number of `operand`, and whether it is a percentage; anything else cannot be computed. */
|
|
17
|
+
function read(name, operand) {
|
|
18
|
+
if (typeof operand !== "string") {
|
|
19
|
+
assertFinite(name, "a value", operand);
|
|
20
|
+
return {
|
|
21
|
+
value: operand,
|
|
22
|
+
isPercent: false
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const match = NUMBER_PATTERN.exec(operand.trim());
|
|
26
|
+
if (match === null) throw unsupported(name, operand);
|
|
27
|
+
return {
|
|
28
|
+
value: Number.parseFloat(match[1]),
|
|
29
|
+
isPercent: match[2] === "%"
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** `value` as the percentage that a style takes. */
|
|
33
|
+
function toPercent(value) {
|
|
34
|
+
return `${tidy(value)}%`;
|
|
35
|
+
}
|
|
36
|
+
/** `operand` multiplied or divided by `factor`; a percentage stays a percentage. */
|
|
37
|
+
function scale(name, operand, factor, isDivision) {
|
|
38
|
+
const { value, isPercent } = read(name, operand);
|
|
39
|
+
assertFinite(name, isDivision ? "the divisor" : "the factor", factor);
|
|
40
|
+
if (isDivision && factor === 0) throw new RangeError(`@umber-style/react-native: ${name}(): the divisor must not be 0`);
|
|
41
|
+
const result = isDivision ? value / factor : value * factor;
|
|
42
|
+
return isPercent ? toPercent(result) : tidy(result);
|
|
43
|
+
}
|
|
44
|
+
/** The sum of `terms`: all numbers, or all percentages (a number 0 is both). */
|
|
45
|
+
function sum(name, terms) {
|
|
46
|
+
const readings = terms.map(({ operand, sign }) => ({
|
|
47
|
+
operand,
|
|
48
|
+
sign,
|
|
49
|
+
...read(name, operand)
|
|
50
|
+
}));
|
|
51
|
+
const isPercent = readings.some((reading) => reading.isPercent);
|
|
52
|
+
let total = 0;
|
|
53
|
+
for (const { operand, sign, value, isPercent: isPercentValue } of readings) {
|
|
54
|
+
if (isPercentValue !== isPercent && operand !== 0) throw unsupported(name, operand);
|
|
55
|
+
total += sign * value;
|
|
56
|
+
}
|
|
57
|
+
return isPercent ? toPercent(total) : tidy(total);
|
|
58
|
+
}
|
|
59
|
+
function multiply(value, factor) {
|
|
60
|
+
return scale("multiply", value, factor, false);
|
|
61
|
+
}
|
|
62
|
+
function divide(value, divisor) {
|
|
63
|
+
return scale("divide", value, divisor, true);
|
|
64
|
+
}
|
|
65
|
+
function add(...values) {
|
|
66
|
+
return sum("add", values.map((operand) => ({
|
|
67
|
+
operand,
|
|
68
|
+
sign: 1
|
|
69
|
+
})));
|
|
70
|
+
}
|
|
71
|
+
function subtract(value, ...values) {
|
|
72
|
+
return sum("subtract", [{
|
|
73
|
+
operand: value,
|
|
74
|
+
sign: 1
|
|
75
|
+
}, ...values.map((operand) => ({
|
|
76
|
+
operand,
|
|
77
|
+
sign: -1
|
|
78
|
+
}))]);
|
|
79
|
+
}
|
|
80
|
+
function negate(value) {
|
|
81
|
+
return scale("negate", value, -1, false);
|
|
82
|
+
}
|
|
83
|
+
exports.add = add;
|
|
84
|
+
exports.divide = divide;
|
|
85
|
+
exports.multiply = multiply;
|
|
86
|
+
exports.negate = negate;
|
|
87
|
+
exports.subtract = subtract;
|
package/dist/calc.d.cts
ADDED
|
@@ -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.d.mts
ADDED
|
@@ -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,83 @@
|
|
|
1
|
+
// src/calc.ts
|
|
2
|
+
const NUMBER_PATTERN = /^(-?(?:\d+(?:\.\d*)?|\.\d+))(px|%)?$/i;
|
|
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 unsupported(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
|
+
/** The number of `operand`, and whether it is a percentage; anything else cannot be computed. */
|
|
17
|
+
function read(name, operand) {
|
|
18
|
+
if (typeof operand !== "string") {
|
|
19
|
+
assertFinite(name, "a value", operand);
|
|
20
|
+
return {
|
|
21
|
+
value: operand,
|
|
22
|
+
isPercent: false
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const match = NUMBER_PATTERN.exec(operand.trim());
|
|
26
|
+
if (match === null) throw unsupported(name, operand);
|
|
27
|
+
return {
|
|
28
|
+
value: Number.parseFloat(match[1]),
|
|
29
|
+
isPercent: match[2] === "%"
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
/** `value` as the percentage that a style takes. */
|
|
33
|
+
function toPercent(value) {
|
|
34
|
+
return `${tidy(value)}%`;
|
|
35
|
+
}
|
|
36
|
+
/** `operand` multiplied or divided by `factor`; a percentage stays a percentage. */
|
|
37
|
+
function scale(name, operand, factor, isDivision) {
|
|
38
|
+
const { value, isPercent } = read(name, operand);
|
|
39
|
+
assertFinite(name, isDivision ? "the divisor" : "the factor", factor);
|
|
40
|
+
if (isDivision && factor === 0) throw new RangeError(`@umber-style/react-native: ${name}(): the divisor must not be 0`);
|
|
41
|
+
const result = isDivision ? value / factor : value * factor;
|
|
42
|
+
return isPercent ? toPercent(result) : tidy(result);
|
|
43
|
+
}
|
|
44
|
+
/** The sum of `terms`: all numbers, or all percentages (a number 0 is both). */
|
|
45
|
+
function sum(name, terms) {
|
|
46
|
+
const readings = terms.map(({ operand, sign }) => ({
|
|
47
|
+
operand,
|
|
48
|
+
sign,
|
|
49
|
+
...read(name, operand)
|
|
50
|
+
}));
|
|
51
|
+
const isPercent = readings.some((reading) => reading.isPercent);
|
|
52
|
+
let total = 0;
|
|
53
|
+
for (const { operand, sign, value, isPercent: isPercentValue } of readings) {
|
|
54
|
+
if (isPercentValue !== isPercent && operand !== 0) throw unsupported(name, operand);
|
|
55
|
+
total += sign * value;
|
|
56
|
+
}
|
|
57
|
+
return isPercent ? toPercent(total) : tidy(total);
|
|
58
|
+
}
|
|
59
|
+
function multiply(value, factor) {
|
|
60
|
+
return scale("multiply", value, factor, false);
|
|
61
|
+
}
|
|
62
|
+
function divide(value, divisor) {
|
|
63
|
+
return scale("divide", value, divisor, true);
|
|
64
|
+
}
|
|
65
|
+
function add(...values) {
|
|
66
|
+
return sum("add", values.map((operand) => ({
|
|
67
|
+
operand,
|
|
68
|
+
sign: 1
|
|
69
|
+
})));
|
|
70
|
+
}
|
|
71
|
+
function subtract(value, ...values) {
|
|
72
|
+
return sum("subtract", [{
|
|
73
|
+
operand: value,
|
|
74
|
+
sign: 1
|
|
75
|
+
}, ...values.map((operand) => ({
|
|
76
|
+
operand,
|
|
77
|
+
sign: -1
|
|
78
|
+
}))]);
|
|
79
|
+
}
|
|
80
|
+
function negate(value) {
|
|
81
|
+
return scale("negate", value, -1, false);
|
|
82
|
+
}
|
|
83
|
+
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 };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
const require_conditions = require("./conditions.cjs");
|
|
3
|
+
let _umber_style_core = require("@umber-style/core");
|
|
4
|
+
const require_ThemeValue = require("./ThemeValue.cjs");
|
|
5
|
+
const require_styled = require("./styled.cjs");
|
|
6
|
+
const require_toNativeStyle = require("./toNativeStyle.cjs");
|
|
7
|
+
let react_native = require("react-native");
|
|
8
|
+
// src/createStyle.ts
|
|
9
|
+
/** Registers `source` with the platform StyleSheet, without the keys that only the web has. */
|
|
10
|
+
function register(source) {
|
|
11
|
+
const pairs = Object.entries(source).map(([key, style]) => [key, require_toNativeStyle.toNativeStyle(style)]);
|
|
12
|
+
return {
|
|
13
|
+
entries: react_native.StyleSheet.create(Object.fromEntries(pairs)),
|
|
14
|
+
source,
|
|
15
|
+
isConditional: Object.values(source).some(require_toNativeStyle.hasConditions)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** The entries of `sheet`, with the ones that have a `when()` or `whenScheme()` key resolved against `condition` (not registered: they change with the providers). */
|
|
19
|
+
function resolveEntries(sheet, condition) {
|
|
20
|
+
if (!sheet.isConditional) return sheet.entries;
|
|
21
|
+
const pairs = Object.entries(sheet.source).map(([key, style]) => [key, require_toNativeStyle.hasConditions(style) ? require_toNativeStyle.toNativeStyle(style, condition) : sheet.entries[key]]);
|
|
22
|
+
return Object.fromEntries(pairs);
|
|
23
|
+
}
|
|
24
|
+
/** Makes the React Native styling over a holder of the base theme. */
|
|
25
|
+
function bindHolder(holder) {
|
|
26
|
+
const { ThemeValueProvider, useTheme, useThemeValue, useCondition } = require_ThemeValue.createThemeValue(holder);
|
|
27
|
+
function ThemeProvider({ theme: partial, colorScheme, children }) {
|
|
28
|
+
holder.baseTheme ??= partial;
|
|
29
|
+
return (0, react.createElement)(ThemeValueProvider, {
|
|
30
|
+
theme: partial,
|
|
31
|
+
colorScheme
|
|
32
|
+
}, children);
|
|
33
|
+
}
|
|
34
|
+
function create(styles) {
|
|
35
|
+
if (typeof styles !== "function") {
|
|
36
|
+
let registered;
|
|
37
|
+
return (0, _umber_style_core.createLazyRecord)(Object.keys(styles), (key) => {
|
|
38
|
+
registered ??= register(styles);
|
|
39
|
+
return registered.entries[key];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const byTheme = /* @__PURE__ */ new WeakMap();
|
|
43
|
+
return { resolve(current, condition) {
|
|
44
|
+
let sheet = byTheme.get(current);
|
|
45
|
+
if (!sheet) {
|
|
46
|
+
sheet = register(styles(current));
|
|
47
|
+
byTheme.set(current, sheet);
|
|
48
|
+
}
|
|
49
|
+
return resolveEntries(sheet, condition);
|
|
50
|
+
} };
|
|
51
|
+
}
|
|
52
|
+
const useStyle = ((sheet) => {
|
|
53
|
+
const theme = useThemeValue();
|
|
54
|
+
const condition = useCondition();
|
|
55
|
+
if (!(0, _umber_style_core.isThemedSheet)(sheet)) return sheet;
|
|
56
|
+
if (theme === void 0) throw new Error(_umber_style_core.NO_THEME_MESSAGE);
|
|
57
|
+
return sheet.resolve(theme, condition);
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
styled: require_styled.createStyled(useTheme, useCondition),
|
|
61
|
+
StyleSheet: {
|
|
62
|
+
...react_native.StyleSheet,
|
|
63
|
+
create
|
|
64
|
+
},
|
|
65
|
+
useStyle,
|
|
66
|
+
apply: (...entries) => ({ style: entries.filter((entry) => !!entry) }),
|
|
67
|
+
ThemeProvider,
|
|
68
|
+
useTheme,
|
|
69
|
+
themeClass: () => "",
|
|
70
|
+
when: require_conditions.createNativeThemeKey,
|
|
71
|
+
whenScheme: _umber_style_core.createSchemeKey
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** Makes the styling of one theme shape for React Native, apart from the default one; the options are those of the web and are not used. */
|
|
75
|
+
function createStyle(theme, _options = {}) {
|
|
76
|
+
return bindHolder({ baseTheme: theme });
|
|
77
|
+
}
|
|
78
|
+
exports.bindHolder = bindHolder;
|
|
79
|
+
exports.createStyle = createStyle;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NativeStyle, StyleOptions } from "./types.cjs";
|
|
2
|
+
/** Where the base theme lives: set by the outermost provider, or by the app. */
|
|
3
|
+
export interface NativeHolder<T> {
|
|
4
|
+
baseTheme: T | undefined;
|
|
5
|
+
}
|
|
6
|
+
/** Makes the React Native styling over a holder of the base theme. */
|
|
7
|
+
export declare function bindHolder<T extends object>(holder: NativeHolder<T>): NativeStyle<T>;
|
|
8
|
+
/** Makes the styling of one theme shape for React Native, apart from the default one; the options are those of the web and are not used. */
|
|
9
|
+
export declare function createStyle<T extends object>(theme: T, _options?: StyleOptions<T>): NativeStyle<T>;
|
|
10
|
+
export type { StyleOptions };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { NativeStyle, StyleOptions } from "./types.mjs";
|
|
2
|
+
/** Where the base theme lives: set by the outermost provider, or by the app. */
|
|
3
|
+
export interface NativeHolder<T> {
|
|
4
|
+
baseTheme: T | undefined;
|
|
5
|
+
}
|
|
6
|
+
/** Makes the React Native styling over a holder of the base theme. */
|
|
7
|
+
export declare function bindHolder<T extends object>(holder: NativeHolder<T>): NativeStyle<T>;
|
|
8
|
+
/** Makes the styling of one theme shape for React Native, apart from the default one; the options are those of the web and are not used. */
|
|
9
|
+
export declare function createStyle<T extends object>(theme: T, _options?: StyleOptions<T>): NativeStyle<T>;
|
|
10
|
+
export type { StyleOptions };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { createElement } from "react";
|
|
2
|
+
import { createNativeThemeKey } from "./conditions.mjs";
|
|
3
|
+
import { NO_THEME_MESSAGE, createLazyRecord, createSchemeKey, isThemedSheet } from "@umber-style/core";
|
|
4
|
+
import { createThemeValue } from "./ThemeValue.mjs";
|
|
5
|
+
import { createStyled } from "./styled.mjs";
|
|
6
|
+
import { platformStyleSheet } from "./platform.mjs";
|
|
7
|
+
import { hasConditions, toNativeStyle } from "./toNativeStyle.mjs";
|
|
8
|
+
// src/createStyle.ts
|
|
9
|
+
/** Registers `source` with the platform StyleSheet, without the keys that only the web has. */
|
|
10
|
+
function register(source) {
|
|
11
|
+
const pairs = Object.entries(source).map(([key, style]) => [key, toNativeStyle(style)]);
|
|
12
|
+
return {
|
|
13
|
+
entries: platformStyleSheet.create(Object.fromEntries(pairs)),
|
|
14
|
+
source,
|
|
15
|
+
isConditional: Object.values(source).some(hasConditions)
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** The entries of `sheet`, with the ones that have a `when()` or `whenScheme()` key resolved against `condition` (not registered: they change with the providers). */
|
|
19
|
+
function resolveEntries(sheet, condition) {
|
|
20
|
+
if (!sheet.isConditional) return sheet.entries;
|
|
21
|
+
const pairs = Object.entries(sheet.source).map(([key, style]) => [key, hasConditions(style) ? toNativeStyle(style, condition) : sheet.entries[key]]);
|
|
22
|
+
return Object.fromEntries(pairs);
|
|
23
|
+
}
|
|
24
|
+
/** Makes the React Native styling over a holder of the base theme. */
|
|
25
|
+
function bindHolder(holder) {
|
|
26
|
+
const { ThemeValueProvider, useTheme, useThemeValue, useCondition } = createThemeValue(holder);
|
|
27
|
+
function ThemeProvider({ theme: partial, colorScheme, children }) {
|
|
28
|
+
holder.baseTheme ??= partial;
|
|
29
|
+
return createElement(ThemeValueProvider, {
|
|
30
|
+
theme: partial,
|
|
31
|
+
colorScheme
|
|
32
|
+
}, children);
|
|
33
|
+
}
|
|
34
|
+
function create(styles) {
|
|
35
|
+
if (typeof styles !== "function") {
|
|
36
|
+
let registered;
|
|
37
|
+
return createLazyRecord(Object.keys(styles), (key) => {
|
|
38
|
+
registered ??= register(styles);
|
|
39
|
+
return registered.entries[key];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const byTheme = /* @__PURE__ */ new WeakMap();
|
|
43
|
+
return { resolve(current, condition) {
|
|
44
|
+
let sheet = byTheme.get(current);
|
|
45
|
+
if (!sheet) {
|
|
46
|
+
sheet = register(styles(current));
|
|
47
|
+
byTheme.set(current, sheet);
|
|
48
|
+
}
|
|
49
|
+
return resolveEntries(sheet, condition);
|
|
50
|
+
} };
|
|
51
|
+
}
|
|
52
|
+
const useStyle = ((sheet) => {
|
|
53
|
+
const theme = useThemeValue();
|
|
54
|
+
const condition = useCondition();
|
|
55
|
+
if (!isThemedSheet(sheet)) return sheet;
|
|
56
|
+
if (theme === void 0) throw new Error(NO_THEME_MESSAGE);
|
|
57
|
+
return sheet.resolve(theme, condition);
|
|
58
|
+
});
|
|
59
|
+
return {
|
|
60
|
+
styled: createStyled(useTheme, useCondition),
|
|
61
|
+
StyleSheet: {
|
|
62
|
+
...platformStyleSheet,
|
|
63
|
+
create
|
|
64
|
+
},
|
|
65
|
+
useStyle,
|
|
66
|
+
apply: (...entries) => ({ style: entries.filter((entry) => !!entry) }),
|
|
67
|
+
ThemeProvider,
|
|
68
|
+
useTheme,
|
|
69
|
+
themeClass: () => "",
|
|
70
|
+
when: createNativeThemeKey,
|
|
71
|
+
whenScheme: createSchemeKey
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/** Makes the styling of one theme shape for React Native, apart from the default one; the options are those of the web and are not used. */
|
|
75
|
+
function createStyle(theme, _options = {}) {
|
|
76
|
+
return bindHolder({ baseTheme: theme });
|
|
77
|
+
}
|
|
78
|
+
export { bindHolder, createStyle };
|