@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.
- package/LICENSE +21 -0
- package/README.md +11 -1
- 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 +104 -0
- package/dist/ThemeProvider.mjs +104 -0
- package/dist/ThemeProvider.server.cjs +140 -0
- package/dist/ThemeProvider.server.mjs +140 -0
- package/dist/ThemeValue.cjs +64 -0
- package/dist/ThemeValue.d.cts +3 -0
- package/dist/ThemeValue.d.mts +4 -0
- package/dist/ThemeValue.mjs +62 -0
- package/dist/_virtual/_rolldown/runtime.cjs +22 -0
- package/dist/createStyle.cjs +66 -0
- package/dist/createStyle.d.cts +9 -0
- package/dist/createStyle.d.mts +9 -0
- package/dist/createStyle.mjs +65 -0
- package/dist/createStyle.server.cjs +60 -0
- package/dist/createStyle.server.d.cts +8 -0
- package/dist/createStyle.server.d.mts +8 -0
- package/dist/createStyle.server.mjs +59 -0
- package/dist/defaultStyle.cjs +68 -0
- package/dist/defaultStyle.d.cts +29 -0
- package/dist/defaultStyle.d.mts +29 -0
- package/dist/defaultStyle.mjs +68 -0
- package/dist/defaultStyle.server.cjs +52 -0
- package/dist/defaultStyle.server.d.cts +27 -0
- package/dist/defaultStyle.server.d.mts +27 -0
- package/dist/defaultStyle.server.mjs +52 -0
- package/dist/domProps.cjs +27 -0
- package/dist/domProps.mjs +27 -0
- package/dist/index.cjs +72 -0
- package/dist/index.d.cts +28 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.mjs +7 -0
- package/dist/index.server.cjs +66 -0
- package/dist/index.server.d.cts +26 -0
- package/dist/index.server.d.mts +26 -0
- package/dist/index.server.mjs +5 -0
- package/dist/plainTheme.cjs +49 -0
- package/dist/plainTheme.mjs +49 -0
- package/dist/serverContext.cjs +14 -0
- package/dist/serverContext.d.cts +2 -0
- package/dist/serverContext.d.mts +2 -0
- package/dist/serverContext.mjs +14 -0
- package/dist/styleElements.cjs +99 -0
- package/dist/styleElements.d.cts +1 -0
- package/dist/styleElements.d.mts +2 -0
- package/dist/styleElements.mjs +95 -0
- package/dist/styled.cjs +51 -0
- package/dist/styled.mjs +51 -0
- package/dist/styled.server.cjs +24 -0
- package/dist/styled.server.mjs +24 -0
- package/dist/styledParts.cjs +67 -0
- package/dist/styledParts.mjs +67 -0
- package/dist/types.d.cts +274 -0
- package/dist/types.d.mts +274 -0
- 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 +88 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 soroush.tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
# @umber-style/react
|
|
2
|
+
|
|
3
|
+
The React binding of UmberStyle: `styled`, `ThemeProvider`, `useTheme`, `StyleSheet`, `useStyle` and `apply`, with a server build for server components.
|
|
4
|
+
|
|
5
|
+
Install: `pnpm add @umber-style/react`.
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
import { styled, ThemeProvider } from '@umber-style/react'
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Documentation: [docs](../../docs/README.md), and [React](../../docs/react.md) for this package.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
// src/CacheProvider.tsx
|
|
4
|
+
/** The cache that the nearest `CacheProvider` gives, or undefined with none above. */
|
|
5
|
+
const CacheContext = (0, react.createContext)(void 0);
|
|
6
|
+
let isProvided = false;
|
|
7
|
+
/** The cache of the nearest `CacheProvider`, or undefined with none above (read only once one has rendered). */
|
|
8
|
+
function useProvidedCache() {
|
|
9
|
+
return isProvided ? (0, react.use)(CacheContext) : void 0;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Gives the subtree below it its cache: every rule that a component of the binding writes there goes to
|
|
13
|
+
* `value` (classes, sheet entries, global rules, keyframes, the root theme and the theme classes of the
|
|
14
|
+
* providers below), through the engine of the same theme over that cache. Outside any provider the
|
|
15
|
+
* engine's own cache is used. Under it no `<style href precedence>` element is rendered on a server: the
|
|
16
|
+
* app sends the rules of the cache itself (a registry, or `@umber-style/server`).
|
|
17
|
+
*/
|
|
18
|
+
function CacheProvider({ value, children }) {
|
|
19
|
+
isProvided = true;
|
|
20
|
+
return (0, react.createElement)(CacheContext.Provider, { value }, children);
|
|
21
|
+
}
|
|
22
|
+
exports.CacheProvider = CacheProvider;
|
|
23
|
+
exports.useProvidedCache = useProvidedCache;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ReactElement } from "react";
|
|
2
|
+
import { CacheProviderProps } from "./types.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* Gives the subtree below it its cache: every rule that a component of the binding writes there goes to
|
|
5
|
+
* `value` (classes, sheet entries, global rules, keyframes, the root theme and the theme classes of the
|
|
6
|
+
* providers below), through the engine of the same theme over that cache. Outside any provider the
|
|
7
|
+
* engine's own cache is used. Under it no `<style href precedence>` element is rendered on a server: the
|
|
8
|
+
* app sends the rules of the cache itself (a registry, or `@umber-style/server`).
|
|
9
|
+
*/
|
|
10
|
+
export declare function CacheProvider({ value, children }: CacheProviderProps): ReactElement;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ReactElement } from "react";
|
|
2
|
+
import "@umber-style/core";
|
|
3
|
+
import { CacheProviderProps } from "./types.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Gives the subtree below it its cache: every rule that a component of the binding writes there goes to
|
|
6
|
+
* `value` (classes, sheet entries, global rules, keyframes, the root theme and the theme classes of the
|
|
7
|
+
* providers below), through the engine of the same theme over that cache. Outside any provider the
|
|
8
|
+
* engine's own cache is used. Under it no `<style href precedence>` element is rendered on a server: the
|
|
9
|
+
* app sends the rules of the cache itself (a registry, or `@umber-style/server`).
|
|
10
|
+
*/
|
|
11
|
+
export declare function CacheProvider({ value, children }: CacheProviderProps): ReactElement;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, createElement, use } from "react";
|
|
3
|
+
// src/CacheProvider.tsx
|
|
4
|
+
/** The cache that the nearest `CacheProvider` gives, or undefined with none above. */
|
|
5
|
+
const CacheContext = createContext(void 0);
|
|
6
|
+
let isProvided = false;
|
|
7
|
+
/** The cache of the nearest `CacheProvider`, or undefined with none above (read only once one has rendered). */
|
|
8
|
+
function useProvidedCache() {
|
|
9
|
+
return isProvided ? use(CacheContext) : void 0;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Gives the subtree below it its cache: every rule that a component of the binding writes there goes to
|
|
13
|
+
* `value` (classes, sheet entries, global rules, keyframes, the root theme and the theme classes of the
|
|
14
|
+
* providers below), through the engine of the same theme over that cache. Outside any provider the
|
|
15
|
+
* engine's own cache is used. Under it no `<style href precedence>` element is rendered on a server: the
|
|
16
|
+
* app sends the rules of the cache itself (a registry, or `@umber-style/server`).
|
|
17
|
+
*/
|
|
18
|
+
function CacheProvider({ value, children }) {
|
|
19
|
+
isProvided = true;
|
|
20
|
+
return createElement(CacheContext.Provider, { value }, children);
|
|
21
|
+
}
|
|
22
|
+
export { CacheProvider, useProvidedCache };
|
package/dist/Global.cjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
const require_CacheProvider = require("./CacheProvider.cjs");
|
|
3
|
+
// src/Global.ts
|
|
4
|
+
/** What is released while nothing is written. */
|
|
5
|
+
function releaseNothing() {}
|
|
6
|
+
/** Writes `sets` into the cache of `engine`, in order, as rules that a component rendered, and returns the function that releases them all. */
|
|
7
|
+
function insertSets(engine, sets) {
|
|
8
|
+
const releases = sets.map((set) => engine.cache.insertGlobal(set, { isRendered: true }));
|
|
9
|
+
return () => {
|
|
10
|
+
for (const release of releases) release();
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Makes `Global` over an engine: it reads the view of the theme of the nearest provider through
|
|
15
|
+
* `useThemeView` (references for tokens, real values for raw keys) and writes the rules of its styles into the cache.
|
|
16
|
+
* In the browser an insertion effect writes them, before the page changes, and releases them when it
|
|
17
|
+
* unmounts; when the rules change it writes the new ones first and then releases the old ones, so that
|
|
18
|
+
* there is no moment with neither. The cache counts the rules of each set: two elements with equal
|
|
19
|
+
* styles share them, and an effect that runs twice in development writes them back. Rules that a server
|
|
20
|
+
* render sent are taken over, not written again. Where there is no document (a server render) no effect
|
|
21
|
+
* runs and nothing unmounts, so the rules are written for good, during the render, and the elements that
|
|
22
|
+
* `createServerElements` gives for the sets are rendered: the style elements of the sets, or the marker
|
|
23
|
+
* that names them (under a `CacheProvider`, `cache`, and without style elements), so that the CSS
|
|
24
|
+
* extracted for the page holds them and the CSS of another page does not.
|
|
25
|
+
*/
|
|
26
|
+
function createGlobal(engine, useThemeView, createServerElements) {
|
|
27
|
+
return function Global({ styles }) {
|
|
28
|
+
const cache = require_CacheProvider.useProvidedCache();
|
|
29
|
+
const local = cache === void 0 ? engine : engine.withCache(cache);
|
|
30
|
+
const view = useThemeView(cache);
|
|
31
|
+
const sets = (0, react.useMemo)(() => local.resolveGlobal(styles, view), [
|
|
32
|
+
local,
|
|
33
|
+
styles,
|
|
34
|
+
view
|
|
35
|
+
]);
|
|
36
|
+
const held = (0, react.useRef)(releaseNothing);
|
|
37
|
+
(0, react.useInsertionEffect)(() => {
|
|
38
|
+
const previous = held.current;
|
|
39
|
+
held.current = insertSets(local, sets);
|
|
40
|
+
previous();
|
|
41
|
+
}, [local, sets]);
|
|
42
|
+
(0, react.useInsertionEffect)(() => () => {
|
|
43
|
+
held.current();
|
|
44
|
+
held.current = releaseNothing;
|
|
45
|
+
}, []);
|
|
46
|
+
if (typeof document !== "undefined") return null;
|
|
47
|
+
insertSets(local, sets);
|
|
48
|
+
const elements = createServerElements(sets, cache);
|
|
49
|
+
return elements.length === 0 ? null : (0, react.createElement)(react.Fragment, null, ...elements);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
exports.createGlobal = createGlobal;
|
package/dist/Global.mjs
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { Fragment, createElement, useInsertionEffect, useMemo, useRef } from "react";
|
|
2
|
+
import { useProvidedCache } from "./CacheProvider.mjs";
|
|
3
|
+
// src/Global.ts
|
|
4
|
+
/** What is released while nothing is written. */
|
|
5
|
+
function releaseNothing() {}
|
|
6
|
+
/** Writes `sets` into the cache of `engine`, in order, as rules that a component rendered, and returns the function that releases them all. */
|
|
7
|
+
function insertSets(engine, sets) {
|
|
8
|
+
const releases = sets.map((set) => engine.cache.insertGlobal(set, { isRendered: true }));
|
|
9
|
+
return () => {
|
|
10
|
+
for (const release of releases) release();
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Makes `Global` over an engine: it reads the view of the theme of the nearest provider through
|
|
15
|
+
* `useThemeView` (references for tokens, real values for raw keys) and writes the rules of its styles into the cache.
|
|
16
|
+
* In the browser an insertion effect writes them, before the page changes, and releases them when it
|
|
17
|
+
* unmounts; when the rules change it writes the new ones first and then releases the old ones, so that
|
|
18
|
+
* there is no moment with neither. The cache counts the rules of each set: two elements with equal
|
|
19
|
+
* styles share them, and an effect that runs twice in development writes them back. Rules that a server
|
|
20
|
+
* render sent are taken over, not written again. Where there is no document (a server render) no effect
|
|
21
|
+
* runs and nothing unmounts, so the rules are written for good, during the render, and the elements that
|
|
22
|
+
* `createServerElements` gives for the sets are rendered: the style elements of the sets, or the marker
|
|
23
|
+
* that names them (under a `CacheProvider`, `cache`, and without style elements), so that the CSS
|
|
24
|
+
* extracted for the page holds them and the CSS of another page does not.
|
|
25
|
+
*/
|
|
26
|
+
function createGlobal(engine, useThemeView, createServerElements) {
|
|
27
|
+
return function Global({ styles }) {
|
|
28
|
+
const cache = useProvidedCache();
|
|
29
|
+
const local = cache === void 0 ? engine : engine.withCache(cache);
|
|
30
|
+
const view = useThemeView(cache);
|
|
31
|
+
const sets = useMemo(() => local.resolveGlobal(styles, view), [
|
|
32
|
+
local,
|
|
33
|
+
styles,
|
|
34
|
+
view
|
|
35
|
+
]);
|
|
36
|
+
const held = useRef(releaseNothing);
|
|
37
|
+
useInsertionEffect(() => {
|
|
38
|
+
const previous = held.current;
|
|
39
|
+
held.current = insertSets(local, sets);
|
|
40
|
+
previous();
|
|
41
|
+
}, [local, sets]);
|
|
42
|
+
useInsertionEffect(() => () => {
|
|
43
|
+
held.current();
|
|
44
|
+
held.current = releaseNothing;
|
|
45
|
+
}, []);
|
|
46
|
+
if (typeof document !== "undefined") return null;
|
|
47
|
+
insertSets(local, sets);
|
|
48
|
+
const elements = createServerElements(sets, cache);
|
|
49
|
+
return elements.length === 0 ? null : createElement(Fragment, null, ...elements);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export { createGlobal };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
const require_GlobalRelease = require("./GlobalRelease.cjs");
|
|
3
|
+
const require_styleElements = require("./styleElements.cjs");
|
|
4
|
+
// src/Global.server.ts
|
|
5
|
+
/**
|
|
6
|
+
* Makes `Global` for server components: no hook, no context. A function of the theme reads the view of
|
|
7
|
+
* the theme that `readTheme` gives: the base theme (references for tokens, real values for raw keys), else
|
|
8
|
+
* the theme of the first provider of the request, else an empty theme. The rules are written into the cache
|
|
9
|
+
* for good, as a server unmounts nothing, and sent as the style elements of each set and of the
|
|
10
|
+
* animations it uses: React sends each `href` once. After them comes `GlobalRelease`, a client component
|
|
11
|
+
* (an import of a `'use client'` file is a reference to it here), with the sets as plain data: in the
|
|
12
|
+
* browser it lets the cache take the rules over and release them when the page is left.
|
|
13
|
+
*/
|
|
14
|
+
function createGlobal(engine, readTheme) {
|
|
15
|
+
return function Global({ styles }) {
|
|
16
|
+
const sets = engine.resolveGlobal(styles, engine.createThemeView(readTheme()));
|
|
17
|
+
for (const set of sets) engine.cache.insertGlobal(set, { isRendered: true });
|
|
18
|
+
const written = sets.filter(({ rules }) => rules.length > 0);
|
|
19
|
+
const release = written.length === 0 ? [] : [(0, react.createElement)(require_GlobalRelease.GlobalRelease, {
|
|
20
|
+
key: "release",
|
|
21
|
+
prefix: engine.cache.prefix,
|
|
22
|
+
sets: JSON.stringify(written.map(({ id, rules }) => [id, rules]))
|
|
23
|
+
})];
|
|
24
|
+
return (0, react.createElement)(react.Fragment, null, ...require_styleElements.createGlobalElements(engine.cache, sets), ...release);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
exports.createGlobal = createGlobal;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Fragment, createElement } from "react";
|
|
2
|
+
import { GlobalRelease } from "./GlobalRelease.mjs";
|
|
3
|
+
import { createGlobalElements } from "./styleElements.mjs";
|
|
4
|
+
// src/Global.server.ts
|
|
5
|
+
/**
|
|
6
|
+
* Makes `Global` for server components: no hook, no context. A function of the theme reads the view of
|
|
7
|
+
* the theme that `readTheme` gives: the base theme (references for tokens, real values for raw keys), else
|
|
8
|
+
* the theme of the first provider of the request, else an empty theme. The rules are written into the cache
|
|
9
|
+
* for good, as a server unmounts nothing, and sent as the style elements of each set and of the
|
|
10
|
+
* animations it uses: React sends each `href` once. After them comes `GlobalRelease`, a client component
|
|
11
|
+
* (an import of a `'use client'` file is a reference to it here), with the sets as plain data: in the
|
|
12
|
+
* browser it lets the cache take the rules over and release them when the page is left.
|
|
13
|
+
*/
|
|
14
|
+
function createGlobal(engine, readTheme) {
|
|
15
|
+
return function Global({ styles }) {
|
|
16
|
+
const sets = engine.resolveGlobal(styles, engine.createThemeView(readTheme()));
|
|
17
|
+
for (const set of sets) engine.cache.insertGlobal(set, { isRendered: true });
|
|
18
|
+
const written = sets.filter(({ rules }) => rules.length > 0);
|
|
19
|
+
const release = written.length === 0 ? [] : [createElement(GlobalRelease, {
|
|
20
|
+
key: "release",
|
|
21
|
+
prefix: engine.cache.prefix,
|
|
22
|
+
sets: JSON.stringify(written.map(({ id, rules }) => [id, rules]))
|
|
23
|
+
})];
|
|
24
|
+
return createElement(Fragment, null, ...createGlobalElements(engine.cache, sets), ...release);
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export { createGlobal };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
let _umber_style_core = require("@umber-style/core");
|
|
5
|
+
react = require_runtime.__toESM(react, 1);
|
|
6
|
+
// src/GlobalRelease.ts
|
|
7
|
+
/**
|
|
8
|
+
* The browser part of the `Global` of a server component, which renders it after the style elements of
|
|
9
|
+
* its sets. Those elements stay in the document for good (React keeps a style element it hoisted), so in
|
|
10
|
+
* the browser this component lets the cache of the prefix take the rules over, as the `Global` of the
|
|
11
|
+
* client build does with the rules a server sent: the cache deletes them from the element's style sheet
|
|
12
|
+
* when this component unmounts (a client-side navigation away from the page), and writes them itself when
|
|
13
|
+
* it mounts again and the element no longer has them. The text of the element keeps the rules: nothing
|
|
14
|
+
* parses it again, since React neither moves nor rewrites a style element that is in the head. Where the
|
|
15
|
+
* page has no cache of the prefix (no client component made one), it does nothing and the rules stay.
|
|
16
|
+
*/
|
|
17
|
+
function GlobalRelease({ prefix, sets }) {
|
|
18
|
+
react.useInsertionEffect(() => {
|
|
19
|
+
const cache = (0, _umber_style_core.findCache)(prefix, document);
|
|
20
|
+
if (cache === void 0) return void 0;
|
|
21
|
+
const releases = JSON.parse(sets).map(([id, rules]) => cache.insertGlobal({
|
|
22
|
+
id,
|
|
23
|
+
rules,
|
|
24
|
+
animations: []
|
|
25
|
+
}, { isRendered: true }));
|
|
26
|
+
return () => {
|
|
27
|
+
for (const release of releases) release();
|
|
28
|
+
};
|
|
29
|
+
}, [prefix, sets]);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
exports.GlobalRelease = GlobalRelease;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { findCache } from "@umber-style/core";
|
|
4
|
+
// src/GlobalRelease.ts
|
|
5
|
+
/**
|
|
6
|
+
* The browser part of the `Global` of a server component, which renders it after the style elements of
|
|
7
|
+
* its sets. Those elements stay in the document for good (React keeps a style element it hoisted), so in
|
|
8
|
+
* the browser this component lets the cache of the prefix take the rules over, as the `Global` of the
|
|
9
|
+
* client build does with the rules a server sent: the cache deletes them from the element's style sheet
|
|
10
|
+
* when this component unmounts (a client-side navigation away from the page), and writes them itself when
|
|
11
|
+
* it mounts again and the element no longer has them. The text of the element keeps the rules: nothing
|
|
12
|
+
* parses it again, since React neither moves nor rewrites a style element that is in the head. Where the
|
|
13
|
+
* page has no cache of the prefix (no client component made one), it does nothing and the rules stay.
|
|
14
|
+
*/
|
|
15
|
+
function GlobalRelease({ prefix, sets }) {
|
|
16
|
+
React.useInsertionEffect(() => {
|
|
17
|
+
const cache = findCache(prefix, document);
|
|
18
|
+
if (cache === void 0) return void 0;
|
|
19
|
+
const releases = JSON.parse(sets).map(([id, rules]) => cache.insertGlobal({
|
|
20
|
+
id,
|
|
21
|
+
rules,
|
|
22
|
+
animations: []
|
|
23
|
+
}, { isRendered: true }));
|
|
24
|
+
return () => {
|
|
25
|
+
for (const release of releases) release();
|
|
26
|
+
};
|
|
27
|
+
}, [prefix, sets]);
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
export { GlobalRelease };
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
const require_runtime = require("./_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
let _umber_style_core = require("@umber-style/core");
|
|
5
|
+
react = require_runtime.__toESM(react, 1);
|
|
6
|
+
// src/ProviderScope.tsx
|
|
7
|
+
const NO_MARKS = /* @__PURE__ */ new Map();
|
|
8
|
+
let marksContext;
|
|
9
|
+
/** The context of the marks, made on first use: where this module is loaded with the server build of React, nothing renders it. */
|
|
10
|
+
function getMarksContext() {
|
|
11
|
+
marksContext ??= react.createContext(NO_MARKS);
|
|
12
|
+
return marksContext;
|
|
13
|
+
}
|
|
14
|
+
/** `marks`, with `mark` as the nearest provider of `key`. */
|
|
15
|
+
function addMark(marks, key, mark) {
|
|
16
|
+
return new Map(marks).set(key, mark);
|
|
17
|
+
}
|
|
18
|
+
/** Makes the contexts of one style. */
|
|
19
|
+
function createThemeContexts() {
|
|
20
|
+
return {
|
|
21
|
+
ThemeContext: react.createContext(void 0),
|
|
22
|
+
ViewContext: react.createContext(void 0)
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const prefixStyles = /* @__PURE__ */ new Map();
|
|
26
|
+
let claim;
|
|
27
|
+
/**
|
|
28
|
+
* The default style's claim on a prefix: its contexts exist before its engine and its prefix, which
|
|
29
|
+
* `readPrefix` gives on demand. The prefix is the one it will have when its options are set.
|
|
30
|
+
*/
|
|
31
|
+
function claimPrefixStyle(readPrefix, style) {
|
|
32
|
+
claim = {
|
|
33
|
+
readPrefix,
|
|
34
|
+
style
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* The styles of `prefix`, recorded on first use: the default style's when it claims that prefix, else
|
|
39
|
+
* new contexts. Every style of one prefix (the default one, each `createStyle`) shares them, as they
|
|
40
|
+
* share its marks, and so does the browser part of a provider of the server build, which needs no engine.
|
|
41
|
+
*/
|
|
42
|
+
function readPrefixStyle(prefix) {
|
|
43
|
+
let style = prefixStyles.get(prefix);
|
|
44
|
+
if (style === void 0) {
|
|
45
|
+
style = claim?.readPrefix() === prefix ? claim.style : { contexts: createThemeContexts() };
|
|
46
|
+
prefixStyles.set(prefix, style);
|
|
47
|
+
}
|
|
48
|
+
return style;
|
|
49
|
+
}
|
|
50
|
+
/** The declarations of the variables of `values`, `--name:value;` for each: the text that the engine's `readThemeText` gives. */
|
|
51
|
+
function declareValues(prefix, values, tokens) {
|
|
52
|
+
const declared = (0, _umber_style_core.createTokens)(values, {
|
|
53
|
+
...tokens,
|
|
54
|
+
prefix
|
|
55
|
+
}).declare(values);
|
|
56
|
+
return (0, _umber_style_core.toDeclarationText)(Object.entries(declared));
|
|
57
|
+
}
|
|
58
|
+
/** The declarations of the given theme, and its color-scheme. */
|
|
59
|
+
function declareOwn(prefix, given, theme) {
|
|
60
|
+
const { colorScheme, tokens } = theme;
|
|
61
|
+
return declareValues(prefix, given, tokens) + (colorScheme ? `color-scheme:${colorScheme};` : "");
|
|
62
|
+
}
|
|
63
|
+
/** 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. */
|
|
64
|
+
function createRootStyle(prefix, given, theme) {
|
|
65
|
+
const { base, layerOrder, rootId, tokens, properties } = theme;
|
|
66
|
+
const body = (base === void 0 ? "" : declareValues(prefix, base.theme, tokens)) + declareOwn(prefix, given, theme);
|
|
67
|
+
if (body === "") return void 0;
|
|
68
|
+
const rule = (0, _umber_style_core.createThemeRule)(prefix, layerOrder !== "", _umber_style_core.ROOT_SELECTOR, body);
|
|
69
|
+
return {
|
|
70
|
+
style: {
|
|
71
|
+
href: rootId,
|
|
72
|
+
text: layerOrder + (base?.properties ?? "") + properties + rule
|
|
73
|
+
},
|
|
74
|
+
rule
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
/** The style elements of a nested provider: of its theme class (none when it declares nothing), then of its other classes. */
|
|
78
|
+
function createNestedStyles(prefix, given, theme) {
|
|
79
|
+
const { classId, layerOrder, properties, styles } = theme;
|
|
80
|
+
const own = declareOwn(prefix, given, theme);
|
|
81
|
+
if (own === "") return styles;
|
|
82
|
+
const rule = (0, _umber_style_core.createThemeRule)(prefix, layerOrder !== "", `.${classId}`, own);
|
|
83
|
+
return [{
|
|
84
|
+
href: classId,
|
|
85
|
+
text: layerOrder + properties + rule
|
|
86
|
+
}, ...styles];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Leaves one root rule of `prefix` in the style elements that React keeps for it (`data-precedence`): the
|
|
90
|
+
* one in the element of `rootId`, written again there when an earlier pass deleted it, and none in any
|
|
91
|
+
* other element. React keeps a style element for the life of the document, so after the root theme
|
|
92
|
+
* changed (a refresh that renders another theme, a navigation back) the elements of the earlier ones are
|
|
93
|
+
* still there. Nothing changes until React has put the element of `rootId` in the document. The elements
|
|
94
|
+
* of the browser's cache and of a registry are not touched.
|
|
95
|
+
*/
|
|
96
|
+
function keepRootRule(prefix, rootId, rule, page) {
|
|
97
|
+
const elements = [...page.querySelectorAll(`style[data-precedence="${prefix}"]`)];
|
|
98
|
+
const own = elements.find((element) => element.dataset.href?.split(" ").includes(rootId));
|
|
99
|
+
if (own === void 0) return;
|
|
100
|
+
const pattern = new RegExp(String.raw`^(@layer ${prefix}-theme\s*\{\s*)?:where\(:root\)`);
|
|
101
|
+
for (const element of elements) {
|
|
102
|
+
const sheet = element.sheet;
|
|
103
|
+
let isKept = element !== own;
|
|
104
|
+
for (let index = sheet.cssRules.length - 1; index >= 0; index--) {
|
|
105
|
+
if (!pattern.test(sheet.cssRules[index].cssText)) continue;
|
|
106
|
+
if (isKept) sheet.deleteRule(index);
|
|
107
|
+
isKept = true;
|
|
108
|
+
}
|
|
109
|
+
if (!isKept) sheet.insertRule(rule, sheet.cssRules.length);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* The browser part of a provider of the server build, which renders it with the theme it was given, as
|
|
114
|
+
* plain data, and the data of its rules. Whether a provider of the same engine is above is decided here,
|
|
115
|
+
* in the tree as it is rendered: a server render of a part of the page (a client-side navigation renders
|
|
116
|
+
* only the segment that changed) does not render the providers of the layouts above it, which are mounted
|
|
117
|
+
* in the browser. Rendered on the server as any client component is, it makes the same choice in the
|
|
118
|
+
* first HTML. The rules are made here from the values, with the core's tokens, so the server and the
|
|
119
|
+
* browser write the same text from the same props, with or without an engine of the client build.
|
|
120
|
+
*
|
|
121
|
+
* - Nested: it renders the style element of the theme class of its theme, those of its other classes, and
|
|
122
|
+
* its wrapper.
|
|
123
|
+
* - Outermost: no element; the style element of its root rule (the base theme, then its theme, on
|
|
124
|
+
* `:root`), and, once committed in the browser, it leaves that root rule alone in the document. A
|
|
125
|
+
* provider that has no base theme for that rule (see `ScopeTheme.base`) is nested instead.
|
|
126
|
+
* - With no rules (`isRoot`): no element, no rule.
|
|
127
|
+
*
|
|
128
|
+
* In each case it marks its prefix for the providers below, of both builds, and gives the contexts of
|
|
129
|
+
* its prefix the theme merged over the theme of the provider above it in the page, of either build, else
|
|
130
|
+
* over the base theme of the client build: `useTheme()` of a client component below reads it.
|
|
131
|
+
*/
|
|
132
|
+
function ProviderScope({ prefix, given, theme, children }) {
|
|
133
|
+
const MarksContext = getMarksContext();
|
|
134
|
+
const marks = react.use(MarksContext);
|
|
135
|
+
const above = marks.get(prefix);
|
|
136
|
+
const isNested = theme !== void 0 && (above !== void 0 || !theme.isWhole && theme.base === void 0);
|
|
137
|
+
const as = theme?.as;
|
|
138
|
+
const className = theme?.className;
|
|
139
|
+
const wrapper = react.useMemo(() => isNested ? {
|
|
140
|
+
as,
|
|
141
|
+
className
|
|
142
|
+
} : void 0, [
|
|
143
|
+
isNested,
|
|
144
|
+
as,
|
|
145
|
+
className
|
|
146
|
+
]);
|
|
147
|
+
const scope = theme === void 0 ? above?.scope ?? null : wrapper ?? null;
|
|
148
|
+
const below = react.useMemo(() => addMark(marks, prefix, { scope }), [
|
|
149
|
+
marks,
|
|
150
|
+
prefix,
|
|
151
|
+
scope
|
|
152
|
+
]);
|
|
153
|
+
const { contexts, holder } = readPrefixStyle(prefix);
|
|
154
|
+
const outer = react.use(contexts.ThemeContext) ?? holder?.baseTheme;
|
|
155
|
+
const merged = react.useMemo(() => (0, _umber_style_core.mergeTheme)(outer, given), [outer, given]);
|
|
156
|
+
const styles = react.useMemo(() => {
|
|
157
|
+
if (theme === void 0) return {
|
|
158
|
+
list: [],
|
|
159
|
+
rule: void 0
|
|
160
|
+
};
|
|
161
|
+
if (isNested) return {
|
|
162
|
+
list: createNestedStyles(prefix, given, theme),
|
|
163
|
+
rule: void 0
|
|
164
|
+
};
|
|
165
|
+
const root = createRootStyle(prefix, given, theme);
|
|
166
|
+
return {
|
|
167
|
+
list: root === void 0 ? [] : [root.style],
|
|
168
|
+
rule: root?.rule
|
|
169
|
+
};
|
|
170
|
+
}, [
|
|
171
|
+
prefix,
|
|
172
|
+
given,
|
|
173
|
+
theme,
|
|
174
|
+
isNested
|
|
175
|
+
]);
|
|
176
|
+
const rootRule = styles.rule;
|
|
177
|
+
const rootId = theme?.rootId;
|
|
178
|
+
react.useLayoutEffect(() => {
|
|
179
|
+
if (rootRule !== void 0) keepRootRule(prefix, rootId, rootRule, document);
|
|
180
|
+
}, [
|
|
181
|
+
prefix,
|
|
182
|
+
rootId,
|
|
183
|
+
rootRule
|
|
184
|
+
]);
|
|
185
|
+
const content = react.createElement(MarksContext.Provider, { value: below }, react.createElement(contexts.ThemeContext.Provider, { value: merged }, react.createElement(contexts.ViewContext.Provider, { value: void 0 }, children)));
|
|
186
|
+
return react.createElement(react.Fragment, null, styles.list.map(({ href, text }) => react.createElement("style", {
|
|
187
|
+
key: href,
|
|
188
|
+
href,
|
|
189
|
+
precedence: prefix
|
|
190
|
+
}, text)), wrapper === void 0 ? content : react.createElement(wrapper.as, { className: wrapper.className }, content));
|
|
191
|
+
}
|
|
192
|
+
exports.ProviderScope = ProviderScope;
|
|
193
|
+
exports.addMark = addMark;
|
|
194
|
+
exports.claimPrefixStyle = claimPrefixStyle;
|
|
195
|
+
exports.createNestedStyles = createNestedStyles;
|
|
196
|
+
exports.createRootStyle = createRootStyle;
|
|
197
|
+
exports.createThemeContexts = createThemeContexts;
|
|
198
|
+
exports.declareValues = declareValues;
|
|
199
|
+
exports.getMarksContext = getMarksContext;
|
|
200
|
+
exports.keepRootRule = keepRootRule;
|
|
201
|
+
exports.readPrefixStyle = readPrefixStyle;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import "./styleElements.cjs";
|
|
3
|
+
/**
|
|
4
|
+
* The contexts of one prefix. `ThemeContext` holds the real theme of the nearest provider (its theme
|
|
5
|
+
* merged over the ones above), of either build. `ViewContext` holds its view (references for tokens),
|
|
6
|
+
* which stays the same object while only the values of variables change; a provider of the server build
|
|
7
|
+
* sets it to undefined, and what reads it below makes the view of `ThemeContext`.
|
|
8
|
+
*/
|
|
9
|
+
export interface ThemeContexts<T> {
|
|
10
|
+
/** The real theme of the nearest provider. */
|
|
11
|
+
ThemeContext: React.Context<T | undefined>;
|
|
12
|
+
/** The view of the nearest provider (references for tokens), or undefined where a provider of the server build is nearest. */
|
|
13
|
+
ViewContext: React.Context<T | undefined>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as React from "react";
|
|
2
|
+
import "@umber-style/core";
|
|
3
|
+
import "./styleElements.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* The contexts of one prefix. `ThemeContext` holds the real theme of the nearest provider (its theme
|
|
6
|
+
* merged over the ones above), of either build. `ViewContext` holds its view (references for tokens),
|
|
7
|
+
* which stays the same object while only the values of variables change; a provider of the server build
|
|
8
|
+
* sets it to undefined, and what reads it below makes the view of `ThemeContext`.
|
|
9
|
+
*/
|
|
10
|
+
export interface ThemeContexts<T> {
|
|
11
|
+
/** The real theme of the nearest provider. */
|
|
12
|
+
ThemeContext: React.Context<T | undefined>;
|
|
13
|
+
/** The view of the nearest provider (references for tokens), or undefined where a provider of the server build is nearest. */
|
|
14
|
+
ViewContext: React.Context<T | undefined>;
|
|
15
|
+
}
|