@@ -37,26 +37,26 @@ yarn add @crescendolab/css-var-ts
3737import { cssVarUtils } from " @crescendolab/css-var-ts" ;
3838
3939// 1. Define a base palette
40- const palette = cssVarUtils .define ({
40+ const paletteDefinition = cssVarUtils .define ({
4141 primaryBlue: " #0074D9" ,
4242 accentPink: " #F012BE" ,
4343 neutralBg: " #FFFFFF" ,
4444 neutralFg: " #111111" ,
4545});
4646
4747// 2. Define semantic tokens referencing the palette (type‑safe)
48- const semantic = cssVarUtils .define ({
49- brand: palette .getValue (" primaryBlue" ),
50- text: palette .getValue (" neutralFg" ),
51- background: palette .getValue (" neutralBg" ),
48+ const semanticDefinition = cssVarUtils .define ({
49+ brand: paletteDefinition .getValue (" primaryBlue" ),
50+ text: paletteDefinition .getValue (" neutralFg" ),
51+ background: paletteDefinition .getValue (" neutralBg" ),
5252});
5353
5454// 3. Use in styles
5555const style: React .CSSProperties = {
56- ... palette .cssProps ,
57- ... semantic .cssProps ,
58- color: semantic .getValue (" text" ),
59- backgroundColor: semantic .getValue (" background" ),
56+ ... paletteDefinition .cssProps ,
57+ ... semanticDefinition .cssProps ,
58+ color: semanticDefinition .getValue (" text" ),
59+ backgroundColor: semanticDefinition .getValue (" background" ),
6060};
6161```
6262
@@ -67,36 +67,34 @@ Resulting (example) generated variable keys (random 8‑char suffix) look like:
6767--accentpink-9fe012ab
6868```
6969
70- ---
71-
7270## 🧩 Basic Usage (from Storybook “01_basic”)
7371
7472``` ts
7573import { cssVarUtils } from " @crescendolab/css-var-ts" ;
7674
75+ // Base palette
7776const paletteDefinition = cssVarUtils .define ({
7877 navy: " #001F3F" ,
7978 blue: " #0074D9" ,
8079 aqua: " #7FDBFF" ,
81- // ...
80+ black: " #111111 " ,
8281});
8382
84- const semantic = cssVarUtils .define ({
83+ // Semantic tokens referencing base palette
84+ const semanticDefinition = cssVarUtils .define ({
8585 primary: paletteDefinition .getValue (" navy" ),
8686 foreground: paletteDefinition .getValue (" black" ),
8787});
8888
8989// Override one semantic var dynamically
9090const dynamicStyle = {
9191 ... paletteDefinition .cssProps ,
92- ... semantic .cssProps ,
93- [semantic .getKey (" primary" )]: paletteDefinition .getValue (" blue" ),
94- color: semantic .getValue (" foreground" ),
92+ ... semanticDefinition .cssProps ,
93+ [semanticDefinition .getKey (" primary" )]: paletteDefinition .getValue (" blue" ),
94+ color: semanticDefinition .getValue (" foreground" ),
9595};
9696```
9797
98- Why the two steps? You keep a raw color inventory (can later switch based on theme) and build semantic tokens referencing it. Both sets remain type‑safe.
99-
10098---
10199
102100## 🎨 Integrations
@@ -150,18 +148,42 @@ const button = css({
150148Use ` createCssVarUtils ` to fully control how variable names are produced (e.g. ephemeral / randomized keys).
151149
152150``` ts
153- import { createCssVarUtils } from " @crescendolab/css-var-ts" ;
151+ import {
152+ createCssVarUtils ,
153+ randomString ,
154+ slugify ,
155+ } from " @crescendolab/css-var-ts" ;
156+
157+ const myCssVarUtils = createCssVarUtils ({
158+ recordKeyToCssVarKey : (key ) =>
159+ ` --my-${slugify (key )}-${randomString (8 )} ` as const ,
160+ });
161+
162+ const myDefinition = myCssVarUtils .define ({
163+ primary: " #0074D9" ,
164+ });
165+
166+ myDefinition .getKey (" primary" ); // different each load
167+ ```
168+
169+ #### Static (Deterministic) Keys
170+
171+ If you prefer fully readable, deterministic variable names (no random suffix) you can supply a static strategy. Be sure to manually ensure uniqueness across packages / bundles when using this approach.
172+
173+ ``` ts
174+ import { createCssVarUtils , slugify } from " @crescendolab/css-var-ts" ;
154175
155- const randomCssVarUtils = createCssVarUtils ({
156- recordKeyToCssVarKey : () =>
157- ` --random-${Math .random ().toString (16 ).slice (2 )} ` as const ,
176+ const staticCssVarUtils = createCssVarUtils ({
177+ recordKeyToCssVarKey : (key ) => ` --static-${slugify (key )} ` as const ,
158178});
159179
160- const randomVars = randomCssVarUtils .define ({
180+ const staticDefinition = staticCssVarUtils .define ({
161181 primary: " #0074D9" ,
182+ accent: " #F012BE" ,
162183});
163184
164- randomVars .getKey (" primary" ); // different each load
185+ staticDefinition .getKey (" primary" ); // "--static-primary"
186+ staticDefinition .getValue (" primary" ); // "var(--static-primary)"
165187```
166188
167189### ` @property ` Registration
@@ -185,21 +207,24 @@ CSS.registerProperty({
185207
186208For large-scale web applications (mono-repos, micro frontends, dynamic plugin architectures) you should take extra precautions to avoid accidental variable name collisions and to harden your design system surface.
187209
188- 1 . Strengthen uniqueness: Provide a custom ` recordKeyToCssVarKey ` that injects a namespace (package name) plus a stable build hash or random suffix.
210+ 1 . Strengthen uniqueness: Provide a custom ` recordKeyToCssVarKey ` that injects a namespace (package name) plus a short random suffix. (You can optionally add build / commit info if desired.)
189211
190212 ``` ts
191- import { createCssVarUtils } from " @crescendolab/css-var-ts" ;
213+ import {
214+ createCssVarUtils ,
215+ randomString ,
216+ slugify ,
217+ } from " @crescendolab/css-var-ts" ;
192218
193- const ns = process .env .APP_NAMESPACE ?? " app" ; // e.g. marketing, analytics
194- const buildId = process .env .COMMIT_SHA ?.slice (0 , 7 ) ?? " dev" ;
219+ const namespace = process .env .APP_NAMESPACE ?? " app" ; // e.g. marketing, analytics
195220
196221 const scopedCssVarUtils = createCssVarUtils ({
197- recordKeyToCssVarKey : (k ) =>
198- ` --${ns }-${buildId }-${k }-${ Math . random (). toString ( 36 ). slice ( 2 , 8 )} ` as const ,
222+ recordKeyToCssVarKey : (key ) =>
223+ ` --${namespace }-${slugify ( key ) }-${randomString ( 8 )} ` as const ,
199224 });
200225 ```
201226
202- For deterministic builds replace ` Math.random( )` with a hash of ` (ns + buildId + k) ` .
227+ For deterministic builds replace ` randomString(8 )` with a stable hash (e.g. of ` namespace + key ` ) .
203228
2042292 . Strongly recommended: Register core design tokens via ` @property ` to enforce syntax (e.g. ` <color> ` , ` <length> ` ) and enable smoother transitions & validation.
2052303 . Expose only semantic tokens to feature teams; keep raw palette tokens private to your design system package.
0 commit comments