@umber-style/react-native 0.1.0-alpha.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 (43) hide show
  1. package/dist/CacheProvider.cjs +7 -0
  2. package/dist/CacheProvider.d.cts +4 -0
  3. package/dist/CacheProvider.d.mts +4 -0
  4. package/dist/CacheProvider.mjs +7 -0
  5. package/dist/ThemeValue.cjs +32 -15
  6. package/dist/ThemeValue.mjs +33 -16
  7. package/dist/calc.cjs +11 -8
  8. package/dist/calc.mjs +11 -8
  9. package/dist/createStyle.cjs +133 -29
  10. package/dist/createStyle.d.cts +7 -9
  11. package/dist/createStyle.d.mts +7 -9
  12. package/dist/createStyle.mjs +136 -32
  13. package/dist/defaultStyle.cjs +29 -3
  14. package/dist/defaultStyle.d.cts +15 -4
  15. package/dist/defaultStyle.d.mts +15 -4
  16. package/dist/defaultStyle.mjs +30 -4
  17. package/dist/index.cjs +11 -2
  18. package/dist/index.d.cts +10 -7
  19. package/dist/index.d.mts +10 -7
  20. package/dist/index.mjs +4 -3
  21. package/dist/mediaQuery.cjs +179 -0
  22. package/dist/mediaQuery.d.cts +1 -0
  23. package/dist/mediaQuery.mjs +178 -0
  24. package/dist/nativeTheme.cjs +51 -0
  25. package/dist/nativeTheme.d.cts +15 -0
  26. package/dist/nativeTheme.d.mts +15 -0
  27. package/dist/nativeTheme.mjs +50 -0
  28. package/dist/platform.mjs +1 -1
  29. package/dist/styled.cjs +95 -19
  30. package/dist/styled.mjs +96 -20
  31. package/dist/toNativeStyle.cjs +64 -15
  32. package/dist/toNativeStyle.mjs +65 -16
  33. package/dist/types.d.cts +78 -10
  34. package/dist/types.d.mts +78 -10
  35. package/dist/units.cjs +137 -0
  36. package/dist/units.mjs +133 -0
  37. package/dist/warnStyle.cjs +62 -18
  38. package/dist/warnStyle.mjs +61 -19
  39. package/dist/windowState.cjs +89 -0
  40. package/dist/windowState.d.cts +6 -0
  41. package/dist/windowState.d.mts +5 -0
  42. package/dist/windowState.mjs +85 -0
  43. package/package.json +3 -3
