@umber-style/react 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.
- package/dist/CacheProvider.cjs +23 -0
- package/dist/CacheProvider.d.cts +10 -0
- package/dist/CacheProvider.d.mts +11 -0
- package/dist/CacheProvider.mjs +22 -0
- package/dist/Global.cjs +52 -0
- package/dist/Global.mjs +52 -0
- package/dist/Global.server.cjs +27 -0
- package/dist/Global.server.mjs +27 -0
- package/dist/GlobalRelease.cjs +32 -0
- package/dist/GlobalRelease.mjs +30 -0
- package/dist/ProviderScope.cjs +201 -0
- package/dist/ProviderScope.d.cts +14 -0
- package/dist/ProviderScope.d.mts +15 -0
- package/dist/ProviderScope.mjs +190 -0
- package/dist/ThemeProvider.cjs +94 -27
- package/dist/ThemeProvider.mjs +95 -28
- package/dist/ThemeProvider.server.cjs +130 -25
- package/dist/ThemeProvider.server.mjs +131 -26
- package/dist/ThemeValue.cjs +52 -20
- package/dist/ThemeValue.d.cts +3 -0
- package/dist/ThemeValue.d.mts +4 -0
- package/dist/ThemeValue.mjs +53 -23
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/createStyle.cjs +30 -20
- package/dist/createStyle.d.cts +4 -2
- package/dist/createStyle.d.mts +4 -2
- package/dist/createStyle.mjs +32 -22
- package/dist/createStyle.server.cjs +29 -22
- package/dist/createStyle.server.mjs +31 -24
- package/dist/defaultStyle.cjs +27 -3
- package/dist/defaultStyle.d.cts +18 -11
- package/dist/defaultStyle.d.mts +18 -11
- package/dist/defaultStyle.mjs +28 -4
- package/dist/defaultStyle.server.cjs +13 -5
- package/dist/defaultStyle.server.d.cts +16 -11
- package/dist/defaultStyle.server.d.mts +16 -11
- package/dist/defaultStyle.server.mjs +14 -6
- package/dist/domProps.cjs +27 -0
- package/dist/domProps.mjs +27 -0
- package/dist/index.cjs +12 -1
- package/dist/index.d.cts +17 -7
- package/dist/index.d.mts +17 -7
- package/dist/index.mjs +4 -2
- package/dist/index.server.cjs +6 -1
- package/dist/index.server.d.cts +15 -7
- package/dist/index.server.d.mts +15 -7
- package/dist/index.server.mjs +2 -2
- package/dist/plainTheme.cjs +49 -0
- package/dist/plainTheme.mjs +49 -0
- package/dist/serverContext.cjs +6 -24
- package/dist/serverContext.d.mts +0 -1
- package/dist/serverContext.mjs +6 -23
- package/dist/styleElements.cjs +75 -9
- package/dist/styleElements.d.cts +1 -0
- package/dist/styleElements.d.mts +2 -0
- package/dist/styleElements.mjs +73 -10
- package/dist/styled.cjs +32 -11
- package/dist/styled.mjs +33 -12
- package/dist/styled.server.cjs +6 -4
- package/dist/styled.server.mjs +6 -4
- package/dist/styledParts.cjs +36 -6
- package/dist/styledParts.mjs +37 -7
- package/dist/types.d.cts +191 -18
- package/dist/types.d.mts +191 -18
- package/dist/useMediaQuery.cjs +20 -0
- package/dist/useMediaQuery.d.cts +13 -0
- package/dist/useMediaQuery.d.mts +13 -0
- package/dist/useMediaQuery.mjs +20 -0
- package/package.json +3 -3
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { ROOT_SELECTOR, createThemeRule, createTokens, mergeTheme, toDeclarationText } from "@umber-style/core";
|
|
4
|
+
// src/ProviderScope.tsx
|
|
5
|
+
const NO_MARKS = /* @__PURE__ */ new Map();
|
|
6
|
+
let marksContext;
|
|
7
|
+
/** The context of the marks, made on first use: where this module is loaded with the server build of React, nothing renders it. */
|
|
8
|
+
function getMarksContext() {
|
|
9
|
+
marksContext ??= React.createContext(NO_MARKS);
|
|
10
|
+
return marksContext;
|
|
11
|
+
}
|
|
12
|
+
/** `marks`, with `mark` as the nearest provider of `key`. */
|
|
13
|
+
function addMark(marks, key, mark) {
|
|
14
|
+
return new Map(marks).set(key, mark);
|
|
15
|
+
}
|
|
16
|
+
/** Makes the contexts of one style. */
|
|
17
|
+
function createThemeContexts() {
|
|
18
|
+
return {
|
|
19
|
+
ThemeContext: React.createContext(void 0),
|
|
20
|
+
ViewContext: React.createContext(void 0)
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const prefixStyles = /* @__PURE__ */ new Map();
|
|
24
|
+
let claim;
|
|
25
|
+
/**
|
|
26
|
+
* The default style's claim on a prefix: its contexts exist before its engine and its prefix, which
|
|
27
|
+
* `readPrefix` gives on demand. The prefix is the one it will have when its options are set.
|
|
28
|
+
*/
|
|
29
|
+
function claimPrefixStyle(readPrefix, style) {
|
|
30
|
+
claim = {
|
|
31
|
+
readPrefix,
|
|
32
|
+
style
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The styles of `prefix`, recorded on first use: the default style's when it claims that prefix, else
|
|
37
|
+
* new contexts. Every style of one prefix (the default one, each `createStyle`) shares them, as they
|
|
38
|
+
* share its marks, and so does the browser part of a provider of the server build, which needs no engine.
|
|
39
|
+
*/
|
|
40
|
+
function readPrefixStyle(prefix) {
|
|
41
|
+
let style = prefixStyles.get(prefix);
|
|
42
|
+
if (style === void 0) {
|
|
43
|
+
style = claim?.readPrefix() === prefix ? claim.style : { contexts: createThemeContexts() };
|
|
44
|
+
prefixStyles.set(prefix, style);
|
|
45
|
+
}
|
|
46
|
+
return style;
|
|
47
|
+
}
|
|
48
|
+
/** The declarations of the variables of `values`, `--name:value;` for each: the text that the engine's `readThemeText` gives. */
|
|
49
|
+
function declareValues(prefix, values, tokens) {
|
|
50
|
+
const declared = createTokens(values, {
|
|
51
|
+
...tokens,
|
|
52
|
+
prefix
|
|
53
|
+
}).declare(values);
|
|
54
|
+
return toDeclarationText(Object.entries(declared));
|
|
55
|
+
}
|
|
56
|
+
/** The declarations of the given theme, and its color-scheme. */
|
|
57
|
+
function declareOwn(prefix, given, theme) {
|
|
58
|
+
const { colorScheme, tokens } = theme;
|
|
59
|
+
return declareValues(prefix, given, tokens) + (colorScheme ? `color-scheme:${colorScheme};` : "");
|
|
60
|
+
}
|
|
61
|
+
/** The style element of the root rule (the base theme, then the given one, on `:root`) and that rule alone; none when it declares nothing. */
|
|
62
|
+
function createRootStyle(prefix, given, theme) {
|
|
63
|
+
const { base, layerOrder, rootId, tokens, properties } = theme;
|
|
64
|
+
const body = (base === void 0 ? "" : declareValues(prefix, base.theme, tokens)) + declareOwn(prefix, given, theme);
|
|
65
|
+
if (body === "") return void 0;
|
|
66
|
+
const rule = createThemeRule(prefix, layerOrder !== "", ROOT_SELECTOR, body);
|
|
67
|
+
return {
|
|
68
|
+
style: {
|
|
69
|
+
href: rootId,
|
|
70
|
+
text: layerOrder + (base?.properties ?? "") + properties + rule
|
|
71
|
+
},
|
|
72
|
+
rule
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/** The style elements of a nested provider: of its theme class (none when it declares nothing), then of its other classes. */
|
|
76
|
+
function createNestedStyles(prefix, given, theme) {
|
|
77
|
+
const { classId, layerOrder, properties, styles } = theme;
|
|
78
|
+
const own = declareOwn(prefix, given, theme);
|
|
79
|
+
if (own === "") return styles;
|
|
80
|
+
const rule = createThemeRule(prefix, layerOrder !== "", `.${classId}`, own);
|
|
81
|
+
return [{
|
|
82
|
+
href: classId,
|
|
83
|
+
text: layerOrder + properties + rule
|
|
84
|
+
}, ...styles];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Leaves one root rule of `prefix` in the style elements that React keeps for it (`data-precedence`): the
|
|
88
|
+
* one in the element of `rootId`, written again there when an earlier pass deleted it, and none in any
|
|
89
|
+
* other element. React keeps a style element for the life of the document, so after the root theme
|
|
90
|
+
* changed (a refresh that renders another theme, a navigation back) the elements of the earlier ones are
|
|
91
|
+
* still there. Nothing changes until React has put the element of `rootId` in the document. The elements
|
|
92
|
+
* of the browser's cache and of a registry are not touched.
|
|
93
|
+
*/
|
|
94
|
+
function keepRootRule(prefix, rootId, rule, page) {
|
|
95
|
+
const elements = [...page.querySelectorAll(`style[data-precedence="${prefix}"]`)];
|
|
96
|
+
const own = elements.find((element) => element.dataset.href?.split(" ").includes(rootId));
|
|
97
|
+
if (own === void 0) return;
|
|
98
|
+
const pattern = new RegExp(String.raw`^(@layer ${prefix}-theme\s*\{\s*)?:where\(:root\)`);
|
|
99
|
+
for (const element of elements) {
|
|
100
|
+
const sheet = element.sheet;
|
|
101
|
+
let isKept = element !== own;
|
|
102
|
+
for (let index = sheet.cssRules.length - 1; index >= 0; index--) {
|
|
103
|
+
if (!pattern.test(sheet.cssRules[index].cssText)) continue;
|
|
104
|
+
if (isKept) sheet.deleteRule(index);
|
|
105
|
+
isKept = true;
|
|
106
|
+
}
|
|
107
|
+
if (!isKept) sheet.insertRule(rule, sheet.cssRules.length);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The browser part of a provider of the server build, which renders it with the theme it was given, as
|
|
112
|
+
* plain data, and the data of its rules. Whether a provider of the same engine is above is decided here,
|
|
113
|
+
* in the tree as it is rendered: a server render of a part of the page (a client-side navigation renders
|
|
114
|
+
* only the segment that changed) does not render the providers of the layouts above it, which are mounted
|
|
115
|
+
* in the browser. Rendered on the server as any client component is, it makes the same choice in the
|
|
116
|
+
* first HTML. The rules are made here from the values, with the core's tokens, so the server and the
|
|
117
|
+
* browser write the same text from the same props, with or without an engine of the client build.
|
|
118
|
+
*
|
|
119
|
+
* - Nested: it renders the style element of the theme class of its theme, those of its other classes, and
|
|
120
|
+
* its wrapper.
|
|
121
|
+
* - Outermost: no element; the style element of its root rule (the base theme, then its theme, on
|
|
122
|
+
* `:root`), and, once committed in the browser, it leaves that root rule alone in the document. A
|
|
123
|
+
* provider that has no base theme for that rule (see `ScopeTheme.base`) is nested instead.
|
|
124
|
+
* - With no rules (`isRoot`): no element, no rule.
|
|
125
|
+
*
|
|
126
|
+
* In each case it marks its prefix for the providers below, of both builds, and gives the contexts of
|
|
127
|
+
* its prefix the theme merged over the theme of the provider above it in the page, of either build, else
|
|
128
|
+
* over the base theme of the client build: `useTheme()` of a client component below reads it.
|
|
129
|
+
*/
|
|
130
|
+
function ProviderScope({ prefix, given, theme, children }) {
|
|
131
|
+
const MarksContext = getMarksContext();
|
|
132
|
+
const marks = React.use(MarksContext);
|
|
133
|
+
const above = marks.get(prefix);
|
|
134
|
+
const isNested = theme !== void 0 && (above !== void 0 || !theme.isWhole && theme.base === void 0);
|
|
135
|
+
const as = theme?.as;
|
|
136
|
+
const className = theme?.className;
|
|
137
|
+
const wrapper = React.useMemo(() => isNested ? {
|
|
138
|
+
as,
|
|
139
|
+
className
|
|
140
|
+
} : void 0, [
|
|
141
|
+
isNested,
|
|
142
|
+
as,
|
|
143
|
+
className
|
|
144
|
+
]);
|
|
145
|
+
const scope = theme === void 0 ? above?.scope ?? null : wrapper ?? null;
|
|
146
|
+
const below = React.useMemo(() => addMark(marks, prefix, { scope }), [
|
|
147
|
+
marks,
|
|
148
|
+
prefix,
|
|
149
|
+
scope
|
|
150
|
+
]);
|
|
151
|
+
const { contexts, holder } = readPrefixStyle(prefix);
|
|
152
|
+
const outer = React.use(contexts.ThemeContext) ?? holder?.baseTheme;
|
|
153
|
+
const merged = React.useMemo(() => mergeTheme(outer, given), [outer, given]);
|
|
154
|
+
const styles = React.useMemo(() => {
|
|
155
|
+
if (theme === void 0) return {
|
|
156
|
+
list: [],
|
|
157
|
+
rule: void 0
|
|
158
|
+
};
|
|
159
|
+
if (isNested) return {
|
|
160
|
+
list: createNestedStyles(prefix, given, theme),
|
|
161
|
+
rule: void 0
|
|
162
|
+
};
|
|
163
|
+
const root = createRootStyle(prefix, given, theme);
|
|
164
|
+
return {
|
|
165
|
+
list: root === void 0 ? [] : [root.style],
|
|
166
|
+
rule: root?.rule
|
|
167
|
+
};
|
|
168
|
+
}, [
|
|
169
|
+
prefix,
|
|
170
|
+
given,
|
|
171
|
+
theme,
|
|
172
|
+
isNested
|
|
173
|
+
]);
|
|
174
|
+
const rootRule = styles.rule;
|
|
175
|
+
const rootId = theme?.rootId;
|
|
176
|
+
React.useLayoutEffect(() => {
|
|
177
|
+
if (rootRule !== void 0) keepRootRule(prefix, rootId, rootRule, document);
|
|
178
|
+
}, [
|
|
179
|
+
prefix,
|
|
180
|
+
rootId,
|
|
181
|
+
rootRule
|
|
182
|
+
]);
|
|
183
|
+
const content = React.createElement(MarksContext.Provider, { value: below }, React.createElement(contexts.ThemeContext.Provider, { value: merged }, React.createElement(contexts.ViewContext.Provider, { value: void 0 }, children)));
|
|
184
|
+
return React.createElement(React.Fragment, null, styles.list.map(({ href, text }) => React.createElement("style", {
|
|
185
|
+
key: href,
|
|
186
|
+
href,
|
|
187
|
+
precedence: prefix
|
|
188
|
+
}, text)), wrapper === void 0 ? content : React.createElement(wrapper.as, { className: wrapper.className }, content));
|
|
189
|
+
}
|
|
190
|
+
export { ProviderScope, addMark, claimPrefixStyle, createNestedStyles, createRootStyle, createThemeContexts, declareValues, getMarksContext, keepRootRule, readPrefixStyle };
|
package/dist/ThemeProvider.cjs
CHANGED
|
@@ -1,37 +1,104 @@
|
|
|
1
|
+
"use client";
|
|
1
2
|
let react = require("react");
|
|
3
|
+
let _umber_style_core = require("@umber-style/core");
|
|
4
|
+
const require_CacheProvider = require("./CacheProvider.cjs");
|
|
5
|
+
const require_ProviderScope = require("./ProviderScope.cjs");
|
|
6
|
+
const require_ThemeValue = require("./ThemeValue.cjs");
|
|
2
7
|
let react_jsx_runtime = require("react/jsx-runtime");
|
|
3
8
|
// src/ThemeProvider.tsx
|
|
4
9
|
/**
|
|
5
|
-
* Makes the web `ThemeProvider
|
|
6
|
-
* the
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* Makes the web `ThemeProvider`. Its theme (or what a function of the theme above returns) is merged over
|
|
11
|
+
* the theme above and given to the contexts: the real values, and the view that function styles read.
|
|
12
|
+
*
|
|
13
|
+
* - The outermost provider (none above it) renders no element: its theme is declared on `:root`, written
|
|
14
|
+
* in an insertion effect in the browser (so a change of theme rewrites that rule and renders no styled
|
|
15
|
+
* component) and during the render where there is no document (a server render). It gives the engine
|
|
16
|
+
* its base theme when it has none.
|
|
17
|
+
* - A nested provider renders its wrapper: the theme class of the theme it was given (with the boundary
|
|
18
|
+
* and scheme markers that conditions use), the theme class of its merged theme (every variable, so that
|
|
19
|
+
* it is right where its element is not inside the wrapper of the provider above, in a portal), and the
|
|
20
|
+
* scope class, which has `display: contents`.
|
|
21
|
+
* - With `isRoot` it renders no element and makes no rule: the app puts `themeClass(theme)` on an ancestor
|
|
22
|
+
* of its own, usually `<html>`.
|
|
23
|
+
*
|
|
24
|
+
* On a server, where the build renders style elements, it sends those of its rules, and the stamp.
|
|
25
|
+
*
|
|
26
|
+
* Whether a provider is above is read from the marks that both builds write (`ProviderScope`), by the
|
|
27
|
+
* prefix of the engine's cache: a provider of the server build above makes this one nested too. Its theme
|
|
28
|
+
* is in the same context (the browser part of that provider puts it there, as plain data), so the theme
|
|
29
|
+
* above is the nearest provider's of either build.
|
|
30
|
+
*
|
|
31
|
+
* Under a `CacheProvider` it works with the engine over the cache that the provider gives (the same base
|
|
32
|
+
* theme, the prefix of that cache), sends no style element, and is the outermost one when no provider of
|
|
33
|
+
* that cache is above it: its theme is declared on `:root` in that cache, which is the root of the
|
|
34
|
+
* document of that cache (an iframe's).
|
|
13
35
|
*/
|
|
14
|
-
function createThemeProvider({
|
|
15
|
-
return function ThemeProvider({ theme, colorScheme, className, as
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
36
|
+
function createThemeProvider({ engine: baseEngine, contexts: { ThemeContext, ViewContext }, getServerStyle }) {
|
|
37
|
+
return function ThemeProvider({ theme, colorScheme, className, as = "div", isRoot, children }) {
|
|
38
|
+
const cache = require_CacheProvider.useProvidedCache();
|
|
39
|
+
const engine = cache === void 0 ? baseEngine : baseEngine.withCache(cache);
|
|
40
|
+
const MarksContext = require_ProviderScope.getMarksContext();
|
|
41
|
+
const marks = (0, react.use)(MarksContext);
|
|
42
|
+
const key = require_ThemeValue.readMarkKey(baseEngine, cache);
|
|
43
|
+
const above = marks.get(key);
|
|
44
|
+
const outer = (0, react.useContext)(ThemeContext) ?? engine.baseTheme;
|
|
45
|
+
const isOutermost = above === void 0 && !isRoot;
|
|
46
|
+
const isNested = above !== void 0 && !isRoot;
|
|
47
|
+
const given = (0, react.useMemo)(() => (0, _umber_style_core.resolveThemeInput)(theme, outer ?? _umber_style_core.EMPTY_THEME), [theme, outer]);
|
|
48
|
+
if (!engine.hasTheme) engine.setTheme(given);
|
|
49
|
+
const merged = (0, react.useMemo)(() => (0, _umber_style_core.mergeTheme)(outer, given), [outer, given]);
|
|
50
|
+
const wrapper = (0, react.useMemo)(() => {
|
|
51
|
+
if (!isNested) return void 0;
|
|
52
|
+
const own = engine.themeClass(given, colorScheme);
|
|
53
|
+
const full = engine.themeId(merged, colorScheme);
|
|
54
|
+
return {
|
|
55
|
+
as,
|
|
56
|
+
className: [
|
|
57
|
+
own,
|
|
58
|
+
own.startsWith(`${full} `) ? "" : full,
|
|
59
|
+
engine.scopeClass(),
|
|
60
|
+
className
|
|
61
|
+
].filter(Boolean).join(" ")
|
|
62
|
+
};
|
|
63
|
+
}, [
|
|
64
|
+
engine,
|
|
65
|
+
isNested,
|
|
66
|
+
given,
|
|
67
|
+
merged,
|
|
68
|
+
colorScheme,
|
|
69
|
+
className,
|
|
70
|
+
as
|
|
71
|
+
]);
|
|
72
|
+
const below = (0, react.useMemo)(() => require_ProviderScope.addMark(marks, key, { scope: wrapper ?? above?.scope ?? null }), [
|
|
73
|
+
marks,
|
|
74
|
+
key,
|
|
75
|
+
wrapper,
|
|
76
|
+
above
|
|
77
|
+
]);
|
|
78
|
+
(0, react.useInsertionEffect)(() => {
|
|
79
|
+
if (isOutermost) engine.declareRoot(merged, colorScheme);
|
|
80
|
+
}, [
|
|
81
|
+
engine,
|
|
82
|
+
isOutermost,
|
|
83
|
+
merged,
|
|
84
|
+
colorScheme
|
|
85
|
+
]);
|
|
86
|
+
const rootId = isOutermost && !(typeof document !== "undefined") ? engine.declareRoot(merged, colorScheme) : void 0;
|
|
87
|
+
const content = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MarksContext.Provider, {
|
|
88
|
+
value: below,
|
|
89
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ThemeContext.Provider, {
|
|
90
|
+
value: merged,
|
|
91
|
+
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ViewContext.Provider, {
|
|
92
|
+
value: engine.createThemeView(merged),
|
|
93
|
+
children
|
|
94
|
+
})
|
|
95
|
+
})
|
|
19
96
|
});
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
learnTheme(theme);
|
|
23
|
-
if (server === void 0) return child;
|
|
24
|
-
const made = server.createStyleElements(server.findThemeIds(theme).join(" "));
|
|
25
|
-
return (0, react.createElement)(react.Fragment, null, server.createStamp(), ...made, child);
|
|
26
|
-
}
|
|
27
|
-
const scope = [
|
|
28
|
-
themeClass(theme, colorScheme),
|
|
29
|
-
scopeClass(),
|
|
30
|
-
className
|
|
31
|
-
].filter(Boolean).join(" ");
|
|
32
|
-
const element = (0, react.createElement)(Element, { className: scope }, child);
|
|
97
|
+
const element = wrapper ? (0, react.createElement)(as, { className: wrapper.className }, content) : content;
|
|
98
|
+
const server = cache === void 0 ? getServerStyle() : void 0;
|
|
33
99
|
if (server === void 0) return element;
|
|
34
|
-
|
|
100
|
+
const styled = wrapper?.className ?? rootId ?? engine.findThemeIds(given).join(" ");
|
|
101
|
+
return (0, react.createElement)(react.Fragment, null, server.createStamp(), ...server.createStyleElements(styled), element);
|
|
35
102
|
};
|
|
36
103
|
}
|
|
37
104
|
exports.createThemeProvider = createThemeProvider;
|
package/dist/ThemeProvider.mjs
CHANGED
|
@@ -1,37 +1,104 @@
|
|
|
1
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
import { Fragment, createElement, use, useContext, useInsertionEffect, useMemo } from "react";
|
|
3
|
+
import { EMPTY_THEME, mergeTheme, resolveThemeInput } from "@umber-style/core";
|
|
4
|
+
import { useProvidedCache } from "./CacheProvider.mjs";
|
|
5
|
+
import { addMark, getMarksContext } from "./ProviderScope.mjs";
|
|
6
|
+
import { readMarkKey } from "./ThemeValue.mjs";
|
|
2
7
|
import { jsx } from "react/jsx-runtime";
|
|
3
8
|
// src/ThemeProvider.tsx
|
|
4
9
|
/**
|
|
5
|
-
* Makes the web `ThemeProvider
|
|
6
|
-
* the
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
*
|
|
10
|
+
* Makes the web `ThemeProvider`. Its theme (or what a function of the theme above returns) is merged over
|
|
11
|
+
* the theme above and given to the contexts: the real values, and the view that function styles read.
|
|
12
|
+
*
|
|
13
|
+
* - The outermost provider (none above it) renders no element: its theme is declared on `:root`, written
|
|
14
|
+
* in an insertion effect in the browser (so a change of theme rewrites that rule and renders no styled
|
|
15
|
+
* component) and during the render where there is no document (a server render). It gives the engine
|
|
16
|
+
* its base theme when it has none.
|
|
17
|
+
* - A nested provider renders its wrapper: the theme class of the theme it was given (with the boundary
|
|
18
|
+
* and scheme markers that conditions use), the theme class of its merged theme (every variable, so that
|
|
19
|
+
* it is right where its element is not inside the wrapper of the provider above, in a portal), and the
|
|
20
|
+
* scope class, which has `display: contents`.
|
|
21
|
+
* - With `isRoot` it renders no element and makes no rule: the app puts `themeClass(theme)` on an ancestor
|
|
22
|
+
* of its own, usually `<html>`.
|
|
23
|
+
*
|
|
24
|
+
* On a server, where the build renders style elements, it sends those of its rules, and the stamp.
|
|
25
|
+
*
|
|
26
|
+
* Whether a provider is above is read from the marks that both builds write (`ProviderScope`), by the
|
|
27
|
+
* prefix of the engine's cache: a provider of the server build above makes this one nested too. Its theme
|
|
28
|
+
* is in the same context (the browser part of that provider puts it there, as plain data), so the theme
|
|
29
|
+
* above is the nearest provider's of either build.
|
|
30
|
+
*
|
|
31
|
+
* Under a `CacheProvider` it works with the engine over the cache that the provider gives (the same base
|
|
32
|
+
* theme, the prefix of that cache), sends no style element, and is the outermost one when no provider of
|
|
33
|
+
* that cache is above it: its theme is declared on `:root` in that cache, which is the root of the
|
|
34
|
+
* document of that cache (an iframe's).
|
|
13
35
|
*/
|
|
14
|
-
function createThemeProvider({
|
|
15
|
-
return function ThemeProvider({ theme, colorScheme, className, as
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
|
|
36
|
+
function createThemeProvider({ engine: baseEngine, contexts: { ThemeContext, ViewContext }, getServerStyle }) {
|
|
37
|
+
return function ThemeProvider({ theme, colorScheme, className, as = "div", isRoot, children }) {
|
|
38
|
+
const cache = useProvidedCache();
|
|
39
|
+
const engine = cache === void 0 ? baseEngine : baseEngine.withCache(cache);
|
|
40
|
+
const MarksContext = getMarksContext();
|
|
41
|
+
const marks = use(MarksContext);
|
|
42
|
+
const key = readMarkKey(baseEngine, cache);
|
|
43
|
+
const above = marks.get(key);
|
|
44
|
+
const outer = useContext(ThemeContext) ?? engine.baseTheme;
|
|
45
|
+
const isOutermost = above === void 0 && !isRoot;
|
|
46
|
+
const isNested = above !== void 0 && !isRoot;
|
|
47
|
+
const given = useMemo(() => resolveThemeInput(theme, outer ?? EMPTY_THEME), [theme, outer]);
|
|
48
|
+
if (!engine.hasTheme) engine.setTheme(given);
|
|
49
|
+
const merged = useMemo(() => mergeTheme(outer, given), [outer, given]);
|
|
50
|
+
const wrapper = useMemo(() => {
|
|
51
|
+
if (!isNested) return void 0;
|
|
52
|
+
const own = engine.themeClass(given, colorScheme);
|
|
53
|
+
const full = engine.themeId(merged, colorScheme);
|
|
54
|
+
return {
|
|
55
|
+
as,
|
|
56
|
+
className: [
|
|
57
|
+
own,
|
|
58
|
+
own.startsWith(`${full} `) ? "" : full,
|
|
59
|
+
engine.scopeClass(),
|
|
60
|
+
className
|
|
61
|
+
].filter(Boolean).join(" ")
|
|
62
|
+
};
|
|
63
|
+
}, [
|
|
64
|
+
engine,
|
|
65
|
+
isNested,
|
|
66
|
+
given,
|
|
67
|
+
merged,
|
|
68
|
+
colorScheme,
|
|
69
|
+
className,
|
|
70
|
+
as
|
|
71
|
+
]);
|
|
72
|
+
const below = useMemo(() => addMark(marks, key, { scope: wrapper ?? above?.scope ?? null }), [
|
|
73
|
+
marks,
|
|
74
|
+
key,
|
|
75
|
+
wrapper,
|
|
76
|
+
above
|
|
77
|
+
]);
|
|
78
|
+
useInsertionEffect(() => {
|
|
79
|
+
if (isOutermost) engine.declareRoot(merged, colorScheme);
|
|
80
|
+
}, [
|
|
81
|
+
engine,
|
|
82
|
+
isOutermost,
|
|
83
|
+
merged,
|
|
84
|
+
colorScheme
|
|
85
|
+
]);
|
|
86
|
+
const rootId = isOutermost && !(typeof document !== "undefined") ? engine.declareRoot(merged, colorScheme) : void 0;
|
|
87
|
+
const content = /* @__PURE__ */ jsx(MarksContext.Provider, {
|
|
88
|
+
value: below,
|
|
89
|
+
children: /* @__PURE__ */ jsx(ThemeContext.Provider, {
|
|
90
|
+
value: merged,
|
|
91
|
+
children: /* @__PURE__ */ jsx(ViewContext.Provider, {
|
|
92
|
+
value: engine.createThemeView(merged),
|
|
93
|
+
children
|
|
94
|
+
})
|
|
95
|
+
})
|
|
19
96
|
});
|
|
20
|
-
const
|
|
21
|
-
|
|
22
|
-
learnTheme(theme);
|
|
23
|
-
if (server === void 0) return child;
|
|
24
|
-
const made = server.createStyleElements(server.findThemeIds(theme).join(" "));
|
|
25
|
-
return createElement(Fragment, null, server.createStamp(), ...made, child);
|
|
26
|
-
}
|
|
27
|
-
const scope = [
|
|
28
|
-
themeClass(theme, colorScheme),
|
|
29
|
-
scopeClass(),
|
|
30
|
-
className
|
|
31
|
-
].filter(Boolean).join(" ");
|
|
32
|
-
const element = createElement(Element, { className: scope }, child);
|
|
97
|
+
const element = wrapper ? createElement(as, { className: wrapper.className }, content) : content;
|
|
98
|
+
const server = cache === void 0 ? getServerStyle() : void 0;
|
|
33
99
|
if (server === void 0) return element;
|
|
34
|
-
|
|
100
|
+
const styled = wrapper?.className ?? rootId ?? engine.findThemeIds(given).join(" ");
|
|
101
|
+
return createElement(Fragment, null, server.createStamp(), ...server.createStyleElements(styled), element);
|
|
35
102
|
};
|
|
36
103
|
}
|
|
37
104
|
export { createThemeProvider };
|
|
@@ -1,35 +1,140 @@
|
|
|
1
1
|
let react = require("react");
|
|
2
|
+
let _umber_style_core = require("@umber-style/core");
|
|
3
|
+
const require_plainTheme = require("./plainTheme.cjs");
|
|
4
|
+
const require_ProviderScope = require("./ProviderScope.cjs");
|
|
2
5
|
const require_styleElements = require("./styleElements.cjs");
|
|
3
6
|
// src/ThemeProvider.server.ts
|
|
4
7
|
/**
|
|
5
|
-
* Makes the `ThemeProvider` for server components
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
*
|
|
8
|
+
* Makes the `ThemeProvider` for server components. A server component has no context, and a server
|
|
9
|
+
* render of a part of the page (a client-side navigation renders only the segment that changed) does not
|
|
10
|
+
* render the providers of the layouts above, so the server cannot tell whether a provider of the same
|
|
11
|
+
* engine is above this one, nor what theme it has. It decides nothing: it renders `ProviderScope`, a
|
|
12
|
+
* client component, with the theme it was given as plain data and what either outcome needs, and that
|
|
13
|
+
* decides in the tree as it is rendered, on the server for the first HTML and in the browser after a
|
|
14
|
+
* navigation. It puts the theme into the theme context of the client build too, merged over the theme
|
|
15
|
+
* of the provider above it in the page, so `useTheme()` of a client component below reads it.
|
|
16
|
+
*
|
|
17
|
+
* The values of the theme are sent once: the rules are made from them in `ProviderScope`. A member that
|
|
18
|
+
* cannot cross to a client component (a function, a symbol, a class instance) is left out, with one
|
|
19
|
+
* warning in development. The first provider of a request also sends the base theme, for the root rule,
|
|
20
|
+
* when the given theme does not declare all of it (one object for each base theme, which a payload holds
|
|
21
|
+
* once), and the stamp of the cache options (a `<meta>`).
|
|
22
|
+
*
|
|
23
|
+
* It never gives the engine a base theme: that comes from `createStyle(theme)` or `configure({ theme })`.
|
|
24
|
+
* A function theme is called with the base theme, in every render. With `isRoot` it makes no rule: it
|
|
25
|
+
* sends the style of the theme classes that the app already made for its theme with `themeClass`, the
|
|
26
|
+
* class it put on an ancestor, and renders `ProviderScope` with no rules.
|
|
12
27
|
*/
|
|
13
|
-
function createThemeProvider(engine,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
28
|
+
function createThemeProvider(engine, { readFirstSlot, readBaseTheme }) {
|
|
29
|
+
const prefix = engine.cache.prefix;
|
|
30
|
+
const { rawKeys, lengthKeys, modes } = engine.tokenOptions;
|
|
31
|
+
const tokens = {
|
|
32
|
+
...rawKeys && { rawKeys },
|
|
33
|
+
...lengthKeys && { lengthKeys },
|
|
34
|
+
...modes && { modes }
|
|
35
|
+
};
|
|
36
|
+
let sentBase;
|
|
37
|
+
let hasWarned = false;
|
|
38
|
+
/** The plain data of `given`, with one warning in development for the first theme that has members left out. */
|
|
39
|
+
const readPlain = (given) => {
|
|
40
|
+
const { theme, dropped } = require_plainTheme.toPlainTheme(given);
|
|
41
|
+
if (dropped.length > 0 && !hasWarned && (0, _umber_style_core.isDevelopment)()) {
|
|
42
|
+
hasWarned = true;
|
|
43
|
+
console.warn(`[${prefix}] A ThemeProvider of a server component was given a theme with members that cannot cross to a client component: ${dropped.join(", ")}. They are left out of the theme that client components read below it, where these members keep the values of the theme above.`);
|
|
44
|
+
}
|
|
45
|
+
return theme;
|
|
46
|
+
};
|
|
47
|
+
/** The base theme as sent, for `base`. */
|
|
48
|
+
const readBase = (base) => {
|
|
49
|
+
if (sentBase?.theme !== base) {
|
|
50
|
+
const { properties } = engine.readThemeText(base);
|
|
51
|
+
sentBase = {
|
|
52
|
+
theme: base,
|
|
53
|
+
sent: {
|
|
54
|
+
theme: require_plainTheme.toPlainTheme(base).theme,
|
|
55
|
+
properties
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
return sentBase.sent;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* What `ProviderScope` needs to render either outcome for the theme `given`. The base theme goes under a
|
|
63
|
+
* theme that does not declare all of it, for its root rule, only from the first provider of a request:
|
|
64
|
+
* one that renders after another one in the same request is below it or beside it, and is not the
|
|
65
|
+
* outermost one of the page.
|
|
66
|
+
*/
|
|
67
|
+
const describe = (given, { colorScheme, className, as = "div" }, isFirst) => {
|
|
68
|
+
const { cache, baseTheme } = engine;
|
|
69
|
+
const text = engine.readThemeText(given);
|
|
70
|
+
const classId = (0, _umber_style_core.createThemeId)(prefix, text.declarations, colorScheme);
|
|
71
|
+
const own = text.declarations + (colorScheme ? `color-scheme:${colorScheme};` : "");
|
|
72
|
+
const baseText = baseTheme && engine.readThemeText(baseTheme);
|
|
73
|
+
const names = new Set(text.names);
|
|
74
|
+
const isWhole = baseText === void 0 || baseText.names.every((name) => names.has(name));
|
|
75
|
+
const base = isWhole || !isFirst ? void 0 : readBase(baseTheme);
|
|
76
|
+
const others = [engine.scopeClass(), className].filter(Boolean).join(" ");
|
|
77
|
+
const rootBody = (base === void 0 ? "" : baseText.declarations) + own;
|
|
78
|
+
return {
|
|
79
|
+
as,
|
|
80
|
+
className: [(0, _umber_style_core.createThemeClassName)(prefix, classId, colorScheme), others].join(" "),
|
|
81
|
+
classId,
|
|
82
|
+
rootId: `${prefix}-root-${(0, _umber_style_core.hashString)(rootBody)}`,
|
|
83
|
+
layerOrder: cache.layerOrder,
|
|
84
|
+
...colorScheme && { colorScheme },
|
|
85
|
+
tokens,
|
|
86
|
+
properties: text.properties,
|
|
87
|
+
isWhole,
|
|
88
|
+
...base && { base },
|
|
89
|
+
styles: require_styleElements.collectStyleTexts(cache, others)
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
const described = /* @__PURE__ */ new WeakMap();
|
|
93
|
+
const readDescription = (given, props, isFirst) => {
|
|
94
|
+
const { colorScheme, className, as } = props;
|
|
95
|
+
const key = [
|
|
96
|
+
colorScheme,
|
|
97
|
+
className,
|
|
98
|
+
as,
|
|
99
|
+
isFirst
|
|
100
|
+
].join("|");
|
|
101
|
+
const known = described.get(given) ?? /* @__PURE__ */ new Map();
|
|
102
|
+
described.set(given, known);
|
|
103
|
+
let made = known.get(key);
|
|
104
|
+
if (made === void 0 || made.base !== engine.baseTheme) {
|
|
105
|
+
made = {
|
|
106
|
+
base: engine.baseTheme,
|
|
107
|
+
theme: describe(given, props, isFirst)
|
|
108
|
+
};
|
|
109
|
+
known.set(key, made);
|
|
110
|
+
}
|
|
111
|
+
return made.theme;
|
|
112
|
+
};
|
|
113
|
+
return function ThemeProvider(props) {
|
|
114
|
+
const { theme, isRoot, children } = props;
|
|
115
|
+
const given = (0, _umber_style_core.resolveThemeInput)(theme, readBaseTheme());
|
|
116
|
+
const slot = readFirstSlot();
|
|
117
|
+
const isFirst = !slot.isSet;
|
|
118
|
+
if (isFirst) {
|
|
119
|
+
slot.isSet = true;
|
|
120
|
+
slot.current = given;
|
|
121
|
+
}
|
|
122
|
+
const stamped = isFirst ? [require_styleElements.createStampElement(engine.cache)] : [];
|
|
123
|
+
const plain = readPlain(given);
|
|
19
124
|
if (isRoot) {
|
|
20
|
-
|
|
21
|
-
const
|
|
22
|
-
|
|
125
|
+
const made = require_styleElements.createStyleElements(engine.cache, engine.findThemeIds(given).join(" "));
|
|
126
|
+
const scope = (0, react.createElement)(require_ProviderScope.ProviderScope, {
|
|
127
|
+
prefix,
|
|
128
|
+
given: plain
|
|
129
|
+
}, children);
|
|
130
|
+
return (0, react.createElement)(react.Fragment, null, ...stamped, ...made, scope);
|
|
23
131
|
}
|
|
24
|
-
const scope =
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
className
|
|
31
|
-
].filter(Boolean).join(" ") };
|
|
32
|
-
return (0, react.createElement)(react.Fragment, null, ...stamped, ...styles, ...require_styleElements.createStyleElements(engine.cache, scoped), (0, react.createElement)(as, props, provided));
|
|
132
|
+
const scope = (0, react.createElement)(require_ProviderScope.ProviderScope, {
|
|
133
|
+
prefix,
|
|
134
|
+
given: plain,
|
|
135
|
+
theme: readDescription(given, props, isFirst)
|
|
136
|
+
}, children);
|
|
137
|
+
return (0, react.createElement)(react.Fragment, null, ...stamped, scope);
|
|
33
138
|
};
|
|
34
139
|
}
|
|
35
140
|
exports.createThemeProvider = createThemeProvider;
|