@umber-style/react 0.1.0-alpha.0 → 0.1.0-alpha.2

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 (69) hide show
  1. package/dist/CacheProvider.cjs +23 -0
  2. package/dist/CacheProvider.d.cts +10 -0
  3. package/dist/CacheProvider.d.mts +11 -0
  4. package/dist/CacheProvider.mjs +22 -0
  5. package/dist/Global.cjs +52 -0
  6. package/dist/Global.mjs +52 -0
  7. package/dist/Global.server.cjs +27 -0
  8. package/dist/Global.server.mjs +27 -0
  9. package/dist/GlobalRelease.cjs +32 -0
  10. package/dist/GlobalRelease.mjs +30 -0
  11. package/dist/ProviderScope.cjs +201 -0
  12. package/dist/ProviderScope.d.cts +14 -0
  13. package/dist/ProviderScope.d.mts +15 -0
  14. package/dist/ProviderScope.mjs +190 -0
  15. package/dist/ThemeProvider.cjs +94 -27
  16. package/dist/ThemeProvider.mjs +95 -28
  17. package/dist/ThemeProvider.server.cjs +130 -25
  18. package/dist/ThemeProvider.server.mjs +131 -26
  19. package/dist/ThemeValue.cjs +52 -20
  20. package/dist/ThemeValue.d.cts +3 -0
  21. package/dist/ThemeValue.d.mts +4 -0
  22. package/dist/ThemeValue.mjs +53 -23
  23. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  24. package/dist/createStyle.cjs +30 -20
  25. package/dist/createStyle.d.cts +4 -2
  26. package/dist/createStyle.d.mts +4 -2
  27. package/dist/createStyle.mjs +32 -22
  28. package/dist/createStyle.server.cjs +29 -22
  29. package/dist/createStyle.server.mjs +31 -24
  30. package/dist/defaultStyle.cjs +27 -3
  31. package/dist/defaultStyle.d.cts +18 -11
  32. package/dist/defaultStyle.d.mts +18 -11
  33. package/dist/defaultStyle.mjs +28 -4
  34. package/dist/defaultStyle.server.cjs +13 -5
  35. package/dist/defaultStyle.server.d.cts +16 -11
  36. package/dist/defaultStyle.server.d.mts +16 -11
  37. package/dist/defaultStyle.server.mjs +14 -6
  38. package/dist/domProps.cjs +27 -0
  39. package/dist/domProps.mjs +27 -0
  40. package/dist/index.cjs +12 -1
  41. package/dist/index.d.cts +17 -7
  42. package/dist/index.d.mts +17 -7
  43. package/dist/index.mjs +4 -2
  44. package/dist/index.server.cjs +6 -1
  45. package/dist/index.server.d.cts +15 -7
  46. package/dist/index.server.d.mts +15 -7
  47. package/dist/index.server.mjs +2 -2
  48. package/dist/plainTheme.cjs +49 -0
  49. package/dist/plainTheme.mjs +49 -0
  50. package/dist/serverContext.cjs +6 -24
  51. package/dist/serverContext.d.mts +0 -1
  52. package/dist/serverContext.mjs +6 -23
  53. package/dist/styleElements.cjs +75 -9
  54. package/dist/styleElements.d.cts +1 -0
  55. package/dist/styleElements.d.mts +2 -0
  56. package/dist/styleElements.mjs +73 -10
  57. package/dist/styled.cjs +32 -11
  58. package/dist/styled.mjs +33 -12
  59. package/dist/styled.server.cjs +6 -4
  60. package/dist/styled.server.mjs +6 -4
  61. package/dist/styledParts.cjs +36 -6
  62. package/dist/styledParts.mjs +37 -7
  63. package/dist/types.d.cts +225 -19
  64. package/dist/types.d.mts +225 -19
  65. package/dist/useMediaQuery.cjs +20 -0
  66. package/dist/useMediaQuery.d.cts +13 -0
  67. package/dist/useMediaQuery.d.mts +13 -0
  68. package/dist/useMediaQuery.mjs +20 -0
  69. package/package.json +3 -3
