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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/dist/CacheProvider.cjs +23 -0
  2. package/dist/CacheProvider.d.cts +10 -0
  3. package/dist/CacheProvider.d.mts +11 -0
  4. package/dist/CacheProvider.mjs +22 -0
  5. package/dist/Global.cjs +52 -0
  6. package/dist/Global.mjs +52 -0
  7. package/dist/Global.server.cjs +27 -0
  8. package/dist/Global.server.mjs +27 -0
  9. package/dist/GlobalRelease.cjs +32 -0
  10. package/dist/GlobalRelease.mjs +30 -0
  11. package/dist/ProviderScope.cjs +201 -0
  12. package/dist/ProviderScope.d.cts +14 -0
  13. package/dist/ProviderScope.d.mts +15 -0
  14. package/dist/ProviderScope.mjs +190 -0
  15. package/dist/ThemeProvider.cjs +94 -27
  16. package/dist/ThemeProvider.mjs +95 -28
  17. package/dist/ThemeProvider.server.cjs +130 -25
  18. package/dist/ThemeProvider.server.mjs +131 -26
  19. package/dist/ThemeValue.cjs +52 -20
  20. package/dist/ThemeValue.d.cts +3 -0
  21. package/dist/ThemeValue.d.mts +4 -0
  22. package/dist/ThemeValue.mjs +53 -23
  23. package/dist/_virtual/_rolldown/runtime.cjs +22 -0
  24. package/dist/createStyle.cjs +30 -20
  25. package/dist/createStyle.d.cts +4 -2
  26. package/dist/createStyle.d.mts +4 -2
  27. package/dist/createStyle.mjs +32 -22
  28. package/dist/createStyle.server.cjs +29 -22
  29. package/dist/createStyle.server.mjs +31 -24
  30. package/dist/defaultStyle.cjs +27 -3
  31. package/dist/defaultStyle.d.cts +18 -11
  32. package/dist/defaultStyle.d.mts +18 -11
  33. package/dist/defaultStyle.mjs +28 -4
  34. package/dist/defaultStyle.server.cjs +13 -5
  35. package/dist/defaultStyle.server.d.cts +16 -11
  36. package/dist/defaultStyle.server.d.mts +16 -11
  37. package/dist/defaultStyle.server.mjs +14 -6
  38. package/dist/domProps.cjs +27 -0
  39. package/dist/domProps.mjs +27 -0
  40. package/dist/index.cjs +12 -1
  41. package/dist/index.d.cts +17 -7
  42. package/dist/index.d.mts +17 -7
  43. package/dist/index.mjs +4 -2
  44. package/dist/index.server.cjs +6 -1
  45. package/dist/index.server.d.cts +15 -7
  46. package/dist/index.server.d.mts +15 -7
  47. package/dist/index.server.mjs +2 -2
  48. package/dist/plainTheme.cjs +49 -0
  49. package/dist/plainTheme.mjs +49 -0
  50. package/dist/serverContext.cjs +6 -24
  51. package/dist/serverContext.d.mts +0 -1
  52. package/dist/serverContext.mjs +6 -23
  53. package/dist/styleElements.cjs +75 -9
  54. package/dist/styleElements.d.cts +1 -0
  55. package/dist/styleElements.d.mts +2 -0
  56. package/dist/styleElements.mjs +73 -10
  57. package/dist/styled.cjs +32 -11
  58. package/dist/styled.mjs +33 -12
  59. package/dist/styled.server.cjs +6 -4
  60. package/dist/styled.server.mjs +6 -4
  61. package/dist/styledParts.cjs +36 -6
  62. package/dist/styledParts.mjs +37 -7
  63. package/dist/types.d.cts +225 -19
  64. package/dist/types.d.mts +225 -19
  65. package/dist/useMediaQuery.cjs +20 -0
  66. package/dist/useMediaQuery.d.cts +13 -0
  67. package/dist/useMediaQuery.d.mts +13 -0
  68. package/dist/useMediaQuery.mjs +20 -0
  69. package/package.json +3 -3
