@umber-style/react 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 (74) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -1
  3. package/dist/CacheProvider.cjs +23 -0
  4. package/dist/CacheProvider.d.cts +10 -0
  5. package/dist/CacheProvider.d.mts +11 -0
  6. package/dist/CacheProvider.mjs +22 -0
  7. package/dist/Global.cjs +52 -0
  8. package/dist/Global.mjs +52 -0
  9. package/dist/Global.server.cjs +27 -0
  10. package/dist/Global.server.mjs +27 -0
  11. package/dist/GlobalRelease.cjs +32 -0
  12. package/dist/GlobalRelease.mjs +30 -0
  13. package/dist/ProviderScope.cjs +201 -0
  14. package/dist/ProviderScope.d.cts +14 -0
  15. package/dist/ProviderScope.d.mts +15 -0
  16. package/dist/ProviderScope.mjs +190 -0
  17. package/dist/ThemeProvider.cjs +104 -0
  18. package/dist/ThemeProvider.mjs +104 -0
  19. package/dist/ThemeProvider.server.cjs +140 -0
  20. package/dist/ThemeProvider.server.mjs +140 -0
  21. package/dist/ThemeValue.cjs +64 -0
  22. package/dist/ThemeValue.d.cts +3 -0
  23. package/dist/ThemeValue.d.mts +4 -0
  24. package/dist/ThemeValue.mjs +62 -0
  25. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  26. package/dist/createStyle.cjs +66 -0
  27. package/dist/createStyle.d.cts +9 -0
  28. package/dist/createStyle.d.mts +9 -0
  29. package/dist/createStyle.mjs +65 -0
  30. package/dist/createStyle.server.cjs +60 -0
  31. package/dist/createStyle.server.d.cts +8 -0
  32. package/dist/createStyle.server.d.mts +8 -0
  33. package/dist/createStyle.server.mjs +59 -0
  34. package/dist/defaultStyle.cjs +68 -0
  35. package/dist/defaultStyle.d.cts +29 -0
  36. package/dist/defaultStyle.d.mts +29 -0
  37. package/dist/defaultStyle.mjs +68 -0
  38. package/dist/defaultStyle.server.cjs +52 -0
  39. package/dist/defaultStyle.server.d.cts +27 -0
  40. package/dist/defaultStyle.server.d.mts +27 -0
  41. package/dist/defaultStyle.server.mjs +52 -0
  42. package/dist/domProps.cjs +27 -0
  43. package/dist/domProps.mjs +27 -0
  44. package/dist/index.cjs +72 -0
  45. package/dist/index.d.cts +28 -0
  46. package/dist/index.d.mts +28 -0
  47. package/dist/index.mjs +7 -0
  48. package/dist/index.server.cjs +66 -0
  49. package/dist/index.server.d.cts +26 -0
  50. package/dist/index.server.d.mts +26 -0
  51. package/dist/index.server.mjs +5 -0
  52. package/dist/plainTheme.cjs +49 -0
  53. package/dist/plainTheme.mjs +49 -0
  54. package/dist/serverContext.cjs +14 -0
  55. package/dist/serverContext.d.cts +2 -0
  56. package/dist/serverContext.d.mts +2 -0
  57. package/dist/serverContext.mjs +14 -0
  58. package/dist/styleElements.cjs +99 -0
  59. package/dist/styleElements.d.cts +1 -0
  60. package/dist/styleElements.d.mts +2 -0
  61. package/dist/styleElements.mjs +95 -0
  62. package/dist/styled.cjs +51 -0
  63. package/dist/styled.mjs +51 -0
  64. package/dist/styled.server.cjs +24 -0
  65. package/dist/styled.server.mjs +24 -0
  66. package/dist/styledParts.cjs +67 -0
  67. package/dist/styledParts.mjs +67 -0
  68. package/dist/types.d.cts +274 -0
  69. package/dist/types.d.mts +274 -0
  70. package/dist/useMediaQuery.cjs +20 -0
  71. package/dist/useMediaQuery.d.cts +13 -0
  72. package/dist/useMediaQuery.d.mts +13 -0
  73. package/dist/useMediaQuery.mjs +20 -0
  74. package/package.json +88 -5