package/dist/units.cjs ADDED
@@ -0,0 +1,137 @@
1
+ // src/units.ts
2
+ /** A number with `px`, `rem`, `%` or no unit, once the white space around it is cut. */
3
+ const NUMBER_PATTERN = /^(-?(?:\d+(?:\.\d*)?|\.\d+))(px|rem|%)?$/i;
4
+ /** The number in `text` and its unit, or undefined when `text` is not a number with one of the units read here. */
5
+ function readNumber(text) {
6
+ const match = NUMBER_PATTERN.exec(text.trim());
7
+ if (match === null) return void 0;
8
+ return {
9
+ value: Number.parseFloat(match[1]),
10
+ unit: (match[2] ?? "").toLowerCase()
11
+ };
12
+ }
13
+ /** The length in `text` as React Native takes it: a number, a number in `px`, or one in `rem` times `remBase`; undefined for anything else. */
14
+ function toLength(text, remBase) {
15
+ const reading = readNumber(text);
16
+ if (reading === void 0 || reading.unit === "%") return void 0;
17
+ return reading.unit === "rem" ? reading.value * remBase : reading.value;
18
+ }
19
+ /**
20
+ * The keys of the platform's view, text and image styles whose value type takes any number, apart from
21
+ * the colours (a colour takes a number too): the ones that are lengths. A string with `px`, `rem` or no
22
+ * unit becomes a number here. The type tests check this list and `NUMBER_KEYS` against the platform types.
23
+ */
24
+ const LENGTH_KEYS = [
25
+ "width",
26
+ "height",
27
+ "minWidth",
28
+ "maxWidth",
29
+ "minHeight",
30
+ "maxHeight",
31
+ "top",
32
+ "right",
33
+ "bottom",
34
+ "left",
35
+ "start",
36
+ "end",
37
+ "inset",
38
+ "insetBlock",
39
+ "insetBlockEnd",
40
+ "insetBlockStart",
41
+ "insetInline",
42
+ "insetInlineEnd",
43
+ "insetInlineStart",
44
+ "margin",
45
+ "marginBlock",
46
+ "marginBlockEnd",
47
+ "marginBlockStart",
48
+ "marginBottom",
49
+ "marginEnd",
50
+ "marginHorizontal",
51
+ "marginInline",
52
+ "marginInlineEnd",
53
+ "marginInlineStart",
54
+ "marginLeft",
55
+ "marginRight",
56
+ "marginStart",
57
+ "marginTop",
58
+ "marginVertical",
59
+ "padding",
60
+ "paddingBlock",
61
+ "paddingBlockEnd",
62
+ "paddingBlockStart",
63
+ "paddingBottom",
64
+ "paddingEnd",
65
+ "paddingHorizontal",
66
+ "paddingInline",
67
+ "paddingInlineEnd",
68
+ "paddingInlineStart",
69
+ "paddingLeft",
70
+ "paddingRight",
71
+ "paddingStart",
72
+ "paddingTop",
73
+ "paddingVertical",
74
+ "gap",
75
+ "rowGap",
76
+ "columnGap",
77
+ "flexBasis",
78
+ "borderWidth",
79
+ "borderBottomWidth",
80
+ "borderEndWidth",
81
+ "borderLeftWidth",
82
+ "borderRightWidth",
83
+ "borderStartWidth",
84
+ "borderTopWidth",
85
+ "borderRadius",
86
+ "borderBottomEndRadius",
87
+ "borderBottomLeftRadius",
88
+ "borderBottomRightRadius",
89
+ "borderBottomStartRadius",
90
+ "borderEndEndRadius",
91
+ "borderEndStartRadius",
92
+ "borderStartEndRadius",
93
+ "borderStartStartRadius",
94
+ "borderTopEndRadius",
95
+ "borderTopLeftRadius",
96
+ "borderTopRightRadius",
97
+ "borderTopStartRadius",
98
+ "outlineOffset",
99
+ "outlineWidth",
100
+ "shadowRadius",
101
+ "textShadowRadius",
102
+ "elevation",
103
+ "fontSize",
104
+ "letterSpacing",
105
+ "lineHeight"
106
+ ];
107
+ /** The keys whose value type takes any number and that are not lengths: a string with no unit becomes a number here. */
108
+ const NUMBER_KEYS = [
109
+ "opacity",
110
+ "shadowOpacity",
111
+ "zIndex",
112
+ "flex",
113
+ "flexGrow",
114
+ "flexShrink",
115
+ "aspectRatio"
116
+ ];
117
+ const LENGTH_KEY_SET = new Set(LENGTH_KEYS);
118
+ const NUMBER_KEY_SET = new Set(NUMBER_KEYS);
119
+ /** The string `value` of `key` as React Native takes it: a number for a key that takes one and a value it can read, else `value` itself. */
120
+ function toNativeValue(key, value, remBase) {
121
+ if (LENGTH_KEY_SET.has(key)) return toLength(value, remBase) ?? value;
122
+ if (!NUMBER_KEY_SET.has(key)) return value;
123
+ const reading = readNumber(value);
124
+ return reading?.unit === "" ? reading.value : value;
125
+ }
126
+ /** Turns, in place, the strings of `style` that are lengths or numbers into numbers (see `LENGTH_KEYS` and `NUMBER_KEYS`). */
127
+ function convertValues(style, remBase) {
128
+ for (const key in style) {
129
+ const value = style[key];
130
+ if (typeof value === "string") style[key] = toNativeValue(key, value, remBase);
131
+ }
132
+ }
133
+ exports.LENGTH_KEYS = LENGTH_KEYS;
134
+ exports.NUMBER_KEYS = NUMBER_KEYS;
135
+ exports.convertValues = convertValues;
136
+ exports.readNumber = readNumber;
137
+ exports.toLength = toLength;
package/dist/units.mjs ADDED
@@ -0,0 +1,133 @@
1
+ // src/units.ts
2
+ /** A number with `px`, `rem`, `%` or no unit, once the white space around it is cut. */
3
+ const NUMBER_PATTERN = /^(-?(?:\d+(?:\.\d*)?|\.\d+))(px|rem|%)?$/i;
4
+ /** The number in `text` and its unit, or undefined when `text` is not a number with one of the units read here. */
5
+ function readNumber(text) {
6
+ const match = NUMBER_PATTERN.exec(text.trim());
7
+ if (match === null) return void 0;
8
+ return {
9
+ value: Number.parseFloat(match[1]),
10
+ unit: (match[2] ?? "").toLowerCase()
11
+ };
12
+ }
13
+ /** The length in `text` as React Native takes it: a number, a number in `px`, or one in `rem` times `remBase`; undefined for anything else. */
14
+ function toLength(text, remBase) {
15
+ const reading = readNumber(text);
16
+ if (reading === void 0 || reading.unit === "%") return void 0;
17
+ return reading.unit === "rem" ? reading.value * remBase : reading.value;
18
+ }
19
+ /**
20
+ * The keys of the platform's view, text and image styles whose value type takes any number, apart from
21
+ * the colours (a colour takes a number too): the ones that are lengths. A string with `px`, `rem` or no
22
+ * unit becomes a number here. The type tests check this list and `NUMBER_KEYS` against the platform types.
23
+ */
24
+ const LENGTH_KEYS = [
25
+ "width",
26
+ "height",
27
+ "minWidth",
28
+ "maxWidth",
29
+ "minHeight",
30
+ "maxHeight",
31
+ "top",
32
+ "right",
33
+ "bottom",
34
+ "left",
35
+ "start",
36
+ "end",
37
+ "inset",
38
+ "insetBlock",
39
+ "insetBlockEnd",
40
+ "insetBlockStart",
41
+ "insetInline",
42
+ "insetInlineEnd",
43
+ "insetInlineStart",
44
+ "margin",
45
+ "marginBlock",
46
+ "marginBlockEnd",
47
+ "marginBlockStart",
48
+ "marginBottom",
49
+ "marginEnd",
50
+ "marginHorizontal",
51
+ "marginInline",
52
+ "marginInlineEnd",
53
+ "marginInlineStart",
54
+ "marginLeft",
55
+ "marginRight",
56
+ "marginStart",
57
+ "marginTop",
58
+ "marginVertical",
59
+ "padding",
60
+ "paddingBlock",
61
+ "paddingBlockEnd",
62
+ "paddingBlockStart",
63
+ "paddingBottom",
64
+ "paddingEnd",
65
+ "paddingHorizontal",
66
+ "paddingInline",
67
+ "paddingInlineEnd",
68
+ "paddingInlineStart",
69
+ "paddingLeft",
70
+ "paddingRight",
71
+ "paddingStart",
72
+ "paddingTop",
73
+ "paddingVertical",
74
+ "gap",
75
+ "rowGap",
76
+ "columnGap",
77
+ "flexBasis",
78
+ "borderWidth",
79
+ "borderBottomWidth",
80
+ "borderEndWidth",
81
+ "borderLeftWidth",
82
+ "borderRightWidth",
83
+ "borderStartWidth",
84
+ "borderTopWidth",
85
+ "borderRadius",
86
+ "borderBottomEndRadius",
87
+ "borderBottomLeftRadius",
88
+ "borderBottomRightRadius",
89
+ "borderBottomStartRadius",
90
+ "borderEndEndRadius",
91
+ "borderEndStartRadius",
92
+ "borderStartEndRadius",
93
+ "borderStartStartRadius",
94
+ "borderTopEndRadius",
95
+ "borderTopLeftRadius",
96
+ "borderTopRightRadius",
97
+ "borderTopStartRadius",
98
+ "outlineOffset",
99
+ "outlineWidth",
100
+ "shadowRadius",
101
+ "textShadowRadius",
102
+ "elevation",
103
+ "fontSize",
104
+ "letterSpacing",
105
+ "lineHeight"
106
+ ];
107
+ /** The keys whose value type takes any number and that are not lengths: a string with no unit becomes a number here. */
108
+ const NUMBER_KEYS = [
109
+ "opacity",
110
+ "shadowOpacity",
111
+ "zIndex",
112
+ "flex",
113
+ "flexGrow",
114
+ "flexShrink",
115
+ "aspectRatio"
116
+ ];
117
+ const LENGTH_KEY_SET = new Set(LENGTH_KEYS);
118
+ const NUMBER_KEY_SET = new Set(NUMBER_KEYS);
119
+ /** The string `value` of `key` as React Native takes it: a number for a key that takes one and a value it can read, else `value` itself. */
120
+ function toNativeValue(key, value, remBase) {
121
+ if (LENGTH_KEY_SET.has(key)) return toLength(value, remBase) ?? value;
122
+ if (!NUMBER_KEY_SET.has(key)) return value;
123
+ const reading = readNumber(value);
124
+ return reading?.unit === "" ? reading.value : value;
125
+ }
126
+ /** Turns, in place, the strings of `style` that are lengths or numbers into numbers (see `LENGTH_KEYS` and `NUMBER_KEYS`). */
127
+ function convertValues(style, remBase) {
128
+ for (const key in style) {
129
+ const value = style[key];
130
+ if (typeof value === "string") style[key] = toNativeValue(key, value, remBase);
131
+ }
132
+ }
133
+ export { LENGTH_KEYS, NUMBER_KEYS, convertValues, readNumber, toLength };
@@ -1,20 +1,17 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ const require_mediaQuery = require("./mediaQuery.cjs");
1
3
  // src/warnStyle.ts
