@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
@@ -0,0 +1,130 @@
1
+ import { BindingOptions, Cache, ColorScheme, DeepPartial, GlobalInput, GlobalStyles, GlobalStylesInput, KeyframesInput, KeyframesOptions, SchemeKey, SchemeName, StaticTemplateValue, StylerOptions, ThemeInput, ThemeKey } from "@umber-style/core";
2
+ import { NativeObjectSheet, NativeSheetStyles, NativeStyleInput, NativeStyleObject } from "./nativeStyle.mjs";
3
+ import { NativeTheme } from "./nativeTheme.mjs";
4
+ import { platformStyleSheet } from "./platform.mjs";
5
+ import { ComponentProps, Context, ElementType, FunctionComponent, ReactElement, ReactNode } from "react";
6
+ /**
7
+ * The options of `createStyle`: the same as on the web, so one call runs on both, and `remBase`. Only
8
+ * `remBase` and `lengthKeys` are used: a theme value under one of `lengthKeys` (default `space`,
9
+ * `sizes`, `radii`, `fontSizes`, `borderWidths`) that is a length in `px`, `rem` or no unit is a number.
10
+ */
11
+ export interface StyleOptions<T = never> extends BindingOptions<T> {
12
+ /** The font size, in density-independent pixels, that `1rem` is, in a theme and in a style. Default 16. */
13
+ remBase?: number;
14
+ }
15
+ /** The options of `styled(tag, options)`: the label of the component, and which props reach the element. */
16
+ export interface StyledOptions extends StylerOptions {
17
+ /** Whether a prop is forwarded. Default: every prop but `as`, `theme` and `style`. */
18
+ shouldForwardProp?: (prop: string) => boolean;
19
+ }
20
+ /** What the `style` prop of a styled component takes: style objects (sheet entries are such objects), in arrays, with falsy values dropped. */
21
+ type StyleValue = NativeStyleObject | false | null | readonly StyleProp[];
22
+ /** What the `style` prop of a styled component takes, or nothing. */
23
+ export type StyleProp = StyleValue | undefined;
24
+ /** A component made by `styled`: it takes the props of the tag, its own props `P`, `as` and `style`. */
25
+ export type StyledComponent<C extends ElementType, P extends object> = FunctionComponent<Omit<ComponentProps<C>, keyof P | 'as' | 'style'> & P & {
26
+ as?: ElementType;
27
+ style?: StyleValue;
28
+ }>;
29
+ /** `styled` of a theme `T`: a tag and options, then the styles of the component. */
30
+ export type Styled<T> = <C extends ElementType>(tag: C, options?: StyledOptions) => <P extends object = object>(...styles: NativeStyleInput<T, P>[]) => StyledComponent<C, P>;
31
+ /** A set of named styles; `E` is what one of them becomes. */
32
+ export interface SheetDefinition<S extends NativeSheetStyles, E> {
33
+ /** Never set at run time: it only carries the types. */
34
+ readonly entries?: { [K in keyof S]: E; };
35
+ }
36
+ /** The props of `ThemeProvider`: the theme of the subtree and its color scheme, and the props of the web that are ignored. */
37
+ export interface ThemeProviderProps<T> {
38
+ /**
39
+ * The theme, or the part of it that this subtree changes; or a function of the theme above (the base
40
+ * theme, or an empty object, for the outermost provider) that returns it. It is merged over the theme above.
41
+ */
42
+ readonly theme: ThemeInput<T>;
43
+ /** The color scheme of this subtree, for `whenScheme`: `light` or `dark`; `light dark` keeps the one above. */
44
+ readonly colorScheme?: ColorScheme;
45
+ /** Ignored on React Native. */
46
+ readonly className?: string;
47
+ /** Accepted and ignored: there are no classes on React Native, so the provider never renders an element. */
48
+ readonly isRoot?: boolean;
49
+ /** The subtree that the theme applies to. */
50
+ readonly children?: ReactNode;
51
+ }
52
+ /** The props of `CacheProvider`: the same as on the web, so that one source compiles for both. */
53
+ export interface CacheProviderProps {
54
+ /** A cache of the web engine: React Native writes no CSS, so it is not read. */
55
+ readonly value: Cache;
56
+ /** What it renders, as it is. */
57
+ readonly children?: ReactNode;
58
+ }
59
+ /** The props of `ThemeScope`: the same as on the web, so one source runs on both. */
60
+ export interface ThemeScopeProps {
61
+ /** What it renders, as it is. */
62
+ readonly children?: ReactNode;
63
+ }
64
+ /** The props of `Global`: the same as on the web, so one source runs on both; they are not used. */
65
+ export interface GlobalProps<T> {
66
+ /** The global styles of the web: accepted, and nothing is written. */
67
+ readonly styles: GlobalInput<T>;
68
+ }
69
+ /** What `css()` takes on React Native: native style objects, nothing, and lists of them. */
70
+ export type NativeCssArgument = NativeStyleObject | null | undefined | false | readonly NativeCssArgument[];
71
+ /** `css()` on React Native: one native style, from styles or from a tagged template. */
72
+ export interface NativeCssFunction {
73
+ (template: TemplateStringsArray, ...values: StaticTemplateValue[]): NativeStyleObject;
74
+ (...styles: NativeCssArgument[]): NativeStyleObject;
75
+ }
76
+ /** The platform StyleSheet, with a `create` that registers the styles, and resolves them with the theme when they are a function of it. */
77
+ export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
78
+ /** A sheet given as an object is registered with the platform on first read and is its registered styles, usable at once, with no hook and no theme. */
79
+ create<K extends string>(styles: NativeObjectSheet<K>): { readonly [P in K]: NativeStyleObject; };
80
+ /** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
81
+ create<K extends string>(styles: (theme: T) => Record<K, NativeStyleObject>): SheetDefinition<Record<K, NativeStyleObject>, NativeStyleObject>;
82
+ };
83
+ /**
84
+ * The styling of the theme shape `T` on React Native. What reads the theme (`useTheme`, a style function,
85
+ * a sheet made from a function, `ThemeContext`) gets it as React Native holds it, `NativeTheme<T>`:
86
+ * lengths are numbers. What takes a theme (`ThemeProvider`, `when`) takes `T`.
87
+ */
88
+ export interface NativeStyle<T> {
89
+ /** Makes a styled component from a component and its styles. */
90
+ styled: Styled<NativeTheme<T>>;
91
+ /** The platform StyleSheet, whose `create` also takes a function of the theme. */
92
+ StyleSheet: NativeStyleSheet<NativeTheme<T>>;
93
+ /** The styles of a sheet; an object sheet is its own registered styles and is returned as it is. */
94
+ useStyle<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
95
+ useStyle<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
96
+ /** The props that apply entries of a sheet: a style array. */
97
+ apply(...entries: (NativeStyleObject | null | undefined | false)[]): {
98
+ style: NativeStyleObject[];
99
+ };
100
+ /**
101
+ * One style made once and passed around: native styles merged in order, the last key winning, each
102
+ * turned into a native style as a sheet entry is (lengths in `px` and `rem` are numbers, web-only keys
103
+ * are dropped); or, as a tag, the template read as CSS text and turned the same way. It returns the
104
+ * native style, for `style`. A function is a `TypeError`: there are no props.
105
+ */
106
+ css: NativeCssFunction;
107
+ /** Gives the subtree below it its theme, merged over the theme above; it renders no element. */
108
+ ThemeProvider(props: ThemeProviderProps<T>): ReactElement;
109
+ /** Renders its children: there is no wrapper to repeat, and a modal stays inside the tree, under the providers above it. It exists so that one source runs on both. */
110
+ ThemeScope(props: ThemeScopeProps): ReactElement;
111
+ /** Always undefined: there are no classes on React Native. */
112
+ useThemeClass(): string | undefined;
113
+ /** The context that holds the real theme of the nearest provider (merged over the ones above, lengths as numbers); undefined with no provider above. */
114
+ ThemeContext: Context<NativeTheme<T> | undefined>;
115
+ /** The real theme values, for JavaScript that needs a number or a name: of the nearest provider, else the base theme, else an empty object. Lengths are numbers. */
116
+ useTheme(): NativeTheme<T>;
117
+ /** Always '': there are no classes on React Native. */
118
+ themeClass(theme: DeepPartial<T>, colorScheme?: ColorScheme): string;
119
+ /** A style key for "the nearest provider has this theme object" (compared by identity; `colorScheme` is not used here). */
120
+ when(theme: DeepPartial<T>, colorScheme?: ColorScheme): ThemeKey;
121
+ /** A style key for "the nearest provider's `colorScheme` is this one"; with none above, the platform's color scheme. */
122
+ whenScheme(scheme: SchemeName): SchemeKey;
123
+ /** The name of an animation, as on the web, so one source runs on both. Nothing runs it on React Native: the `animation*` and `transition*` keys are dropped from a style. */
124
+ keyframes(steps: KeyframesInput<T>, options?: KeyframesOptions): string;
125
+ /** A handle of global styles, as on the web; React Native has no global styles, and `Global` does nothing with it. */
126
+ globalStyles(styles: GlobalStylesInput<T>): GlobalStyles<T>;
127
+ /** Renders nothing and does nothing: React Native has no global styles. It exists so that one source runs on both. */
128
+ Global(props: GlobalProps<T>): ReactElement | null;
129
+ }
130
+ export {};
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 };
@@ -0,0 +1,97 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ const require_mediaQuery = require("./mediaQuery.cjs");
3
+ // src/warnStyle.ts
4
+ /** A length or `auto`: one value of a CSS shorthand. */
5
+ const SHORTHAND_ITEM = String.raw`(?:-?(?:\d+(?:\.\d*)?|\.\d+)(?:%|[a-z]*)|auto)`;
6
+ const SHORTHAND = new RegExp(String.raw`^\s*${SHORTHAND_ITEM}(?:\s+${SHORTHAND_ITEM}){1,3}\s*$`);
7
+ /** A number and a unit, once the white space around them is cut. */
8
+ const UNIT_VALUE = /^-?(?:\d+(?:\.\d*)?|\.\d+)([a-z]+)$/i;
9
+ /** The CSS length units, other than a percentage, `em` and the viewport units, that React Native does not take. */
10
+ const CSS_UNITS = /* @__PURE__ */ new Set([
11
+ "px",
12
+ "rem",
13
+ "ex",
14
+ "ch",
15
+ "pt",
16
+ "pc",
17
+ "cm",
18
+ "mm",
19
+ "in",
20
+ "q"
21
+ ]);
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)/;
27
+ const warned = /* @__PURE__ */ new Set();
28
+ /** Whether this is a development build: `process` may not exist, and then nothing is warned. */
29
+ function isDevelopment() {
30
+ return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
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
+ }
45
+ /** What is wrong with the string `value` of `key` on React Native, or undefined when nothing is. */
46
+ function describeProblem(key, value) {
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.`;
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());
52
+ }
53
+ /**
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.
58
+ */
59
+ function warnAboutValues(style, name) {
60
+ if (!isDevelopment()) return;
61
+ for (const [key, value] of Object.entries(style)) {
62
+ const message = typeof value === "string" ? describeProblem(key, value) : void 0;
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}`;
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);
94
+ }
95
+ exports.warnAboutDroppedKey = warnAboutDroppedKey;
96
+ exports.warnAboutQuery = warnAboutQuery;
97
+ exports.warnAboutValues = warnAboutValues;
@@ -0,0 +1,95 @@
1
+ import { readCondition } from "@umber-style/core";
2
+ import { isMediaKey, readMediaQuery } from "./mediaQuery.mjs";
3
+ // src/warnStyle.ts
4
+ /** A length or `auto`: one value of a CSS shorthand. */
5
+ const SHORTHAND_ITEM = String.raw`(?:-?(?:\d+(?:\.\d*)?|\.\d+)(?:%|[a-z]*)|auto)`;
6
+ const SHORTHAND = new RegExp(String.raw`^\s*${SHORTHAND_ITEM}(?:\s+${SHORTHAND_ITEM}){1,3}\s*$`);
7
+ /** A number and a unit, once the white space around them is cut. */
8
+ const UNIT_VALUE = /^-?(?:\d+(?:\.\d*)?|\.\d+)([a-z]+)$/i;
9
+ /** The CSS length units, other than a percentage, `em` and the viewport units, that React Native does not take. */
10
+ const CSS_UNITS = /* @__PURE__ */ new Set([
11
+ "px",
12
+ "rem",
13
+ "ex",
14
+ "ch",
15
+ "pt",
16
+ "pc",
17
+ "cm",
18
+ "mm",
19
+ "in",
20
+ "q"
21
+ ]);
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)/;
27
+ const warned = /* @__PURE__ */ new Set();
28
+ /** Whether this is a development build: `process` may not exist, and then nothing is warned. */
29
+ function isDevelopment() {
30
+ return typeof process !== "undefined" && process.env.NODE_ENV !== "production";
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
+ }
45
+ /** What is wrong with the string `value` of `key` on React Native, or undefined when nothing is. */
46
+ function describeProblem(key, value) {
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.`;
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());
52
+ }
53
+ /**
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.
58
+ */
59
+ function warnAboutValues(style, name) {
60
+ if (!isDevelopment()) return;
61
+ for (const [key, value] of Object.entries(style)) {
62
+ const message = typeof value === "string" ? describeProblem(key, value) : void 0;
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}`;
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);
94
+ }
95
+ export { warnAboutDroppedKey, warnAboutQuery, warnAboutValues };