@csszyx/vars 0.1.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 csszyx contributors
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/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@csszyx/vars",
3
+ "version": "0.1.0",
4
+ "description": "CSS custom property helpers — inject and patch CSS variables for runtime-driven styling",
5
+ "type": "module",
6
+ "exports": {
7
+ ".": {
8
+ "import": "./src/index.ts",
9
+ "types": "./src/index.ts"
10
+ },
11
+ "./react": {
12
+ "import": "./src/react.ts",
13
+ "types": "./src/react.ts"
14
+ }
15
+ },
16
+ "peerDependencies": {
17
+ "react": ">=18.0.0"
18
+ },
19
+ "peerDependenciesMeta": {
20
+ "react": {
21
+ "optional": true
22
+ }
23
+ },
24
+ "keywords": [
25
+ "csszyx",
26
+ "css-variables",
27
+ "css-custom-properties",
28
+ "runtime-styling"
29
+ ],
30
+ "license": "MIT"
31
+ }
package/src/index.ts ADDED
@@ -0,0 +1,65 @@
1
+ /**
2
+ * @csszyx/vars — CSS custom property helpers for runtime-driven styling.
3
+ *
4
+ * Zero-runtime CSSzyx handles static sz props at build time.
5
+ * This package handles values that come from an API, CMS, or user config
6
+ * at runtime, injected as CSS custom properties so Tailwind-generated
7
+ * classes like `bg-(--cfg-bg)` resolve correctly.
8
+ *
9
+ * Works standalone (no React required). React hook: @csszyx/vars/react
10
+ */
11
+
12
+ /**
13
+ *
14
+ */
15
+ export type SzVars = Record<string, string | number>;
16
+
17
+ /**
18
+ * Apply CSS custom properties to an element.
19
+ * Keys are auto-prefixed with `--` if not already.
20
+ * Returns a cleanup function that removes all applied properties.
21
+ *
22
+ * @param vars - Record of CSS var name → value. Keys auto-prefixed with `--`.
23
+ * @param element - Target element. Defaults to `document.documentElement` (`:root`).
24
+ * @returns Cleanup function that removes all applied properties.
25
+ *
26
+ * @example
27
+ * const cleanup = applySzVars({ 'form-bg': '#fff', 'form-p': '24px' }, formEl);
28
+ * // → formEl.style: --form-bg: #fff; --form-p: 24px
29
+ * // cleanup() → removes them
30
+ */
31
+ export function applySzVars(
32
+ vars: SzVars,
33
+ element: HTMLElement = document.documentElement,
34
+ ): () => void {
35
+ const applied: string[] = [];
36
+
37
+ for (const [key, value] of Object.entries(vars)) {
38
+ const name = key.startsWith('--') ? key : `--${key}`;
39
+ element.style.setProperty(name, String(value));
40
+ applied.push(name);
41
+ }
42
+
43
+ return () => {
44
+ for (const name of applied) {
45
+ element.style.removeProperty(name);
46
+ }
47
+ };
48
+ }
49
+
50
+ /**
51
+ * Update CSS custom properties on an element without full re-apply.
52
+ * Only sets changed values — useful for frequent updates (drag sliders, etc.)
53
+ *
54
+ * @param vars - Record of CSS var name → value. Keys auto-prefixed with `--`.
55
+ * @param element - Target element. Defaults to `document.documentElement` (`:root`).
56
+ */
57
+ export function patchSzVars(
58
+ vars: SzVars,
59
+ element: HTMLElement = document.documentElement,
60
+ ): void {
61
+ for (const [key, value] of Object.entries(vars)) {
62
+ const name = key.startsWith('--') ? key : `--${key}`;
63
+ element.style.setProperty(name, String(value));
64
+ }
65
+ }
package/src/react.ts ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * @csszyx/dynamic/react — React hook for CSS variable injection.
3
+ * Requires React >=18. Import from "@csszyx/dynamic/react".
4
+ */
5
+
6
+ import { type RefObject, useEffect, useRef } from 'react';
7
+
8
+ import { patchSzVars, type SzVars } from './index.js';
9
+
10
+ /**
11
+ * Reactively apply CSS custom properties to an element.
12
+ * Re-applies whenever `vars` content changes.
13
+ *
14
+ * @param vars - Record of CSS var name → value. Keys auto-prefixed with `--`.
15
+ * @param ref - Target element ref. Defaults to document.documentElement (:root).
16
+ *
17
+ * @example
18
+ * const formRef = useRef<HTMLDivElement>(null);
19
+ * useSzVars({ 'form-bg': config.bg, 'form-p': config.padding }, formRef);
20
+ * // → <div ref={formRef}> gets --form-bg and --form-p on every config change
21
+ */
22
+ export function useSzVars(vars: SzVars, ref?: RefObject<HTMLElement | null>): void {
23
+ // Keep a ref to latest vars to avoid stale closure in effect
24
+ const latestVars = useRef(vars);
25
+ latestVars.current = vars;
26
+
27
+ useEffect(() => {
28
+ const el = ref?.current ?? document.documentElement;
29
+ if (!el) {return;}
30
+ patchSzVars(latestVars.current, el);
31
+ });
32
+ // No dep array — runs after every render, intentional:
33
+ // patchSzVars only calls setProperty for entries that changed (cheap DOM op).
34
+ }