@tenphi/tasty 0.0.0-snapshot.2dc73be → 0.0.0-snapshot.2f5d166
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/README.md +288 -250
- package/dist/_virtual/_rolldown/runtime.js +1 -2
- package/dist/chunks/cacheKey.d.ts +1 -0
- package/dist/chunks/cacheKey.js +17 -10
- package/dist/chunks/cacheKey.js.map +1 -1
- package/dist/chunks/definitions.d.ts +1 -1
- package/dist/chunks/definitions.js +2 -3
- package/dist/chunks/definitions.js.map +1 -1
- package/dist/chunks/index.d.ts +1 -0
- package/dist/chunks/renderChunk.d.ts +1 -0
- package/dist/chunks/renderChunk.js +32 -34
- package/dist/chunks/renderChunk.js.map +1 -1
- package/dist/compute-styles.d.ts +31 -0
- package/dist/compute-styles.js +335 -0
- package/dist/compute-styles.js.map +1 -0
- package/dist/config.d.ts +109 -30
- package/dist/config.js +142 -57
- package/dist/config.js.map +1 -1
- package/dist/core/index.d.ts +12 -11
- package/dist/core/index.js +10 -9
- package/dist/counter-style/index.js +51 -0
- package/dist/counter-style/index.js.map +1 -0
- package/dist/debug.d.ts +26 -141
- package/dist/debug.js +357 -637
- package/dist/debug.js.map +1 -1
- package/dist/font-face/index.js +63 -0
- package/dist/font-face/index.js.map +1 -0
- package/dist/hooks/index.d.ts +7 -0
- package/dist/hooks/useCounterStyle.d.ts +52 -0
- package/dist/hooks/useCounterStyle.js +43 -0
- package/dist/hooks/useCounterStyle.js.map +1 -0
- package/dist/hooks/useFontFace.d.ts +45 -0
- package/dist/hooks/useFontFace.js +68 -0
- package/dist/hooks/useFontFace.js.map +1 -0
- package/dist/hooks/useGlobalStyles.d.ts +18 -2
- package/dist/hooks/useGlobalStyles.js +50 -46
- package/dist/hooks/useGlobalStyles.js.map +1 -1
- package/dist/hooks/useKeyframes.d.ts +4 -2
- package/dist/hooks/useKeyframes.js +32 -57
- package/dist/hooks/useKeyframes.js.map +1 -1
- package/dist/hooks/useProperty.d.ts +4 -2
- package/dist/hooks/useProperty.js +29 -45
- package/dist/hooks/useProperty.js.map +1 -1
- package/dist/hooks/useRawCSS.d.ts +8 -44
- package/dist/hooks/useRawCSS.js +82 -27
- package/dist/hooks/useRawCSS.js.map +1 -1
- package/dist/hooks/useStyles.d.ts +4 -9
- package/dist/hooks/useStyles.js +7 -226
- package/dist/hooks/useStyles.js.map +1 -1
- package/dist/index.d.ts +15 -12
- package/dist/index.js +13 -10
- package/dist/injector/index.d.ts +36 -19
- package/dist/injector/index.js +42 -19
- package/dist/injector/index.js.map +1 -1
- package/dist/injector/injector.d.ts +48 -3
- package/dist/injector/injector.js +144 -16
- package/dist/injector/injector.js.map +1 -1
- package/dist/injector/sheet-manager.d.ts +9 -13
- package/dist/injector/sheet-manager.js +39 -70
- package/dist/injector/sheet-manager.js.map +1 -1
- package/dist/injector/types.d.ts +103 -19
- package/dist/keyframes/index.js +1 -1
- package/dist/parser/classify.js +6 -6
- package/dist/parser/classify.js.map +1 -1
- package/dist/parser/const.js +30 -3
- package/dist/parser/const.js.map +1 -1
- package/dist/parser/lru.js +1 -1
- package/dist/parser/lru.js.map +1 -1
- package/dist/parser/parser.js +2 -3
- package/dist/parser/parser.js.map +1 -1
- package/dist/parser/tokenizer.js +1 -1
- package/dist/parser/tokenizer.js.map +1 -1
- package/dist/parser/types.js +1 -1
- package/dist/parser/types.js.map +1 -1
- package/dist/pipeline/conditions.js +1 -1
- package/dist/pipeline/conditions.js.map +1 -1
- package/dist/pipeline/exclusive.js +1 -2
- package/dist/pipeline/exclusive.js.map +1 -1
- package/dist/pipeline/index.d.ts +3 -1
- package/dist/pipeline/index.js +80 -32
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/materialize.js +329 -82
- package/dist/pipeline/materialize.js.map +1 -1
- package/dist/pipeline/parseStateKey.d.ts +1 -1
- package/dist/pipeline/parseStateKey.js +3 -8
- package/dist/pipeline/parseStateKey.js.map +1 -1
- package/dist/pipeline/simplify.js +1 -2
- package/dist/pipeline/simplify.js.map +1 -1
- package/dist/pipeline/warnings.js +1 -1
- package/dist/plugins/index.d.ts +2 -0
- package/dist/plugins/okhsl-plugin.js +3 -277
- package/dist/plugins/okhsl-plugin.js.map +1 -1
- package/dist/plugins/types.d.ts +9 -2
- package/dist/properties/index.js +3 -17
- package/dist/properties/index.js.map +1 -1
- package/dist/properties/property-type-resolver.js +5 -2
- package/dist/properties/property-type-resolver.js.map +1 -1
- package/dist/rsc-cache.js +70 -0
- package/dist/rsc-cache.js.map +1 -0
- package/dist/ssr/astro-client.d.ts +1 -0
- package/dist/ssr/astro-client.js +24 -0
- package/dist/ssr/astro-client.js.map +1 -0
- package/dist/ssr/astro-middleware.d.ts +15 -0
- package/dist/ssr/astro-middleware.js +19 -0
- package/dist/ssr/astro-middleware.js.map +1 -0
- package/dist/ssr/astro.d.ts +85 -8
- package/dist/ssr/astro.js +111 -22
- package/dist/ssr/astro.js.map +1 -1
- package/dist/ssr/async-storage.js +1 -2
- package/dist/ssr/async-storage.js.map +1 -1
- package/dist/ssr/collect-auto-properties.js +29 -11
- package/dist/ssr/collect-auto-properties.js.map +1 -1
- package/dist/ssr/collector.d.ts +17 -0
- package/dist/ssr/collector.js +60 -14
- package/dist/ssr/collector.js.map +1 -1
- package/dist/ssr/format-global-rules.js +1 -1
- package/dist/ssr/format-keyframes.js +1 -2
- package/dist/ssr/format-keyframes.js.map +1 -1
- package/dist/ssr/format-property.js +10 -9
- package/dist/ssr/format-property.js.map +1 -1
- package/dist/ssr/format-rules.js +8 -5
- package/dist/ssr/format-rules.js.map +1 -1
- package/dist/ssr/hydrate.js +1 -2
- package/dist/ssr/hydrate.js.map +1 -1
- package/dist/ssr/index.d.ts +2 -3
- package/dist/ssr/index.js +2 -4
- package/dist/ssr/index.js.map +1 -1
- package/dist/ssr/next.d.ts +1 -3
- package/dist/ssr/next.js +10 -7
- package/dist/ssr/next.js.map +1 -1
- package/dist/ssr/ssr-collector-ref.js +1 -1
- package/dist/states/index.js +13 -259
- package/dist/states/index.js.map +1 -1
- package/dist/static/index.js +1 -2
- package/dist/static/inject.d.ts +5 -0
- package/dist/static/inject.js +17 -0
- package/dist/static/inject.js.map +1 -0
- package/dist/static/tastyStatic.js +1 -2
- package/dist/static/tastyStatic.js.map +1 -1
- package/dist/static/types.js +1 -1
- package/dist/styles/border.d.ts +1 -1
- package/dist/styles/border.js +30 -24
- package/dist/styles/border.js.map +1 -1
- package/dist/styles/color.d.ts +1 -1
- package/dist/styles/color.js +11 -8
- package/dist/styles/color.js.map +1 -1
- package/dist/styles/const.js +17 -0
- package/dist/styles/const.js.map +1 -0
- package/dist/styles/createStyle.js +28 -26
- package/dist/styles/createStyle.js.map +1 -1
- package/dist/styles/dimension.js +15 -3
- package/dist/styles/dimension.js.map +1 -1
- package/dist/styles/directional.js +133 -0
- package/dist/styles/directional.js.map +1 -0
- package/dist/styles/display.d.ts +3 -10
- package/dist/styles/display.js +45 -39
- package/dist/styles/display.js.map +1 -1
- package/dist/styles/fade.d.ts +1 -1
- package/dist/styles/fade.js +9 -5
- package/dist/styles/fade.js.map +1 -1
- package/dist/styles/fill.d.ts +2 -2
- package/dist/styles/fill.js +3 -4
- package/dist/styles/fill.js.map +1 -1
- package/dist/styles/flow.js +1 -1
- package/dist/styles/gap.d.ts +1 -1
- package/dist/styles/gap.js +4 -3
- package/dist/styles/gap.js.map +1 -1
- package/dist/styles/height.d.ts +1 -1
- package/dist/styles/height.js +1 -2
- package/dist/styles/height.js.map +1 -1
- package/dist/styles/index.d.ts +0 -1
- package/dist/styles/index.js +3 -4
- package/dist/styles/index.js.map +1 -1
- package/dist/styles/inset.d.ts +1 -29
- package/dist/styles/inset.js +19 -135
- package/dist/styles/inset.js.map +1 -1
- package/dist/styles/list.d.ts +5 -5
- package/dist/styles/list.js +4 -2
- package/dist/styles/list.js.map +1 -1
- package/dist/styles/margin.d.ts +1 -1
- package/dist/styles/margin.js +17 -89
- package/dist/styles/margin.js.map +1 -1
- package/dist/styles/outline.d.ts +1 -1
- package/dist/styles/outline.js +6 -16
- package/dist/styles/outline.js.map +1 -1
- package/dist/styles/padding.d.ts +1 -1
- package/dist/styles/padding.js +17 -89
- package/dist/styles/padding.js.map +1 -1
- package/dist/styles/placement.d.ts +37 -0
- package/dist/styles/placement.js +74 -0
- package/dist/styles/placement.js.map +1 -0
- package/dist/styles/predefined.d.ts +4 -4
- package/dist/styles/predefined.js +9 -10
- package/dist/styles/predefined.js.map +1 -1
- package/dist/styles/preset.d.ts +6 -1
- package/dist/styles/preset.js +54 -53
- package/dist/styles/preset.js.map +1 -1
- package/dist/styles/radius.d.ts +1 -3
- package/dist/styles/radius.js +38 -6
- package/dist/styles/radius.js.map +1 -1
- package/dist/styles/scrollMargin.d.ts +24 -0
- package/dist/styles/scrollMargin.js +32 -0
- package/dist/styles/scrollMargin.js.map +1 -0
- package/dist/styles/scrollbar.d.ts +1 -1
- package/dist/styles/scrollbar.js +8 -5
- package/dist/styles/scrollbar.js.map +1 -1
- package/dist/styles/shadow.d.ts +1 -1
- package/dist/styles/shadow.js +4 -3
- package/dist/styles/shadow.js.map +1 -1
- package/dist/styles/shared.js +17 -0
- package/dist/styles/shared.js.map +1 -0
- package/dist/styles/transition.d.ts +1 -1
- package/dist/styles/transition.js +5 -4
- package/dist/styles/transition.js.map +1 -1
- package/dist/styles/types.d.ts +84 -15
- package/dist/styles/width.d.ts +1 -1
- package/dist/styles/width.js +1 -2
- package/dist/styles/width.js.map +1 -1
- package/dist/tasty.d.ts +46 -893
- package/dist/tasty.js +96 -59
- package/dist/tasty.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/utils/cache-wrapper.js +4 -9
- package/dist/utils/cache-wrapper.js.map +1 -1
- package/dist/utils/case-converter.js +1 -1
- package/dist/utils/color-math.d.ts +46 -0
- package/dist/utils/color-math.js +749 -0
- package/dist/utils/color-math.js.map +1 -0
- package/dist/utils/color-space.d.ts +5 -0
- package/dist/utils/color-space.js +228 -0
- package/dist/utils/color-space.js.map +1 -0
- package/dist/utils/colors.js +3 -2
- package/dist/utils/colors.js.map +1 -1
- package/dist/utils/dotize.js +1 -1
- package/dist/utils/dotize.js.map +1 -1
- package/dist/utils/filter-base-props.d.ts +1 -1
- package/dist/utils/filter-base-props.js +1 -1
- package/dist/utils/filter-base-props.js.map +1 -1
- package/dist/utils/get-display-name.js +1 -1
- package/dist/utils/has-keys.js +13 -0
- package/dist/utils/has-keys.js.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/is-dev-env.js +1 -1
- package/dist/utils/is-dev-env.js.map +1 -1
- package/dist/utils/is-valid-element-type.js +1 -1
- package/dist/utils/merge-styles.js +1 -2
- package/dist/utils/merge-styles.js.map +1 -1
- package/dist/utils/mod-attrs.d.ts +0 -2
- package/dist/utils/mod-attrs.js +3 -4
- package/dist/utils/mod-attrs.js.map +1 -1
- package/dist/utils/process-tokens.d.ts +3 -17
- package/dist/utils/process-tokens.js +19 -107
- package/dist/utils/process-tokens.js.map +1 -1
- package/dist/utils/resolve-recipes.js +1 -2
- package/dist/utils/resolve-recipes.js.map +1 -1
- package/dist/utils/selector-transform.js +1 -1
- package/dist/utils/string.js +1 -1
- package/dist/utils/styles.d.ts +3 -81
- package/dist/utils/styles.js +29 -539
- package/dist/utils/styles.js.map +1 -1
- package/dist/utils/typography.d.ts +24 -13
- package/dist/utils/typography.js +15 -17
- package/dist/utils/typography.js.map +1 -1
- package/dist/utils/warnings.js +1 -1
- package/dist/zero/babel.d.ts +63 -8
- package/dist/zero/babel.js +163 -44
- package/dist/zero/babel.js.map +1 -1
- package/dist/zero/css-writer.js +1 -2
- package/dist/zero/css-writer.js.map +1 -1
- package/dist/zero/extractor.js +54 -3
- package/dist/zero/extractor.js.map +1 -1
- package/dist/zero/index.js +1 -2
- package/dist/zero/next.d.ts +28 -1
- package/dist/zero/next.js +70 -15
- package/dist/zero/next.js.map +1 -1
- package/docs/PIPELINE.md +519 -0
- package/docs/README.md +31 -0
- package/docs/adoption.md +296 -0
- package/docs/comparison.md +420 -0
- package/docs/configuration.md +127 -12
- package/docs/debug.md +152 -339
- package/docs/design-system.md +424 -0
- package/docs/{usage.md → dsl.md} +379 -386
- package/docs/getting-started.md +217 -0
- package/docs/injector.md +43 -27
- package/docs/methodology.md +616 -0
- package/docs/runtime.md +575 -0
- package/docs/ssr.md +144 -81
- package/docs/styles.md +48 -26
- package/docs/tasty-static.md +166 -22
- package/package.json +27 -12
- package/dist/ssr/context.d.ts +0 -8
- package/dist/ssr/context.js +0 -14
- package/dist/ssr/context.js.map +0 -1
- package/dist/styles/align.d.ts +0 -15
- package/dist/styles/align.js +0 -14
- package/dist/styles/align.js.map +0 -1
- package/dist/styles/justify.d.ts +0 -15
- package/dist/styles/justify.js +0 -14
- package/dist/styles/justify.js.map +0 -1
- package/dist/tokens/typography.d.ts +0 -19
- package/dist/tokens/typography.js +0 -237
- package/dist/tokens/typography.js.map +0 -1
- package/dist/utils/hsl-to-rgb.js +0 -38
- package/dist/utils/hsl-to-rgb.js.map +0 -1
- package/dist/utils/okhsl-to-rgb.js +0 -296
- package/dist/utils/okhsl-to-rgb.js.map +0 -1
|
@@ -1,27 +1,30 @@
|
|
|
1
1
|
import { renderStyles } from "../pipeline/index.js";
|
|
2
2
|
import { getConfig } from "../config.js";
|
|
3
3
|
import { injectGlobal } from "../injector/index.js";
|
|
4
|
-
import {
|
|
5
|
-
import { collectAutoInferredProperties } from "../ssr/collect-auto-properties.js";
|
|
6
|
-
import { TastySSRContext } from "../ssr/context.js";
|
|
7
|
-
import { getRegisteredSSRCollector } from "../ssr/ssr-collector-ref.js";
|
|
4
|
+
import { getRSCCache, isServerEnvironment, pushRSCCSS } from "../rsc-cache.js";
|
|
5
|
+
import { collectAutoInferredProperties, collectAutoInferredPropertiesRSC } from "../ssr/collect-auto-properties.js";
|
|
8
6
|
import { formatGlobalRules } from "../ssr/format-global-rules.js";
|
|
9
|
-
import {
|
|
10
|
-
|
|
7
|
+
import { getRegisteredSSRCollector } from "../ssr/ssr-collector-ref.js";
|
|
8
|
+
import { resolveRecipes } from "../utils/resolve-recipes.js";
|
|
9
|
+
import { hashString } from "../utils/hash.js";
|
|
11
10
|
//#region src/hooks/useGlobalStyles.ts
|
|
12
|
-
|
|
13
|
-
if (reactContext) return reactContext;
|
|
14
|
-
return getRegisteredSSRCollector();
|
|
15
|
-
}
|
|
11
|
+
const clientGlobalEntries = /* @__PURE__ */ new Map();
|
|
16
12
|
/**
|
|
17
|
-
*
|
|
13
|
+
* Inject global styles for a given selector.
|
|
18
14
|
* Useful for styling elements by selector without generating classNames.
|
|
19
15
|
*
|
|
20
16
|
* SSR-aware: when a ServerStyleCollector is available, CSS is collected
|
|
21
17
|
* during the render phase instead of being injected into the DOM.
|
|
22
18
|
*
|
|
19
|
+
* Works in all environments: client, SSR with collector, and React Server Components.
|
|
20
|
+
*
|
|
21
|
+
* Injected styles are permanent — they are not cleaned up on component unmount.
|
|
22
|
+
* Use the `id` option for update tracking when styles change over the
|
|
23
|
+
* component lifecycle.
|
|
24
|
+
*
|
|
23
25
|
* @param selector - CSS selector to apply styles to (e.g., '.my-class', ':root', 'body')
|
|
24
26
|
* @param styles - Tasty styles object
|
|
27
|
+
* @param options - Optional settings including `id` for update tracking
|
|
25
28
|
*
|
|
26
29
|
* @example
|
|
27
30
|
* ```tsx
|
|
@@ -36,48 +39,49 @@ function resolveSSRCollector(reactContext) {
|
|
|
36
39
|
* }
|
|
37
40
|
* ```
|
|
38
41
|
*/
|
|
39
|
-
function useGlobalStyles(selector, styles) {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
42
|
+
function useGlobalStyles(selector, styles, options) {
|
|
43
|
+
if (!styles) return;
|
|
44
|
+
if (!selector) {
|
|
45
|
+
console.warn("[Tasty] useGlobalStyles: selector is required and cannot be empty. Styles will not be injected.");
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const ssrCollector = getRegisteredSSRCollector();
|
|
49
|
+
const isServer = !ssrCollector && isServerEnvironment();
|
|
50
|
+
const slotKey = options?.id ?? selector;
|
|
51
|
+
const stylesKey = JSON.stringify(styles);
|
|
52
|
+
if (!ssrCollector && !isServer) {
|
|
53
|
+
const existing = clientGlobalEntries.get(slotKey);
|
|
54
|
+
if (existing && existing.stylesKey === stylesKey) return;
|
|
55
|
+
}
|
|
56
|
+
const resolvedStyles = resolveRecipes(styles);
|
|
57
|
+
const styleResults = renderStyles(resolvedStyles, selector);
|
|
58
|
+
if (styleResults.length === 0) return;
|
|
59
|
+
if (ssrCollector) {
|
|
56
60
|
ssrCollector.collectInternals();
|
|
57
61
|
const css = formatGlobalRules(styleResults);
|
|
58
62
|
if (css) {
|
|
59
|
-
const key = `global:${selector}:${css
|
|
63
|
+
const key = `global:${selector}:${hashString(css)}`;
|
|
60
64
|
ssrCollector.collectGlobalStyles(key, css);
|
|
61
65
|
}
|
|
62
66
|
if (getConfig().autoPropertyTypes !== false) collectAutoInferredProperties(styleResults, ssrCollector, resolvedStyles);
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
}
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
if (isServer) {
|
|
70
|
+
const rscCache = getRSCCache();
|
|
71
|
+
const css = formatGlobalRules(styleResults);
|
|
72
|
+
if (css) pushRSCCSS(rscCache, options?.id ? `__global:${options.id}` : `__global:${selector}:${hashString(css)}`, css);
|
|
73
|
+
if (getConfig().autoPropertyTypes !== false) collectAutoInferredPropertiesRSC(styleResults, rscCache, resolvedStyles);
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
const existing = clientGlobalEntries.get(slotKey);
|
|
77
|
+
if (existing) existing.dispose();
|
|
78
|
+
const { dispose } = injectGlobal(styleResults);
|
|
79
|
+
clientGlobalEntries.set(slotKey, {
|
|
80
|
+
stylesKey,
|
|
81
|
+
dispose
|
|
82
|
+
});
|
|
79
83
|
}
|
|
80
|
-
|
|
81
84
|
//#endregion
|
|
82
85
|
export { useGlobalStyles };
|
|
86
|
+
|
|
83
87
|
//# sourceMappingURL=useGlobalStyles.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useGlobalStyles.js","names":[],"sources":["../../src/hooks/useGlobalStyles.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"useGlobalStyles.js","names":[],"sources":["../../src/hooks/useGlobalStyles.ts"],"sourcesContent":["import { getConfig } from '../config';\nimport { injectGlobal } from '../injector';\nimport type { StyleResult } from '../pipeline';\nimport { renderStyles } from '../pipeline';\nimport { getRSCCache, isServerEnvironment, pushRSCCSS } from '../rsc-cache';\nimport {\n collectAutoInferredProperties,\n collectAutoInferredPropertiesRSC,\n} from '../ssr/collect-auto-properties';\nimport { formatGlobalRules } from '../ssr/format-global-rules';\nimport { getRegisteredSSRCollector } from '../ssr/ssr-collector-ref';\nimport type { Styles } from '../styles/types';\nimport { hashString } from '../utils/hash';\nimport { resolveRecipes } from '../utils/resolve-recipes';\n\ninterface UseGlobalStylesOptions {\n /**\n * Stable identifier for update tracking (client-only). When provided,\n * changing the styles will dispose the previous injection and inject the\n * new one. Without an id, the selector is used as the slot key.\n * In RSC mode, renders are single-pass so update tracking does not apply.\n */\n id?: string;\n}\n\ninterface ClientGlobalEntry {\n stylesKey: string;\n dispose: () => void;\n}\n\nconst clientGlobalEntries = new Map<string, ClientGlobalEntry>();\n\n/* @internal — used only for tests */\nexport function _resetGlobalStylesCache(): void {\n clientGlobalEntries.clear();\n}\n\n/**\n * Inject global styles for a given selector.\n * Useful for styling elements by selector without generating classNames.\n *\n * SSR-aware: when a ServerStyleCollector is available, CSS is collected\n * during the render phase instead of being injected into the DOM.\n *\n * Works in all environments: client, SSR with collector, and React Server Components.\n *\n * Injected styles are permanent — they are not cleaned up on component unmount.\n * Use the `id` option for update tracking when styles change over the\n * component lifecycle.\n *\n * @param selector - CSS selector to apply styles to (e.g., '.my-class', ':root', 'body')\n * @param styles - Tasty styles object\n * @param options - Optional settings including `id` for update tracking\n *\n * @example\n * ```tsx\n * function MyComponent() {\n * useGlobalStyles('.card', {\n * padding: '2x',\n * radius: '1r',\n * fill: '#white',\n * });\n *\n * return <div className=\"card\">Content</div>;\n * }\n * ```\n */\nexport function useGlobalStyles(\n selector: string,\n styles?: Styles,\n options?: UseGlobalStylesOptions,\n): void {\n if (!styles) return;\n\n if (!selector) {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(\n '[Tasty] useGlobalStyles: selector is required and cannot be empty. ' +\n 'Styles will not be injected.',\n );\n }\n return;\n }\n\n const ssrCollector = getRegisteredSSRCollector();\n const isServer = !ssrCollector && isServerEnvironment();\n\n const slotKey = options?.id ?? selector;\n const stylesKey = JSON.stringify(styles);\n\n // Client fast path: skip resolveRecipes/renderStyles if styles haven't changed\n if (!ssrCollector && !isServer) {\n const existing = clientGlobalEntries.get(slotKey);\n if (existing && existing.stylesKey === stylesKey) return;\n }\n\n const resolvedStyles = resolveRecipes(styles);\n\n const styleResults = renderStyles(resolvedStyles, selector) as StyleResult[];\n\n if (styleResults.length === 0) return;\n\n if (ssrCollector) {\n ssrCollector.collectInternals();\n\n const css = formatGlobalRules(styleResults);\n if (css) {\n const key = `global:${selector}:${hashString(css)}`;\n ssrCollector.collectGlobalStyles(key, css);\n }\n\n if (getConfig().autoPropertyTypes !== false) {\n collectAutoInferredProperties(styleResults, ssrCollector, resolvedStyles);\n }\n return;\n }\n\n if (isServer) {\n const rscCache = getRSCCache();\n const css = formatGlobalRules(styleResults);\n if (css) {\n const key = options?.id\n ? `__global:${options.id}`\n : `__global:${selector}:${hashString(css)}`;\n pushRSCCSS(rscCache, key, css);\n }\n\n if (getConfig().autoPropertyTypes !== false) {\n collectAutoInferredPropertiesRSC(styleResults, rscCache, resolvedStyles);\n }\n return;\n }\n\n // Client path\n const existing = clientGlobalEntries.get(slotKey);\n if (existing) {\n existing.dispose();\n }\n\n const { dispose } = injectGlobal(styleResults);\n clientGlobalEntries.set(slotKey, { stylesKey, dispose });\n}\n"],"mappings":";;;;;;;;;;AA8BA,MAAM,sCAAsB,IAAI,KAAgC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqChE,SAAgB,gBACd,UACA,QACA,SACM;AACN,KAAI,CAAC,OAAQ;AAEb,KAAI,CAAC,UAAU;AAEX,UAAQ,KACN,kGAED;AAEH;;CAGF,MAAM,eAAe,2BAA2B;CAChD,MAAM,WAAW,CAAC,gBAAgB,qBAAqB;CAEvD,MAAM,UAAU,SAAS,MAAM;CAC/B,MAAM,YAAY,KAAK,UAAU,OAAO;AAGxC,KAAI,CAAC,gBAAgB,CAAC,UAAU;EAC9B,MAAM,WAAW,oBAAoB,IAAI,QAAQ;AACjD,MAAI,YAAY,SAAS,cAAc,UAAW;;CAGpD,MAAM,iBAAiB,eAAe,OAAO;CAE7C,MAAM,eAAe,aAAa,gBAAgB,SAAS;AAE3D,KAAI,aAAa,WAAW,EAAG;AAE/B,KAAI,cAAc;AAChB,eAAa,kBAAkB;EAE/B,MAAM,MAAM,kBAAkB,aAAa;AAC3C,MAAI,KAAK;GACP,MAAM,MAAM,UAAU,SAAS,GAAG,WAAW,IAAI;AACjD,gBAAa,oBAAoB,KAAK,IAAI;;AAG5C,MAAI,WAAW,CAAC,sBAAsB,MACpC,+BAA8B,cAAc,cAAc,eAAe;AAE3E;;AAGF,KAAI,UAAU;EACZ,MAAM,WAAW,aAAa;EAC9B,MAAM,MAAM,kBAAkB,aAAa;AAC3C,MAAI,IAIF,YAAW,UAHC,SAAS,KACjB,YAAY,QAAQ,OACpB,YAAY,SAAS,GAAG,WAAW,IAAI,IACjB,IAAI;AAGhC,MAAI,WAAW,CAAC,sBAAsB,MACpC,kCAAiC,cAAc,UAAU,eAAe;AAE1E;;CAIF,MAAM,WAAW,oBAAoB,IAAI,QAAQ;AACjD,KAAI,SACF,UAAS,SAAS;CAGpB,MAAM,EAAE,YAAY,aAAa,aAAa;AAC9C,qBAAoB,IAAI,SAAS;EAAE;EAAW;EAAS,CAAC"}
|
|
@@ -6,8 +6,10 @@ interface UseKeyframesOptions {
|
|
|
6
6
|
root?: Document | ShadowRoot;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* Inject CSS @keyframes and return the generated animation name.
|
|
10
|
+
* Deduplicates by content — identical steps always return the same name.
|
|
11
|
+
*
|
|
12
|
+
* Works in all environments: client, SSR with collector, and React Server Components.
|
|
11
13
|
*
|
|
12
14
|
* @example Basic usage - steps object is the dependency
|
|
13
15
|
* ```tsx
|
|
@@ -1,69 +1,44 @@
|
|
|
1
1
|
import { keyframes } from "../injector/index.js";
|
|
2
|
-
import {
|
|
2
|
+
import { getRSCCache, isServerEnvironment, pushRSCCSS } from "../rsc-cache.js";
|
|
3
3
|
import { formatKeyframesCSS } from "../ssr/format-keyframes.js";
|
|
4
4
|
import { getRegisteredSSRCollector } from "../ssr/ssr-collector-ref.js";
|
|
5
|
-
import { useContext, useInsertionEffect, useMemo, useRef } from "react";
|
|
6
|
-
|
|
7
5
|
//#region src/hooks/useKeyframes.ts
|
|
8
|
-
|
|
9
|
-
if (reactContext) return reactContext;
|
|
10
|
-
return getRegisteredSSRCollector();
|
|
11
|
-
}
|
|
6
|
+
const clientContentToName = /* @__PURE__ */ new Map();
|
|
12
7
|
function useKeyframes(stepsOrFactory, depsOrOptions, options) {
|
|
13
|
-
const ssrCollector = resolveSSRCollector(useContext(TastySSRContext));
|
|
14
8
|
const isFactory = typeof stepsOrFactory === "function";
|
|
15
|
-
const deps = isFactory && Array.isArray(depsOrOptions) ? depsOrOptions : void 0;
|
|
16
9
|
const opts = isFactory ? options : depsOrOptions;
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
opts?.name,
|
|
43
|
-
opts?.root
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
useInsertionEffect(() => {
|
|
47
|
-
effectResultRef.current?.dispose();
|
|
48
|
-
effectResultRef.current = null;
|
|
49
|
-
if (stepsData) effectResultRef.current = keyframes(stepsData, {
|
|
50
|
-
name: opts?.name,
|
|
51
|
-
root: opts?.root
|
|
52
|
-
});
|
|
53
|
-
return () => {
|
|
54
|
-
effectResultRef.current?.dispose();
|
|
55
|
-
effectResultRef.current = null;
|
|
56
|
-
renderResultRef.current?.dispose();
|
|
57
|
-
renderResultRef.current = null;
|
|
58
|
-
};
|
|
59
|
-
}, [
|
|
60
|
-
stepsData,
|
|
61
|
-
opts?.name,
|
|
62
|
-
opts?.root
|
|
63
|
-
]);
|
|
10
|
+
const steps = isFactory ? stepsOrFactory() : stepsOrFactory;
|
|
11
|
+
if (!steps || Object.keys(steps).length === 0) return "";
|
|
12
|
+
const ssrCollector = getRegisteredSSRCollector();
|
|
13
|
+
if (ssrCollector) {
|
|
14
|
+
const actualName = ssrCollector.allocateKeyframeName(opts?.name);
|
|
15
|
+
const css = formatKeyframesCSS(actualName, steps);
|
|
16
|
+
ssrCollector.collectKeyframes(actualName, css);
|
|
17
|
+
return actualName;
|
|
18
|
+
}
|
|
19
|
+
if (isServerEnvironment()) {
|
|
20
|
+
const rscCache = getRSCCache();
|
|
21
|
+
const contentHash = JSON.stringify(steps);
|
|
22
|
+
const key = `__kf:${opts?.name ?? ""}:${contentHash}`;
|
|
23
|
+
const existingName = rscCache.generatedNames.get(key);
|
|
24
|
+
if (existingName) return existingName;
|
|
25
|
+
const actualName = opts?.name ?? `k${rscCache.keyframesCounter++}`;
|
|
26
|
+
pushRSCCSS(rscCache, key, formatKeyframesCSS(actualName, steps));
|
|
27
|
+
rscCache.generatedNames.set(key, actualName);
|
|
28
|
+
return actualName;
|
|
29
|
+
}
|
|
30
|
+
const contentHash = JSON.stringify(steps);
|
|
31
|
+
const cacheKey = `${opts?.name ?? ""}:${contentHash}`;
|
|
32
|
+
const cachedName = clientContentToName.get(cacheKey);
|
|
33
|
+
if (cachedName) return cachedName;
|
|
34
|
+
const name = keyframes(steps, {
|
|
35
|
+
name: opts?.name,
|
|
36
|
+
root: opts?.root
|
|
37
|
+
}).toString();
|
|
38
|
+
clientContentToName.set(cacheKey, name);
|
|
64
39
|
return name;
|
|
65
40
|
}
|
|
66
|
-
|
|
67
41
|
//#endregion
|
|
68
42
|
export { useKeyframes };
|
|
43
|
+
|
|
69
44
|
//# sourceMappingURL=useKeyframes.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useKeyframes.js","names":[],"sources":["../../src/hooks/useKeyframes.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"useKeyframes.js","names":[],"sources":["../../src/hooks/useKeyframes.ts"],"sourcesContent":["import { keyframes } from '../injector';\nimport type { KeyframesSteps } from '../injector/types';\nimport { getRSCCache, isServerEnvironment, pushRSCCSS } from '../rsc-cache';\nimport { formatKeyframesCSS } from '../ssr/format-keyframes';\nimport { getRegisteredSSRCollector } from '../ssr/ssr-collector-ref';\n\ninterface UseKeyframesOptions {\n name?: string;\n root?: Document | ShadowRoot;\n}\n\nconst clientContentToName = new Map<string, string>();\n\n/* @internal — used only for tests */\nexport function _resetKeyframesCache(): void {\n clientContentToName.clear();\n}\n\n/**\n * Inject CSS @keyframes and return the generated animation name.\n * Deduplicates by content — identical steps always return the same name.\n *\n * Works in all environments: client, SSR with collector, and React Server Components.\n *\n * @example Basic usage - steps object is the dependency\n * ```tsx\n * function MyComponent() {\n * const bounce = useKeyframes({\n * '0%': { transform: 'scale(1)' },\n * '50%': { transform: 'scale(1.1)' },\n * '100%': { transform: 'scale(1)' },\n * });\n *\n * return <div style={{ animation: `${bounce} 1s infinite` }}>Bouncing</div>;\n * }\n * ```\n *\n * @example With custom name\n * ```tsx\n * function MyComponent() {\n * const fadeIn = useKeyframes(\n * { from: { opacity: 0 }, to: { opacity: 1 } },\n * { name: 'fadeIn' }\n * );\n *\n * return <div style={{ animation: `${fadeIn} 0.3s ease-out` }}>Fading in</div>;\n * }\n * ```\n *\n * @example Factory function with dependencies\n * ```tsx\n * function MyComponent({ scale }: { scale: number }) {\n * const pulse = useKeyframes(\n * () => ({\n * '0%': { transform: 'scale(1)' },\n * '100%': { transform: `scale(${scale})` },\n * }),\n * [scale]\n * );\n *\n * return <div style={{ animation: `${pulse} 1s infinite` }}>Pulsing</div>;\n * }\n * ```\n */\n\n// Overload 1: Static steps object\nexport function useKeyframes(\n steps: KeyframesSteps,\n options?: UseKeyframesOptions,\n): string;\n\n// Overload 2: Factory function with dependencies\nexport function useKeyframes(\n factory: () => KeyframesSteps,\n deps: readonly unknown[],\n options?: UseKeyframesOptions,\n): string;\n\n// Implementation\nexport function useKeyframes(\n stepsOrFactory: KeyframesSteps | (() => KeyframesSteps),\n depsOrOptions?: readonly unknown[] | UseKeyframesOptions,\n options?: UseKeyframesOptions,\n): string {\n const isFactory = typeof stepsOrFactory === 'function';\n\n const opts = isFactory\n ? options\n : (depsOrOptions as UseKeyframesOptions | undefined);\n\n const steps = isFactory\n ? (stepsOrFactory as () => KeyframesSteps)()\n : (stepsOrFactory as KeyframesSteps);\n\n if (!steps || Object.keys(steps).length === 0) {\n return '';\n }\n\n const ssrCollector = getRegisteredSSRCollector();\n\n if (ssrCollector) {\n const actualName = ssrCollector.allocateKeyframeName(opts?.name);\n const css = formatKeyframesCSS(actualName, steps);\n ssrCollector.collectKeyframes(actualName, css);\n return actualName;\n }\n\n if (isServerEnvironment()) {\n const rscCache = getRSCCache();\n const contentHash = JSON.stringify(steps);\n const key = `__kf:${opts?.name ?? ''}:${contentHash}`;\n\n const existingName = rscCache.generatedNames.get(key);\n if (existingName) return existingName;\n\n const actualName = opts?.name ?? `k${rscCache.keyframesCounter++}`;\n const css = formatKeyframesCSS(actualName, steps);\n pushRSCCSS(rscCache, key, css);\n rscCache.generatedNames.set(key, actualName);\n return actualName;\n }\n\n // Client path: stable name via content-based dedup\n const contentHash = JSON.stringify(steps);\n const cacheKey = `${opts?.name ?? ''}:${contentHash}`;\n\n const cachedName = clientContentToName.get(cacheKey);\n if (cachedName) {\n return cachedName;\n }\n\n const result = keyframes(steps, {\n name: opts?.name,\n root: opts?.root,\n });\n\n const name = result.toString();\n clientContentToName.set(cacheKey, name);\n\n return name;\n}\n"],"mappings":";;;;;AAWA,MAAM,sCAAsB,IAAI,KAAqB;AAoErD,SAAgB,aACd,gBACA,eACA,SACQ;CACR,MAAM,YAAY,OAAO,mBAAmB;CAE5C,MAAM,OAAO,YACT,UACC;CAEL,MAAM,QAAQ,YACT,gBAAyC,GACzC;AAEL,KAAI,CAAC,SAAS,OAAO,KAAK,MAAM,CAAC,WAAW,EAC1C,QAAO;CAGT,MAAM,eAAe,2BAA2B;AAEhD,KAAI,cAAc;EAChB,MAAM,aAAa,aAAa,qBAAqB,MAAM,KAAK;EAChE,MAAM,MAAM,mBAAmB,YAAY,MAAM;AACjD,eAAa,iBAAiB,YAAY,IAAI;AAC9C,SAAO;;AAGT,KAAI,qBAAqB,EAAE;EACzB,MAAM,WAAW,aAAa;EAC9B,MAAM,cAAc,KAAK,UAAU,MAAM;EACzC,MAAM,MAAM,QAAQ,MAAM,QAAQ,GAAG,GAAG;EAExC,MAAM,eAAe,SAAS,eAAe,IAAI,IAAI;AACrD,MAAI,aAAc,QAAO;EAEzB,MAAM,aAAa,MAAM,QAAQ,IAAI,SAAS;AAE9C,aAAW,UAAU,KADT,mBAAmB,YAAY,MAAM,CACnB;AAC9B,WAAS,eAAe,IAAI,KAAK,WAAW;AAC5C,SAAO;;CAIT,MAAM,cAAc,KAAK,UAAU,MAAM;CACzC,MAAM,WAAW,GAAG,MAAM,QAAQ,GAAG,GAAG;CAExC,MAAM,aAAa,oBAAoB,IAAI,SAAS;AACpD,KAAI,WACF,QAAO;CAQT,MAAM,OALS,UAAU,OAAO;EAC9B,MAAM,MAAM;EACZ,MAAM,MAAM;EACb,CAAC,CAEkB,UAAU;AAC9B,qBAAoB,IAAI,UAAU,KAAK;AAEvC,QAAO"}
|
|
@@ -22,17 +22,19 @@ interface UsePropertyOptions {
|
|
|
22
22
|
root?: Document | ShadowRoot;
|
|
23
23
|
}
|
|
24
24
|
/**
|
|
25
|
-
*
|
|
25
|
+
* Register a CSS @property custom property.
|
|
26
26
|
* This enables advanced features like animating custom properties.
|
|
27
27
|
*
|
|
28
28
|
* Note: @property rules are global and persistent once defined.
|
|
29
|
-
* The
|
|
29
|
+
* The function ensures the property is only registered once per root.
|
|
30
30
|
*
|
|
31
31
|
* Accepts tasty token syntax for the property name:
|
|
32
32
|
* - `$name` → defines `--name`
|
|
33
33
|
* - `#name` → defines `--name-color` (auto-sets syntax: '<color>', defaults initialValue: 'transparent')
|
|
34
34
|
* - `--name` → defines `--name` (legacy format)
|
|
35
35
|
*
|
|
36
|
+
* Works in all environments: client, SSR with collector, and React Server Components.
|
|
37
|
+
*
|
|
36
38
|
* @param name - The property token ($name, #name) or CSS property name (--name)
|
|
37
39
|
* @param options - Property configuration
|
|
38
40
|
*
|
|
@@ -1,26 +1,22 @@
|
|
|
1
1
|
import { getGlobalInjector } from "../config.js";
|
|
2
|
+
import { getRSCCache, isServerEnvironment, pushRSCCSS } from "../rsc-cache.js";
|
|
2
3
|
import { formatPropertyCSS } from "../ssr/format-property.js";
|
|
3
|
-
import { TastySSRContext } from "../ssr/context.js";
|
|
4
4
|
import { getRegisteredSSRCollector } from "../ssr/ssr-collector-ref.js";
|
|
5
|
-
import { useContext, useInsertionEffect, useMemo } from "react";
|
|
6
|
-
|
|
7
5
|
//#region src/hooks/useProperty.ts
|
|
8
|
-
function resolveSSRCollector(reactContext) {
|
|
9
|
-
if (reactContext) return reactContext;
|
|
10
|
-
return getRegisteredSSRCollector();
|
|
11
|
-
}
|
|
12
6
|
/**
|
|
13
|
-
*
|
|
7
|
+
* Register a CSS @property custom property.
|
|
14
8
|
* This enables advanced features like animating custom properties.
|
|
15
9
|
*
|
|
16
10
|
* Note: @property rules are global and persistent once defined.
|
|
17
|
-
* The
|
|
11
|
+
* The function ensures the property is only registered once per root.
|
|
18
12
|
*
|
|
19
13
|
* Accepts tasty token syntax for the property name:
|
|
20
14
|
* - `$name` → defines `--name`
|
|
21
15
|
* - `#name` → defines `--name-color` (auto-sets syntax: '<color>', defaults initialValue: 'transparent')
|
|
22
16
|
* - `--name` → defines `--name` (legacy format)
|
|
23
17
|
*
|
|
18
|
+
* Works in all environments: client, SSR with collector, and React Server Components.
|
|
19
|
+
*
|
|
24
20
|
* @param name - The property token ($name, #name) or CSS property name (--name)
|
|
25
21
|
* @param options - Property configuration
|
|
26
22
|
*
|
|
@@ -62,21 +58,12 @@ function resolveSSRCollector(reactContext) {
|
|
|
62
58
|
* ```
|
|
63
59
|
*/
|
|
64
60
|
function useProperty(name, options) {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
initialValue: options.initialValue
|
|
72
|
-
});
|
|
73
|
-
}, [
|
|
74
|
-
options?.syntax,
|
|
75
|
-
options?.inherits,
|
|
76
|
-
options?.initialValue
|
|
77
|
-
]);
|
|
78
|
-
useMemo(() => {
|
|
79
|
-
if (!ssrCollector || !name) return;
|
|
61
|
+
if (!name) {
|
|
62
|
+
console.warn(`[Tasty] useProperty: property name is required`);
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const ssrCollector = getRegisteredSSRCollector();
|
|
66
|
+
if (ssrCollector) {
|
|
80
67
|
ssrCollector.collectInternals();
|
|
81
68
|
const css = formatPropertyCSS(name, {
|
|
82
69
|
syntax: options?.syntax,
|
|
@@ -84,31 +71,28 @@ function useProperty(name, options) {
|
|
|
84
71
|
initialValue: options?.initialValue
|
|
85
72
|
});
|
|
86
73
|
if (css) ssrCollector.collectProperty(name, css);
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
useInsertionEffect(() => {
|
|
93
|
-
if (!name) {
|
|
94
|
-
console.warn(`[Tasty] useProperty: property name is required`);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
const injector = getGlobalInjector();
|
|
98
|
-
if (injector.isPropertyDefined(name, { root: options?.root })) return;
|
|
99
|
-
injector.property(name, {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
if (isServerEnvironment()) {
|
|
77
|
+
const rscCache = getRSCCache();
|
|
78
|
+
const css = formatPropertyCSS(name, {
|
|
100
79
|
syntax: options?.syntax,
|
|
101
80
|
inherits: options?.inherits,
|
|
102
|
-
initialValue: options?.initialValue
|
|
103
|
-
root: options?.root
|
|
81
|
+
initialValue: options?.initialValue
|
|
104
82
|
});
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
83
|
+
if (css) pushRSCCSS(rscCache, `__prop:${name}`, css);
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
const injector = getGlobalInjector();
|
|
87
|
+
if (injector.isPropertyDefined(name, { root: options?.root })) return;
|
|
88
|
+
injector.property(name, {
|
|
89
|
+
syntax: options?.syntax,
|
|
90
|
+
inherits: options?.inherits,
|
|
91
|
+
initialValue: options?.initialValue,
|
|
92
|
+
root: options?.root
|
|
93
|
+
});
|
|
110
94
|
}
|
|
111
|
-
|
|
112
95
|
//#endregion
|
|
113
96
|
export { useProperty };
|
|
97
|
+
|
|
114
98
|
//# sourceMappingURL=useProperty.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useProperty.js","names":[],"sources":["../../src/hooks/useProperty.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"file":"useProperty.js","names":[],"sources":["../../src/hooks/useProperty.ts"],"sourcesContent":["import { getGlobalInjector } from '../config';\nimport { pushRSCCSS, getRSCCache, isServerEnvironment } from '../rsc-cache';\nimport { formatPropertyCSS } from '../ssr/format-property';\nimport { getRegisteredSSRCollector } from '../ssr/ssr-collector-ref';\n\nexport interface UsePropertyOptions {\n /**\n * CSS syntax string for the property (e.g., '<color>', '<length>', '<angle>').\n * For color tokens (#name), this is auto-set to '<color>' and cannot be overridden.\n * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@property/syntax\n */\n syntax?: string;\n /**\n * Whether the property inherits from parent elements\n * @default true\n */\n inherits?: boolean;\n /**\n * Initial value for the property.\n * For color tokens (#name), this defaults to 'transparent' if not specified.\n */\n initialValue?: string | number;\n /**\n * Shadow root or document to inject into\n */\n root?: Document | ShadowRoot;\n}\n\n/**\n * Register a CSS @property custom property.\n * This enables advanced features like animating custom properties.\n *\n * Note: @property rules are global and persistent once defined.\n * The function ensures the property is only registered once per root.\n *\n * Accepts tasty token syntax for the property name:\n * - `$name` → defines `--name`\n * - `#name` → defines `--name-color` (auto-sets syntax: '<color>', defaults initialValue: 'transparent')\n * - `--name` → defines `--name` (legacy format)\n *\n * Works in all environments: client, SSR with collector, and React Server Components.\n *\n * @param name - The property token ($name, #name) or CSS property name (--name)\n * @param options - Property configuration\n *\n * @example Basic property with token syntax\n * ```tsx\n * function Spinner() {\n * useProperty('$rotation', {\n * syntax: '<angle>',\n * inherits: false,\n * initialValue: '0deg',\n * });\n *\n * return <div className=\"spinner\" />;\n * }\n * ```\n *\n * @example Color property with token syntax (auto-sets syntax)\n * ```tsx\n * function MyComponent() {\n * useProperty('#theme', {\n * initialValue: 'red', // syntax: '<color>' is auto-set\n * });\n *\n * // Now --theme-color can be animated with CSS transitions\n * return <div style={{ '--theme-color': 'blue' } as React.CSSProperties}>Colored</div>;\n * }\n * ```\n *\n * @example Legacy format (still supported)\n * ```tsx\n * function ResizableBox() {\n * useProperty('--box-size', {\n * syntax: '<length>',\n * initialValue: '100px',\n * });\n *\n * return <div style={{ width: 'var(--box-size)' }} />;\n * }\n * ```\n */\nexport function useProperty(name: string, options?: UsePropertyOptions): void {\n if (!name) {\n if (process.env.NODE_ENV !== 'production') {\n console.warn(`[Tasty] useProperty: property name is required`);\n }\n return;\n }\n\n const ssrCollector = getRegisteredSSRCollector();\n\n if (ssrCollector) {\n ssrCollector.collectInternals();\n\n const css = formatPropertyCSS(name, {\n syntax: options?.syntax,\n inherits: options?.inherits,\n initialValue: options?.initialValue,\n });\n if (css) {\n ssrCollector.collectProperty(name, css);\n }\n return;\n }\n\n if (isServerEnvironment()) {\n const rscCache = getRSCCache();\n const css = formatPropertyCSS(name, {\n syntax: options?.syntax,\n inherits: options?.inherits,\n initialValue: options?.initialValue,\n });\n if (css) {\n pushRSCCSS(rscCache, `__prop:${name}`, css);\n }\n return;\n }\n\n const injector = getGlobalInjector();\n\n if (injector.isPropertyDefined(name, { root: options?.root })) {\n return;\n }\n\n injector.property(name, {\n syntax: options?.syntax,\n inherits: options?.inherits,\n initialValue: options?.initialValue,\n root: options?.root,\n });\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkFA,SAAgB,YAAY,MAAc,SAAoC;AAC5E,KAAI,CAAC,MAAM;AAEP,UAAQ,KAAK,iDAAiD;AAEhE;;CAGF,MAAM,eAAe,2BAA2B;AAEhD,KAAI,cAAc;AAChB,eAAa,kBAAkB;EAE/B,MAAM,MAAM,kBAAkB,MAAM;GAClC,QAAQ,SAAS;GACjB,UAAU,SAAS;GACnB,cAAc,SAAS;GACxB,CAAC;AACF,MAAI,IACF,cAAa,gBAAgB,MAAM,IAAI;AAEzC;;AAGF,KAAI,qBAAqB,EAAE;EACzB,MAAM,WAAW,aAAa;EAC9B,MAAM,MAAM,kBAAkB,MAAM;GAClC,QAAQ,SAAS;GACjB,UAAU,SAAS;GACnB,cAAc,SAAS;GACxB,CAAC;AACF,MAAI,IACF,YAAW,UAAU,UAAU,QAAQ,IAAI;AAE7C;;CAGF,MAAM,WAAW,mBAAmB;AAEpC,KAAI,SAAS,kBAAkB,MAAM,EAAE,MAAM,SAAS,MAAM,CAAC,CAC3D;AAGF,UAAS,SAAS,MAAM;EACtB,QAAQ,SAAS;EACjB,UAAU,SAAS;EACnB,cAAc,SAAS;EACvB,MAAM,SAAS;EAChB,CAAC"}
|
|
@@ -1,51 +1,15 @@
|
|
|
1
1
|
//#region src/hooks/useRawCSS.d.ts
|
|
2
2
|
interface UseRawCSSOptions {
|
|
3
3
|
root?: Document | ShadowRoot;
|
|
4
|
+
/**
|
|
5
|
+
* Stable identifier for update tracking (client-only). When provided,
|
|
6
|
+
* changing the CSS content will dispose the previous injection and inject
|
|
7
|
+
* the new one. Without an id, deduplication is purely content-based (same
|
|
8
|
+
* CSS is injected only once). In RSC mode, renders are single-pass so
|
|
9
|
+
* update tracking does not apply.
|
|
10
|
+
*/
|
|
11
|
+
id?: string;
|
|
4
12
|
}
|
|
5
|
-
/**
|
|
6
|
-
* Hook to inject raw CSS text directly without parsing.
|
|
7
|
-
* This is a low-overhead alternative for injecting global CSS that doesn't need tasty processing.
|
|
8
|
-
*
|
|
9
|
-
* The CSS is inserted into a separate style element (data-tasty-raw) to avoid conflicts
|
|
10
|
-
* with tasty's chunked style sheets.
|
|
11
|
-
*
|
|
12
|
-
* @example Static CSS string
|
|
13
|
-
* ```tsx
|
|
14
|
-
* function GlobalStyles() {
|
|
15
|
-
* useRawCSS(`
|
|
16
|
-
* body {
|
|
17
|
-
* margin: 0;
|
|
18
|
-
* padding: 0;
|
|
19
|
-
* font-family: sans-serif;
|
|
20
|
-
* }
|
|
21
|
-
* `);
|
|
22
|
-
*
|
|
23
|
-
* return null;
|
|
24
|
-
* }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @example Factory function with dependencies (like useMemo)
|
|
28
|
-
* ```tsx
|
|
29
|
-
* function ThemeStyles({ theme }: { theme: 'light' | 'dark' }) {
|
|
30
|
-
* useRawCSS(() => `
|
|
31
|
-
* :root {
|
|
32
|
-
* --bg-color: ${theme === 'dark' ? '#1a1a1a' : '#ffffff'};
|
|
33
|
-
* --text-color: ${theme === 'dark' ? '#ffffff' : '#1a1a1a'};
|
|
34
|
-
* }
|
|
35
|
-
* `, [theme]);
|
|
36
|
-
*
|
|
37
|
-
* return null;
|
|
38
|
-
* }
|
|
39
|
-
* ```
|
|
40
|
-
*
|
|
41
|
-
* @example With options
|
|
42
|
-
* ```tsx
|
|
43
|
-
* function ShadowStyles({ shadowRoot }) {
|
|
44
|
-
* useRawCSS(() => `.scoped { color: red; }`, [], { root: shadowRoot });
|
|
45
|
-
* return null;
|
|
46
|
-
* }
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
13
|
declare function useRawCSS(css: string, options?: UseRawCSSOptions): void;
|
|
50
14
|
declare function useRawCSS(factory: () => string, deps: readonly unknown[], options?: UseRawCSSOptions): void;
|
|
51
15
|
//#endregion
|