@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,120 @@
1
+ let react = require("react");
2
+ const require_toNativeStyle = require("./toNativeStyle.cjs");
3
+ const require_windowState = require("./windowState.cjs");
4
+ let react_native = require("react-native");
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
+ }
20
+ /**
21
+ * Makes `styled` for React Native. Its object styles are registered with the platform StyleSheet
22
+ * once, when the component is made; its function styles are computed with the real theme, which
23
+ * `useTheme` reads, on each render. A style with a `when()` or `whenScheme()` key is resolved on each
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.
30
+ */
31
+ function createStyled(useTheme, useCondition, remBase) {
32
+ const styled = (tag, options = {}) => {
33
+ if (typeof tag === "string") throw new TypeError(`styled needs a component on React Native, not the tag "${tag}".`);
34
+ const { shouldForwardProp: shouldForward } = options;
35
+ const settings = {
36
+ remBase,
37
+ name: options.name ?? readComponentName(tag)
38
+ };
39
+ return (...styles) => {
40
+ const keys = styles.map((style) => typeof style === "function" || !style ? NO_KEYS : require_toNativeStyle.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}`, require_toNativeStyle.toNativeStyle(style, void 0, settings, state)]] : []);
51
+ return pairs.length > 0 ? react_native.StyleSheet.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", require_windowState.readWindowState());
61
+ byWindow.set(windowKey, found);
62
+ }
63
+ return found;
64
+ };
65
+ const render = (props, key = "", condition, results, state) => {
66
+ const resolved = styles.flatMap((style, index) => {
67
+ switch (kinds[index]) {
68
+ case "fixed": return [registered[`s${index}`]];
69
+ case "window": return [readWindowStyles(key)[`s${index}`]];
70
+ case "conditional": return [require_toNativeStyle.toNativeStyle(style, condition, settings, state)];
71
+ default: {
72
+ const result = results[index];
73
+ return result ? [require_toNativeStyle.toNativeStyle(result, condition, settings, state)] : [];
74
+ }
75
+ }
76
+ });
77
+ const forwarded = {};
78
+ for (const [name, value] of Object.entries(props)) if (!(name === "as" || name === "theme" || name === "style") && (!shouldForward || shouldForward(name))) forwarded[name] = value;
79
+ forwarded.style = [...resolved, props.style].flat(1e400).filter(Boolean);
80
+ return (0, react.createElement)(props.as ?? tag, forwarded);
81
+ };
82
+ const StaticStyled = (props) => render(props);
83
+ const WindowStyled = (props) => render(props, require_windowState.useMediaKey(queries, false, remBase));
84
+ const ConditionalStyled = (props) => {
85
+ const condition = useCondition();
86
+ const key = require_windowState.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 = require_toNativeStyle.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 = require_windowState.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;
112
+ };
113
+ };
114
+ return styled;
115
+ }
116
+ /** The window now, when there are queries to evaluate. */
117
+ function readStateFor(queries) {
118
+ return queries.length > 0 ? require_windowState.readWindowState() : void 0;
119
+ }
120
+ exports.createStyled = createStyled;
@@ -0,0 +1,120 @@
1
+ import { createElement } from "react";
2
+ import { platformStyleSheet } from "./platform.mjs";
3
+ import { collectKeys, toNativeStyle } from "./toNativeStyle.mjs";
4
+ import { readWindowState, useMediaKey } from "./windowState.mjs";
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
+ }
20
+ /**
21
+ * Makes `styled` for React Native. Its object styles are registered with the platform StyleSheet
22
+ * once, when the component is made; its function styles are computed with the real theme, which
23
+ * `useTheme` reads, on each render. A style with a `when()` or `whenScheme()` key is resolved on each
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.
30
+ */
31
+ function createStyled(useTheme, useCondition, remBase) {
32
+ const styled = (tag, options = {}) => {
33
+ if (typeof tag === "string") throw new TypeError(`styled needs a component on React Native, not the tag "${tag}".`);
34
+ const { shouldForwardProp: shouldForward } = options;
35
+ const settings = {
36
+ remBase,
37
+ name: options.name ?? readComponentName(tag)
38
+ };
39
+ return (...styles) => {
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) => {
66
+ const resolved = styles.flatMap((style, index) => {
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
+ }
76
+ });
77
+ const forwarded = {};
78
+ for (const [name, value] of Object.entries(props)) if (!(name === "as" || name === "theme" || name === "style") && (!shouldForward || shouldForward(name))) forwarded[name] = value;
79
+ forwarded.style = [...resolved, props.style].flat(1e400).filter(Boolean);
80
+ return createElement(props.as ?? tag, forwarded);
81
+ };
82
+ const StaticStyled = (props) => render(props);
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;
112
+ };
113
+ };
114
+ return styled;
115
+ }
116
+ /** The window now, when there are queries to evaluate. */
117
+ function readStateFor(queries) {
118
+ return queries.length > 0 ? readWindowState() : void 0;
119
+ }
120
+ export { createStyled };
@@ -0,0 +1,88 @@
1
+ let _umber_style_core = require("@umber-style/core");
2
+ const require_conditions = require("./conditions.cjs");
3
+ const require_mediaQuery = require("./mediaQuery.cjs");
4
+ const require_units = require("./units.cjs");
5
+ const require_warnStyle = require("./warnStyle.cjs");
6
+ // src/toNativeStyle.ts
7
+ const DEFAULT_SETTINGS = { remBase: 16 };
8
+ /** Whether `value` is a plain object: the `native` part of a style. */
9
+ function isNested(value) {
10
+ return typeof value === "object" && value !== null && !Array.isArray(value);
11
+ }
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
+ */
16
+ function isWebOnlyKey(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
+ }
53
+ }
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);
57
+ }
58
+ /**
59
+ * `style` without the web-only keys: `label`, selectors (a key with `&` or one that starts with `:`),
60
+ * at-rules, custom properties, and the `animation*` and `transition*` properties, each dropped whole. Objects and arrays under any other key are kept
61
+ * (`shadowOffset`, `transform`). A style written for both platforms has a `native` part, which is merged
62
+ * over the rest, and a `web` part, which is dropped. A `when()` or `whenScheme()` key is a part that
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.
68
+ */
69
+ function toNativeStyle(style, condition, settings = DEFAULT_SETTINGS, state) {
70
+ const { web: _web, native, ...rest } = style;
71
+ const { name, remBase } = settings;
72
+ const merged = isNested(native) ? {
73
+ ...rest,
74
+ ...native
75
+ } : rest;
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);
85
+ return resolved;
86
+ }
87
+ exports.collectKeys = collectKeys;
88
+ exports.toNativeStyle = toNativeStyle;
@@ -0,0 +1,87 @@
1
+ import { readCondition } from "@umber-style/core";
2
+ import { matchesCondition } from "./conditions.mjs";
3
+ import { isMediaKey, readMediaQuery } from "./mediaQuery.mjs";
4
+ import { convertValues } from "./units.mjs";
5
+ import { warnAboutDroppedKey, warnAboutValues } from "./warnStyle.mjs";
6
+ // src/toNativeStyle.ts
7
+ const DEFAULT_SETTINGS = { remBase: 16 };
8
+ /** Whether `value` is a plain object: the `native` part of a style. */
9
+ function isNested(value) {
10
+ return typeof value === "object" && value !== null && !Array.isArray(value);
11
+ }
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
+ */
16
+ function isWebOnlyKey(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
+ }
53
+ }
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);
57
+ }
58
+ /**
59
+ * `style` without the web-only keys: `label`, selectors (a key with `&` or one that starts with `:`),
60
+ * at-rules, custom properties, and the `animation*` and `transition*` properties, each dropped whole. Objects and arrays under any other key are kept
61
+ * (`shadowOffset`, `transform`). A style written for both platforms has a `native` part, which is merged
62
+ * over the rest, and a `web` part, which is dropped. A `when()` or `whenScheme()` key is a part that
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.
68
+ */
69
+ function toNativeStyle(style, condition, settings = DEFAULT_SETTINGS, state) {
70
+ const { web: _web, native, ...rest } = style;
71
+ const { name, remBase } = settings;
72
+ const merged = isNested(native) ? {
73
+ ...rest,
74
+ ...native
75
+ } : rest;
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);
85
+ return resolved;
86
+ }
87
+ export { collectKeys, toNativeStyle };
@@ -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.cjs";
3
+ import { NativeTheme } from "./nativeTheme.cjs";
4
+ import { platformStyleSheet } from "./platform.cjs";
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 {};