@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/styled.mjs CHANGED
@@ -1,44 +1,120 @@
1
1
  import { createElement } from "react";
2
2
  import { platformStyleSheet } from "./platform.mjs";
3
- import { hasConditions, toNativeStyle } from "./toNativeStyle.mjs";
3
+ import { collectKeys, toNativeStyle } from "./toNativeStyle.mjs";
4
+ import { readWindowState, useMediaKey } from "./windowState.mjs";
4
5
  // src/styled.ts
6
+ const NO_KEYS = {
7
+ isConditional: false,
8
+ hasScheme: false,
9
+ queries: []
10
+ };
11
+ /** The name a component is known by: its `displayName`, else the name of its function; undefined when it has neither. */
12
+ function readComponentName(component) {
13
+ const { displayName, name } = component;
14
+ return displayName || name || void 0;
15
+ }
16
+ /** `lists` joined, each item once, in order. */
17
+ function joinQueries(lists) {
18
+ return [...new Set(lists.flat())];
19
+ }
5
20
  /**
6
21
  * Makes `styled` for React Native. Its object styles are registered with the platform StyleSheet
7
22
  * once, when the component is made; its function styles are computed with the real theme, which
8
23
  * `useTheme` reads, on each render. A style with a `when()` or `whenScheme()` key is resolved on each
9
- * render against the nearest provider, which `useCondition` reads; it is not registered. A component
10
- * without function styles or condition keys reads no context.
24
+ * render against the nearest provider, which `useCondition` reads; it is not registered. An object style
25
+ * with media query keys and no condition key is resolved and registered once for each set of matching
26
+ * queries. A component renders again when the set of its matching queries changes (or the system color
27
+ * scheme, for a `whenScheme()` key that no provider decides); one without function styles, condition
28
+ * keys or media keys reads no context and subscribes to nothing. A `rem` length is `remBase` times its
29
+ * number. The development warnings name the component: its `name` option, else the wrapped component's.
11
30
  */