package/dist/styled.cjs CHANGED
@@ -1,30 +1,51 @@
1
1
  let react = require("react");
2
+ let _umber_style_core = require("@umber-style/core");
3
+ const require_CacheProvider = require("./CacheProvider.cjs");
2
4
  const require_styledParts = require("./styledParts.cjs");
3
5
  // src/styled.ts
4
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
+ /**
5
16
  * Makes `styled` over an engine. A component with a function style reads the theme of the nearest
6
- * provider through `useThemeView`; one with only object styles reads no context. `createStyleElements`
7
- * gives the style elements of a class where the build sends them (a server render), and none in the browser.
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.
8
23
  */
9
24
  function createStyled(engine, useThemeView, createStyleElements) {
10
- /** The element, with the style elements of its class in front of it where there are any. */
11
- const render = (element, forwarded, className) => {
12
- const styles = createStyleElements(className);
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) => {
13
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);
14
30
  return styles.length === 0 ? rendered : (0, react.createElement)(react.Fragment, null, ...styles, rendered);
15
31
  };
16
- const styled = (tag, options = {}) => (...styles) => {
32
+ const canHold = () => typeof document !== "undefined";
33
+ const styled = (tag, options = {}) => (...args) => {
34
+ const styles = (0, _umber_style_core.readStyleArguments)(args);
17
35
  const { styler, resolve, statics } = require_styledParts.prepareStyled(engine, tag, options, styles);
18
36
  const StaticStyled = (props) => {
19
- const { element, forwarded, className } = resolve(props);
20
- return render(element, forwarded, className);
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);
21
40
  };
22
41
  const DynamicStyled = (props) => {
23
- const { element, forwarded, className } = resolve(props, useThemeView());
24
- return render(element, forwarded, className);
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);
25
46
  };
26
47
  return Object.assign(styler.isDynamic ? DynamicStyled : StaticStyled, statics);
27
48
  };
28
- return styled;
49
+ return (0, _umber_style_core.withTagShorthands)(styled);
29
50
  }
30
51
  exports.createStyled = createStyled;
package/dist/styled.mjs CHANGED
@@ -1,30 +1,51 @@
1
- import { Fragment, createElement } from "react";
1
+ import { Fragment, createElement, useInsertionEffect } from "react";
2
+ import { readStyleArguments, withTagShorthands } from "@umber-style/core";
3
+ import { useProvidedCache } from "./CacheProvider.mjs";
2
4
  import { prepareStyled } from "./styledParts.mjs";
3
5
  // src/styled.ts
4
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
+ /**
5
16
  * Makes `styled` over an engine. A component with a function style reads the theme of the nearest
6
- * provider through `useThemeView`; one with only object styles reads no context. `createStyleElements`
7
- * gives the style elements of a class where the build sends them (a server render), and none in the browser.
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.
8
23
  */
9
24
  function createStyled(engine, useThemeView, createStyleElements) {
10
- /** The element, with the style elements of its class in front of it where there are any. */
11
- const render = (element, forwarded, className) => {
12
- const styles = createStyleElements(className);
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) => {
13
27
  const rendered = createElement(element, forwarded);
28
+ if (hold) return createElement(Fragment, null, rendered, createElement(Hold, { hold }));
29
+ const styles = createStyleElements(className, cache);
14
30
  return styles.length === 0 ? rendered : createElement(Fragment, null, ...styles, rendered);
15
31
  };
16
- const styled = (tag, options = {}) => (...styles) => {
32
+ const canHold = () => typeof document !== "undefined";
33
+ const styled = (tag, options = {}) => (...args) => {
34
+ const styles = readStyleArguments(args);
17
35
  const { styler, resolve, statics } = prepareStyled(engine, tag, options, styles);
18
36
  const StaticStyled = (props) => {
19
- const { element, forwarded, className } = resolve(props);
20
- return render(element, forwarded, className);
37
+ const cache = useProvidedCache();
38
+ const { element, forwarded, className, hold } = resolve(props, void 0, canHold(), cache);
39
+ return render(element, forwarded, className, hold, cache);
21
40
  };
22
41
  const DynamicStyled = (props) => {
23
- const { element, forwarded, className } = resolve(props, useThemeView());
24
- return render(element, forwarded, className);
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);
25
46
  };
26
47
  return Object.assign(styler.isDynamic ? DynamicStyled : StaticStyled, statics);
27
48
  };
28
- return styled;
49
+ return withTagShorthands(styled);
29
50
  }
