@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
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { cache } from "react";
|
|
2
|
+
// src/serverContext.ts
|
|
3
|
+
/**
|
|
4
|
+
* The slot of the current request, for server components, which have no React context: `React.cache`
|
|
5
|
+
* keeps it for the length of one request, starting at `defaultValue`. It is never shared between two
|
|
6
|
+
* requests, so nothing written there depends on the order in which requests come.
|
|
7
|
+
*/
|
|
8
|
+
function createRequestSlot(defaultValue, cache$1 = cache) {
|
|
9
|
+
return cache$1(() => ({
|
|
10
|
+
current: defaultValue,
|
|
11
|
+
isSet: false
|
|
12
|
+
}));
|
|
13
|
+
}
|
|
14
|
+
export { createRequestSlot };
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let _umber_style_core = require("@umber-style/core");
|
|
3
|
+
// src/styleElements.ts
|
|
4
|
+
/** The `<style href precedence>` element of `style`, in the precedence of the prefix of `cache`. */
|
|
5
|
+
const toElement = (cache) => ({ href, text }) => (0, react.createElement)("style", {
|
|
6
|
+
key: href,
|
|
7
|
+
href,
|
|
8
|
+
precedence: cache.prefix
|
|
9
|
+
}, text);
|
|
10
|
+
/**
|
|
11
|
+
* The style elements of the animations in `names` that `sent` does not hold yet, which it adds them to:
|
|
12
|
+
* the `@keyframes` rule that the cache wrote, with the animation name as `href`, so that React sends an
|
|
13
|
+
* animation that many classes use once. The rule is outside every layer, so the element needs no order
|
|
14
|
+
* statement.
|
|
15
|
+
*/
|
|
16
|
+
function collectKeyframesTexts(cache, names, sent) {
|
|
17
|
+
const texts = [];
|
|
18
|
+
for (const name of names) {
|
|
19
|
+
if (sent.has(name)) continue;
|
|
20
|
+
sent.add(name);
|
|
21
|
+
texts.push({
|
|
22
|
+
href: name,
|
|
23
|
+
text: cache.getRules(name).join("")
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return texts;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The style elements, as data, of the classes in `className`: React 19 moves them to the head and renders
|
|
30
|
+
* each `href` once. Each one starts with the order statement of the layers (none without layers), so
|
|
31
|
+
* that it is right on its own: declaring a layer again does not change the order. A name that the cache
|
|
32
|
+
* made no rules for (a class of someone else) gets none. A theme class comes with every variable it has,
|
|
33
|
+
* used or not: its element is sent before the elements that use them, so it cannot know which are used. A
|
|
34
|
+
* class that uses animations comes after the element of each of them, once.
|
|
35
|
+
*/
|
|
36
|
+
function collectStyleTexts(cache, className) {
|
|
37
|
+
const sent = /* @__PURE__ */ new Set();
|
|
38
|
+
return (className ?? "").split(/\s+/).filter(Boolean).flatMap((name) => {
|
|
39
|
+
const rules = cache.getFullRules(name);
|
|
40
|
+
if (rules.length === 0) return [];
|
|
41
|
+
return [...collectKeyframesTexts(cache, cache.getAnimations(name), sent), {
|
|
42
|
+
href: name,
|
|
43
|
+
text: [cache.layerOrder, ...rules].join("")
|
|
44
|
+
}];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** The `<style href precedence>` elements for the classes in `className` (see `collectStyleTexts`); the precedence is the prefix of the cache. */
|
|
48
|
+
function createStyleElements(cache, className) {
|
|
49
|
+
return collectStyleTexts(cache, className).map(toElement(cache));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The `<style href precedence>` elements of global rules that the cache wrote (`insertGlobal`): for each set
|
|
53
|
+
* with rules, the elements of the animations it uses, then its own, with its id as `href` and the order
|
|
54
|
+
* statement of the layers first. Each `href` once.
|
|
55
|
+
*/
|
|
56
|
+
function createGlobalElements(cache, sets) {
|
|
57
|
+
const sent = /* @__PURE__ */ new Set();
|
|
58
|
+
return sets.flatMap(({ id, rules, animations }) => {
|
|
59
|
+
if (rules.length === 0 || sent.has(id)) return [];
|
|
60
|
+
sent.add(id);
|
|
61
|
+
return [...collectKeyframesTexts(cache, animations, sent), {
|
|
62
|
+
href: id,
|
|
63
|
+
text: [cache.layerOrder, ...rules].join("")
|
|
64
|
+
}];
|
|
65
|
+
}).map(toElement(cache));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The marker of global rules that a server render sends in place of their style elements (none is sent
|
|
69
|
+
* under a cache provider, or without `hasServerStyleElements`): a `<meta name="<prefix>-global">` whose
|
|
70
|
+
* `content` is the ids of the sets that have rules. React moves it to the head, and `extractPageStyles`
|
|
71
|
+
* reads it, so that the CSS of this page holds the sets and the CSS of another page does not. Like the
|
|
72
|
+
* stamp, it is rendered where there is no document only; a hydration skips it. None when no set has rules.
|
|
73
|
+
*/
|
|
74
|
+
function createGlobalMarkerElements(cache, sets) {
|
|
75
|
+
const marker = (0, _umber_style_core.createGlobalMarker)(cache.prefix, sets);
|
|
76
|
+
if (marker === void 0) return [];
|
|
77
|
+
return [(0, react.createElement)("meta", {
|
|
78
|
+
key: marker.name,
|
|
79
|
+
name: marker.name,
|
|
80
|
+
content: marker.ids
|
|
81
|
+
})];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The element that carries the options of the cache, for the browser to compare with its own: a `<meta>`
|
|
85
|
+
* named `<prefix>-options`, which React moves to the head. It is not a style element, because a server
|
|
86
|
+
* render merges the style elements of one precedence into one and keeps the attributes of none of them.
|
|
87
|
+
*/
|
|
88
|
+
function createStampElement(cache) {
|
|
89
|
+
return (0, react.createElement)("meta", {
|
|
90
|
+
key: `${cache.prefix}-options`,
|
|
91
|
+
name: `${cache.prefix}-options`,
|
|
92
|
+
content: cache.fingerprint
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
exports.collectStyleTexts = collectStyleTexts;
|
|
96
|
+
exports.createGlobalElements = createGlobalElements;
|
|
97
|
+
exports.createGlobalMarkerElements = createGlobalMarkerElements;
|
|
98
|
+
exports.createStampElement = createStampElement;
|
|
99
|
+
exports.createStyleElements = createStyleElements;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { createElement } from "react";
|
|
2
|
+
import { createGlobalMarker } from "@umber-style/core";
|
|
3
|
+
// src/styleElements.ts
|
|
4
|
+
/** The `<style href precedence>` element of `style`, in the precedence of the prefix of `cache`. */
|
|
5
|
+
const toElement = (cache) => ({ href, text }) => createElement("style", {
|
|
6
|
+
key: href,
|
|
7
|
+
href,
|
|
8
|
+
precedence: cache.prefix
|
|
9
|
+
}, text);
|
|
10
|
+
/**
|
|
11
|
+
* The style elements of the animations in `names` that `sent` does not hold yet, which it adds them to:
|
|
12
|
+
* the `@keyframes` rule that the cache wrote, with the animation name as `href`, so that React sends an
|
|
13
|
+
* animation that many classes use once. The rule is outside every layer, so the element needs no order
|
|
14
|
+
* statement.
|
|
15
|
+
*/
|
|
16
|
+
function collectKeyframesTexts(cache, names, sent) {
|
|
17
|
+
const texts = [];
|
|
18
|
+
for (const name of names) {
|
|
19
|
+
if (sent.has(name)) continue;
|
|
20
|
+
sent.add(name);
|
|
21
|
+
texts.push({
|
|
22
|
+
href: name,
|
|
23
|
+
text: cache.getRules(name).join("")
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
return texts;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The style elements, as data, of the classes in `className`: React 19 moves them to the head and renders
|
|
30
|
+
* each `href` once. Each one starts with the order statement of the layers (none without layers), so
|
|
31
|
+
* that it is right on its own: declaring a layer again does not change the order. A name that the cache
|
|
32
|
+
* made no rules for (a class of someone else) gets none. A theme class comes with every variable it has,
|
|
33
|
+
* used or not: its element is sent before the elements that use them, so it cannot know which are used. A
|
|
34
|
+
* class that uses animations comes after the element of each of them, once.
|
|
35
|
+
*/
|
|
36
|
+
function collectStyleTexts(cache, className) {
|
|
37
|
+
const sent = /* @__PURE__ */ new Set();
|
|
38
|
+
return (className ?? "").split(/\s+/).filter(Boolean).flatMap((name) => {
|
|
39
|
+
const rules = cache.getFullRules(name);
|
|
40
|
+
if (rules.length === 0) return [];
|
|
41
|
+
return [...collectKeyframesTexts(cache, cache.getAnimations(name), sent), {
|
|
42
|
+
href: name,
|
|
43
|
+
text: [cache.layerOrder, ...rules].join("")
|
|
44
|
+
}];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** The `<style href precedence>` elements for the classes in `className` (see `collectStyleTexts`); the precedence is the prefix of the cache. */
|
|
48
|
+
function createStyleElements(cache, className) {
|
|
49
|
+
return collectStyleTexts(cache, className).map(toElement(cache));
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The `<style href precedence>` elements of global rules that the cache wrote (`insertGlobal`): for each set
|
|
53
|
+
* with rules, the elements of the animations it uses, then its own, with its id as `href` and the order
|
|
54
|
+
* statement of the layers first. Each `href` once.
|
|
55
|
+
*/
|
|
56
|
+
function createGlobalElements(cache, sets) {
|
|
57
|
+
const sent = /* @__PURE__ */ new Set();
|
|
58
|
+
return sets.flatMap(({ id, rules, animations }) => {
|
|
59
|
+
if (rules.length === 0 || sent.has(id)) return [];
|
|
60
|
+
sent.add(id);
|
|
61
|
+
return [...collectKeyframesTexts(cache, animations, sent), {
|
|
62
|
+
href: id,
|
|
63
|
+
text: [cache.layerOrder, ...rules].join("")
|
|
64
|
+
}];
|
|
65
|
+
}).map(toElement(cache));
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* The marker of global rules that a server render sends in place of their style elements (none is sent
|
|
69
|
+
* under a cache provider, or without `hasServerStyleElements`): a `<meta name="<prefix>-global">` whose
|
|
70
|
+
* `content` is the ids of the sets that have rules. React moves it to the head, and `extractPageStyles`
|
|
71
|
+
* reads it, so that the CSS of this page holds the sets and the CSS of another page does not. Like the
|
|
72
|
+
* stamp, it is rendered where there is no document only; a hydration skips it. None when no set has rules.
|
|
73
|
+
*/
|
|
74
|
+
function createGlobalMarkerElements(cache, sets) {
|
|
75
|
+
const marker = createGlobalMarker(cache.prefix, sets);
|
|
76
|
+
if (marker === void 0) return [];
|
|
77
|
+
return [createElement("meta", {
|
|
78
|
+
key: marker.name,
|
|
79
|
+
name: marker.name,
|
|
80
|
+
content: marker.ids
|
|
81
|
+
})];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The element that carries the options of the cache, for the browser to compare with its own: a `<meta>`
|
|
85
|
+
* named `<prefix>-options`, which React moves to the head. It is not a style element, because a server
|
|
86
|
+
* render merges the style elements of one precedence into one and keeps the attributes of none of them.
|
|
87
|
+
*/
|
|
88
|
+
function createStampElement(cache) {
|
|
89
|
+
return createElement("meta", {
|
|
90
|
+
key: `${cache.prefix}-options`,
|
|
91
|
+
name: `${cache.prefix}-options`,
|
|
92
|
+
content: cache.fingerprint
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
export { collectStyleTexts, createGlobalElements, createGlobalMarkerElements, createStampElement, createStyleElements };
|
package/dist/styled.cjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let _umber_style_core = require("@umber-style/core");
|
|
3
|
+
const require_CacheProvider = require("./CacheProvider.cjs");
|
|
4
|
+
const require_styledParts = require("./styledParts.cjs");
|
|
5
|
+
// src/styled.ts
|
|
6
|
+
/**
|
|
7
|
+
* Counts the element next to it as a user of its held class, from the commit that shows the class to the
|
|
8
|
+
* commit that drops it or the unmount. An insertion effect runs in the commit, before the browser paints:
|
|
9
|
+
* a render that is thrown away holds nothing, and the class, which render wrote, is still in the sheet.
|
|
10
|
+
*/
|
|
11
|
+
function Hold({ hold }) {
|
|
12
|
+
(0, react.useInsertionEffect)(() => hold(), [hold]);
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Makes `styled` over an engine. A component with a function style reads the theme of the nearest
|
|
17
|
+
* provider through `useThemeView`; one with only object styles reads no context, until a `CacheProvider`
|
|
18
|
+
* has rendered: from then on each component reads the cache of the nearest one, and makes its class with
|
|
19
|
+
* the engine over that cache. `createStyleElements` gives the style elements of a class where the build
|
|
20
|
+
* sends them (a server render outside any cache provider), and none in the browser.
|
|
21
|
+
* In the browser, an element given a plain object in `style` holds the class that object made (`Hold`,
|
|
22
|
+
* rendered after it only then), so that the class is removed when no element uses it.
|
|
23
|
+
*/
|
|
24
|
+
function createStyled(engine, useThemeView, createStyleElements) {
|
|
25
|
+
/** The element, with the style elements of its class in front of it where there are any, and its `Hold` after it where it holds a class. */
|
|
26
|
+
const render = (element, forwarded, className, hold, cache) => {
|
|
27
|
+
const rendered = (0, react.createElement)(element, forwarded);
|
|
28
|
+
if (hold) return (0, react.createElement)(react.Fragment, null, rendered, (0, react.createElement)(Hold, { hold }));
|
|
29
|
+
const styles = createStyleElements(className, cache);
|
|
30
|
+
return styles.length === 0 ? rendered : (0, react.createElement)(react.Fragment, null, ...styles, rendered);
|
|
31
|
+
};
|
|
32
|
+
const canHold = () => typeof document !== "undefined";
|
|
33
|
+
const styled = (tag, options = {}) => (...args) => {
|
|
34
|
+
const styles = (0, _umber_style_core.readStyleArguments)(args);
|
|
35
|
+
const { styler, resolve, statics } = require_styledParts.prepareStyled(engine, tag, options, styles);
|
|
36
|
+
const StaticStyled = (props) => {
|
|
37
|
+
const cache = require_CacheProvider.useProvidedCache();
|
|
38
|
+
const { element, forwarded, className, hold } = resolve(props, void 0, canHold(), cache);
|
|
39
|
+
return render(element, forwarded, className, hold, cache);
|
|
40
|
+
};
|
|
41
|
+
const DynamicStyled = (props) => {
|
|
42
|
+
const cache = require_CacheProvider.useProvidedCache();
|
|
43
|
+
const view = useThemeView(cache);
|
|
44
|
+
const { element, forwarded, className, hold } = resolve(props, view, canHold(), cache);
|
|
45
|
+
return render(element, forwarded, className, hold, cache);
|
|
46
|
+
};
|
|
47
|
+
return Object.assign(styler.isDynamic ? DynamicStyled : StaticStyled, statics);
|
|
48
|
+
};
|
|
49
|
+
return (0, _umber_style_core.withTagShorthands)(styled);
|
|
50
|
+
}
|
|
51
|
+
exports.createStyled = createStyled;
|
package/dist/styled.mjs
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { Fragment, createElement, useInsertionEffect } from "react";
|
|
2
|
+
import { readStyleArguments, withTagShorthands } from "@umber-style/core";
|
|
3
|
+
import { useProvidedCache } from "./CacheProvider.mjs";
|
|
4
|
+
import { prepareStyled } from "./styledParts.mjs";
|
|
5
|
+
// src/styled.ts
|
|
6
|
+
/**
|
|
7
|
+
* Counts the element next to it as a user of its held class, from the commit that shows the class to the
|
|
8
|
+
* commit that drops it or the unmount. An insertion effect runs in the commit, before the browser paints:
|
|
9
|
+
* a render that is thrown away holds nothing, and the class, which render wrote, is still in the sheet.
|
|
10
|
+
*/
|
|
11
|
+
function Hold({ hold }) {
|
|
12
|
+
useInsertionEffect(() => hold(), [hold]);
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Makes `styled` over an engine. A component with a function style reads the theme of the nearest
|
|
17
|
+
* provider through `useThemeView`; one with only object styles reads no context, until a `CacheProvider`
|
|
18
|
+
* has rendered: from then on each component reads the cache of the nearest one, and makes its class with
|
|
19
|
+
* the engine over that cache. `createStyleElements` gives the style elements of a class where the build
|
|
20
|
+
* sends them (a server render outside any cache provider), and none in the browser.
|
|
21
|
+
* In the browser, an element given a plain object in `style` holds the class that object made (`Hold`,
|
|
22
|
+
* rendered after it only then), so that the class is removed when no element uses it.
|
|
23
|
+
*/
|
|
24
|
+
function createStyled(engine, useThemeView, createStyleElements) {
|
|
25
|
+
/** The element, with the style elements of its class in front of it where there are any, and its `Hold` after it where it holds a class. */
|
|
26
|
+
const render = (element, forwarded, className, hold, cache) => {
|
|
27
|
+
const rendered = createElement(element, forwarded);
|
|
28
|
+
if (hold) return createElement(Fragment, null, rendered, createElement(Hold, { hold }));
|
|
29
|
+
const styles = createStyleElements(className, cache);
|
|
30
|
+
return styles.length === 0 ? rendered : createElement(Fragment, null, ...styles, rendered);
|
|
31
|
+
};
|
|
32
|
+
const canHold = () => typeof document !== "undefined";
|
|
33
|
+
const styled = (tag, options = {}) => (...args) => {
|
|
34
|
+
const styles = readStyleArguments(args);
|
|
35
|
+
const { styler, resolve, statics } = prepareStyled(engine, tag, options, styles);
|
|
36
|
+
const StaticStyled = (props) => {
|
|
37
|
+
const cache = useProvidedCache();
|
|
38
|
+
const { element, forwarded, className, hold } = resolve(props, void 0, canHold(), cache);
|
|
39
|
+
return render(element, forwarded, className, hold, cache);
|
|
40
|
+
};
|
|
41
|
+
const DynamicStyled = (props) => {
|
|
42
|
+
const cache = useProvidedCache();
|
|
43
|
+
const view = useThemeView(cache);
|
|
44
|
+
const { element, forwarded, className, hold } = resolve(props, view, canHold(), cache);
|
|
45
|
+
return render(element, forwarded, className, hold, cache);
|
|
46
|
+
};
|
|
47
|
+
return Object.assign(styler.isDynamic ? DynamicStyled : StaticStyled, statics);
|
|
48
|
+
};
|
|
49
|
+
return withTagShorthands(styled);
|
|
50
|
+
}
|
|
51
|
+
export { createStyled };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let _umber_style_core = require("@umber-style/core");
|
|
3
|
+
const require_styleElements = require("./styleElements.cjs");
|
|
4
|
+
const require_styledParts = require("./styledParts.cjs");
|
|
5
|
+
// src/styled.server.ts
|
|
6
|
+
/**
|
|
7
|
+
* Makes `styled` for server components: no hook, no context. A function style reads the view of the
|
|
8
|
+
* theme that `readTheme` gives: the base theme (tokens as references, its real values for the rest), else
|
|
9
|
+
* the theme of the first provider of the request, else an empty theme. Each element comes with the `<style>` of its class.
|
|
10
|
+
*/
|
|
11
|
+
function createStyled(engine, readTheme) {
|
|
12
|
+
const styled = (tag, options = {}) => (...args) => {
|
|
13
|
+
const styles = (0, _umber_style_core.readStyleArguments)(args);
|
|
14
|
+
const { styler, resolve, statics } = require_styledParts.prepareStyled(engine, tag, options, styles);
|
|
15
|
+
const Styled = (props) => {
|
|
16
|
+
const theme = styler.isDynamic ? engine.createThemeView(readTheme()) : void 0;
|
|
17
|
+
const { element, forwarded, className } = resolve(props, theme);
|
|
18
|
+
return (0, react.createElement)(react.Fragment, null, ...require_styleElements.createStyleElements(engine.cache, className), (0, react.createElement)(element, forwarded));
|
|
19
|
+
};
|
|
20
|
+
return Object.assign(Styled, statics);
|
|
21
|
+
};
|
|
22
|
+
return (0, _umber_style_core.withTagShorthands)(styled);
|
|
23
|
+
}
|
|
24
|
+
exports.createStyled = createStyled;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Fragment, createElement } from "react";
|
|
2
|
+
import { readStyleArguments, withTagShorthands } from "@umber-style/core";
|
|
3
|
+
import { createStyleElements } from "./styleElements.mjs";
|
|
4
|
+
import { prepareStyled } from "./styledParts.mjs";
|
|
5
|
+
// src/styled.server.ts
|
|
6
|
+
/**
|
|
7
|
+
* Makes `styled` for server components: no hook, no context. A function style reads the view of the
|
|
8
|
+
* theme that `readTheme` gives: the base theme (tokens as references, its real values for the rest), else
|
|
9
|
+
* the theme of the first provider of the request, else an empty theme. Each element comes with the `<style>` of its class.
|
|
10
|
+
*/
|
|
11
|
+
function createStyled(engine, readTheme) {
|
|
12
|
+
const styled = (tag, options = {}) => (...args) => {
|
|
13
|
+
const styles = readStyleArguments(args);
|
|
14
|
+
const { styler, resolve, statics } = prepareStyled(engine, tag, options, styles);
|
|
15
|
+
const Styled = (props) => {
|
|
16
|
+
const theme = styler.isDynamic ? engine.createThemeView(readTheme()) : void 0;
|
|
17
|
+
const { element, forwarded, className } = resolve(props, theme);
|
|
18
|
+
return createElement(Fragment, null, ...createStyleElements(engine.cache, className), createElement(element, forwarded));
|
|
19
|
+
};
|
|
20
|
+
return Object.assign(Styled, statics);
|
|
21
|
+
};
|
|
22
|
+
return withTagShorthands(styled);
|
|
23
|
+
}
|
|
24
|
+
export { createStyled };
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
let _umber_style_core = require("@umber-style/core");
|
|
2
|
+
const require_domProps = require("./domProps.cjs");
|
|
3
|
+
// src/styledParts.ts
|
|
4
|
+
/** Whether `tag` is a component made by `styled`. */
|
|
5
|
+
function isStyledComponent(tag) {
|
|
6
|
+
return typeof tag === "function" && "_styles" in tag;
|
|
7
|
+
}
|
|
8
|
+
/** The filter of the props of `element`: the one given, else for a string tag the attributes of HTML and SVG (a custom element, with a hyphen in its name, receives every prop); none for a component. */
|
|
9
|
+
function readFilter(element, given) {
|
|
10
|
+
if (typeof element !== "string") return void 0;
|
|
11
|
+
return given ?? (element.includes("-") ? void 0 : require_domProps.isDomProp);
|
|
12
|
+
}
|
|
13
|
+
/** What both builds of `styled` share: the styler, and the element and props that a component renders. */
|
|
14
|
+
function prepareStyled(engine, tag, options, styles) {
|
|
15
|
+
const inner = isStyledComponent(tag) ? tag : void 0;
|
|
16
|
+
const baseTag = inner?._tag ?? tag;
|
|
17
|
+
const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
|
|
18
|
+
const target = [inner?._target, options.target].filter(Boolean).join(" ") || void 0;
|
|
19
|
+
const styler = engine.createStyler(options, styles, inner?._styles);
|
|
20
|
+
const stylers = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
const readStyler = (cache) => {
|
|
22
|
+
const local = engine.withCache(cache);
|
|
23
|
+
if (local === engine) return styler;
|
|
24
|
+
let made = stylers.get(local);
|
|
25
|
+
if (made === void 0) {
|
|
26
|
+
made = local.createStyler(options, styles, inner?._styles);
|
|
27
|
+
stylers.set(local, made);
|
|
28
|
+
}
|
|
29
|
+
return made;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The element, its props and the engine class for `props`; `theme` is the view that function styles
|
|
33
|
+
* read. With `canHold` (the browser), a plain object in `style` makes a class that the element holds:
|
|
34
|
+
* `hold` counts it, and is undefined when there is nothing to hold. With `cache` (a cache provider's)
|
|
35
|
+
* the class is made by the engine over that cache, and written there.
|
|
36
|
+
*/
|
|
37
|
+
const resolve = (props, theme, canHold = false, cache) => {
|
|
38
|
+
const own = cache === void 0 ? styler : readStyler(cache);
|
|
39
|
+
const extra = [...(0, _umber_style_core.flattenStyle)(props.style), props.className];
|
|
40
|
+
const held = canHold && props.style ? own.getHeldClass(props, extra, theme) : void 0;
|
|
41
|
+
let className = held ? held.className : own.getClassName(props, extra, theme);
|
|
42
|
+
if (target) className = className ? `${className} ${target}` : target;
|
|
43
|
+
const element = props.as ?? baseTag;
|
|
44
|
+
const filter = readFilter(element, shouldForward);
|
|
45
|
+
const forwarded = {};
|
|
46
|
+
for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "className" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
|
|
47
|
+
forwarded.className = className || void 0;
|
|
48
|
+
return {
|
|
49
|
+
element,
|
|
50
|
+
forwarded,
|
|
51
|
+
className,
|
|
52
|
+
hold: held?.hold
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
styler,
|
|
57
|
+
resolve,
|
|
58
|
+
statics: {
|
|
59
|
+
_styles: styler.styles,
|
|
60
|
+
_tag: baseTag,
|
|
61
|
+
_shouldForwardProp: shouldForward,
|
|
62
|
+
_target: target,
|
|
63
|
+
toString: (0, _umber_style_core.createComponentSelector)(options.target, styler)
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
exports.prepareStyled = prepareStyled;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { createComponentSelector, flattenStyle } from "@umber-style/core";
|
|
2
|
+
import { isDomProp } from "./domProps.mjs";
|
|
3
|
+
// src/styledParts.ts
|
|
4
|
+
/** Whether `tag` is a component made by `styled`. */
|
|
5
|
+
function isStyledComponent(tag) {
|
|
6
|
+
return typeof tag === "function" && "_styles" in tag;
|
|
7
|
+
}
|
|
8
|
+
/** The filter of the props of `element`: the one given, else for a string tag the attributes of HTML and SVG (a custom element, with a hyphen in its name, receives every prop); none for a component. */
|
|
9
|
+
function readFilter(element, given) {
|
|
10
|
+
if (typeof element !== "string") return void 0;
|
|
11
|
+
return given ?? (element.includes("-") ? void 0 : isDomProp);
|
|
12
|
+
}
|
|
13
|
+
/** What both builds of `styled` share: the styler, and the element and props that a component renders. */
|
|
14
|
+
function prepareStyled(engine, tag, options, styles) {
|
|
15
|
+
const inner = isStyledComponent(tag) ? tag : void 0;
|
|
16
|
+
const baseTag = inner?._tag ?? tag;
|
|
17
|
+
const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
|
|
18
|
+
const target = [inner?._target, options.target].filter(Boolean).join(" ") || void 0;
|
|
19
|
+
const styler = engine.createStyler(options, styles, inner?._styles);
|
|
20
|
+
const stylers = /* @__PURE__ */ new WeakMap();
|
|
21
|
+
const readStyler = (cache) => {
|
|
22
|
+
const local = engine.withCache(cache);
|
|
23
|
+
if (local === engine) return styler;
|
|
24
|
+
let made = stylers.get(local);
|
|
25
|
+
if (made === void 0) {
|
|
26
|
+
made = local.createStyler(options, styles, inner?._styles);
|
|
27
|
+
stylers.set(local, made);
|
|
28
|
+
}
|
|
29
|
+
return made;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* The element, its props and the engine class for `props`; `theme` is the view that function styles
|
|
33
|
+
* read. With `canHold` (the browser), a plain object in `style` makes a class that the element holds:
|
|
34
|
+
* `hold` counts it, and is undefined when there is nothing to hold. With `cache` (a cache provider's)
|
|
35
|
+
* the class is made by the engine over that cache, and written there.
|
|
36
|
+
*/
|
|
37
|
+
const resolve = (props, theme, canHold = false, cache) => {
|
|
38
|
+
const own = cache === void 0 ? styler : readStyler(cache);
|
|
39
|
+
const extra = [...flattenStyle(props.style), props.className];
|
|
40
|
+
const held = canHold && props.style ? own.getHeldClass(props, extra, theme) : void 0;
|
|
41
|
+
let className = held ? held.className : own.getClassName(props, extra, theme);
|
|
42
|
+
if (target) className = className ? `${className} ${target}` : target;
|
|
43
|
+
const element = props.as ?? baseTag;
|
|
44
|
+
const filter = readFilter(element, shouldForward);
|
|
45
|
+
const forwarded = {};
|
|
46
|
+
for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "className" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
|
|
47
|
+
forwarded.className = className || void 0;
|
|
48
|
+
return {
|
|
49
|
+
element,
|
|
50
|
+
forwarded,
|
|
51
|
+
className,
|
|
52
|
+
hold: held?.hold
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
return {
|
|
56
|
+
styler,
|
|
57
|
+
resolve,
|
|
58
|
+
statics: {
|
|
59
|
+
_styles: styler.styles,
|
|
60
|
+
_tag: baseTag,
|
|
61
|
+
_shouldForwardProp: shouldForward,
|
|
62
|
+
_target: target,
|
|
63
|
+
toString: createComponentSelector(options.target, styler)
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export { prepareStyled };
|