12
- function createStyled(useTheme, useCondition) {
31
+ function createStyled(useTheme, useCondition, remBase) {
13
32
  const styled = (tag, options = {}) => {
14
33
  if (typeof tag === "string") throw new TypeError(`styled needs a component on React Native, not the tag "${tag}".`);
15
34
  const { shouldForwardProp: shouldForward } = options;
35
+ const settings = {
36
+ remBase,
37
+ name: options.name ?? readComponentName(tag)
38
+ };
16
39
  return (...styles) => {
17
- const isDynamic = styles.some((style) => typeof style === "function");
18
- const isConditional = styles.some((style) => typeof style !== "function" && !!style && hasConditions(style));
19
- const fixed = Object.fromEntries(styles.flatMap((style, index) => typeof style === "function" || !style || hasConditions(style) ? [] : [[`s${index}`, toNativeStyle(style)]]));
20
- const registered = Object.keys(fixed).length > 0 ? platformStyleSheet.create(fixed) : fixed;
21
- const render = (props, theme, condition) => {
40
+ const keys = styles.map((style) => typeof style === "function" || !style ? NO_KEYS : collectKeys(style) ?? NO_KEYS);
41
+ const kinds = styles.map((style, index) => {
42
+ if (typeof style === "function") return "function";
43
+ if (keys[index].isConditional) return "conditional";
44
+ return keys[index].queries.length > 0 ? "window" : "fixed";
45
+ });
46
+ const queries = joinQueries(keys.map((found) => found.queries));
47
+ const hasScheme = keys.some((found) => found.hasScheme);
48
+ /** The object styles of `kind`, resolved for `state` and registered with the platform. */
49
+ const register = (kind, state) => {
50
+ const pairs = styles.flatMap((style, index) => kinds[index] === kind && style ? [[`s${index}`, toNativeStyle(style, void 0, settings, state)]] : []);
51
+ return pairs.length > 0 ? platformStyleSheet.create(Object.fromEntries(pairs)) : {};
52
+ };
53
+ const registered = register("fixed");
54
+ const byWindow = /* @__PURE__ */ new Map();
55
+ /** The styles that depend on the window only, for the key of a render: registered once for each set of matching queries. */
56
+ const readWindowStyles = (key) => {
57
+ const windowKey = key.slice(0, queries.length);
58
+ let found = byWindow.get(windowKey);
59
+ if (found === void 0) {
60
+ found = register("window", readWindowState());
61
+ byWindow.set(windowKey, found);
62
+ }
63
+ return found;
64
+ };
65
+ const render = (props, key = "", condition, results, state) => {
22
66
  const resolved = styles.flatMap((style, index) => {
23
- if (typeof style !== "function") return [style && hasConditions(style) ? toNativeStyle(style, condition) : registered[`s${index}`]];
24
- const result = style({
25
- ...props,
26
- theme
27
- });
28
- return result ? [toNativeStyle(result, condition)] : [];
67
+ switch (kinds[index]) {
68
+ case "fixed": return [registered[`s${index}`]];
69
+ case "window": return [readWindowStyles(key)[`s${index}`]];
70
+ case "conditional": return [toNativeStyle(style, condition, settings, state)];
71
+ default: {
72
+ const result = results[index];
73
+ return result ? [toNativeStyle(result, condition, settings, state)] : [];
74
+ }
75
+ }
29
76
  });
30
77
  const forwarded = {};
31
- for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "style") && (!shouldForward || shouldForward(key))) forwarded[key] = value;
78
+ for (const [name, value] of Object.entries(props)) if (!(name === "as" || name === "theme" || name === "style") && (!shouldForward || shouldForward(name))) forwarded[name] = value;
32
79
  forwarded.style = [...resolved, props.style].flat(1e400).filter(Boolean);
33
80
  return createElement(props.as ?? tag, forwarded);
34
81
  };
35
82
  const StaticStyled = (props) => render(props);
36
- const DynamicStyled = (props) => render(props, useTheme(), useCondition());
37
- const ConditionalStyled = (props) => render(props, void 0, useCondition());
38
- if (isDynamic) return DynamicStyled;
39
- return isConditional ? ConditionalStyled : StaticStyled;
83
+ const WindowStyled = (props) => render(props, useMediaKey(queries, false, remBase));
84
+ const ConditionalStyled = (props) => {
85
+ const condition = useCondition();
86
+ const key = useMediaKey(queries, hasScheme && condition.colorScheme === void 0, remBase);
87
+ return render(props, key, condition, void 0, readStateFor(queries));
88
+ };
89
+ const DynamicStyled = (props) => {
90
+ const theme = useTheme();
91
+ const condition = useCondition();
92
+ const results = [];
93
+ let found;
94
+ for (let index = 0; index < styles.length; index++) {
95
+ const style = styles[index];
96
+ if (typeof style !== "function") continue;
97
+ const result = style({
98
+ ...props,
99
+ theme
100
+ });
101
+ results[index] = result;
102
+ if (result) found = collectKeys(result, found);
103
+ }
104
+ const all = found === void 0 ? queries : joinQueries([queries, found.queries]);
105
+ const shouldWatchScheme = (hasScheme || !!found?.hasScheme) && condition.colorScheme === void 0;
106
+ const key = useMediaKey(all, shouldWatchScheme, remBase);
107
+ return render(props, key, condition, results, readStateFor(all));
108
+ };
109
+ if (kinds.includes("function")) return DynamicStyled;
110
+ if (kinds.includes("conditional")) return ConditionalStyled;
111
+ return queries.length > 0 ? WindowStyled : StaticStyled;
40
112
  };
41
113
  };
42
114
  return styled;
43
115
  }
116
+ /** The window now, when there are queries to evaluate. */
117
+ function readStateFor(queries) {
118
+ return queries.length > 0 ? readWindowState() : void 0;
119
+ }
44
120
  export { createStyled };
@@ -1,39 +1,88 @@
1
1
  let _umber_style_core = require("@umber-style/core");
2
2
  const require_conditions = require("./conditions.cjs");
3
+ const require_mediaQuery = require("./mediaQuery.cjs");
4
+ const require_units = require("./units.cjs");
3
5
  const require_warnStyle = require("./warnStyle.cjs");
4
6
  // src/toNativeStyle.ts
7
+ const DEFAULT_SETTINGS = { remBase: 16 };
5
8
  /** Whether `value` is a plain object: the `native` part of a style. */
6
9
  function isNested(value) {
7
10
  return typeof value === "object" && value !== null && !Array.isArray(value);
8
11
  }