30
51
  export { createStyled };
@@ -1,14 +1,16 @@
1
1
  let react = require("react");
2
+ let _umber_style_core = require("@umber-style/core");
2
3
  const require_styleElements = require("./styleElements.cjs");
3
4
  const require_styledParts = require("./styledParts.cjs");
4
5
  // src/styled.server.ts
5
6
  /**
6
7
  * Makes `styled` for server components: no hook, no context. A function style reads the view of the
7
- * theme that `readTheme` gives, which is the root theme of the request. Each element comes with the
8
- * `<style>` of its class.
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.
9
10
  */
10
11
  function createStyled(engine, readTheme) {
11
- const styled = (tag, options = {}) => (...styles) => {
12
+ const styled = (tag, options = {}) => (...args) => {
13
+ const styles = (0, _umber_style_core.readStyleArguments)(args);
12
14
  const { styler, resolve, statics } = require_styledParts.prepareStyled(engine, tag, options, styles);
13
15
  const Styled = (props) => {
14
16
  const theme = styler.isDynamic ? engine.createThemeView(readTheme()) : void 0;
@@ -17,6 +19,6 @@ function createStyled(engine, readTheme) {
17
19
  };
18
20
  return Object.assign(Styled, statics);
19
21
  };
20
- return styled;
22
+ return (0, _umber_style_core.withTagShorthands)(styled);
21
23
  }
22
24
  exports.createStyled = createStyled;
@@ -1,14 +1,16 @@
1
1
  import { Fragment, createElement } from "react";
2
+ import { readStyleArguments, withTagShorthands } from "@umber-style/core";
2
3
  import { createStyleElements } from "./styleElements.mjs";
3
4
  import { prepareStyled } from "./styledParts.mjs";
4
5
  // src/styled.server.ts
5
6
  /**
6
7
  * Makes `styled` for server components: no hook, no context. A function style reads the view of the
7
- * theme that `readTheme` gives, which is the root theme of the request. Each element comes with the
8
- * `<style>` of its class.
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.
9
10
  */
10
11
  function createStyled(engine, readTheme) {
11
- const styled = (tag, options = {}) => (...styles) => {
12
+ const styled = (tag, options = {}) => (...args) => {
13
+ const styles = readStyleArguments(args);
12
14
  const { styler, resolve, statics } = prepareStyled(engine, tag, options, styles);
13
15
  const Styled = (props) => {
14
16
  const theme = styler.isDynamic ? engine.createThemeView(readTheme()) : void 0;
@@ -17,6 +19,6 @@ function createStyled(engine, readTheme) {
17
19
  };
18
20
  return Object.assign(Styled, statics);
19
21
  };
20
- return styled;
22
+ return withTagShorthands(styled);
21
23
  }
22
24
  export { createStyled };
@@ -1,27 +1,55 @@
1
1
  let _umber_style_core = require("@umber-style/core");
2
+ const require_domProps = require("./domProps.cjs");
2
3
  // src/styledParts.ts
3
4
  /** Whether `tag` is a component made by `styled`. */
4
5
  function isStyledComponent(tag) {
5
6
  return typeof tag === "function" && "_styles" in tag;
6
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
+ }
7
13
  /** What both builds of `styled` share: the styler, and the element and props that a component renders. */
8
14
  function prepareStyled(engine, tag, options, styles) {
9
15
  const inner = isStyledComponent(tag) ? tag : void 0;
10
16
  const baseTag = inner?._tag ?? tag;
11
17
  const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
18
+ const target = [inner?._target, options.target].filter(Boolean).join(" ") || void 0;
12
19
  const styler = engine.createStyler(options, styles, inner?._styles);
13
- /** The element, its props and the engine class for `props`; `theme` is the view that function styles read. */
14
- const resolve = (props, theme) => {
15
- const className = styler.getClassName(props, [...(0, _umber_style_core.flattenStyle)(props.style), props.className], theme);
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;
16
43
  const element = props.as ?? baseTag;
17
- const filter = typeof element === "string" ? shouldForward : void 0;
44
+ const filter = readFilter(element, shouldForward);
18
45
  const forwarded = {};
19
46
  for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "className" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
20
47
  forwarded.className = className || void 0;
21
48
  return {
22
49
  element,
23
50
  forwarded,
24
- className
51
+ className,
52
+ hold: held?.hold
25
53
  };
26
54
  };
27
55
  return {
@@ -30,7 +58,9 @@ function prepareStyled(engine, tag, options, styles) {
30
58
  statics: {
31
59
  _styles: styler.styles,
32
60
  _tag: baseTag,
33
- _shouldForwardProp: shouldForward
61
+ _shouldForwardProp: shouldForward,
62
+ _target: target,
63
+ toString: (0, _umber_style_core.createComponentSelector)(options.target, styler)
34
64
  }
35
65
  };
36
66
  }
@@ -1,27 +1,55 @@
1
- import { flattenStyle } from "@umber-style/core";
1
+ import { createComponentSelector, flattenStyle } from "@umber-style/core";
2
+ import { isDomProp } from "./domProps.mjs";
2
3
  // src/styledParts.ts
3
4
  /** Whether `tag` is a component made by `styled`. */
4
5
  function isStyledComponent(tag) {
5
6
  return typeof tag === "function" && "_styles" in tag;
6
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
+ }
7
13
  /** What both builds of `styled` share: the styler, and the element and props that a component renders. */
8
14
  function prepareStyled(engine, tag, options, styles) {
9
15
  const inner = isStyledComponent(tag) ? tag : void 0;
10
16
  const baseTag = inner?._tag ?? tag;
11
17
  const shouldForward = options.shouldForwardProp ?? inner?._shouldForwardProp;
18
+ const target = [inner?._target, options.target].filter(Boolean).join(" ") || void 0;
12
19
  const styler = engine.createStyler(options, styles, inner?._styles);
13
- /** The element, its props and the engine class for `props`; `theme` is the view that function styles read. */
14
- const resolve = (props, theme) => {
15
- const className = styler.getClassName(props, [...flattenStyle(props.style), props.className], theme);
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;
16
43
  const element = props.as ?? baseTag;
17
- const filter = typeof element === "string" ? shouldForward : void 0;
44
+ const filter = readFilter(element, shouldForward);
18
45
  const forwarded = {};
19
46
  for (const [key, value] of Object.entries(props)) if (!(key === "as" || key === "theme" || key === "className" || key === "style") && (!filter || filter(key))) forwarded[key] = value;
20
47
  forwarded.className = className || void 0;
21
48
  return {
22
49
  element,
23
50
  forwarded,
24
- className
51
+ className,
52
+ hold: held?.hold
25
53
  };
26
54
  };
27
55
  return {
@@ -30,7 +58,9 @@ function prepareStyled(engine, tag, options, styles) {
30
58
  statics: {
31
59
  _styles: styler.styles,
32
60
  _tag: baseTag,
33
- _shouldForwardProp: shouldForward
61
+ _shouldForwardProp: shouldForward,
62
+ _target: target,
63
+ toString: createComponentSelector(options.target, styler)
34
64
  }
35
65
  };
36
66
  }