@@ -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 };
@@ -0,0 +1,104 @@
1
+ "use client";
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");
7
+ let react_jsx_runtime = require("react/jsx-runtime");
8
+ // src/ThemeProvider.tsx
9
+ /**
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).
35
+ */
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
+ })
96
+ });
97
+ const element = wrapper ? (0, react.createElement)(as, { className: wrapper.className }, content) : content;
98
+ const server = cache === void 0 ? getServerStyle() : void 0;
99
+ if (server === void 0) return element;
100
+ const styled = wrapper?.className ?? rootId ?? engine.findThemeIds(given).join(" ");
101
+ return (0, react.createElement)(react.Fragment, null, server.createStamp(), ...server.createStyleElements(styled), element);
102
+ };
103
+ }
104
+ exports.createThemeProvider = createThemeProvider;
@@ -0,0 +1,104 @@
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";
7
+ import { jsx } from "react/jsx-runtime";
8
+ // src/ThemeProvider.tsx
9
+ /**
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).
35
+ */
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
+ })
96
+ });
97
+ const element = wrapper ? createElement(as, { className: wrapper.className }, content) : content;
98
+ const server = cache === void 0 ? getServerStyle() : void 0;
99
+ if (server === void 0) return element;
100
+ const styled = wrapper?.className ?? rootId ?? engine.findThemeIds(given).join(" ");
101
+ return createElement(Fragment, null, server.createStamp(), ...server.createStyleElements(styled), element);
102
+ };
103
+ }
104
+ export { createThemeProvider };
@@ -0,0 +1,140 @@
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");
5
+ const require_styleElements = require("./styleElements.cjs");
6
+ // src/ThemeProvider.server.ts
7
+ /**
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.
27
+ */
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);
124
+ if (isRoot) {
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);
131
+ }
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);
138
+ };
139
+ }
140
+ exports.createThemeProvider = createThemeProvider;
@@ -0,0 +1,140 @@
1
+ import { Fragment, createElement } from "react";
2
+ import { createThemeClassName, createThemeId, hashString, isDevelopment, resolveThemeInput } from "@umber-style/core";
3
+ import { toPlainTheme } from "./plainTheme.mjs";
4
+ import { ProviderScope } from "./ProviderScope.mjs";
5
+ import { collectStyleTexts, createStampElement, createStyleElements } from "./styleElements.mjs";
6
+ // src/ThemeProvider.server.ts
7
+ /**
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.
27
+ */
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 } = toPlainTheme(given);
41
+ if (dropped.length > 0 && !hasWarned && 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: 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 = 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: [createThemeClassName(prefix, classId, colorScheme), others].join(" "),
81
+ classId,
82
+ rootId: `${prefix}-root-${hashString(rootBody)}`,
83
+ layerOrder: cache.layerOrder,
84
+ ...colorScheme && { colorScheme },
85
+ tokens,
86
+ properties: text.properties,
87
+ isWhole,
88
+ ...base && { base },
89
+ styles: 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 = 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 ? [createStampElement(engine.cache)] : [];
123
+ const plain = readPlain(given);
124
+ if (isRoot) {
125
+ const made = createStyleElements(engine.cache, engine.findThemeIds(given).join(" "));
126
+ const scope = createElement(ProviderScope, {
127
+ prefix,
128
+ given: plain
129
+ }, children);
130
+ return createElement(Fragment, null, ...stamped, ...made, scope);
131
+ }
132
+ const scope = createElement(ProviderScope, {
133
+ prefix,
134
+ given: plain,
135
+ theme: readDescription(given, props, isFirst)
136
+ }, children);
137
+ return createElement(Fragment, null, ...stamped, scope);
138
+ };
139
+ }
140
+ export { createThemeProvider };