9
- /** Whether the key `name` exists only on the web: a selector, an at-rule, a custom property or `label`. */
12
+ /**
13
+ * Whether the key `name` exists only on the web: a selector, an at-rule, a custom property, `label`, or
14
+ * an `animation*` or `transition*` property (running an animation on React Native is not built yet).
15
+ */
10
16
  function isWebOnlyKey(name) {
11
- return name === "label" || name.includes("&") || /^(?::|@|--)/.test(name);
17
+ return name === "label" || name.includes("&") || /^(?::|@|--|animation|transition)/.test(name);
18
+ }
19
+ /**
20
+ * The condition and media keys of `style`, added to `into` (see `StyleKeys`); undefined when there are
21
+ * none and no `into`, so that a style without them costs no object.
22
+ */
23
+ function collectKeys(style, into) {
24
+ let found = into;
25
+ for (const key in style) {
26
+ const value = style[key];
27
+ if (!isNested(value)) continue;
28
+ if (key === "native") {
29
+ found = collectKeys(value, found);
30
+ continue;
31
+ }
32
+ const isMedia = require_mediaQuery.isMediaKey(key);
33
+ const condition = isMedia ? void 0 : (0, _umber_style_core.readCondition)(key);
34
+ if (!isMedia && condition === void 0) continue;
35
+ found ??= {
36
+ isConditional: false,
37
+ hasScheme: false,
38
+ queries: []
39
+ };
40
+ addKey(found, key, condition);
41
+ found = collectKeys(value, found);
42
+ }
43
+ return found;
44
+ }
45
+ /** Adds the key `key` of a part to `found`: a media query (no `condition`) once, else that there is a condition, and whether it is a scheme. */
46
+ function addKey(found, key, condition) {
47
+ if (condition === void 0) {
48
+ if (!found.queries.includes(key)) found.queries.push(key);
49
+ } else {
50
+ found.isConditional = true;
51
+ found.hasScheme ||= condition.kind === "scheme";
52
+ }
12
53
  }
