@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.
- package/LICENSE +21 -0
- package/README.md +11 -1
- package/dist/CacheProvider.cjs +7 -0
- package/dist/CacheProvider.d.cts +4 -0
- package/dist/CacheProvider.d.mts +4 -0
- package/dist/CacheProvider.mjs +7 -0
- package/dist/ThemeValue.cjs +68 -0
- package/dist/ThemeValue.mjs +68 -0
- package/dist/alpha.cjs +16 -0
- package/dist/alpha.d.cts +8 -0
- package/dist/alpha.d.mts +8 -0
- package/dist/alpha.mjs +16 -0
- package/dist/calc.cjs +90 -0
- package/dist/calc.d.cts +24 -0
- package/dist/calc.d.mts +24 -0
- package/dist/calc.mjs +86 -0
- package/dist/conditions.cjs +29 -0
- package/dist/conditions.mjs +27 -0
- package/dist/createStyle.cjs +183 -0
- package/dist/createStyle.d.cts +8 -0
- package/dist/createStyle.d.mts +8 -0
- package/dist/createStyle.mjs +182 -0
- package/dist/defaultStyle.cjs +69 -0
- package/dist/defaultStyle.d.cts +34 -0
- package/dist/defaultStyle.d.mts +34 -0
- package/dist/defaultStyle.mjs +69 -0
- package/dist/index.cjs +41 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.mts +21 -0
- package/dist/index.mjs +8 -0
- package/dist/mediaQuery.cjs +179 -0
- package/dist/mediaQuery.d.cts +1 -0
- package/dist/mediaQuery.mjs +178 -0
- package/dist/nativeStyle.d.cts +32 -0
- package/dist/nativeStyle.d.mts +32 -0
- package/dist/nativeTheme.cjs +51 -0
- package/dist/nativeTheme.d.cts +15 -0
- package/dist/nativeTheme.d.mts +15 -0
- package/dist/nativeTheme.mjs +50 -0
- package/dist/platform.d.cts +2 -0
- package/dist/platform.d.mts +2 -0
- package/dist/platform.mjs +1 -0
- package/dist/styled.cjs +120 -0
- package/dist/styled.mjs +120 -0
- package/dist/toNativeStyle.cjs +88 -0
- package/dist/toNativeStyle.mjs +87 -0
- package/dist/types.d.cts +130 -0
- package/dist/types.d.mts +130 -0
- package/dist/units.cjs +137 -0
- package/dist/units.mjs +133 -0
- package/dist/warnStyle.cjs +97 -0
- package/dist/warnStyle.mjs +95 -0
- package/dist/windowState.cjs +89 -0
- package/dist/windowState.d.cts +6 -0
- package/dist/windowState.d.mts +5 -0
- package/dist/windowState.mjs +85 -0
- package/package.json +77 -5
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
const require_conditions = require("./conditions.cjs");
|
|
3
|
+
let _umber_style_core = require("@umber-style/core");
|
|
4
|
+
const require_ThemeValue = require("./ThemeValue.cjs");
|
|
5
|
+
const require_nativeTheme = require("./nativeTheme.cjs");
|
|
6
|
+
const require_styled = require("./styled.cjs");
|
|
7
|
+
const require_toNativeStyle = require("./toNativeStyle.cjs");
|
|
8
|
+
const require_windowState = require("./windowState.cjs");
|
|
9
|
+
let react_native = require("react-native");
|
|
10
|
+
// src/createStyle.ts
|
|
11
|
+
const NO_QUERIES = [];
|
|
12
|
+
const NO_KEYS = {
|
|
13
|
+
isConditional: false,
|
|
14
|
+
hasScheme: false,
|
|
15
|
+
queries: []
|
|
16
|
+
};
|
|
17
|
+
/** The name of the sheet entry `key` in the development warnings. */
|
|
18
|
+
const toEntryName = (key) => `sheet entry "${key}"`;
|
|
19
|
+
/** Registers `source` with the platform StyleSheet, without the keys that only the web has; a `rem` length is `remBase` times its number. */
|
|
20
|
+
function register(source, remBase) {
|
|
21
|
+
const pairs = Object.entries(source).map(([key, style]) => [key, require_toNativeStyle.toNativeStyle(style, void 0, {
|
|
22
|
+
remBase,
|
|
23
|
+
name: toEntryName(key)
|
|
24
|
+
})]);
|
|
25
|
+
const entries = react_native.StyleSheet.create(Object.fromEntries(pairs));
|
|
26
|
+
let all;
|
|
27
|
+
for (const key in source) all = require_toNativeStyle.collectKeys(source[key], all);
|
|
28
|
+
const keys = {};
|
|
29
|
+
if (all !== void 0) for (const key in source) keys[key] = require_toNativeStyle.collectKeys(source[key]) ?? NO_KEYS;
|
|
30
|
+
return {
|
|
31
|
+
entries,
|
|
32
|
+
source,
|
|
33
|
+
keys,
|
|
34
|
+
isConditional: all?.isConditional ?? false,
|
|
35
|
+
queries: all?.queries ?? NO_QUERIES,
|
|
36
|
+
hasScheme: all?.hasScheme ?? false
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** The entries of `sheet` for the key of a render, with the ones that depend on the window only resolved and registered once for each set of matching queries. */
|
|
40
|
+
function readWindowEntries(sheet, key, remBase) {
|
|
41
|
+
const { entries, keys, queries, source } = sheet;
|
|
42
|
+
const windowKey = key.slice(0, queries.length);
|
|
43
|
+
const byWindow = sheet.byWindow ??= /* @__PURE__ */ new Map();
|
|
44
|
+
let found = byWindow.get(windowKey);
|
|
45
|
+
if (found === void 0) {
|
|
46
|
+
const state = require_windowState.readWindowState();
|
|
47
|
+
const pairs = Object.entries(source).flatMap(([name, style]) => keys[name].queries.length > 0 && !keys[name].isConditional ? [[name, require_toNativeStyle.toNativeStyle(style, void 0, {
|
|
48
|
+
remBase,
|
|
49
|
+
name: toEntryName(name)
|
|
50
|
+
}, state)]] : []);
|
|
51
|
+
found = {
|
|
52
|
+
...entries,
|
|
53
|
+
...react_native.StyleSheet.create(Object.fromEntries(pairs))
|
|
54
|
+
};
|
|
55
|
+
byWindow.set(windowKey, found);
|
|
56
|
+
}
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The entries of `sheet` for the key of a render: the ones that depend on the window only as registered
|
|
61
|
+
* for the matching queries, and the ones that have a `when()` or `whenScheme()` key resolved against
|
|
62
|
+
* `condition` and the window (not registered: they change with the providers).
|
|
63
|
+
*/
|
|
64
|
+
function resolveEntries(sheet, condition, key, remBase) {
|
|
65
|
+
const { isConditional, keys, queries, source } = sheet;
|
|
66
|
+
const hasQueries = queries.length > 0;
|
|
67
|
+
const entries = hasQueries ? readWindowEntries(sheet, key, remBase) : sheet.entries;
|
|
68
|
+
if (!isConditional) return entries;
|
|
69
|
+
const state = hasQueries ? require_windowState.readWindowState() : void 0;
|
|
70
|
+
const pairs = Object.entries(source).map(([name, style]) => [name, keys[name].isConditional ? require_toNativeStyle.toNativeStyle(style, condition, {
|
|
71
|
+
remBase,
|
|
72
|
+
name: toEntryName(name)
|
|
73
|
+
}, state) : entries[name]]);
|
|
74
|
+
return Object.fromEntries(pairs);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `css()` on React Native: the styles (lists flattened, falsy values dropped), or the style of a tagged
|
|
78
|
+
* template read as CSS text, each turned into a native style as a sheet entry is, merged in order with
|
|
79
|
+
* the last key winning. Throws a `TypeError` for a function and for anything else that is not a style.
|
|
80
|
+
*/
|
|
81
|
+
function mergeNativeStyles(args, remBase) {
|
|
82
|
+
const template = (0, _umber_style_core.readStaticTemplate)(args, "css");
|
|
83
|
+
if (!template) (0, _umber_style_core.rejectFunctions)(args, "css");
|
|
84
|
+
const styles = template ? [template] : args.flat(1e400).filter(Boolean);
|
|
85
|
+
const settings = {
|
|
86
|
+
remBase,
|
|
87
|
+
name: "css()"
|
|
88
|
+
};
|
|
89
|
+
const merged = {};
|
|
90
|
+
for (const style of styles) {
|
|
91
|
+
if (typeof style !== "object") throw new TypeError(`css(): ${JSON.stringify(style)} is not a style: give style objects.`);
|
|
92
|
+
Object.assign(merged, require_toNativeStyle.toNativeStyle(style, void 0, settings));
|
|
93
|
+
}
|
|
94
|
+
return merged;
|
|
95
|
+
}
|
|
96
|
+
/** `Global` on React Native: there are no global styles, so it renders nothing and does nothing. */
|
|
97
|
+
function Global(_props) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
/** `ThemeScope` on React Native: there is no wrapper to repeat, and a modal stays inside the tree, so it renders its children. */
|
|
101
|
+
function ThemeScope({ children }) {
|
|
102
|
+
return (0, react.createElement)(react.Fragment, null, children);
|
|
103
|
+
}
|
|
104
|
+
/** `useThemeClass` on React Native: there are no classes. */
|
|
105
|
+
function useThemeClass() {}
|
|
106
|
+
/**
|
|
107
|
+
* Makes the React Native styling over a holder of the base theme; the theme context is made here unless
|
|
108
|
+
* given. Of `options`, `remBase` and `lengthKeys` are used: a theme is converted under `lengthKeys`.
|
|
109
|
+
*/
|
|
110
|
+
function bindHolder(holder, context, options = {}) {
|
|
111
|
+
const { remBase = 16, lengthKeys = require_nativeTheme.DEFAULT_LENGTH_KEYS } = options;
|
|
112
|
+
const { ThemeContext, ThemeValueProvider, useTheme, useCondition } = require_ThemeValue.createThemeValue(holder, (theme) => require_nativeTheme.toNativeTheme(theme, lengthKeys, remBase), context);
|
|
113
|
+
function ThemeProvider({ theme, colorScheme, children }) {
|
|
114
|
+
return (0, react.createElement)(ThemeValueProvider, {
|
|
115
|
+
theme,
|
|
116
|
+
colorScheme
|
|
117
|
+
}, children);
|
|
118
|
+
}
|
|
119
|
+
function create(styles) {
|
|
120
|
+
if (typeof styles !== "function") {
|
|
121
|
+
let registered;
|
|
122
|
+
return (0, _umber_style_core.createLazyRecord)(Object.keys(styles), (key) => {
|
|
123
|
+
registered ??= register(styles, remBase);
|
|
124
|
+
return registered.entries[key];
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const byTheme = /* @__PURE__ */ new WeakMap();
|
|
128
|
+
const prepare = (current) => {
|
|
129
|
+
let sheet = byTheme.get(current);
|
|
130
|
+
if (!sheet) {
|
|
131
|
+
sheet = register(styles(current), remBase);
|
|
132
|
+
byTheme.set(current, sheet);
|
|
133
|
+
}
|
|
134
|
+
return sheet;
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
prepare,
|
|
138
|
+
resolve(current, condition) {
|
|
139
|
+
const sheet = prepare(current);
|
|
140
|
+
return resolveEntries(sheet, condition, require_windowState.readMediaKey(sheet.queries, false, remBase), remBase);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const useStyle = ((sheet) => {
|
|
145
|
+
const theme = useTheme();
|
|
146
|
+
const condition = useCondition();
|
|
147
|
+
const prepared = (0, _umber_style_core.isThemedSheet)(sheet) ? sheet.prepare(theme) : void 0;
|
|
148
|
+
const shouldWatchScheme = !!prepared?.hasScheme && condition.colorScheme === void 0;
|
|
149
|
+
const key = require_windowState.useMediaKey(prepared?.queries ?? NO_QUERIES, shouldWatchScheme, remBase);
|
|
150
|
+
return prepared === void 0 ? sheet : resolveEntries(prepared, condition, key, remBase);
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
styled: require_styled.createStyled(useTheme, useCondition, remBase),
|
|
154
|
+
StyleSheet: {
|
|
155
|
+
...react_native.StyleSheet,
|
|
156
|
+
create
|
|
157
|
+
},
|
|
158
|
+
useStyle,
|
|
159
|
+
apply: (...entries) => ({ style: entries.filter((entry) => !!entry) }),
|
|
160
|
+
css: ((...args) => mergeNativeStyles(args, remBase)),
|
|
161
|
+
ThemeProvider,
|
|
162
|
+
ThemeScope,
|
|
163
|
+
ThemeContext,
|
|
164
|
+
useTheme,
|
|
165
|
+
useThemeClass,
|
|
166
|
+
themeClass: () => "",
|
|
167
|
+
when: require_conditions.createNativeThemeKey,
|
|
168
|
+
whenScheme: _umber_style_core.createSchemeKey,
|
|
169
|
+
keyframes: _umber_style_core.keyframes,
|
|
170
|
+
globalStyles: _umber_style_core.globalStyles,
|
|
171
|
+
Global
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Makes the styling of one theme shape for React Native, apart from the default one. The options are
|
|
176
|
+
* those of the web, so that one call runs on both platforms, with `remBase` added; of them only
|
|
177
|
+
* `remBase` and `lengthKeys` are used.
|
|
178
|
+
*/
|
|
179
|
+
function createStyle(theme, options = {}) {
|
|
180
|
+
return bindHolder({ baseTheme: theme }, void 0, options);
|
|
181
|
+
}
|
|
182
|
+
exports.bindHolder = bindHolder;
|
|
183
|
+
exports.createStyle = createStyle;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { NativeStyle, StyleOptions } from "./types.cjs";
|
|
2
|
+
import "./nativeTheme.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Makes the styling of one theme shape for React Native, apart from the default one. The options are
|
|
5
|
+
* those of the web, so that one call runs on both platforms, with `remBase` added; of them only
|
|
6
|
+
* `remBase` and `lengthKeys` are used.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): NativeStyle<T>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import "react";
|
|
2
|
+
import { NativeStyle, StyleOptions } from "./types.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Makes the styling of one theme shape for React Native, apart from the default one. The options are
|
|
5
|
+
* those of the web, so that one call runs on both platforms, with `remBase` added; of them only
|
|
6
|
+
* `remBase` and `lengthKeys` are used.
|
|
7
|
+
*/
|
|
8
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): NativeStyle<T>;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { Fragment, createElement } from "react";
|
|
2
|
+
import { createNativeThemeKey } from "./conditions.mjs";
|
|
3
|
+
import { createLazyRecord, createSchemeKey, globalStyles, isThemedSheet, keyframes, readStaticTemplate, rejectFunctions } from "@umber-style/core";
|
|
4
|
+
import { createThemeValue } from "./ThemeValue.mjs";
|
|
5
|
+
import { DEFAULT_LENGTH_KEYS, toNativeTheme } from "./nativeTheme.mjs";
|
|
6
|
+
import { createStyled } from "./styled.mjs";
|
|
7
|
+
import { platformStyleSheet } from "./platform.mjs";
|
|
8
|
+
import { collectKeys, toNativeStyle } from "./toNativeStyle.mjs";
|
|
9
|
+
import { readMediaKey, readWindowState, useMediaKey } from "./windowState.mjs";
|
|
10
|
+
// src/createStyle.ts
|
|
11
|
+
const NO_QUERIES = [];
|
|
12
|
+
const NO_KEYS = {
|
|
13
|
+
isConditional: false,
|
|
14
|
+
hasScheme: false,
|
|
15
|
+
queries: []
|
|
16
|
+
};
|
|
17
|
+
/** The name of the sheet entry `key` in the development warnings. */
|
|
18
|
+
const toEntryName = (key) => `sheet entry "${key}"`;
|
|
19
|
+
/** Registers `source` with the platform StyleSheet, without the keys that only the web has; a `rem` length is `remBase` times its number. */
|
|
20
|
+
function register(source, remBase) {
|
|
21
|
+
const pairs = Object.entries(source).map(([key, style]) => [key, toNativeStyle(style, void 0, {
|
|
22
|
+
remBase,
|
|
23
|
+
name: toEntryName(key)
|
|
24
|
+
})]);
|
|
25
|
+
const entries = platformStyleSheet.create(Object.fromEntries(pairs));
|
|
26
|
+
let all;
|
|
27
|
+
for (const key in source) all = collectKeys(source[key], all);
|
|
28
|
+
const keys = {};
|
|
29
|
+
if (all !== void 0) for (const key in source) keys[key] = collectKeys(source[key]) ?? NO_KEYS;
|
|
30
|
+
return {
|
|
31
|
+
entries,
|
|
32
|
+
source,
|
|
33
|
+
keys,
|
|
34
|
+
isConditional: all?.isConditional ?? false,
|
|
35
|
+
queries: all?.queries ?? NO_QUERIES,
|
|
36
|
+
hasScheme: all?.hasScheme ?? false
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** The entries of `sheet` for the key of a render, with the ones that depend on the window only resolved and registered once for each set of matching queries. */
|
|
40
|
+
function readWindowEntries(sheet, key, remBase) {
|
|
41
|
+
const { entries, keys, queries, source } = sheet;
|
|
42
|
+
const windowKey = key.slice(0, queries.length);
|
|
43
|
+
const byWindow = sheet.byWindow ??= /* @__PURE__ */ new Map();
|
|
44
|
+
let found = byWindow.get(windowKey);
|
|
45
|
+
if (found === void 0) {
|
|
46
|
+
const state = readWindowState();
|
|
47
|
+
const pairs = Object.entries(source).flatMap(([name, style]) => keys[name].queries.length > 0 && !keys[name].isConditional ? [[name, toNativeStyle(style, void 0, {
|
|
48
|
+
remBase,
|
|
49
|
+
name: toEntryName(name)
|
|
50
|
+
}, state)]] : []);
|
|
51
|
+
found = {
|
|
52
|
+
...entries,
|
|
53
|
+
...platformStyleSheet.create(Object.fromEntries(pairs))
|
|
54
|
+
};
|
|
55
|
+
byWindow.set(windowKey, found);
|
|
56
|
+
}
|
|
57
|
+
return found;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* The entries of `sheet` for the key of a render: the ones that depend on the window only as registered
|
|
61
|
+
* for the matching queries, and the ones that have a `when()` or `whenScheme()` key resolved against
|
|
62
|
+
* `condition` and the window (not registered: they change with the providers).
|
|
63
|
+
*/
|
|
64
|
+
function resolveEntries(sheet, condition, key, remBase) {
|
|
65
|
+
const { isConditional, keys, queries, source } = sheet;
|
|
66
|
+
const hasQueries = queries.length > 0;
|
|
67
|
+
const entries = hasQueries ? readWindowEntries(sheet, key, remBase) : sheet.entries;
|
|
68
|
+
if (!isConditional) return entries;
|
|
69
|
+
const state = hasQueries ? readWindowState() : void 0;
|
|
70
|
+
const pairs = Object.entries(source).map(([name, style]) => [name, keys[name].isConditional ? toNativeStyle(style, condition, {
|
|
71
|
+
remBase,
|
|
72
|
+
name: toEntryName(name)
|
|
73
|
+
}, state) : entries[name]]);
|
|
74
|
+
return Object.fromEntries(pairs);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* `css()` on React Native: the styles (lists flattened, falsy values dropped), or the style of a tagged
|
|
78
|
+
* template read as CSS text, each turned into a native style as a sheet entry is, merged in order with
|
|
79
|
+
* the last key winning. Throws a `TypeError` for a function and for anything else that is not a style.
|
|
80
|
+
*/
|
|
81
|
+
function mergeNativeStyles(args, remBase) {
|
|
82
|
+
const template = readStaticTemplate(args, "css");
|
|
83
|
+
if (!template) rejectFunctions(args, "css");
|
|
84
|
+
const styles = template ? [template] : args.flat(1e400).filter(Boolean);
|
|
85
|
+
const settings = {
|
|
86
|
+
remBase,
|
|
87
|
+
name: "css()"
|
|
88
|
+
};
|
|
89
|
+
const merged = {};
|
|
90
|
+
for (const style of styles) {
|
|
91
|
+
if (typeof style !== "object") throw new TypeError(`css(): ${JSON.stringify(style)} is not a style: give style objects.`);
|
|
92
|
+
Object.assign(merged, toNativeStyle(style, void 0, settings));
|
|
93
|
+
}
|
|
94
|
+
return merged;
|
|
95
|
+
}
|
|
96
|
+
/** `Global` on React Native: there are no global styles, so it renders nothing and does nothing. */
|
|
97
|
+
function Global(_props) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
/** `ThemeScope` on React Native: there is no wrapper to repeat, and a modal stays inside the tree, so it renders its children. */
|
|
101
|
+
function ThemeScope({ children }) {
|
|
102
|
+
return createElement(Fragment, null, children);
|
|
103
|
+
}
|
|
104
|
+
/** `useThemeClass` on React Native: there are no classes. */
|
|
105
|
+
function useThemeClass() {}
|
|
106
|
+
/**
|
|
107
|
+
* Makes the React Native styling over a holder of the base theme; the theme context is made here unless
|
|
108
|
+
* given. Of `options`, `remBase` and `lengthKeys` are used: a theme is converted under `lengthKeys`.
|
|
109
|
+
*/
|
|
110
|
+
function bindHolder(holder, context, options = {}) {
|
|
111
|
+
const { remBase = 16, lengthKeys = DEFAULT_LENGTH_KEYS } = options;
|
|
112
|
+
const { ThemeContext, ThemeValueProvider, useTheme, useCondition } = createThemeValue(holder, (theme) => toNativeTheme(theme, lengthKeys, remBase), context);
|
|
113
|
+
function ThemeProvider({ theme, colorScheme, children }) {
|
|
114
|
+
return createElement(ThemeValueProvider, {
|
|
115
|
+
theme,
|
|
116
|
+
colorScheme
|
|
117
|
+
}, children);
|
|
118
|
+
}
|
|
119
|
+
function create(styles) {
|
|
120
|
+
if (typeof styles !== "function") {
|
|
121
|
+
let registered;
|
|
122
|
+
return createLazyRecord(Object.keys(styles), (key) => {
|
|
123
|
+
registered ??= register(styles, remBase);
|
|
124
|
+
return registered.entries[key];
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
const byTheme = /* @__PURE__ */ new WeakMap();
|
|
128
|
+
const prepare = (current) => {
|
|
129
|
+
let sheet = byTheme.get(current);
|
|
130
|
+
if (!sheet) {
|
|
131
|
+
sheet = register(styles(current), remBase);
|
|
132
|
+
byTheme.set(current, sheet);
|
|
133
|
+
}
|
|
134
|
+
return sheet;
|
|
135
|
+
};
|
|
136
|
+
return {
|
|
137
|
+
prepare,
|
|
138
|
+
resolve(current, condition) {
|
|
139
|
+
const sheet = prepare(current);
|
|
140
|
+
return resolveEntries(sheet, condition, readMediaKey(sheet.queries, false, remBase), remBase);
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
const useStyle = ((sheet) => {
|
|
145
|
+
const theme = useTheme();
|
|
146
|
+
const condition = useCondition();
|
|
147
|
+
const prepared = isThemedSheet(sheet) ? sheet.prepare(theme) : void 0;
|
|
148
|
+
const shouldWatchScheme = !!prepared?.hasScheme && condition.colorScheme === void 0;
|
|
149
|
+
const key = useMediaKey(prepared?.queries ?? NO_QUERIES, shouldWatchScheme, remBase);
|
|
150
|
+
return prepared === void 0 ? sheet : resolveEntries(prepared, condition, key, remBase);
|
|
151
|
+
});
|
|
152
|
+
return {
|
|
153
|
+
styled: createStyled(useTheme, useCondition, remBase),
|
|
154
|
+
StyleSheet: {
|
|
155
|
+
...platformStyleSheet,
|
|
156
|
+
create
|
|
157
|
+
},
|
|
158
|
+
useStyle,
|
|
159
|
+
apply: (...entries) => ({ style: entries.filter((entry) => !!entry) }),
|
|
160
|
+
css: ((...args) => mergeNativeStyles(args, remBase)),
|
|
161
|
+
ThemeProvider,
|
|
162
|
+
ThemeScope,
|
|
163
|
+
ThemeContext,
|
|
164
|
+
useTheme,
|
|
165
|
+
useThemeClass,
|
|
166
|
+
themeClass: () => "",
|
|
167
|
+
when: createNativeThemeKey,
|
|
168
|
+
whenScheme: createSchemeKey,
|
|
169
|
+
keyframes,
|
|
170
|
+
globalStyles,
|
|
171
|
+
Global
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Makes the styling of one theme shape for React Native, apart from the default one. The options are
|
|
176
|
+
* those of the web, so that one call runs on both platforms, with `remBase` added; of them only
|
|
177
|
+
* `remBase` and `lengthKeys` are used.
|
|
178
|
+
*/
|
|
179
|
+
function createStyle(theme, options = {}) {
|
|
180
|
+
return bindHolder({ baseTheme: theme }, void 0, options);
|
|
181
|
+
}
|
|
182
|
+
export { bindHolder, createStyle };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let _umber_style_core = require("@umber-style/core");
|
|
3
|
+
const require_createStyle = require("./createStyle.cjs");
|
|
4
|
+
const require_windowState = require("./windowState.cjs");
|
|
5
|
+
let react_native = require("react-native");
|
|
6
|
+
// src/defaultStyle.ts
|
|
7
|
+
/** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
|
|
8
|
+
function createDefaultStyle() {
|
|
9
|
+
const holder = { baseTheme: void 0 };
|
|
10
|
+
let bound;
|
|
11
|
+
let settings = {};
|
|
12
|
+
const ThemeContext = (0, react.createContext)(void 0);
|
|
13
|
+
const getStyle = () => {
|
|
14
|
+
bound ??= require_createStyle.bindHolder(holder, ThemeContext, settings);
|
|
15
|
+
return bound;
|
|
16
|
+
};
|
|
17
|
+
/** Gives the default style a base theme and its options (`remBase`, `lengthKeys`). Throws once the default style was used. */
|
|
18
|
+
function configure(options) {
|
|
19
|
+
if (bound) throw new Error("configure must be called before the default style is used.");
|
|
20
|
+
holder.baseTheme = options.theme;
|
|
21
|
+
settings = options;
|
|
22
|
+
}
|
|
23
|
+
const styled = ((tag, options) => getStyle().styled(tag, options));
|
|
24
|
+
const StyleSheet = {
|
|
25
|
+
...react_native.StyleSheet,
|
|
26
|
+
create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : (0, _umber_style_core.createDeferredRecord)(styles, () => getStyle().StyleSheet.create(styles)))
|
|
27
|
+
};
|
|
28
|
+
const useStyle = ((sheet) => getStyle().useStyle(sheet));
|
|
29
|
+
function useTheme() {
|
|
30
|
+
return getStyle().useTheme();
|
|
31
|
+
}
|
|
32
|
+
function ThemeProvider(props) {
|
|
33
|
+
return getStyle().ThemeProvider(props);
|
|
34
|
+
}
|
|
35
|
+
function Global(props) {
|
|
36
|
+
return getStyle().Global(props);
|
|
37
|
+
}
|
|
38
|
+
function ThemeScope(props) {
|
|
39
|
+
return getStyle().ThemeScope(props);
|
|
40
|
+
}
|
|
41
|
+
function useThemeClass() {
|
|
42
|
+
return getStyle().useThemeClass();
|
|
43
|
+
}
|
|
44
|
+
/** Whether the media query `query` matches the window; a `rem` or `em` length in it is the `remBase` of `configure`. */
|
|
45
|
+
function useMediaQuery(query, options = {}) {
|
|
46
|
+
return require_windowState.useMediaMatch(query, options, settings.remBase ?? 16);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
configure,
|
|
50
|
+
styled,
|
|
51
|
+
StyleSheet,
|
|
52
|
+
useStyle,
|
|
53
|
+
useTheme,
|
|
54
|
+
ThemeProvider,
|
|
55
|
+
ThemeScope,
|
|
56
|
+
useThemeClass,
|
|
57
|
+
useMediaQuery,
|
|
58
|
+
ThemeContext,
|
|
59
|
+
apply: ((...entries) => getStyle().apply(...entries)),
|
|
60
|
+
css: ((...args) => (0, _umber_style_core.createDeferredObject)(() => getStyle().css(...args))),
|
|
61
|
+
when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
|
|
62
|
+
whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
|
|
63
|
+
themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme)),
|
|
64
|
+
keyframes: _umber_style_core.keyframes,
|
|
65
|
+
globalStyles: _umber_style_core.globalStyles,
|
|
66
|
+
Global
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
exports.createDefaultStyle = createDefaultStyle;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { GlobalProps, NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, Styled, ThemeProviderProps, ThemeScopeProps } from "./types.cjs";
|
|
2
|
+
import { NativeSheetStyles, NativeStyleObject } from "./nativeStyle.cjs";
|
|
3
|
+
import { Theme } from "@umber-style/core";
|
|
4
|
+
import { NativeTheme } from "./nativeTheme.cjs";
|
|
5
|
+
import { MediaQueryOptions } from "./windowState.cjs";
|
|
6
|
+
/** The options of `configure()`: those of `createStyle` for the default style, and its base theme. */
|
|
7
|
+
export interface DefaultStyleOptions extends StyleOptions<Theme> {
|
|
8
|
+
/** The base theme, for an app that has no provider at all. */
|
|
9
|
+
theme?: Theme;
|
|
10
|
+
}
|
|
11
|
+
/** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
|
|
12
|
+
export declare function createDefaultStyle(): {
|
|
13
|
+
configure: (options: DefaultStyleOptions) => void;
|
|
14
|
+
styled: Styled<NativeTheme<Theme>>;
|
|
15
|
+
StyleSheet: NativeStyleSheet<NativeTheme<Theme>>;
|
|
16
|
+
useStyle: {
|
|
17
|
+
<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
|
|
18
|
+
<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
|
|
19
|
+
};
|
|
20
|
+
useTheme: () => NativeTheme<Theme>;
|
|
21
|
+
ThemeProvider: (props: ThemeProviderProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
22
|
+
ThemeScope: (props: ThemeScopeProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
23
|
+
useThemeClass: () => string | undefined;
|
|
24
|
+
useMediaQuery: (query: string, options?: MediaQueryOptions) => boolean;
|
|
25
|
+
ThemeContext: import("react").Context<NativeTheme<Theme> | undefined>;
|
|
26
|
+
apply: NativeStyle<Theme>['apply'];
|
|
27
|
+
css: NativeStyle<Theme>['css'];
|
|
28
|
+
when: NativeStyle<Theme>['when'];
|
|
29
|
+
whenScheme: NativeStyle<Theme>['whenScheme'];
|
|
30
|
+
themeClass: NativeStyle<Theme>['themeClass'];
|
|
31
|
+
keyframes: NativeStyle<Theme>['keyframes'];
|
|
32
|
+
globalStyles: NativeStyle<Theme>['globalStyles'];
|
|
33
|
+
Global: (props: GlobalProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | null;
|
|
34
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { GlobalProps, NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, Styled, ThemeProviderProps, ThemeScopeProps } from "./types.mjs";
|
|
2
|
+
import { NativeSheetStyles, NativeStyleObject } from "./nativeStyle.mjs";
|
|
3
|
+
import { Theme } from "@umber-style/core";
|
|
4
|
+
import { NativeTheme } from "./nativeTheme.mjs";
|
|
5
|
+
import { MediaQueryOptions } from "./windowState.mjs";
|
|
6
|
+
/** The options of `configure()`: those of `createStyle` for the default style, and its base theme. */
|
|
7
|
+
export interface DefaultStyleOptions extends StyleOptions<Theme> {
|
|
8
|
+
/** The base theme, for an app that has no provider at all. */
|
|
9
|
+
theme?: Theme;
|
|
10
|
+
}
|
|
11
|
+
/** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
|
|
12
|
+
export declare function createDefaultStyle(): {
|
|
13
|
+
configure: (options: DefaultStyleOptions) => void;
|
|
14
|
+
styled: Styled<NativeTheme<Theme>>;
|
|
15
|
+
StyleSheet: NativeStyleSheet<NativeTheme<Theme>>;
|
|
16
|
+
useStyle: {
|
|
17
|
+
<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
|
|
18
|
+
<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
|
|
19
|
+
};
|
|
20
|
+
useTheme: () => NativeTheme<Theme>;
|
|
21
|
+
ThemeProvider: (props: ThemeProviderProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
22
|
+
ThemeScope: (props: ThemeScopeProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
23
|
+
useThemeClass: () => string | undefined;
|
|
24
|
+
useMediaQuery: (query: string, options?: MediaQueryOptions) => boolean;
|
|
25
|
+
ThemeContext: import("react").Context<NativeTheme<Theme> | undefined>;
|
|
26
|
+
apply: NativeStyle<Theme>['apply'];
|
|
27
|
+
css: NativeStyle<Theme>['css'];
|
|
28
|
+
when: NativeStyle<Theme>['when'];
|
|
29
|
+
whenScheme: NativeStyle<Theme>['whenScheme'];
|
|
30
|
+
themeClass: NativeStyle<Theme>['themeClass'];
|
|
31
|
+
keyframes: NativeStyle<Theme>['keyframes'];
|
|
32
|
+
globalStyles: NativeStyle<Theme>['globalStyles'];
|
|
33
|
+
Global: (props: GlobalProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | null;
|
|
34
|
+
};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { createContext } from "react";
|
|
2
|
+
import { createDeferredObject, createDeferredRecord, globalStyles, keyframes } from "@umber-style/core";
|
|
3
|
+
import { bindHolder } from "./createStyle.mjs";
|
|
4
|
+
import { platformStyleSheet } from "./platform.mjs";
|
|
5
|
+
import { useMediaMatch } from "./windowState.mjs";
|
|
6
|
+
// src/defaultStyle.ts
|
|
7
|
+
/** Makes the default styling: one holder of the base theme, behind `styled`, `ThemeProvider` and the rest. */
|
|
8
|
+
function createDefaultStyle() {
|
|
9
|
+
const holder = { baseTheme: void 0 };
|
|
10
|
+
let bound;
|
|
11
|
+
let settings = {};
|
|
12
|
+
const ThemeContext = createContext(void 0);
|
|
13
|
+
const getStyle = () => {
|
|
14
|
+
bound ??= bindHolder(holder, ThemeContext, settings);
|
|
15
|
+
return bound;
|
|
16
|
+
};
|
|
17
|
+
/** Gives the default style a base theme and its options (`remBase`, `lengthKeys`). Throws once the default style was used. */
|
|
18
|
+
function configure(options) {
|
|
19
|
+
if (bound) throw new Error("configure must be called before the default style is used.");
|
|
20
|
+
holder.baseTheme = options.theme;
|
|
21
|
+
settings = options;
|
|
22
|
+
}
|
|
23
|
+
const styled = ((tag, options) => getStyle().styled(tag, options));
|
|
24
|
+
const StyleSheet = {
|
|
25
|
+
...platformStyleSheet,
|
|
26
|
+
create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : createDeferredRecord(styles, () => getStyle().StyleSheet.create(styles)))
|
|
27
|
+
};
|
|
28
|
+
const useStyle = ((sheet) => getStyle().useStyle(sheet));
|
|
29
|
+
function useTheme() {
|
|
30
|
+
return getStyle().useTheme();
|
|
31
|
+
}
|
|
32
|
+
function ThemeProvider(props) {
|
|
33
|
+
return getStyle().ThemeProvider(props);
|
|
34
|
+
}
|
|
35
|
+
function Global(props) {
|
|
36
|
+
return getStyle().Global(props);
|
|
37
|
+
}
|
|
38
|
+
function ThemeScope(props) {
|
|
39
|
+
return getStyle().ThemeScope(props);
|
|
40
|
+
}
|
|
41
|
+
function useThemeClass() {
|
|
42
|
+
return getStyle().useThemeClass();
|
|
43
|
+
}
|
|
44
|
+
/** Whether the media query `query` matches the window; a `rem` or `em` length in it is the `remBase` of `configure`. */
|
|
45
|
+
function useMediaQuery(query, options = {}) {
|
|
46
|
+
return useMediaMatch(query, options, settings.remBase ?? 16);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
configure,
|
|
50
|
+
styled,
|
|
51
|
+
StyleSheet,
|
|
52
|
+
useStyle,
|
|
53
|
+
useTheme,
|
|
54
|
+
ThemeProvider,
|
|
55
|
+
ThemeScope,
|
|
56
|
+
useThemeClass,
|
|
57
|
+
useMediaQuery,
|
|
58
|
+
ThemeContext,
|
|
59
|
+
apply: ((...entries) => getStyle().apply(...entries)),
|
|
60
|
+
css: ((...args) => createDeferredObject(() => getStyle().css(...args))),
|
|
61
|
+
when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
|
|
62
|
+
whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
|
|
63
|
+
themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme)),
|
|
64
|
+
keyframes,
|
|
65
|
+
globalStyles,
|
|
66
|
+
Global
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export { createDefaultStyle };
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
const require_defaultStyle = require("./defaultStyle.cjs");
|
|
3
|
+
let _umber_style_core = require("@umber-style/core");
|
|
4
|
+
const require_CacheProvider = require("./CacheProvider.cjs");
|
|
5
|
+
const require_alpha = require("./alpha.cjs");
|
|
6
|
+
const require_calc = require("./calc.cjs");
|
|
7
|
+
const require_createStyle = require("./createStyle.cjs");
|
|
8
|
+
const { configure, styled, StyleSheet, useStyle, useTheme, ThemeProvider, ThemeScope, useThemeClass, useMediaQuery, ThemeContext, apply, css, themeClass, when, whenScheme, keyframes, globalStyles, Global } = require_defaultStyle.createDefaultStyle();
|
|
9
|
+
exports.CacheProvider = require_CacheProvider.CacheProvider;
|
|
10
|
+
exports.Global = Global;
|
|
11
|
+
exports.StyleSheet = StyleSheet;
|
|
12
|
+
exports.ThemeContext = ThemeContext;
|
|
13
|
+
exports.ThemeProvider = ThemeProvider;
|
|
14
|
+
exports.ThemeScope = ThemeScope;
|
|
15
|
+
exports.add = require_calc.add;
|
|
16
|
+
exports.alpha = require_alpha.alpha;
|
|
17
|
+
exports.apply = apply;
|
|
18
|
+
Object.defineProperty(exports, "at", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function() {
|
|
21
|
+
return _umber_style_core.at;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
exports.configure = configure;
|
|
25
|
+
exports.createDefaultStyle = require_defaultStyle.createDefaultStyle;
|
|
26
|
+
exports.createStyle = require_createStyle.createStyle;
|
|
27
|
+
exports.css = css;
|
|
28
|
+
exports.divide = require_calc.divide;
|
|
29
|
+
exports.globalStyles = globalStyles;
|
|
30
|
+
exports.keyframes = keyframes;
|
|
31
|
+
exports.multiply = require_calc.multiply;
|
|
32
|
+
exports.negate = require_calc.negate;
|
|
33
|
+
exports.styled = styled;
|
|
34
|
+
exports.subtract = require_calc.subtract;
|
|
35
|
+
exports.themeClass = themeClass;
|
|
36
|
+
exports.useMediaQuery = useMediaQuery;
|
|
37
|
+
exports.useStyle = useStyle;
|
|
38
|
+
exports.useTheme = useTheme;
|
|
39
|
+
exports.useThemeClass = useThemeClass;
|
|
40
|
+
exports.when = when;
|
|
41
|
+
exports.whenScheme = whenScheme;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { at } from "@umber-style/core";
|
|
2
|
+
import { DefaultStyleOptions, createDefaultStyle } from "./defaultStyle.cjs";
|
|
3
|
+
import { NativeLength, NativeTheme } from "./nativeTheme.cjs";
|
|
4
|
+
import { CacheProviderProps, GlobalProps, NativeCssArgument, NativeCssFunction, NativeStyle, NativeStyleSheet, SheetDefinition, StyleOptions, StyleProp, Styled, StyledComponent, StyledOptions, ThemeProviderProps, ThemeScopeProps } from "./types.cjs";
|
|
5
|
+
import { NativeObjectSheet, NativeSheetInput, NativeSheetStyles, NativeStyleFunction, NativeStyleInput, NativeStyleObject, NativeStyleResult } from "./nativeStyle.cjs";
|
|
6
|
+
import { MediaQueryOptions } from "./windowState.cjs";
|
|
7
|
+
import { CacheProvider } from "./CacheProvider.cjs";
|
|
8
|
+
import { alpha } from "./alpha.cjs";
|
|
9
|
+
import { Operand, Percent, add, divide, multiply, negate, subtract } from "./calc.cjs";
|
|
10
|
+
import { createStyle } from "./createStyle.cjs";
|
|
11
|
+
export declare const configure: (options: DefaultStyleOptions) => void, styled: Styled<NativeTheme<import("@umber-style/core").Theme>>, StyleSheet: NativeStyleSheet<NativeTheme<import("@umber-style/core").Theme>>, useStyle: {
|
|
12
|
+
<K extends string>(sheet: { readonly [P in K]: NativeStyleObject; }): { readonly [P in K]: NativeStyleObject; };
|
|
13
|
+
<S extends NativeSheetStyles>(sheet: SheetDefinition<S, NativeStyleObject>): { [K in keyof S]: NativeStyleObject; };
|
|
14
|
+
}, useTheme: () => NativeTheme<import("@umber-style/core").Theme>, ThemeProvider: (props: ThemeProviderProps<import("@umber-style/core").Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>, ThemeScope: (props: ThemeScopeProps) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>, useThemeClass: () => string | undefined, useMediaQuery: (query: string, options?: MediaQueryOptions) => boolean, ThemeContext: import("react").Context<NativeTheme<import("@umber-style/core").Theme> | undefined>, apply: (...entries: (NativeStyleObject | null | undefined | false)[]) => {
|
|
15
|
+
style: NativeStyleObject[];
|
|
16
|
+
}, css: NativeCssFunction, themeClass: (theme: {
|
|
17
|
+
readonly _theme?: never;
|
|
18
|
+
}, colorScheme?: import("@umber-style/core").ColorScheme) => string, when: (theme: {
|
|
19
|
+
readonly _theme?: never;
|
|
20
|
+
}, colorScheme?: import("@umber-style/core").ColorScheme) => import("@umber-style/core").ThemeKey, whenScheme: (scheme: import("@umber-style/core").SchemeName) => import("@umber-style/core").SchemeKey, keyframes: (steps: import("@umber-style/core").KeyframesInput<import("@umber-style/core").Theme>, options?: import("@umber-style/core").KeyframesOptions) => string, globalStyles: (styles: import("@umber-style/core").GlobalStylesInput<import("@umber-style/core").Theme>) => import("@umber-style/core").GlobalStyles<import("@umber-style/core").Theme>, Global: (props: GlobalProps<import("@umber-style/core").Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | null;
|
|
21
|
+
export { CacheProvider, CacheProviderProps, DefaultStyleOptions, GlobalProps, type MediaQueryOptions, NativeCssArgument, NativeCssFunction, type NativeLength, NativeObjectSheet, NativeSheetInput, NativeSheetStyles, NativeStyle, NativeStyleFunction, NativeStyleInput, NativeStyleObject, NativeStyleResult, NativeStyleSheet, type NativeTheme, Operand, Percent, SheetDefinition, StyleOptions, StyleProp, Styled, StyledComponent, StyledOptions, ThemeProviderProps, ThemeScopeProps, add, alpha, at, createDefaultStyle, createStyle, divide, multiply, negate, subtract };
|