@csszyx/vars 0.10.11 → 0.10.12
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 +77 -0
- package/package.json +1 -1
- package/src/react.ts +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# @csszyx/vars
|
|
2
|
+
|
|
3
|
+
CSS custom-property helpers for [CSSzyx](https://github.com/nguyennhutien/csszyx)
|
|
4
|
+
— inject and patch CSS variables for runtime-driven values.
|
|
5
|
+
|
|
6
|
+
Zero-runtime CSSzyx handles static `sz` props at build time. This package
|
|
7
|
+
handles the **values** that arrive at runtime (API, CMS, user config): inject
|
|
8
|
+
them as CSS custom properties so build-time Tailwind classes like
|
|
9
|
+
`bg-(--cfg-bg)` resolve against them. The classes stay static, safelisted, and
|
|
10
|
+
mangled — only the variable values move at runtime.
|
|
11
|
+
|
|
12
|
+
```tsx
|
|
13
|
+
// Build time (static, zero runtime):
|
|
14
|
+
<form sz={{ bg: "--cfg-bg", p: "--cfg-p" }} />;
|
|
15
|
+
|
|
16
|
+
// Runtime — point the variables at the config values:
|
|
17
|
+
import { applySzVars } from "@csszyx/vars";
|
|
18
|
+
applySzVars(
|
|
19
|
+
{ "cfg-bg": config.background, "cfg-p": `${config.padding}px` },
|
|
20
|
+
formEl,
|
|
21
|
+
);
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pnpm add @csszyx/vars
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## API
|
|
31
|
+
|
|
32
|
+
### `applySzVars(vars, element?)`
|
|
33
|
+
|
|
34
|
+
Sets each entry as a CSS custom property on `element` (defaults to `:root`).
|
|
35
|
+
Keys are auto-prefixed with `--`. Returns a cleanup function that removes
|
|
36
|
+
everything it set.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
const cleanup = applySzVars({ "form-bg": "#fff", "form-p": "24px" }, formEl);
|
|
40
|
+
// formEl.style: --form-bg: #fff; --form-p: 24px
|
|
41
|
+
cleanup(); // removes them
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### `patchSzVars(vars, element?)`
|
|
45
|
+
|
|
46
|
+
Sets values without tracking a cleanup — for frequent updates (drag sliders,
|
|
47
|
+
animation-driven values) where you overwrite the same properties each tick.
|
|
48
|
+
|
|
49
|
+
## React
|
|
50
|
+
|
|
51
|
+
```tsx
|
|
52
|
+
import { useSzVars } from "@csszyx/vars/react";
|
|
53
|
+
|
|
54
|
+
function ConfigurableForm({ config }) {
|
|
55
|
+
const ref = useRef<HTMLFormElement>(null);
|
|
56
|
+
// Re-applies whenever the values change; targets :root if no ref given.
|
|
57
|
+
useSzVars({ "form-bg": config.bg, "form-p": `${config.p}px` }, ref);
|
|
58
|
+
return <form ref={ref} sz={{ bg: "--form-bg", p: "--form-p" }} />;
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Requires React ≥ 18. The core helpers work standalone — no React needed.
|
|
63
|
+
|
|
64
|
+
## vars vs `@csszyx/dynamic`
|
|
65
|
+
|
|
66
|
+
- **`@csszyx/vars`** — the _rule_ is known at build time, only the _value_
|
|
67
|
+
changes: `bg-(--cfg-bg)` with a runtime-fed variable. Cheapest option; no CSS
|
|
68
|
+
is generated at runtime.
|
|
69
|
+
- **[`@csszyx/dynamic`](https://www.npmjs.com/package/@csszyx/dynamic)** — the
|
|
70
|
+
_rule itself_ is unknown until runtime (arbitrary sz objects from a CMS).
|
|
71
|
+
Generates and injects CSS in the browser.
|
|
72
|
+
|
|
73
|
+
Prefer vars when a CSS variable can express the change.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
package/package.json
CHANGED
package/src/react.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @csszyx/
|
|
3
|
-
* Requires React >=18. Import from "@csszyx/
|
|
2
|
+
* @csszyx/vars/react — React hook for CSS variable injection.
|
|
3
|
+
* Requires React >=18. Import from "@csszyx/vars/react".
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { type RefObject, useEffect, useRef } from 'react';
|