2
4
  /** A length or `auto`: one value of a CSS shorthand. */
3
5
  const SHORTHAND_ITEM = String.raw`(?:-?(?:\d+(?:\.\d*)?|\.\d+)(?:%|[a-z]*)|auto)`;
4
6
  const SHORTHAND = new RegExp(String.raw`^\s*${SHORTHAND_ITEM}(?:\s+${SHORTHAND_ITEM}){1,3}\s*$`);
5
7
  /** A number and a unit, once the white space around them is cut. */
6
8
  const UNIT_VALUE = /^-?(?:\d+(?:\.\d*)?|\.\d+)([a-z]+)$/i;
7
- /** The CSS length units, other than a percentage, that React Native does not take. */
9
+ /** The CSS length units, other than a percentage, `em` and the viewport units, that React Native does not take. */
8
10
  const CSS_UNITS = /* @__PURE__ */ new Set([
9
11
  "px",
10
- "em",
11
12
  "rem",
12
13
  "ex",
13
14
  "ch",
14
- "vh",
15
- "vw",
16
- "vmin",
17
- "vmax",
18
15
  "pt",
19
16
  "pc",
20
17
  "cm",
@@ -22,32 +19,79 @@ const CSS_UNITS = /* @__PURE__ */ new Set([
22
19
  "in",
23
20
  "q"
24
21
  ]);
25
- /** Whether `value` is a number with a CSS length unit other than a percentage. */
26
- function hasCssUnit(value) {
27
- const match = UNIT_VALUE.exec(value.trim());
28
- return match !== null && CSS_UNITS.has(match[1].toLowerCase());
29
- }
22
+ /** The viewport units: `vh`, `vw`, `vmin`, `vmax`, `vi`, `vb`, and their small, large and dynamic forms (`dvh`). */
23
+ const VIEWPORT_UNIT = /^[sld]?v(?:h|w|i|b|min|max)$/;
24
+ const CALC = /calc\(/i;
25
+ const CURRENT_COLOR = /currentcolor/i;
26
+ const ANIMATION_KEY = /^(?:animation|transition)/;
30
27
  const warned = /* @__PURE__ */ new Set();
31
28
  /** Whether this is a development build: `process` may not exist, and then nothing is warned. */
32
29
  function isDevelopment() {
33
30
  return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
34
31
  }
32
+ /** Warns `message` once, with the name of the component (or sheet entry) in front when it is known. */
33
+ function warnOnce(message, name) {
34
+ const text = name === void 0 ? message : `${name}: ${message}`;
35
+ if (warned.has(text)) return;
36
+ warned.add(text);
37
+ console.warn(`@umber-style/react-native: ${text}`);
38
+ }
39
+ /** What is wrong with the CSS unit `unit` of `key: value`, or undefined when it is not a CSS unit. */
40
+ function describeUnit(key, value, unit) {
41
+ if (unit === "em") return `"${key}: ${value}" is in em, which has no equivalent on React Native (a style does not know the font size around it); it is passed as it is. Write a number, or a length in rem.`;
42
+ if (VIEWPORT_UNIT.test(unit)) return `"${key}: ${value}" is in a viewport unit, which has no equivalent in a React Native style; it is passed as it is. Write a percentage, or a number computed from the window size.`;
43
+ if (CSS_UNITS.has(unit)) return `"${key}: ${value}" has a CSS unit; React Native takes a number (density-independent pixels) or a percentage.`;
44
+ }
35
45
  /** What is wrong with the string `value` of `key` on React Native, or undefined when nothing is. */
36
46
  function describeProblem(key, value) {
37
47
  if (SHORTHAND.test(value)) return `"${key}: ${value}" is a shorthand with several values; React Native takes one value for each key. Set each side or axis on its own.`;
38
- if (hasCssUnit(value)) return `"${key}: ${value}" has a CSS unit; React Native takes a number (density-independent pixels) or a percentage.`;
48
+ if (CALC.test(value)) return `"${key}: ${value}" is a calc() expression, which React Native does not compute; it is passed as it is. Use add, subtract, multiply and divide, which return numbers.`;
49
+ if (CURRENT_COLOR.test(value)) return `"${key}: ${value}" uses currentColor, which React Native does not have; it is passed as it is. Give the colour itself.`;
50
+ const match = UNIT_VALUE.exec(value.trim());
51
+ return match === null ? void 0 : describeUnit(key, value, match[1].toLowerCase());
39
52
  }
40
53
  /**
41
- * In development, warns (once for each distinct message) about the values that the types cannot reject:
42
- * a shorthand string with several values, or a number with a CSS unit.
54
+ * In development, warns (once for each distinct message) about the values that the types cannot reject
55
+ * and that were not converted: a shorthand string with several values, a number with a CSS unit (`em`
56
+ * and the viewport units have no equivalent), a `calc()` expression, and `currentColor`. `name` is the
57
+ * component or sheet entry the style belongs to, when it is known.
43
58
  */
44
- function warnAboutValues(style) {
59
+ function warnAboutValues(style, name) {
45
60
  if (!isDevelopment()) return;
46
61
  for (const [key, value] of Object.entries(style)) {
47
62
  const message = typeof value === "string" ? describeProblem(key, value) : void 0;
48
- if (message === void 0 || warned.has(message)) continue;
49
- warned.add(message);
50
- console.warn(`@umber-style/react-native: ${message}`);
63
+ if (message !== void 0) warnOnce(message, name);
64
+ }
65
+ }
66
+ /** Why the web-only key `key` is dropped, or undefined when dropping it is not a mistake (`label`, a condition key, a media query that is evaluated). */
67
+ function describeDropped(key) {
68
+ if (key === "label" || (0, _umber_style_core.readCondition)(key) !== void 0) return void 0;
69
+ if (require_mediaQuery.isMediaKey(key)) {
70
+ const { problem } = require_mediaQuery.readMediaQuery(key);
71
+ return problem === void 0 ? void 0 : `React Native does not evaluate ${problem}`;
51
72
  }
73
+ if (key.startsWith("@")) return "React Native has no at-rules";
74
+ if (key.startsWith("--")) return "React Native has no custom properties";
75
+ if (ANIMATION_KEY.test(key)) return "animations do not run on React Native yet";
76
+ return "React Native has no selectors";
77
+ }
78
+ /**
79
+ * In development, warns (once for each distinct message) that the web-only key `key`, written outside
80
+ * the `web` part of a style, is dropped: a selector, an at-rule, a custom property, or an `animation*` or
81
+ * `transition*` key, or a media query with something the binding does not evaluate. `label`, a `when()` or
82
+ * `whenScheme()` key that does not match, and a media query that does not match are not mistakes.
83
+ */
84
+ function warnAboutDroppedKey(key, name) {
85
+ if (!isDevelopment()) return;
86
+ const reason = describeDropped(key);
87
+ if (reason === void 0) return;
88
+ warnOnce(`"${key}" is dropped: ${reason}. A key for the web only goes in the web part of a shared style.`, name);
89
+ }
90
+ /** In development, warns (once for each query) that `useMediaQuery(query)` is always false: the binding does not evaluate `problem` in it. */
91
+ function warnAboutQuery(query, problem) {
92
+ if (!isDevelopment()) return;
93
+ warnOnce(`useMediaQuery("${query}") is false: React Native does not evaluate ${problem}.`, void 0);
52
94
  }
95
+ exports.warnAboutDroppedKey = warnAboutDroppedKey;
96
+ exports.warnAboutQuery = warnAboutQuery;
53
97
  exports.warnAboutValues = warnAboutValues;
@@ -1,20 +1,17 @@
1
+ import { readCondition } from "@umber-style/core";
2
+ import { isMediaKey, readMediaQuery } from "./mediaQuery.mjs";
1
3
  // src/warnStyle.ts
2
4
  /** A length or `auto`: one value of a CSS shorthand. */
3
5
  const SHORTHAND_ITEM = String.raw`(?:-?(?:\d+(?:\.\d*)?|\.\d+)(?:%|[a-z]*)|auto)`;
4
6
  const SHORTHAND = new RegExp(String.raw`^\s*${SHORTHAND_ITEM}(?:\s+${SHORTHAND_ITEM}){1,3}\s*$`);
5
7
  /** A number and a unit, once the white space around them is cut. */
6
8
  const UNIT_VALUE = /^-?(?:\d+(?:\.\d*)?|\.\d+)([a-z]+)$/i;
7
- /** The CSS length units, other than a percentage, that React Native does not take. */
9
+ /** The CSS length units, other than a percentage, `em` and the viewport units, that React Native does not take. */
8
10
  const CSS_UNITS = /* @__PURE__ */ new Set([
9
11
  "px",
10
- "em",
11
12
  "rem",
12
13
  "ex",
13
14
  "ch",
14
- "vh",
15
- "vw",
16
- "vmin",
17
- "vmax",
18
15
  "pt",
19
16
  "pc",
20
17
  "cm",
@@ -22,32 +19,77 @@ const CSS_UNITS = /* @__PURE__ */ new Set([
22
19
  "in",
23
20
  "q"
24
21
  ]);
25
- /** Whether `value` is a number with a CSS length unit other than a percentage. */
26
- function hasCssUnit(value) {
27
- const match = UNIT_VALUE.exec(value.trim());
28
- return match !== null && CSS_UNITS.has(match[1].toLowerCase());
29
- }
22
+ /** The viewport units: `vh`, `vw`, `vmin`, `vmax`, `vi`, `vb`, and their small, large and dynamic forms (`dvh`). */
23
+ const VIEWPORT_UNIT = /^[sld]?v(?:h|w|i|b|min|max)$/;
24
+ const CALC = /calc\(/i;
25
+ const CURRENT_COLOR = /currentcolor/i;
26
+ const ANIMATION_KEY = /^(?:animation|transition)/;
30
27
  const warned = /* @__PURE__ */ new Set();
31
28
  /** Whether this is a development build: `process` may not exist, and then nothing is warned. */
32
29
  function isDevelopment() {
33
30
  return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
34
31
  }
32
+ /** Warns `message` once, with the name of the component (or sheet entry) in front when it is known. */
33
+ function warnOnce(message, name) {
34
+ const text = name === void 0 ? message : `${name}: ${message}`;
35
+ if (warned.has(text)) return;
36
+ warned.add(text);
37
+ console.warn(`@umber-style/react-native: ${text}`);
38
+ }
39
+ /** What is wrong with the CSS unit `unit` of `key: value`, or undefined when it is not a CSS unit. */
40
+ function describeUnit(key, value, unit) {
41
+ if (unit === "em") return `"${key}: ${value}" is in em, which has no equivalent on React Native (a style does not know the font size around it); it is passed as it is. Write a number, or a length in rem.`;
42
+ if (VIEWPORT_UNIT.test(unit)) return `"${key}: ${value}" is in a viewport unit, which has no equivalent in a React Native style; it is passed as it is. Write a percentage, or a number computed from the window size.`;
43
+ if (CSS_UNITS.has(unit)) return `"${key}: ${value}" has a CSS unit; React Native takes a number (density-independent pixels) or a percentage.`;
44
+ }
35
45
  /** What is wrong with the string `value` of `key` on React Native, or undefined when nothing is. */
36
46
  function describeProblem(key, value) {
37
47
  if (SHORTHAND.test(value)) return `"${key}: ${value}" is a shorthand with several values; React Native takes one value for each key. Set each side or axis on its own.`;
38
- if (hasCssUnit(value)) return `"${key}: ${value}" has a CSS unit; React Native takes a number (density-independent pixels) or a percentage.`;
48
+ if (CALC.test(value)) return `"${key}: ${value}" is a calc() expression, which React Native does not compute; it is passed as it is. Use add, subtract, multiply and divide, which return numbers.`;
49
+ if (CURRENT_COLOR.test(value)) return `"${key}: ${value}" uses currentColor, which React Native does not have; it is passed as it is. Give the colour itself.`;
50
+ const match = UNIT_VALUE.exec(value.trim());
51
+ return match === null ? void 0 : describeUnit(key, value, match[1].toLowerCase());
39
52
  }
40
53
  /**
41
- * In development, warns (once for each distinct message) about the values that the types cannot reject:
42
- * a shorthand string with several values, or a number with a CSS unit.
54
+ * In development, warns (once for each distinct message) about the values that the types cannot reject
55
+ * and that were not converted: a shorthand string with several values, a number with a CSS unit (`em`
56
+ * and the viewport units have no equivalent), a `calc()` expression, and `currentColor`. `name` is the
57
+ * component or sheet entry the style belongs to, when it is known.
43
58
  */
44
- function warnAboutValues(style) {
59
+ function warnAboutValues(style, name) {
45
60
  if (!isDevelopment()) return;
46
61
  for (const [key, value] of Object.entries(style)) {
47
62
  const message = typeof value === "string" ? describeProblem(key, value) : void 0;
48
- if (message === void 0 || warned.has(message)) continue;
49
- warned.add(message);
50
- console.warn(`@umber-style/react-native: ${message}`);
63
+ if (message !== void 0) warnOnce(message, name);
64
+ }
65
+ }
66
+ /** Why the web-only key `key` is dropped, or undefined when dropping it is not a mistake (`label`, a condition key, a media query that is evaluated). */
67
+ function describeDropped(key) {
68
+ if (key === "label" || readCondition(key) !== void 0) return void 0;
69
+ if (isMediaKey(key)) {
70
+ const { problem } = readMediaQuery(key);
71
+ return problem === void 0 ? void 0 : `React Native does not evaluate ${problem}`;
51
72
  }
73
+ if (key.startsWith("@")) return "React Native has no at-rules";
74
+ if (key.startsWith("--")) return "React Native has no custom properties";
75
+ if (ANIMATION_KEY.test(key)) return "animations do not run on React Native yet";
76
+ return "React Native has no selectors";
77
+ }
78
+ /**
79
+ * In development, warns (once for each distinct message) that the web-only key `key`, written outside
80
+ * the `web` part of a style, is dropped: a selector, an at-rule, a custom property, or an `animation*` or
81
+ * `transition*` key, or a media query with something the binding does not evaluate. `label`, a `when()` or
82
+ * `whenScheme()` key that does not match, and a media query that does not match are not mistakes.
83
+ */
84
+ function warnAboutDroppedKey(key, name) {
85
+ if (!isDevelopment()) return;
86
+ const reason = describeDropped(key);
87
+ if (reason === void 0) return;
88
+ warnOnce(`"${key}" is dropped: ${reason}. A key for the web only goes in the web part of a shared style.`, name);
89
+ }
90
+ /** In development, warns (once for each query) that `useMediaQuery(query)` is always false: the binding does not evaluate `problem` in it. */
91
+ function warnAboutQuery(query, problem) {
92
+ if (!isDevelopment()) return;
93
+ warnOnce(`useMediaQuery("${query}") is false: React Native does not evaluate ${problem}.`, void 0);
52
94
  }
53
- export { warnAboutValues };
95
+ export { warnAboutDroppedKey, warnAboutQuery, warnAboutValues };
@@ -0,0 +1,89 @@
1
+ let react = require("react");
2
+ const require_mediaQuery = require("./mediaQuery.cjs");
3
+ const require_warnStyle = require("./warnStyle.cjs");
4
+ let react_native = require("react-native");
5
+ // src/windowState.ts
6
+ /**
7
+ * Whether the system asks for reduced motion. The platform answers asynchronously, so this is false (no
8
+ * preference, the platform's default) until it does: the answer is asked for when the first component
9
+ * subscribes, and a change re-renders the components whose queries it changes.
10
+ */
11
+ let isReducedMotion = false;
12
+ const listeners = /* @__PURE__ */ new Set();
13
+ const subscriptions = [];
14
+ /** Tells every subscriber that the window or a system setting changed. */
15
+ function notify() {
16
+ for (const listener of listeners) listener();
17
+ }
18
+ function setReducedMotion(value) {
19
+ if (value === isReducedMotion) return;
20
+ isReducedMotion = value;
21
+ notify();
22
+ }
23
+ /** The window and the system settings now: the size and the color scheme are read from the platform, reduced motion is the last answer. */
24
+ function readWindowState() {
25
+ const { width, height } = react_native.Dimensions.get("window");
26
+ return {
27
+ width,
28
+ height,
29
+ colorScheme: react_native.Appearance.getColorScheme(),
30
+ isReducedMotion
31
+ };
32
+ }
33
+ /**
34
+ * Subscribes `listener` to the changes of the window size, the system color scheme and reduced motion.
35
+ * The binding listens to the platform once, for all its subscribers, while it has any.
36
+ */
37
+ function subscribeToWindow(listener) {
38
+ listeners.add(listener);
39
+ if (listeners.size === 1) {
40
+ subscriptions.push(react_native.Dimensions.addEventListener("change", notify), react_native.Appearance.addChangeListener(notify), react_native.AccessibilityInfo.addEventListener("reduceMotionChanged", setReducedMotion));
41
+ react_native.AccessibilityInfo.isReduceMotionEnabled().then(setReducedMotion);
42
+ }
43
+ return () => {
44
+ listeners.delete(listener);
45
+ if (listeners.size === 0) for (const subscription of subscriptions.splice(0)) subscription.remove();
46
+ };
47
+ }
48
+ const subscribeToNothing = () => () => {};
49
+ const readNothing = () => "";
50
+ /**
51
+ * The key of what `queries` give now: one character for each, `1` where it matches, then the system
52
+ * color scheme when `hasScheme` (for a `whenScheme()` key that no provider decides). Two renders with
53
+ * the same key resolve the same style.
54
+ */
55
+ function readMediaKey(queries, hasScheme, remBase) {
56
+ let key = "";
57
+ if (queries.length > 0) {
58
+ const state = readWindowState();
59
+ for (const query of queries) key += require_mediaQuery.readMediaQuery(query).matches(state, remBase) ? "1" : "0";
60
+ }
61
+ return hasScheme ? `${key}:${String(react_native.Appearance.getColorScheme())}` : key;
62
+ }
63
+ /**
64
+ * `readMediaKey` in a component, which renders again when the key changes, not when the width changes
65
+ * inside the same set of matching queries. With no query and no scheme it subscribes to nothing.
66
+ */
67
+ function useMediaKey(queries, hasScheme, remBase) {
68
+ const read = queries.length > 0 || hasScheme ? createKeyReader(queries, hasScheme, remBase) : readNothing;
69
+ return (0, react.useSyncExternalStore)(read === readNothing ? subscribeToNothing : subscribeToWindow, read, read);
70
+ }
71
+ /** `readMediaKey` of these arguments; made apart, so that a component that watches nothing makes no function. */
72
+ function createKeyReader(queries, hasScheme, remBase) {
73
+ return () => readMediaKey(queries, hasScheme, remBase);
74
+ }
75
+ /**
76
+ * Whether the media query `query` matches the window, on the store and the parser of the media keys of
77
+ * styles; the component renders again when the answer changes. A query the binding cannot evaluate is
78
+ * false, with a development warning. A render to a string gives `defaultMatches`.
79
+ */
80
+ function useMediaMatch(query, options, remBase) {
81
+ const { matches, problem } = require_mediaQuery.readMediaQuery(query);
82
+ if (problem !== void 0) require_warnStyle.warnAboutQuery(query, problem);
83
+ return (0, react.useSyncExternalStore)(problem === void 0 ? subscribeToWindow : subscribeToNothing, () => matches(readWindowState(), remBase), () => options.defaultMatches ?? false);
84
+ }
85
+ exports.readMediaKey = readMediaKey;
86
+ exports.readWindowState = readWindowState;
87
+ exports.subscribeToWindow = subscribeToWindow;
88
+ exports.useMediaKey = useMediaKey;
89
+ exports.useMediaMatch = useMediaMatch;
@@ -0,0 +1,6 @@
1
+ import "./mediaQuery.cjs";
2
+ /** The options of `useMediaQuery`. */
3
+ export interface MediaQueryOptions {
4
+ /** The answer of a render to a string, where there is no window. Default false. */
5
+ defaultMatches?: boolean;
6
+ }
@@ -0,0 +1,5 @@
1
+ /** The options of `useMediaQuery`. */
2
+ export interface MediaQueryOptions {
3
+ /** The answer of a render to a string, where there is no window. Default false. */
4
+ defaultMatches?: boolean;
5
+ }