13
- /** Whether `style`, or its native part, has a `when()` or `whenScheme()` key: then the result depends on the providers above. */
14
- function hasConditions(style) {
15
- const { native } = style;
16
- return [...Object.keys(style), ...isNested(native) ? Object.keys(native) : []].some((key) => (0, _umber_style_core.readCondition)(key) !== void 0);
54
+ /** Whether the key `key` of a part is a media query that holds for `state`; with no `state` none does. */
55
+ function matchesMedia(key, state, remBase) {
56
+ return state !== void 0 && require_mediaQuery.isMediaKey(key) && require_mediaQuery.readMediaQuery(key).matches(state, remBase);
17
57
  }
18
58
  /**
19
59
  * `style` without the web-only keys: `label`, selectors (a key with `&` or one that starts with `:`),
20
- * at-rules and custom properties, each dropped whole. Objects and arrays under any other key are kept
60
+ * at-rules, custom properties, and the `animation*` and `transition*` properties, each dropped whole. Objects and arrays under any other key are kept
21
61
  * (`shadowOffset`, `transform`). A style written for both platforms has a `native` part, which is merged
22
62
  * over the rest, and a `web` part, which is dropped. A `when()` or `whenScheme()` key is a part that
23
- * applies when it matches `condition` (the nearest provider): it is merged over the rest, in order; with no
24
- * `condition` it is dropped.
63
+ * applies when it matches `condition` (the nearest provider), and a media query key one that applies when
64
+ * it holds for `state` (the window): the parts that apply are merged over the plain keys, in the order
65
+ * they are written, as the web writes them after the base rule; with no `condition` or `state` they are
66
+ * dropped. A string that is a length or a number becomes a number (`convertValues`).
67
+ * In development a dropped key that is a mistake, and a value that is not converted, are warned about.
25
68
  */
26
- function toNativeStyle(style, condition) {
69
+ function toNativeStyle(style, condition, settings = DEFAULT_SETTINGS, state) {
27
70
  const { web: _web, native, ...rest } = style;
71
+ const { name, remBase } = settings;
28
72
  const merged = isNested(native) ? {
29
73
  ...rest,
30
74
  ...native
31
75
  } : rest;
32
- const resolved = Object.fromEntries(Object.entries(merged).filter(([key]) => !isWebOnlyKey(key)));
33
- const matching = Object.entries(merged).filter(([key, part]) => isNested(part) && require_conditions.matchesCondition(key, condition));
34
- for (const [, part] of matching) Object.assign(resolved, toNativeStyle(part, condition));
35
- require_warnStyle.warnAboutValues(resolved);
76
+ const resolved = Object.fromEntries(Object.entries(merged).filter(([key]) => {
77
+ if (!isWebOnlyKey(key)) return true;
78
+ require_warnStyle.warnAboutDroppedKey(key, name);
79
+ return false;
80
+ }));
81
+ const matching = Object.entries(merged).filter(([key, part]) => isNested(part) && (require_conditions.matchesCondition(key, condition) || matchesMedia(key, state, remBase)));
82
+ for (const [, part] of matching) Object.assign(resolved, toNativeStyle(part, condition, settings, state));
83
+ require_units.convertValues(resolved, remBase);
84
+ require_warnStyle.warnAboutValues(resolved, name);
36
85
  return resolved;
37
86
  }
38
- exports.hasConditions = hasConditions;
87
+ exports.collectKeys = collectKeys;
39
88
  exports.toNativeStyle = toNativeStyle;
@@ -1,38 +1,87 @@
1
1
  import { readCondition } from "@umber-style/core";
2
2
  import { matchesCondition } from "./conditions.mjs";
3
- import { warnAboutValues } from "./warnStyle.mjs";
3
+ import { isMediaKey, readMediaQuery } from "./mediaQuery.mjs";
4
+ import { convertValues } from "./units.mjs";
5
+ import { warnAboutDroppedKey, warnAboutValues } from "./warnStyle.mjs";
4
6
  // src/toNativeStyle.ts
7
+ const DEFAULT_SETTINGS = { remBase: 16 };
5
8
  /** Whether `value` is a plain object: the `native` part of a style. */
6
9
  function isNested(value) {
7
10
  return typeof value === "object" && value !== null && !Array.isArray(value);
8
11
  }
9
- /** Whether the key `name` exists only on the web: a selector, an at-rule, a custom property or `label`. */
12
+ /**
13
+ * Whether the key `name` exists only on the web: a selector, an at-rule, a custom property, `label`, or
14
+ * an `animation*` or `transition*` property (running an animation on React Native is not built yet).
15
+ */
10
16
  function isWebOnlyKey(name) {
11
- return name === "label" || name.includes("&") || /^(?::|@|--)/.test(name);
17
+ return name === "label" || name.includes("&") || /^(?::|@|--|animation|transition)/.test(name);
18
+ }
19
+ /**
20
+ * The condition and media keys of `style`, added to `into` (see `StyleKeys`); undefined when there are
21
+ * none and no `into`, so that a style without them costs no object.
22
+ */
23
+ function collectKeys(style, into) {
24
+ let found = into;
25
+ for (const key in style) {
26
+ const value = style[key];
27
+ if (!isNested(value)) continue;
28
+ if (key === "native") {
29
+ found = collectKeys(value, found);
30
+ continue;
31
+ }
32
+ const isMedia = isMediaKey(key);
33
+ const condition = isMedia ? void 0 : readCondition(key);
34
+ if (!isMedia && condition === void 0) continue;
35
+ found ??= {
36
+ isConditional: false,
37
+ hasScheme: false,
38
+ queries: []
39
+ };
40
+ addKey(found, key, condition);
41
+ found = collectKeys(value, found);
42
+ }
43
+ return found;
44
+ }
45
+ /** Adds the key `key` of a part to `found`: a media query (no `condition`) once, else that there is a condition, and whether it is a scheme. */
46
+ function addKey(found, key, condition) {
47
+ if (condition === void 0) {
48
+ if (!found.queries.includes(key)) found.queries.push(key);
49
+ } else {
50
+ found.isConditional = true;
51
+ found.hasScheme ||= condition.kind === "scheme";
52
+ }
12
53
  }
13
- /** Whether `style`, or its native part, has a `when()` or `whenScheme()` key: then the result depends on the providers above. */
14
- function hasConditions(style) {
15
- const { native } = style;
16
- return [...Object.keys(style), ...isNested(native) ? Object.keys(native) : []].some((key) => readCondition(key) !== void 0);
54
+ /** Whether the key `key` of a part is a media query that holds for `state`; with no `state` none does. */
55
+ function matchesMedia(key, state, remBase) {
56
+ return state !== void 0 && isMediaKey(key) && readMediaQuery(key).matches(state, remBase);
17
57
  }
18
58
  /**
19
59
  * `style` without the web-only keys: `label`, selectors (a key with `&` or one that starts with `:`),
20
- * at-rules and custom properties, each dropped whole. Objects and arrays under any other key are kept
60
+ * at-rules, custom properties, and the `animation*` and `transition*` properties, each dropped whole. Objects and arrays under any other key are kept
21
61
  * (`shadowOffset`, `transform`). A style written for both platforms has a `native` part, which is merged
22
62
  * over the rest, and a `web` part, which is dropped. A `when()` or `whenScheme()` key is a part that
23
- * applies when it matches `condition` (the nearest provider): it is merged over the rest, in order; with no
24
- * `condition` it is dropped.
63
+ * applies when it matches `condition` (the nearest provider), and a media query key one that applies when
64
+ * it holds for `state` (the window): the parts that apply are merged over the plain keys, in the order
65
+ * they are written, as the web writes them after the base rule; with no `condition` or `state` they are
66
+ * dropped. A string that is a length or a number becomes a number (`convertValues`).
67
+ * In development a dropped key that is a mistake, and a value that is not converted, are warned about.
25
68
  */
26
- function toNativeStyle(style, condition) {
69
+ function toNativeStyle(style, condition, settings = DEFAULT_SETTINGS, state) {
27
70
  const { web: _web, native, ...rest } = style;
71
+ const { name, remBase } = settings;
28
72
  const merged = isNested(native) ? {
29
73
  ...rest,
30
74
  ...native
31
75
  } : rest;
32
- const resolved = Object.fromEntries(Object.entries(merged).filter(([key]) => !isWebOnlyKey(key)));
33
- const matching = Object.entries(merged).filter(([key, part]) => isNested(part) && matchesCondition(key, condition));
34
- for (const [, part] of matching) Object.assign(resolved, toNativeStyle(part, condition));
35
- warnAboutValues(resolved);
76
+ const resolved = Object.fromEntries(Object.entries(merged).filter(([key]) => {
77
+ if (!isWebOnlyKey(key)) return true;
78
+ warnAboutDroppedKey(key, name);
79
+ return false;
80
+ }));
81
+ const matching = Object.entries(merged).filter(([key, part]) => isNested(part) && (matchesCondition(key, condition) || matchesMedia(key, state, remBase)));
82
+ for (const [, part] of matching) Object.assign(resolved, toNativeStyle(part, condition, settings, state));
83
+ convertValues(resolved, remBase);
84
+ warnAboutValues(resolved, name);
36
85
  return resolved;
37
86
  }
38
- export { hasConditions, toNativeStyle };
87
+ export { collectKeys, toNativeStyle };
package/dist/types.d.cts CHANGED
@@ -1,37 +1,78 @@
1
- import { BindingOptions, ColorScheme, DeepPartial, SchemeKey, SchemeName, StylerOptions, ThemeKey } from "@umber-style/core";
1
+ import { BindingOptions, Cache, ColorScheme, DeepPartial, GlobalInput, GlobalStyles, GlobalStylesInput, KeyframesInput, KeyframesOptions, SchemeKey, SchemeName, StaticTemplateValue, StylerOptions, ThemeInput, ThemeKey } from "@umber-style/core";
2
2
  import { NativeObjectSheet, NativeSheetStyles, NativeStyleInput, NativeStyleObject } from "./nativeStyle.cjs";
3
+ import { NativeTheme } from "./nativeTheme.cjs";
3
4
  import { platformStyleSheet } from "./platform.cjs";
4
- import { ComponentProps, ElementType, FunctionComponent, ReactElement, ReactNode } from "react";
5
- /** The options of `createStyle`: the same as on the web, so one call runs on both; they are not used. */
6
- export type StyleOptions<T = never> = BindingOptions<T>;
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. */
7
16
  export interface StyledOptions extends StylerOptions {
8
17
  /** Whether a prop is forwarded. Default: every prop but `as`, `theme` and `style`. */
9
18
  shouldForwardProp?: (prop: string) => boolean;
10
19
  }
11
20
  /** What the `style` prop of a styled component takes: style objects (sheet entries are such objects), in arrays, with falsy values dropped. */
12
21
  type StyleValue = NativeStyleObject | false | null | readonly StyleProp[];
22
+ /** What the `style` prop of a styled component takes, or nothing. */
13
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`. */
14
25
  export type StyledComponent<C extends ElementType, P extends object> = FunctionComponent<Omit<ComponentProps<C>, keyof P | 'as' | 'style'> & P & {
15
26
  as?: ElementType;
16
27
  style?: StyleValue;
17
28
  }>;
29
+ /** `styled` of a theme `T`: a tag and options, then the styles of the component. */
18
30
  export type Styled<T> = <C extends ElementType>(tag: C, options?: StyledOptions) => <P extends object = object>(...styles: NativeStyleInput<T, P>[]) => StyledComponent<C, P>;
19
31
  /** A set of named styles; `E` is what one of them becomes. */
20
32
  export interface SheetDefinition<S extends NativeSheetStyles, E> {
21
33
  /** Never set at run time: it only carries the types. */
22
34
  readonly entries?: { [K in keyof S]: E; };
23
35
  }
36
+ /** The props of `ThemeProvider`: the theme of the subtree and its color scheme, and the props of the web that are ignored. */
24
37
  export interface ThemeProviderProps<T> {
25
- /** The theme, or the part of it that this subtree changes. */
26
- readonly theme: DeepPartial<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>;
27
43
  /** The color scheme of this subtree, for `whenScheme`: `light` or `dark`; `light dark` keeps the one above. */
28
44
  readonly colorScheme?: ColorScheme;
29
45
  /** Ignored on React Native. */
30
46
  readonly className?: string;
31
47
  /** Accepted and ignored: there are no classes on React Native, so the provider never renders an element. */
32
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. */
33
57
  readonly children?: ReactNode;
34
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
+ }
35
76
  /** The platform StyleSheet, with a `create` that registers the styles, and resolves them with the theme when they are a function of it. */
36
77
  export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
37
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. */
@@ -39,9 +80,16 @@ export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
39
80
  /** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
40
81
  create<K extends string>(styles: (theme: T) => Record<K, NativeStyleObject>): SheetDefinition<Record<K, NativeStyleObject>, NativeStyleObject>;
41
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
+ */
42
88
  export interface NativeStyle<T> {
43
- styled: Styled<T>;
44
- StyleSheet: NativeStyleSheet<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>>;
45
93
  /** The styles of a sheet; an object sheet is its own registered styles and is returned as it is. */
46
94
  useStyle<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
47
95
  useStyle<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
@@ -49,14 +97,34 @@ export interface NativeStyle<T> {
49
97
  apply(...entries: (NativeStyleObject | null | undefined | false)[]): {
50
98
  style: NativeStyleObject[];
51
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. */
52
108
  ThemeProvider(props: ThemeProviderProps<T>): ReactElement;
53
- /** The real theme values, for JavaScript that needs a number or a name. */
54
- useTheme(): T;
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>;
55
117
  /** Always '': there are no classes on React Native. */
56
118
  themeClass(theme: DeepPartial<T>, colorScheme?: ColorScheme): string;
57
119
  /** A style key for "the nearest provider has this theme object" (compared by identity; `colorScheme` is not used here). */
58
120
  when(theme: DeepPartial<T>, colorScheme?: ColorScheme): ThemeKey;
59
121
  /** A style key for "the nearest provider's `colorScheme` is this one"; with none above, the platform's color scheme. */
60
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;
61
129
  }
62
130
  export {};
package/dist/types.d.mts CHANGED
@@ -1,37 +1,78 @@
1
- import { BindingOptions, ColorScheme, DeepPartial, SchemeKey, SchemeName, StylerOptions, ThemeKey } from "@umber-style/core";
1
+ import { BindingOptions, Cache, ColorScheme, DeepPartial, GlobalInput, GlobalStyles, GlobalStylesInput, KeyframesInput, KeyframesOptions, SchemeKey, SchemeName, StaticTemplateValue, StylerOptions, ThemeInput, ThemeKey } from "@umber-style/core";
2
2
  import { NativeObjectSheet, NativeSheetStyles, NativeStyleInput, NativeStyleObject } from "./nativeStyle.mjs";
3
+ import { NativeTheme } from "./nativeTheme.mjs";
3
4
  import { platformStyleSheet } from "./platform.mjs";
4
- import { ComponentProps, ElementType, FunctionComponent, ReactElement, ReactNode } from "react";
5
- /** The options of `createStyle`: the same as on the web, so one call runs on both; they are not used. */
6
- export type StyleOptions<T = never> = BindingOptions<T>;
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. */
7
16
  export interface StyledOptions extends StylerOptions {
8
17
  /** Whether a prop is forwarded. Default: every prop but `as`, `theme` and `style`. */
9
18
  shouldForwardProp?: (prop: string) => boolean;
10
19
  }
11
20
  /** What the `style` prop of a styled component takes: style objects (sheet entries are such objects), in arrays, with falsy values dropped. */
12
21
  type StyleValue = NativeStyleObject | false | null | readonly StyleProp[];
22
+ /** What the `style` prop of a styled component takes, or nothing. */
13
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`. */
14
25
  export type StyledComponent<C extends ElementType, P extends object> = FunctionComponent<Omit<ComponentProps<C>, keyof P | 'as' | 'style'> & P & {
15
26
  as?: ElementType;
16
27
  style?: StyleValue;
17
28
  }>;
29
+ /** `styled` of a theme `T`: a tag and options, then the styles of the component. */
18
30
  export type Styled<T> = <C extends ElementType>(tag: C, options?: StyledOptions) => <P extends object = object>(...styles: NativeStyleInput<T, P>[]) => StyledComponent<C, P>;
19
31
  /** A set of named styles; `E` is what one of them becomes. */
20
32
  export interface SheetDefinition<S extends NativeSheetStyles, E> {
21
33
  /** Never set at run time: it only carries the types. */
22
34
  readonly entries?: { [K in keyof S]: E; };
23
35
  }
36
+ /** The props of `ThemeProvider`: the theme of the subtree and its color scheme, and the props of the web that are ignored. */
24
37
  export interface ThemeProviderProps<T> {
25
- /** The theme, or the part of it that this subtree changes. */
26
- readonly theme: DeepPartial<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>;
27
43
  /** The color scheme of this subtree, for `whenScheme`: `light` or `dark`; `light dark` keeps the one above. */
28
44
  readonly colorScheme?: ColorScheme;
29
45
  /** Ignored on React Native. */
30
46
  readonly className?: string;
31
47
  /** Accepted and ignored: there are no classes on React Native, so the provider never renders an element. */
32
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. */
33
57
  readonly children?: ReactNode;
34
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
+ }
35
76
  /** The platform StyleSheet, with a `create` that registers the styles, and resolves them with the theme when they are a function of it. */
36
77
  export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
37
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. */
@@ -39,9 +80,16 @@ export type NativeStyleSheet<T> = Omit<typeof platformStyleSheet, 'create'> & {
39
80
  /** A sheet given as a function of the theme is for `useStyle`, which resolves it against the theme. */
40
81
  create<K extends string>(styles: (theme: T) => Record<K, NativeStyleObject>): SheetDefinition<Record<K, NativeStyleObject>, NativeStyleObject>;
41
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
+ */
42
88
  export interface NativeStyle<T> {
43
- styled: Styled<T>;
44
- StyleSheet: NativeStyleSheet<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>>;
45
93
  /** The styles of a sheet; an object sheet is its own registered styles and is returned as it is. */
46
94
  useStyle<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
47
95
  useStyle<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
@@ -49,14 +97,34 @@ export interface NativeStyle<T> {
49
97
  apply(...entries: (NativeStyleObject | null | undefined | false)[]): {
50
98
  style: NativeStyleObject[];
51
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. */
52
108
  ThemeProvider(props: ThemeProviderProps<T>): ReactElement;
53
- /** The real theme values, for JavaScript that needs a number or a name. */
54
- useTheme(): T;
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>;
55
117
  /** Always '': there are no classes on React Native. */
56
118
  themeClass(theme: DeepPartial<T>, colorScheme?: ColorScheme): string;
57
119
  /** A style key for "the nearest provider has this theme object" (compared by identity; `colorScheme` is not used here). */
58
120
  when(theme: DeepPartial<T>, colorScheme?: ColorScheme): ThemeKey;
59
121
  /** A style key for "the nearest provider's `colorScheme` is this one"; with none above, the platform's color scheme. */
60
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;
61
129
  }
62
130
  export {};