@umber-style/react 0.0.0-stage → 0.1.0-alpha.0
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 +10 -2
- package/dist/ThemeProvider.cjs +37 -0
- package/dist/ThemeProvider.mjs +37 -0
- package/dist/ThemeProvider.server.cjs +35 -0
- package/dist/ThemeProvider.server.mjs +35 -0
- package/dist/ThemeValue.cjs +32 -0
- package/dist/ThemeValue.mjs +32 -0
- package/dist/createStyle.cjs +56 -0
- package/dist/createStyle.d.cts +7 -0
- package/dist/createStyle.d.mts +7 -0
- package/dist/createStyle.mjs +55 -0
- package/dist/createStyle.server.cjs +53 -0
- package/dist/createStyle.server.d.cts +8 -0
- package/dist/createStyle.server.d.mts +8 -0
- package/dist/createStyle.server.mjs +52 -0
- package/dist/defaultStyle.cjs +44 -0
- package/dist/defaultStyle.d.cts +22 -0
- package/dist/defaultStyle.d.mts +22 -0
- package/dist/defaultStyle.mjs +44 -0
- package/dist/defaultStyle.server.cjs +44 -0
- package/dist/defaultStyle.server.d.cts +22 -0
- package/dist/defaultStyle.server.d.mts +22 -0
- package/dist/defaultStyle.server.mjs +44 -0
- package/dist/index.cjs +61 -0
- package/dist/index.d.cts +18 -0
- package/dist/index.d.mts +18 -0
- package/dist/index.mjs +5 -0
- package/dist/index.server.cjs +61 -0
- package/dist/index.server.d.cts +18 -0
- package/dist/index.server.d.mts +18 -0
- package/dist/index.server.mjs +5 -0
- package/dist/serverContext.cjs +32 -0
- package/dist/serverContext.d.cts +2 -0
- package/dist/serverContext.d.mts +3 -0
- package/dist/serverContext.mjs +31 -0
- package/dist/styleElements.cjs +33 -0
- package/dist/styleElements.mjs +32 -0
- package/dist/styled.cjs +30 -0
- package/dist/styled.mjs +30 -0
- package/dist/styled.server.cjs +22 -0
- package/dist/styled.server.mjs +22 -0
- package/dist/styledParts.cjs +37 -0
- package/dist/styledParts.mjs +37 -0
- package/dist/types.d.cts +101 -0
- package/dist/types.d.mts +101 -0
- package/package.json +90 -3
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,3 +1,11 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @umber-style/react
|
|
2
2
|
|
|
3
|
-
|
|
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,37 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
3
|
+
// src/ThemeProvider.tsx
|
|
4
|
+
/**
|
|
5
|
+
* Makes the web `ThemeProvider`: an element that carries the theme class, around the part that holds
|
|
6
|
+
* the real values. With `isRoot` it renders no element, no class and no rule: it gives the engine its
|
|
7
|
+
* base theme and feeds the context. The app puts `themeClass(theme, colorScheme)` on an ancestor of
|
|
8
|
+
* its own, usually `<html>`, which makes the rule, so that the class can change without a render. Why:
|
|
9
|
+
* the wrapper of a normal provider declares the whole theme below the element that has the class, and
|
|
10
|
+
* so hides a class that the app changes above it. On a server, where the build renders style elements,
|
|
11
|
+
* the provider sends those of its theme class and the scope class (a root provider: of the theme classes
|
|
12
|
+
* already made for its theme), and the stamp of the cache options.
|
|
13
|
+
*/
|
|
14
|
+
function createThemeProvider({ learnTheme, themeClass, scopeClass, ThemeValueProvider, getServerStyle }) {
|
|
15
|
+
return function ThemeProvider({ theme, colorScheme, className, as: Element = "div", isRoot, children }) {
|
|
16
|
+
const child = /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ThemeValueProvider, {
|
|
17
|
+
theme,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
const server = getServerStyle();
|
|
21
|
+
if (isRoot) {
|
|
22
|
+
learnTheme(theme);
|
|
23
|
+
if (server === void 0) return child;
|
|
24
|
+
const made = server.createStyleElements(server.findThemeIds(theme).join(" "));
|
|
25
|
+
return (0, react.createElement)(react.Fragment, null, server.createStamp(), ...made, child);
|
|
26
|
+
}
|
|
27
|
+
const scope = [
|
|
28
|
+
themeClass(theme, colorScheme),
|
|
29
|
+
scopeClass(),
|
|
30
|
+
className
|
|
31
|
+
].filter(Boolean).join(" ");
|
|
32
|
+
const element = (0, react.createElement)(Element, { className: scope }, child);
|
|
33
|
+
if (server === void 0) return element;
|
|
34
|
+
return (0, react.createElement)(react.Fragment, null, server.createStamp(), ...server.createStyleElements(server.themeId(theme, colorScheme)), ...server.createStyleElements(scopeClass()), element);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
exports.createThemeProvider = createThemeProvider;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Fragment, createElement } from "react";
|
|
2
|
+
import { jsx } from "react/jsx-runtime";
|
|
3
|
+
// src/ThemeProvider.tsx
|
|
4
|
+
/**
|
|
5
|
+
* Makes the web `ThemeProvider`: an element that carries the theme class, around the part that holds
|
|
6
|
+
* the real values. With `isRoot` it renders no element, no class and no rule: it gives the engine its
|
|
7
|
+
* base theme and feeds the context. The app puts `themeClass(theme, colorScheme)` on an ancestor of
|
|
8
|
+
* its own, usually `<html>`, which makes the rule, so that the class can change without a render. Why:
|
|
9
|
+
* the wrapper of a normal provider declares the whole theme below the element that has the class, and
|
|
10
|
+
* so hides a class that the app changes above it. On a server, where the build renders style elements,
|
|
11
|
+
* the provider sends those of its theme class and the scope class (a root provider: of the theme classes
|
|
12
|
+
* already made for its theme), and the stamp of the cache options.
|
|
13
|
+
*/
|
|
14
|
+
function createThemeProvider({ learnTheme, themeClass, scopeClass, ThemeValueProvider, getServerStyle }) {
|
|
15
|
+
return function ThemeProvider({ theme, colorScheme, className, as: Element = "div", isRoot, children }) {
|
|
16
|
+
const child = /* @__PURE__ */ jsx(ThemeValueProvider, {
|
|
17
|
+
theme,
|
|
18
|
+
children
|
|
19
|
+
});
|
|
20
|
+
const server = getServerStyle();
|
|
21
|
+
if (isRoot) {
|
|
22
|
+
learnTheme(theme);
|
|
23
|
+
if (server === void 0) return child;
|
|
24
|
+
const made = server.createStyleElements(server.findThemeIds(theme).join(" "));
|
|
25
|
+
return createElement(Fragment, null, server.createStamp(), ...made, child);
|
|
26
|
+
}
|
|
27
|
+
const scope = [
|
|
28
|
+
themeClass(theme, colorScheme),
|
|
29
|
+
scopeClass(),
|
|
30
|
+
className
|
|
31
|
+
].filter(Boolean).join(" ");
|
|
32
|
+
const element = createElement(Element, { className: scope }, child);
|
|
33
|
+
if (server === void 0) return element;
|
|
34
|
+
return createElement(Fragment, null, server.createStamp(), ...server.createStyleElements(server.themeId(theme, colorScheme)), ...server.createStyleElements(scopeClass()), element);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export { createThemeProvider };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
let react = require("react");
|
|
2
|
+
const require_styleElements = require("./styleElements.cjs");
|
|
3
|
+
// src/ThemeProvider.server.ts
|
|
4
|
+
/**
|
|
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.
|
|
12
|
+
*/
|
|
13
|
+
function createThemeProvider(engine, root, stamp) {
|
|
14
|
+
return function ThemeProvider({ theme, colorScheme, className, as = "div", isRoot, children }) {
|
|
15
|
+
const provided = (0, react.createElement)(root.Provider, { value: theme }, children);
|
|
16
|
+
const slot = stamp.slot();
|
|
17
|
+
const stamped = slot.isSet ? [] : [require_styleElements.createStampElement(engine.cache)];
|
|
18
|
+
slot.isSet = true;
|
|
19
|
+
if (isRoot) {
|
|
20
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
21
|
+
const made = require_styleElements.createStyleElements(engine.cache, engine.findThemeIds(theme).join(" "));
|
|
22
|
+
return (0, react.createElement)(react.Fragment, null, ...stamped, ...made, provided);
|
|
23
|
+
}
|
|
24
|
+
const scope = engine.themeClass(theme, colorScheme);
|
|
25
|
+
const styles = require_styleElements.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 (0, react.createElement)(react.Fragment, null, ...stamped, ...styles, ...require_styleElements.createStyleElements(engine.cache, scoped), (0, react.createElement)(as, props, provided));
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
exports.createThemeProvider = createThemeProvider;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Fragment, createElement } from "react";
|
|
2
|
+
import { createStampElement, createStyleElements } from "./styleElements.mjs";
|
|
3
|
+
// src/ThemeProvider.server.ts
|
|
4
|
+
/**
|
|
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.
|
|
12
|
+
*/
|
|
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;
|
|
19
|
+
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);
|
|
23
|
+
}
|
|
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));
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
export { createThemeProvider };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
let react = require("react");
|
|
3
|
+
let _umber_style_core = require("@umber-style/core");
|
|
4
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
5
|
+
// 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
|
+
});
|
|
16
|
+
}
|
|
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;
|
|
20
|
+
}
|
|
21
|
+
function useTheme() {
|
|
22
|
+
const value = useThemeValue();
|
|
23
|
+
if (value === void 0) throw new Error(_umber_style_core.NO_THEME_MESSAGE);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
ThemeValueProvider,
|
|
28
|
+
useTheme,
|
|
29
|
+
useThemeValue
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
exports.createThemeValue = createThemeValue;
|
|
@@ -0,0 +1,32 @@
|
|
|
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";
|
|
5
|
+
// 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
|
+
});
|
|
16
|
+
}
|
|
17
|
+
/** The theme above, or the base theme, or undefined when there is none. */
|
|
18
|
+
function useThemeValue() {
|
|
19
|
+
return useContext(ThemeContext) ?? holder.baseTheme;
|
|
20
|
+
}
|
|
21
|
+
function useTheme() {
|
|
22
|
+
const value = useThemeValue();
|
|
23
|
+
if (value === void 0) throw new Error(NO_THEME_MESSAGE);
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
ThemeValueProvider,
|
|
28
|
+
useTheme,
|
|
29
|
+
useThemeValue
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export { createThemeValue };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
let _umber_style_core = require("@umber-style/core");
|
|
2
|
+
const require_styleElements = require("./styleElements.cjs");
|
|
3
|
+
const require_styled = require("./styled.cjs");
|
|
4
|
+
const require_ThemeProvider = require("./ThemeProvider.cjs");
|
|
5
|
+
const require_ThemeValue = require("./ThemeValue.cjs");
|
|
6
|
+
// 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
|
+
const hasStyleElements = () => hasServerStyleElements && typeof document === "undefined";
|
|
11
|
+
const renderStyleElements = (className) => hasStyleElements() ? require_styleElements.createStyleElements(engine.cache, className) : [];
|
|
12
|
+
const useThemeView = () => engine.createThemeView(useTheme());
|
|
13
|
+
const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
|
|
14
|
+
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);
|
|
18
|
+
});
|
|
19
|
+
function create(styles) {
|
|
20
|
+
return engine.createSheet(styles);
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
engine,
|
|
24
|
+
cache: engine.cache,
|
|
25
|
+
styled: require_styled.createStyled(engine, useThemeView, renderStyleElements),
|
|
26
|
+
StyleSheet: { create },
|
|
27
|
+
useStyle,
|
|
28
|
+
apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
|
|
29
|
+
ThemeProvider: require_ThemeProvider.createThemeProvider({
|
|
30
|
+
getServerStyle: () => hasStyleElements() ? {
|
|
31
|
+
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
|
|
42
|
+
}),
|
|
43
|
+
useTheme,
|
|
44
|
+
themeClass,
|
|
45
|
+
when: engine.when,
|
|
46
|
+
whenScheme: engine.whenScheme
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Makes the React styling of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
50
|
+
function createStyle(theme, options = {}) {
|
|
51
|
+
const engine = options.engine ?? (0, _umber_style_core.createEngine)(theme, options);
|
|
52
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
53
|
+
return bindEngine(engine, options);
|
|
54
|
+
}
|
|
55
|
+
exports.bindEngine = bindEngine;
|
|
56
|
+
exports.createStyle = createStyle;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Engine } from "@umber-style/core";
|
|
2
|
+
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 of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
6
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): WebStyle<T>;
|
|
7
|
+
export type { StyleOptions };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Engine } from "@umber-style/core";
|
|
2
|
+
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 of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
6
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): WebStyle<T>;
|
|
7
|
+
export type { StyleOptions };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { createEngine, isThemedSheet } from "@umber-style/core";
|
|
2
|
+
import { createStampElement, createStyleElements } from "./styleElements.mjs";
|
|
3
|
+
import { createStyled } from "./styled.mjs";
|
|
4
|
+
import { createThemeProvider } from "./ThemeProvider.mjs";
|
|
5
|
+
import { createThemeValue } from "./ThemeValue.mjs";
|
|
6
|
+
// 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
|
+
const hasStyleElements = () => hasServerStyleElements && typeof document === "undefined";
|
|
11
|
+
const renderStyleElements = (className) => hasStyleElements() ? createStyleElements(engine.cache, className) : [];
|
|
12
|
+
const useThemeView = () => engine.createThemeView(useTheme());
|
|
13
|
+
const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
|
|
14
|
+
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);
|
|
18
|
+
});
|
|
19
|
+
function create(styles) {
|
|
20
|
+
return engine.createSheet(styles);
|
|
21
|
+
}
|
|
22
|
+
return {
|
|
23
|
+
engine,
|
|
24
|
+
cache: engine.cache,
|
|
25
|
+
styled: createStyled(engine, useThemeView, renderStyleElements),
|
|
26
|
+
StyleSheet: { create },
|
|
27
|
+
useStyle,
|
|
28
|
+
apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
|
|
29
|
+
ThemeProvider: createThemeProvider({
|
|
30
|
+
getServerStyle: () => hasStyleElements() ? {
|
|
31
|
+
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
|
|
42
|
+
}),
|
|
43
|
+
useTheme,
|
|
44
|
+
themeClass,
|
|
45
|
+
when: engine.when,
|
|
46
|
+
whenScheme: engine.whenScheme
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Makes the React styling of one theme shape, apart from the default one: its own engine, or the one given. */
|
|
50
|
+
function createStyle(theme, options = {}) {
|
|
51
|
+
const engine = options.engine ?? createEngine(theme, options);
|
|
52
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
53
|
+
return bindEngine(engine, options);
|
|
54
|
+
}
|
|
55
|
+
export { bindEngine, createStyle };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
let _umber_style_core = require("@umber-style/core");
|
|
2
|
+
const require_serverContext = require("./serverContext.cjs");
|
|
3
|
+
const require_styled_server = require("./styled.server.cjs");
|
|
4
|
+
const require_ThemeProvider_server = require("./ThemeProvider.server.cjs");
|
|
5
|
+
// src/createStyle.server.ts
|
|
6
|
+
/** Makes the styling for server components over an engine; `cache` makes the slot of a request, `React.cache` unless a test gives one. */
|
|
7
|
+
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;
|
|
18
|
+
}
|
|
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;
|
|
26
|
+
};
|
|
27
|
+
const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
|
|
28
|
+
const useStyle = ((sheet) => (0, _umber_style_core.isThemedSheet)(sheet) ? sheet.resolve(engine.createThemeView(readTheme())) : sheet);
|
|
29
|
+
function create(styles) {
|
|
30
|
+
return engine.createSheet(styles);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
engine,
|
|
34
|
+
cache: engine.cache,
|
|
35
|
+
styled: require_styled_server.createStyled(engine, readTheme),
|
|
36
|
+
StyleSheet: { create },
|
|
37
|
+
useStyle,
|
|
38
|
+
apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
|
|
39
|
+
ThemeProvider: require_ThemeProvider_server.createThemeProvider(engine, root, stamp),
|
|
40
|
+
readTheme,
|
|
41
|
+
themeClass,
|
|
42
|
+
when: engine.when,
|
|
43
|
+
whenScheme: engine.whenScheme
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Makes the styling for server components of one theme shape: its own engine, or the one given. */
|
|
47
|
+
function createStyle(theme, options = {}) {
|
|
48
|
+
const engine = options.engine ?? (0, _umber_style_core.createEngine)(theme, options);
|
|
49
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
50
|
+
return bindEngine(engine);
|
|
51
|
+
}
|
|
52
|
+
exports.bindEngine = bindEngine;
|
|
53
|
+
exports.createStyle = createStyle;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Engine } from "@umber-style/core";
|
|
2
|
+
import { CacheFunction } from "./serverContext.cjs";
|
|
3
|
+
import { ServerStyle, StyleOptions } from "./types.cjs";
|
|
4
|
+
/** Makes the styling for server components over an engine; `cache` makes the slot of a request, `React.cache` unless a test gives one. */
|
|
5
|
+
export declare function bindEngine<T extends object>(engine: Engine<T>, cache?: CacheFunction): ServerStyle<T>;
|
|
6
|
+
/** Makes the styling for server components of one theme shape: its own engine, or the one given. */
|
|
7
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): ServerStyle<T>;
|
|
8
|
+
export type { StyleOptions };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Engine } from "@umber-style/core";
|
|
2
|
+
import { CacheFunction } from "./serverContext.mjs";
|
|
3
|
+
import { ServerStyle, StyleOptions } from "./types.mjs";
|
|
4
|
+
/** Makes the styling for server components over an engine; `cache` makes the slot of a request, `React.cache` unless a test gives one. */
|
|
5
|
+
export declare function bindEngine<T extends object>(engine: Engine<T>, cache?: CacheFunction): ServerStyle<T>;
|
|
6
|
+
/** Makes the styling for server components of one theme shape: its own engine, or the one given. */
|
|
7
|
+
export declare function createStyle<T extends object>(theme: T, options?: StyleOptions<T>): ServerStyle<T>;
|
|
8
|
+
export type { StyleOptions };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { NO_THEME_MESSAGE, createEngine, isThemedSheet, mergeDeep } from "@umber-style/core";
|
|
2
|
+
import { createServerOnlyContext, readServerContext } from "./serverContext.mjs";
|
|
3
|
+
import { createStyled } from "./styled.server.mjs";
|
|
4
|
+
import { createThemeProvider } from "./ThemeProvider.server.mjs";
|
|
5
|
+
// src/createStyle.server.ts
|
|
6
|
+
/** Makes the styling for server components over an engine; `cache` makes the slot of a request, `React.cache` unless a test gives one. */
|
|
7
|
+
function bindEngine(engine, cache) {
|
|
8
|
+
const root = createServerOnlyContext(void 0, cache);
|
|
9
|
+
const stamp = 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 = readServerContext(root);
|
|
14
|
+
const base = engine.baseTheme;
|
|
15
|
+
if (rootTheme === void 0) {
|
|
16
|
+
if (base === void 0) throw new Error(NO_THEME_MESSAGE);
|
|
17
|
+
return base;
|
|
18
|
+
}
|
|
19
|
+
if (rootTheme === base) return rootTheme;
|
|
20
|
+
let theme = merged.get(rootTheme);
|
|
21
|
+
if (theme === void 0) {
|
|
22
|
+
theme = mergeDeep(base, rootTheme);
|
|
23
|
+
merged.set(rootTheme, theme);
|
|
24
|
+
}
|
|
25
|
+
return theme;
|
|
26
|
+
};
|
|
27
|
+
const themeClass = (partial, colorScheme) => engine.themeClass(partial, colorScheme);
|
|
28
|
+
const useStyle = ((sheet) => isThemedSheet(sheet) ? sheet.resolve(engine.createThemeView(readTheme())) : sheet);
|
|
29
|
+
function create(styles) {
|
|
30
|
+
return engine.createSheet(styles);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
engine,
|
|
34
|
+
cache: engine.cache,
|
|
35
|
+
styled: createStyled(engine, readTheme),
|
|
36
|
+
StyleSheet: { create },
|
|
37
|
+
useStyle,
|
|
38
|
+
apply: (...entries) => ({ className: engine.css(...entries) || void 0 }),
|
|
39
|
+
ThemeProvider: createThemeProvider(engine, root, stamp),
|
|
40
|
+
readTheme,
|
|
41
|
+
themeClass,
|
|
42
|
+
when: engine.when,
|
|
43
|
+
whenScheme: engine.whenScheme
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Makes the styling for server components of one theme shape: its own engine, or the one given. */
|
|
47
|
+
function createStyle(theme, options = {}) {
|
|
48
|
+
const engine = options.engine ?? createEngine(theme, options);
|
|
49
|
+
if (!engine.hasTheme) engine.setTheme(theme);
|
|
50
|
+
return bindEngine(engine);
|
|
51
|
+
}
|
|
52
|
+
export { bindEngine, createStyle };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
let _umber_style_core = require("@umber-style/core");
|
|
2
|
+
const require_createStyle = require("./createStyle.cjs");
|
|
3
|
+
// src/defaultStyle.ts
|
|
4
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
5
|
+
function createDefaultStyle() {
|
|
6
|
+
let pending = {};
|
|
7
|
+
let bound;
|
|
8
|
+
const getStyle = () => {
|
|
9
|
+
if (!bound) {
|
|
10
|
+
const engine = pending.engine ?? (0, _umber_style_core.createLazyEngine)(pending);
|
|
11
|
+
if (pending.theme) engine.setTheme(pending.theme);
|
|
12
|
+
bound = require_createStyle.bindEngine(engine, pending);
|
|
13
|
+
}
|
|
14
|
+
return bound;
|
|
15
|
+
};
|
|
16
|
+
/** Sets the options of the default engine, and gives it a base theme or an engine to share. Throws once the default style was used. */
|
|
17
|
+
function configure(options) {
|
|
18
|
+
if (bound) throw new Error("configure must be called before the default style is used.");
|
|
19
|
+
pending = options;
|
|
20
|
+
}
|
|
21
|
+
const styled = ((tag, options) => getStyle().styled(tag, options));
|
|
22
|
+
const StyleSheet = { create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : (0, _umber_style_core.createDeferredRecord)(styles, () => getStyle().StyleSheet.create(styles))) };
|
|
23
|
+
const useStyle = ((sheet) => getStyle().useStyle(sheet));
|
|
24
|
+
function useTheme() {
|
|
25
|
+
return getStyle().useTheme();
|
|
26
|
+
}
|
|
27
|
+
function ThemeProvider(props) {
|
|
28
|
+
return getStyle().ThemeProvider(props);
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
configure,
|
|
32
|
+
getEngine: () => getStyle().engine,
|
|
33
|
+
styled,
|
|
34
|
+
StyleSheet,
|
|
35
|
+
useStyle,
|
|
36
|
+
useTheme,
|
|
37
|
+
ThemeProvider,
|
|
38
|
+
apply: ((...entries) => getStyle().apply(...entries)),
|
|
39
|
+
when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
|
|
40
|
+
whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
|
|
41
|
+
themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
exports.createDefaultStyle = createDefaultStyle;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DefaultStyleOptions, SheetDefinition, Styled, WebStyle, WebThemeProviderProps } from "./types.cjs";
|
|
2
|
+
import { Theme, WebStyleObject } from "@umber-style/core";
|
|
3
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
4
|
+
export declare function createDefaultStyle(): {
|
|
5
|
+
configure: (options: DefaultStyleOptions) => void;
|
|
6
|
+
getEngine: () => import("@umber-style/core").Engine<Theme>;
|
|
7
|
+
styled: Styled<Theme>;
|
|
8
|
+
StyleSheet: {
|
|
9
|
+
create<K extends string>(styles: import("@umber-style/core").ObjectSheet<K>): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
10
|
+
create<K extends string>(styles: (theme: Theme) => Record<K, WebStyleObject>): SheetDefinition<Record<K, WebStyleObject>, import("@umber-style/core").SheetEntry>;
|
|
11
|
+
};
|
|
12
|
+
useStyle: {
|
|
13
|
+
<K extends string>(sheet: { readonly [P in K]: import("@umber-style/core").SheetEntry; }): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
14
|
+
<S extends import("@umber-style/core").SheetStyles>(sheet: SheetDefinition<S, import("@umber-style/core").SheetEntry>): { [K in keyof S]: import("@umber-style/core").SheetEntry; };
|
|
15
|
+
};
|
|
16
|
+
useTheme: () => Theme;
|
|
17
|
+
ThemeProvider: (props: WebThemeProviderProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
18
|
+
apply: WebStyle<Theme>['apply'];
|
|
19
|
+
when: WebStyle<Theme>['when'];
|
|
20
|
+
whenScheme: WebStyle<Theme>['whenScheme'];
|
|
21
|
+
themeClass: WebStyle<Theme>['themeClass'];
|
|
22
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { DefaultStyleOptions, SheetDefinition, Styled, WebStyle, WebThemeProviderProps } from "./types.mjs";
|
|
2
|
+
import { Theme, WebStyleObject } from "@umber-style/core";
|
|
3
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
4
|
+
export declare function createDefaultStyle(): {
|
|
5
|
+
configure: (options: DefaultStyleOptions) => void;
|
|
6
|
+
getEngine: () => import("@umber-style/core").Engine<Theme>;
|
|
7
|
+
styled: Styled<Theme>;
|
|
8
|
+
StyleSheet: {
|
|
9
|
+
create<K extends string>(styles: import("@umber-style/core").ObjectSheet<K>): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
10
|
+
create<K extends string>(styles: (theme: Theme) => Record<K, WebStyleObject>): SheetDefinition<Record<K, WebStyleObject>, import("@umber-style/core").SheetEntry>;
|
|
11
|
+
};
|
|
12
|
+
useStyle: {
|
|
13
|
+
<K extends string>(sheet: { readonly [P in K]: import("@umber-style/core").SheetEntry; }): { readonly [P in K]: import("@umber-style/core").SheetEntry; };
|
|
14
|
+
<S extends import("@umber-style/core").SheetStyles>(sheet: SheetDefinition<S, import("@umber-style/core").SheetEntry>): { [K in keyof S]: import("@umber-style/core").SheetEntry; };
|
|
15
|
+
};
|
|
16
|
+
useTheme: () => Theme;
|
|
17
|
+
ThemeProvider: (props: WebThemeProviderProps<Theme>) => import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>>;
|
|
18
|
+
apply: WebStyle<Theme>['apply'];
|
|
19
|
+
when: WebStyle<Theme>['when'];
|
|
20
|
+
whenScheme: WebStyle<Theme>['whenScheme'];
|
|
21
|
+
themeClass: WebStyle<Theme>['themeClass'];
|
|
22
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createDeferredRecord, createLazyEngine } from "@umber-style/core";
|
|
2
|
+
import { bindEngine } from "./createStyle.mjs";
|
|
3
|
+
// src/defaultStyle.ts
|
|
4
|
+
/** Makes the default styling: one engine, made on first use, behind `styled`, `ThemeProvider` and the rest. */
|
|
5
|
+
function createDefaultStyle() {
|
|
6
|
+
let pending = {};
|
|
7
|
+
let bound;
|
|
8
|
+
const getStyle = () => {
|
|
9
|
+
if (!bound) {
|
|
10
|
+
const engine = pending.engine ?? createLazyEngine(pending);
|
|
11
|
+
if (pending.theme) engine.setTheme(pending.theme);
|
|
12
|
+
bound = bindEngine(engine, pending);
|
|
13
|
+
}
|
|
14
|
+
return bound;
|
|
15
|
+
};
|
|
16
|
+
/** Sets the options of the default engine, and gives it a base theme or an engine to share. Throws once the default style was used. */
|
|
17
|
+
function configure(options) {
|
|
18
|
+
if (bound) throw new Error("configure must be called before the default style is used.");
|
|
19
|
+
pending = options;
|
|
20
|
+
}
|
|
21
|
+
const styled = ((tag, options) => getStyle().styled(tag, options));
|
|
22
|
+
const StyleSheet = { create: ((styles) => typeof styles === "function" ? getStyle().StyleSheet.create(styles) : createDeferredRecord(styles, () => getStyle().StyleSheet.create(styles))) };
|
|
23
|
+
const useStyle = ((sheet) => getStyle().useStyle(sheet));
|
|
24
|
+
function useTheme() {
|
|
25
|
+
return getStyle().useTheme();
|
|
26
|
+
}
|
|
27
|
+
function ThemeProvider(props) {
|
|
28
|
+
return getStyle().ThemeProvider(props);
|
|
29
|
+
}
|
|
30
|
+
return {
|
|
31
|
+
configure,
|
|
32
|
+
getEngine: () => getStyle().engine,
|
|
33
|
+
styled,
|
|
34
|
+
StyleSheet,
|
|
35
|
+
useStyle,
|
|
36
|
+
useTheme,
|
|
37
|
+
ThemeProvider,
|
|
38
|
+
apply: ((...entries) => getStyle().apply(...entries)),
|
|
39
|
+
when: ((theme, colorScheme) => getStyle().when(theme, colorScheme)),
|
|
40
|
+
whenScheme: ((scheme) => getStyle().whenScheme(scheme)),
|
|
41
|
+
themeClass: ((theme, colorScheme) => getStyle().themeClass(theme, colorScheme))
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export { createDefaultStyle };
|