@@ -1,35 +1,140 @@
1
1
  import { Fragment, createElement } from "react";
2
- import { createStampElement, createStyleElements } from "./styleElements.mjs";
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";
3
6
  // src/ThemeProvider.server.ts
4
7
  /**
5
- * Makes the `ThemeProvider` for server components: an element with the theme class and its `<style>`.
6
- * It does not give the theme to client components: a theme holds functions, which cannot cross to
7
- * them. A client component that calls `useTheme()` needs a `ThemeProvider` imported in a client file.
8
- * The first provider of a request also gives the root theme that function styles read. A root provider
9
- * (`isRoot`) renders no element and makes no rule: it sends the style of the theme classes that the app
10
- * already made for its theme with `themeClass`, which is the class it put on an ancestor. The first
11
- * provider of a request also renders the stamp of the cache options (a `<meta>`), once.
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, root, stamp) {
14
- return function ThemeProvider({ theme, colorScheme, className, as = "div", isRoot, children }) {
15
- const provided = createElement(root.Provider, { value: theme }, children);
16
- const slot = stamp.slot();
17
- const stamped = slot.isSet ? [] : [createStampElement(engine.cache)];
18
- slot.isSet = true;
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);
19
124
  if (isRoot) {
20
- if (!engine.hasTheme) engine.setTheme(theme);
21
- const made = createStyleElements(engine.cache, engine.findThemeIds(theme).join(" "));
22
- return createElement(Fragment, null, ...stamped, ...made, provided);
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);
23
131
  }
24
- const scope = engine.themeClass(theme, colorScheme);
25
- const styles = createStyleElements(engine.cache, engine.themeId(theme, colorScheme));
26
- const scoped = engine.scopeClass();
27
- const props = { className: [
28
- scope,
29
- scoped,
30
- className
31
- ].filter(Boolean).join(" ") };
32
- return createElement(Fragment, null, ...stamped, ...styles, ...createStyleElements(engine.cache, scoped), createElement(as, props, provided));
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);
33
138
  };
34
139
  }
35
140
  export { createThemeProvider };
@@ -1,32 +1,64 @@
1
1
  "use client";
2
2
  let react = require("react");
3
3
  let _umber_style_core = require("@umber-style/core");
4
- let react_jsx_runtime = require("react/jsx-runtime");
4
+ const require_CacheProvider = require("./CacheProvider.cjs");
5
+ const require_ProviderScope = require("./ProviderScope.cjs");
5
6
  // src/ThemeValue.tsx
6
- /** The one React context of the engine: it holds the real theme values, and the base theme of `holder` is its default. */
7
- function createThemeValue(holder) {
8
- const ThemeContext = (0, react.createContext)(void 0);
9
- function ThemeValueProvider({ theme, children }) {
10
- const outer = (0, react.useContext)(ThemeContext) ?? holder.baseTheme;
11
- const value = (0, react.useMemo)(() => outer === void 0 ? theme : (0, _umber_style_core.mergeDeep)(outer, theme), [outer, theme]);
12
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ThemeContext.Provider, {
13
- value,
14
- children
15
- });
7
+ /**
8
+ * The key of the marks for `engine` under the cache that a `CacheProvider` gives (`cache`): the prefix of
9
+ * the engine's cache, which a provider of the server build marks too, or the given cache when it is
10
+ * another one.
11
+ */
12
+ function readMarkKey(engine, cache) {
13
+ return cache === void 0 || cache === engine.cache ? engine.cache.prefix : cache;
14
+ }
15
+ /** The hooks of one style, and `ThemeScope`, over its contexts. */
16
+ function createThemeValue(holder, { ThemeContext, ViewContext }) {
17
+ /** The theme where no provider is above: the base theme, or the empty theme. */
18
+ const readOwnTheme = () => holder.baseTheme ?? _umber_style_core.EMPTY_THEME;
19
+ /** The real theme of the nearest provider, the base theme, or an empty object. */
20
+ function useTheme() {
21
+ return (0, react.useContext)(ThemeContext) ?? readOwnTheme();
16
22
  }
17
- /** The theme above, or the base theme, or undefined when there is none. */
18
- function useThemeValue() {
19
- return (0, react.useContext)(ThemeContext) ?? holder.baseTheme;
23
+ /**
24
+ * The view of the theme that function styles read: of the nearest provider, else of the base theme (or
25
+ * of the empty theme). It is the view that a provider of the client build gives; below a provider of the
26
+ * server build, which gives none, and under a `CacheProvider` (`cache`, which `useProvidedCache` read),
27
+ * it is made from the real theme of the nearest provider, by the engine over that cache: its references
28
+ * name the variables of that cache's prefix. The theme context is read only then, so that a component
29
+ * that reads a view given renders again only when the view changes.
30
+ */
31
+ function useThemeView(cache) {
32
+ const view = (0, react.useContext)(ViewContext);
33
+ const local = cache === void 0 ? holder : holder.withCache(cache);
34
+ if (local === holder && view !== void 0) return view;
35
+ return local.createThemeView((0, react.use)(ThemeContext) ?? readOwnTheme());
20
36
  }
21
- function useTheme() {
22
- const value = useThemeValue();
23
- if (value === void 0) throw new Error(_umber_style_core.NO_THEME_MESSAGE);
24
- return value;
37
+ /** The wrapper of the nearest nested provider of either build: null under the outermost one, undefined with none. */
38
+ function useScope() {
39
+ return (0, react.use)(require_ProviderScope.getMarksContext()).get(readMarkKey(holder, require_CacheProvider.useProvidedCache()))?.scope;
40
+ }
41
+ /** The class string of the wrapper of the nearest nested provider; undefined under the outermost one or none. */
42
+ function useThemeClass() {
43
+ return useScope()?.className;
44
+ }
45
+ /**
46
+ * Renders the wrapper of the nearest nested provider again around its children (the same element and
47
+ * classes), for content that a portal puts somewhere else in the document; under the outermost provider,
48
+ * or none, its children alone.
49
+ */
50
+ function ThemeScope({ children }) {
51
+ const scope = useScope();
52
+ if (!scope) return (0, react.createElement)(react.Fragment, null, children);
53
+ return (0, react.createElement)(scope.as, { className: scope.className }, children);
25
54
  }
26
55
  return {
27
- ThemeValueProvider,
28
56
  useTheme,
29
- useThemeValue
57
+ useThemeView,
58
+ useThemeClass,
59
+ ThemeScope
30
60
  };
31
61
  }
62
+ exports.createThemeContexts = require_ProviderScope.createThemeContexts;
32
63
  exports.createThemeValue = createThemeValue;
64
+ exports.readMarkKey = readMarkKey;
@@ -0,0 +1,3 @@
1
+ import { ThemeContexts } from "./ProviderScope.cjs";
2
+ import "./types.cjs";
3
+ export type { ThemeContexts };
@@ -0,0 +1,4 @@
1
+ import "@umber-style/core";
2
+ import { ThemeContexts } from "./ProviderScope.mjs";
3
+ import "./types.mjs";
4
+ export type { ThemeContexts };
@@ -1,32 +1,62 @@
1
1
  "use client";
2
- import { createContext, useContext, useMemo } from "react";
3
- import { NO_THEME_MESSAGE, mergeDeep } from "@umber-style/core";
4
- import { jsx } from "react/jsx-runtime";
2
+ import { Fragment, createElement, use, useContext } from "react";
3
+ import { EMPTY_THEME } from "@umber-style/core";
4
+ import { useProvidedCache } from "./CacheProvider.mjs";
5
+ import { createThemeContexts, getMarksContext } from "./ProviderScope.mjs";
5
6
  // src/ThemeValue.tsx
6
- /** The one React context of the engine: it holds the real theme values, and the base theme of `holder` is its default. */
7
- function createThemeValue(holder) {
8
- const ThemeContext = createContext(void 0);
9
- function ThemeValueProvider({ theme, children }) {
10
- const outer = useContext(ThemeContext) ?? holder.baseTheme;
11
- const value = useMemo(() => outer === void 0 ? theme : mergeDeep(outer, theme), [outer, theme]);
12
- return /* @__PURE__ */ jsx(ThemeContext.Provider, {
13
- value,
14
- children
15
- });
7
+ /**
8
+ * The key of the marks for `engine` under the cache that a `CacheProvider` gives (`cache`): the prefix of
9
+ * the engine's cache, which a provider of the server build marks too, or the given cache when it is
10
+ * another one.
11
+ */
12
+ function readMarkKey(engine, cache) {
13
+ return cache === void 0 || cache === engine.cache ? engine.cache.prefix : cache;
14
+ }
15
+ /** The hooks of one style, and `ThemeScope`, over its contexts. */
16
+ function createThemeValue(holder, { ThemeContext, ViewContext }) {
17
+ /** The theme where no provider is above: the base theme, or the empty theme. */
18
+ const readOwnTheme = () => holder.baseTheme ?? EMPTY_THEME;
19
+ /** The real theme of the nearest provider, the base theme, or an empty object. */
20
+ function useTheme() {
21
+ return useContext(ThemeContext) ?? readOwnTheme();
16
22
  }
17
- /** The theme above, or the base theme, or undefined when there is none. */
18
- function useThemeValue() {
19
- return useContext(ThemeContext) ?? holder.baseTheme;
23
+ /**
24
+ * The view of the theme that function styles read: of the nearest provider, else of the base theme (or
25
+ * of the empty theme). It is the view that a provider of the client build gives; below a provider of the
26
+ * server build, which gives none, and under a `CacheProvider` (`cache`, which `useProvidedCache` read),
27
+ * it is made from the real theme of the nearest provider, by the engine over that cache: its references
28
+ * name the variables of that cache's prefix. The theme context is read only then, so that a component
29
+ * that reads a view given renders again only when the view changes.
30
+ */
31
+ function useThemeView(cache) {
32
+ const view = useContext(ViewContext);
33
+ const local = cache === void 0 ? holder : holder.withCache(cache);
34
+ if (local === holder && view !== void 0) return view;
35
+ return local.createThemeView(use(ThemeContext) ?? readOwnTheme());
20
36
  }
21
- function useTheme() {
22
- const value = useThemeValue();
23
- if (value === void 0) throw new Error(NO_THEME_MESSAGE);
24
- return value;
37
+ /** The wrapper of the nearest nested provider of either build: null under the outermost one, undefined with none. */
38
+ function useScope() {
39
+ return use(getMarksContext()).get(readMarkKey(holder, useProvidedCache()))?.scope;
40
+ }
41
+ /** The class string of the wrapper of the nearest nested provider; undefined under the outermost one or none. */
42
+ function useThemeClass() {
43
+ return useScope()?.className;
44
+ }
45
+ /**
46
+ * Renders the wrapper of the nearest nested provider again around its children (the same element and
47
+ * classes), for content that a portal puts somewhere else in the document; under the outermost provider,
48
+ * or none, its children alone.
49
+ */
50
+ function ThemeScope({ children }) {
51
+ const scope = useScope();
52
+ if (!scope) return createElement(Fragment, null, children);
53
+ return createElement(scope.as, { className: scope.className }, children);
25
54
  }
26
55
  return {
27
- ThemeValueProvider,
28
56
  useTheme,
29
- useThemeValue
57
+ useThemeView,
58
+ useThemeClass,
59
+ ThemeScope
30
60
  };
31
61
  }
32
- export { createThemeValue };
62
+ export { createThemeContexts, createThemeValue, readMarkKey };
@@ -0,0 +1,22 @@
1
+ // \0rolldown/runtime.js
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule || !__hasOwnProp.call(mod, "default") ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+ exports.__toESM = __toESM;
@@ -1,20 +1,29 @@
1
1
  let _umber_style_core = require("@umber-style/core");
2
+ const require_CacheProvider = require("./CacheProvider.cjs");
3
+ const require_Global = require("./Global.cjs");
4
+ const require_ProviderScope = require("./ProviderScope.cjs");
2
5
  const require_styleElements = require("./styleElements.cjs");
3
6
  const require_styled = require("./styled.cjs");
4
7
  const require_ThemeProvider = require("./ThemeProvider.cjs");
5
8
  const require_ThemeValue = require("./ThemeValue.cjs");
6
9
  // src/createStyle.ts
7
- /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
8
- function bindEngine(engine, { hasServerStyleElements = true } = {}) {
9
- const { ThemeValueProvider, useTheme, useThemeValue } = require_ThemeValue.createThemeValue(engine);
10
+ /** The contexts of the prefix of `engine`, which every style of that prefix shares; this engine's base theme is the one a provider of the server build merges over, unless another style of the prefix gave one. */
11
+ function readSharedContexts(engine) {
12
+ const shared = require_ProviderScope.readPrefixStyle(engine.cache.prefix);
13
+ shared.holder ??= engine;
14
+ return shared.contexts;
15
+ }
16
+ /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`; `contexts` are those of its prefix unless given. */
17
+ function bindEngine(engine, { hasServerStyleElements = true } = {}, contexts = readSharedContexts(engine)) {
18
+ const { useTheme, useThemeView, useThemeClass, ThemeScope } = require_ThemeValue.createThemeValue(engine, contexts);
10
19
  const hasStyleElements = () => hasServerStyleElements && typeof document === "undefined";
11
- const renderStyleElements = (className) => hasStyleElements() ? require_styleElements.createStyleElements(engine.cache, className) : [];
12
- const useThemeView = () => engine.createThemeView(useTheme());
20
+ const renderStyleElements = (className, cache) => cache === void 0 && hasStyleElements() ? require_styleElements.createStyleElements(engine.cache, className) : [];
13
21
  const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
14
22
  const useStyle = ((sheet) => {
15
- const real = useThemeValue();
16
- if (!(0, _umber_style_core.isThemedSheet)(sheet)) return sheet;
17
- return sheet.resolve(real !== void 0 ? engine.createThemeView(real) : void 0);
23
+ const cache = require_CacheProvider.useProvidedCache();
24
+ const view = useThemeView(cache);
25
+ const entries = (0, _umber_style_core.isThemedSheet)(sheet) ? sheet.resolve(view) : sheet;
26
+ return cache === void 0 ? entries : engine.withCache(cache).adoptSheet(entries);
18
27
  });
19
28
  function create(styles) {
20
29
  return engine.createSheet(styles);
@@ -25,25 +34,26 @@ function bindEngine(engine, { hasServerStyleElements = true } = {}) {
25
34
  styled: require_styled.createStyled(engine, useThemeView, renderStyleElements),
26
35
  StyleSheet: { create },
27
36
  useStyle,
28
- apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
37
+ apply: (...entries) => ({ className: ((0, _umber_style_core.findEntryCache)(entries) ?? engine.cache).css(...entries) || void 0 }),
38
+ css: ((...args) => (0, _umber_style_core.createCssEntry)(engine.cache, args)),
29
39
  ThemeProvider: require_ThemeProvider.createThemeProvider({
40
+ engine,
41
+ contexts,
30
42
  getServerStyle: () => hasStyleElements() ? {
31
43
  createStyleElements: renderStyleElements,
32
- createStamp: () => require_styleElements.createStampElement(engine.cache),
33
- findThemeIds: engine.findThemeIds,
34
- themeId: engine.themeId
35
- } : void 0,
36
- learnTheme: (theme) => {
37
- if (!engine.hasTheme) engine.setTheme(theme);
38
- },
39
- themeClass,
40
- scopeClass: engine.scopeClass,
41
- ThemeValueProvider
44
+ createStamp: () => require_styleElements.createStampElement(engine.cache)
45
+ } : void 0
42
46
  }),
47
+ ThemeScope,
48
+ ThemeContext: contexts.ThemeContext,
43
49
  useTheme,
50
+ useThemeClass,
44
51
  themeClass,
45
52
  when: engine.when,
46
- whenScheme: engine.whenScheme
53
+ whenScheme: engine.whenScheme,
54
+ keyframes: engine.keyframes,
55
+ globalStyles: _umber_style_core.globalStyles,
56
+ Global: require_Global.createGlobal(engine, useThemeView, (sets, cache) => cache === void 0 && hasStyleElements() ? require_styleElements.createGlobalElements(engine.cache, sets) : require_styleElements.createGlobalMarkerElements(cache ?? engine.cache, sets))
47
57
  };
48
58
  }
49
59
  /** Makes the React styling of one theme shape, apart from the default one: its own engine, or the one given. */
@@ -1,7 +1,9 @@
1
1
  import { Engine } from "@umber-style/core";
2
+ import "./ThemeValue.cjs";
3
+ import { ThemeContexts } from "./ProviderScope.cjs";
2
4
  import { ServerStyleOptions, StyleOptions, WebStyle } from "./types.cjs";
3
- /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
4
- export declare function bindEngine<T extends object>(engine: Engine<T>, { hasServerStyleElements }?: ServerStyleOptions): WebStyle<T>;
5
+ /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`; `contexts` are those of its prefix unless given. */
6
+ export declare function bindEngine<T extends object>(engine: Engine<T>, { hasServerStyleElements }?: ServerStyleOptions, contexts?: ThemeContexts<T>): WebStyle<T>;
5
7
  /** Makes the React styling of one theme shape, apart from the default one: its own engine, or the one given. */
6
8
  export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): WebStyle<T>;
7
9
  export type { StyleOptions };
@@ -1,7 +1,9 @@
1
1
  import { Engine } from "@umber-style/core";
2
+ import "./ThemeValue.mjs";
3
+ import { ThemeContexts } from "./ProviderScope.mjs";
2
4
  import { ServerStyleOptions, StyleOptions, WebStyle } from "./types.mjs";
3
- /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
4
- export declare function bindEngine<T extends object>(engine: Engine<T>, { hasServerStyleElements }?: ServerStyleOptions): WebStyle<T>;
5
+ /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`; `contexts` are those of its prefix unless given. */
6
+ export declare function bindEngine<T extends object>(engine: Engine<T>, { hasServerStyleElements }?: ServerStyleOptions, contexts?: ThemeContexts<T>): WebStyle<T>;
5
7
  /** Makes the React styling of one theme shape, apart from the default one: its own engine, or the one given. */
6
8
  export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): WebStyle<T>;
7
9
  export type { StyleOptions };
@@ -1,20 +1,29 @@
1
- import { createEngine, isThemedSheet } from "@umber-style/core";
2
- import { createStampElement, createStyleElements } from "./styleElements.mjs";
1
+ import { createCssEntry, createEngine, findEntryCache, globalStyles, isThemedSheet } from "@umber-style/core";
2
+ import { useProvidedCache } from "./CacheProvider.mjs";
3
+ import { createGlobal } from "./Global.mjs";
4
+ import { readPrefixStyle } from "./ProviderScope.mjs";
5
+ import { createGlobalElements, createGlobalMarkerElements, createStampElement, createStyleElements } from "./styleElements.mjs";
3
6
  import { createStyled } from "./styled.mjs";
4
7
  import { createThemeProvider } from "./ThemeProvider.mjs";
5
8
  import { createThemeValue } from "./ThemeValue.mjs";
6
9
  // src/createStyle.ts
7
- /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`. */
8
- function bindEngine(engine, { hasServerStyleElements = true } = {}) {
9
- const { ThemeValueProvider, useTheme, useThemeValue } = createThemeValue(engine);
10
+ /** The contexts of the prefix of `engine`, which every style of that prefix shares; this engine's base theme is the one a provider of the server build merges over, unless another style of the prefix gave one. */
11
+ function readSharedContexts(engine) {
12
+ const shared = readPrefixStyle(engine.cache.prefix);
13
+ shared.holder ??= engine;
14
+ return shared.contexts;
15
+ }
16
+ /** Makes the React styling over an engine: `styled`, sheets, the theme provider and `useTheme`; `contexts` are those of its prefix unless given. */
17
+ function bindEngine(engine, { hasServerStyleElements = true } = {}, contexts = readSharedContexts(engine)) {
18
+ const { useTheme, useThemeView, useThemeClass, ThemeScope } = createThemeValue(engine, contexts);
10
19
  const hasStyleElements = () => hasServerStyleElements && typeof document === "undefined";
11
- const renderStyleElements = (className) => hasStyleElements() ? createStyleElements(engine.cache, className) : [];
12
- const useThemeView = () => engine.createThemeView(useTheme());
20
+ const renderStyleElements = (className, cache) => cache === void 0 && hasStyleElements() ? createStyleElements(engine.cache, className) : [];
13
21
  const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
14
22
  const useStyle = ((sheet) => {
15
- const real = useThemeValue();
16
- if (!isThemedSheet(sheet)) return sheet;
17
- return sheet.resolve(real !== void 0 ? engine.createThemeView(real) : void 0);
23
+ const cache = useProvidedCache();
24
+ const view = useThemeView(cache);
25
+ const entries = isThemedSheet(sheet) ? sheet.resolve(view) : sheet;
26
+ return cache === void 0 ? entries : engine.withCache(cache).adoptSheet(entries);
18
27
  });
19
28
  function create(styles) {
20
29
  return engine.createSheet(styles);
@@ -25,25 +34,26 @@ function bindEngine(engine, { hasServerStyleElements = true } = {}) {
25
34
  styled: createStyled(engine, useThemeView, renderStyleElements),
26
35
  StyleSheet: { create },
27
36
  useStyle,
28
- apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
37
+ apply: (...entries) => ({ className: (findEntryCache(entries) ?? engine.cache).css(...entries) || void 0 }),
38
+ css: ((...args) => createCssEntry(engine.cache, args)),
29
39
  ThemeProvider: createThemeProvider({
40
+ engine,
41
+ contexts,
30
42
  getServerStyle: () => hasStyleElements() ? {
31
43
  createStyleElements: renderStyleElements,
32
- createStamp: () => createStampElement(engine.cache),
33
- findThemeIds: engine.findThemeIds,
34
- themeId: engine.themeId
35
- } : void 0,
36
- learnTheme: (theme) => {
37
- if (!engine.hasTheme) engine.setTheme(theme);
38
- },
39
- themeClass,
40
- scopeClass: engine.scopeClass,
41
- ThemeValueProvider
44
+ createStamp: () => createStampElement(engine.cache)
45
+ } : void 0
42
46
  }),
47
+ ThemeScope,
48
+ ThemeContext: contexts.ThemeContext,
43
49
  useTheme,
50
+ useThemeClass,
44
51
  themeClass,
45
52
  when: engine.when,
46
- whenScheme: engine.whenScheme
53
+ whenScheme: engine.whenScheme,
54
+ keyframes: engine.keyframes,
55
+ globalStyles,
56
+ Global: createGlobal(engine, useThemeView, (sets, cache) => cache === void 0 && hasStyleElements() ? createGlobalElements(engine.cache, sets) : createGlobalMarkerElements(cache ?? engine.cache, sets))
47
57
  };
48
58
  }
49
59
  /** Makes the React styling of one theme shape, apart from the default one: its own engine, or the one given. */
@@ -1,46 +1,53 @@
1
1
  let _umber_style_core = require("@umber-style/core");
2
+ const require_Global_server = require("./Global.server.cjs");
2
3
  const require_serverContext = require("./serverContext.cjs");
3
4
  const require_styled_server = require("./styled.server.cjs");
4
5
  const require_ThemeProvider_server = require("./ThemeProvider.server.cjs");
5
6
  // src/createStyle.server.ts
6
7
  /** Makes the styling for server components over an engine; `cache` makes the slot of a request, `React.cache` unless a test gives one. */
7
8
  function bindEngine(engine, cache) {
8
- const root = require_serverContext.createServerOnlyContext(void 0, cache);
9
- const stamp = require_serverContext.createServerOnlyContext(false, cache);
10
- const merged = /* @__PURE__ */ new WeakMap();
11
- /** The root theme of the request, or the base theme of the engine; throws when there is neither. */
12
- const readTheme = () => {
13
- const rootTheme = require_serverContext.readServerContext(root);
14
- const base = engine.baseTheme;
15
- if (rootTheme === void 0) {
16
- if (base === void 0) throw new Error(_umber_style_core.NO_THEME_MESSAGE);
17
- return base;
9
+ const readFirstSlot = require_serverContext.createRequestSlot(void 0, cache);
10
+ let hasWarned = false;
11
+ const readBaseTheme = () => engine.baseTheme ?? _umber_style_core.EMPTY_THEME;
12
+ /**
13
+ * The base theme: the one given to `createStyle(theme)` or `configure({ theme })`, the same in every
14
+ * render of every request, whatever providers render. With none, an empty object, and one warning in
15
+ * development. A server component cannot see the providers above it: what needs the nearest theme's
16
+ * values is a client component, whose `useTheme()` reads them.
17
+ */
18
+ const useTheme = () => {
19
+ if (engine.baseTheme === void 0 && !hasWarned && (0, _umber_style_core.isDevelopment)()) {
20
+ hasWarned = true;
21
+ console.warn(`[${engine.cache.prefix}] useTheme() in a server component returns the base theme, and there is none: give the theme to createStyle(theme) or configure({ theme }) to read it in server components. It returns an empty object.`);
18
22
  }
19
- if (rootTheme === base) return rootTheme;
20
- let theme = merged.get(rootTheme);
21
- if (theme === void 0) {
22
- theme = (0, _umber_style_core.mergeDeep)(base, rootTheme);
23
- merged.set(rootTheme, theme);
24
- }
25
- return theme;
23
+ return readBaseTheme();
26
24
  };
25
+ const readViewTheme = () => engine.baseTheme ?? readFirstSlot().current ?? _umber_style_core.EMPTY_THEME;
27
26
  const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
28
- const useStyle = ((sheet) => (0, _umber_style_core.isThemedSheet)(sheet) ? sheet.resolve(engine.createThemeView(readTheme())) : sheet);
27
+ const useStyle = ((sheet) => (0, _umber_style_core.isThemedSheet)(sheet) ? sheet.resolve(engine.createThemeView(readViewTheme())) : sheet);
29
28
  function create(styles) {
30
29
  return engine.createSheet(styles);
31
30
  }
32
31
  return {
33
32
  engine,
34
33
  cache: engine.cache,
35
- styled: require_styled_server.createStyled(engine, readTheme),
34
+ styled: require_styled_server.createStyled(engine, readViewTheme),
36
35
  StyleSheet: { create },
37
36
  useStyle,
38
37
  apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
39
- ThemeProvider: require_ThemeProvider_server.createThemeProvider(engine, root, stamp),
40
- readTheme,
38
+ css: ((...args) => (0, _umber_style_core.createCssEntry)(engine.cache, args)),
39
+ ThemeProvider: require_ThemeProvider_server.createThemeProvider(engine, {
40
+ readFirstSlot,
41
+ readBaseTheme
42
+ }),
43
+ useTheme,
44
+ readTheme: useTheme,
41
45
  themeClass,
42
46
  when: engine.when,
43
- whenScheme: engine.whenScheme
47
+ whenScheme: engine.whenScheme,
48
+ keyframes: engine.keyframes,
49
+ globalStyles: _umber_style_core.globalStyles,
50
+ Global: require_Global_server.createGlobal(engine, readViewTheme)
44
51
  };
45
52
  }
46
53
  /** Makes the styling for server components of one theme shape: its own engine, or the one given. */