hextimator 0.3.2 → 0.5.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/CHANGELOG.md +30 -1
- package/dist/{HextimatePaletteBuilder-xqJwP_G2.d.ts → HextimatePaletteBuilder-C8I05ZmB.d.ts} +28 -5
- package/dist/{chunk-2OMHLWID.js → chunk-HCVIQY7G.js} +81 -23
- package/dist/cli.js +105 -25
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/react.d.ts +48 -96
- package/dist/react.js +277 -137
- package/llms.txt +34 -16
- package/package.json +1 -1
package/dist/react.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
hextimate
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-HCVIQY7G.js";
|
|
4
4
|
|
|
5
5
|
// src/react/hextimator-provider.tsx
|
|
6
6
|
import {
|
|
7
7
|
useCallback,
|
|
8
|
-
useEffect
|
|
9
|
-
useMemo
|
|
8
|
+
useEffect,
|
|
9
|
+
useMemo,
|
|
10
10
|
useState
|
|
11
11
|
} from "react";
|
|
12
12
|
|
|
@@ -16,50 +16,11 @@ var HextimatorContext = createContext(
|
|
|
16
16
|
null
|
|
17
17
|
);
|
|
18
18
|
|
|
19
|
-
// src/react/mode.ts
|
|
20
|
-
import { useSyncExternalStore } from "react";
|
|
21
|
-
var darkQuery = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
22
|
-
function useOsPrefersDark() {
|
|
23
|
-
return useSyncExternalStore(
|
|
24
|
-
(cb) => {
|
|
25
|
-
if (!darkQuery) return () => {
|
|
26
|
-
};
|
|
27
|
-
darkQuery.addEventListener("change", cb);
|
|
28
|
-
return () => darkQuery.removeEventListener("change", cb);
|
|
29
|
-
},
|
|
30
|
-
() => darkQuery?.matches ?? false,
|
|
31
|
-
() => false
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
function applyModeToDOM(mode, darkMode) {
|
|
35
|
-
if (typeof document === "undefined") return;
|
|
36
|
-
if (darkMode === false || darkMode.type === "media") return;
|
|
37
|
-
const root = document.documentElement;
|
|
38
|
-
if (darkMode.type === "data") {
|
|
39
|
-
const attr = darkMode.attribute ?? "data-theme";
|
|
40
|
-
if (mode) {
|
|
41
|
-
root.setAttribute(attr, mode);
|
|
42
|
-
} else {
|
|
43
|
-
root.removeAttribute(attr);
|
|
44
|
-
}
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
const cls = darkMode.className ?? "dark";
|
|
48
|
-
const lightCls = cls === "dark" ? "light" : `not-${cls}`;
|
|
49
|
-
root.classList.remove(cls, lightCls);
|
|
50
|
-
if (mode) {
|
|
51
|
-
root.classList.add(mode === "dark" ? cls : lightCls);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// src/react/use-hextimator.ts
|
|
56
|
-
import { useEffect, useMemo } from "react";
|
|
57
|
-
|
|
58
19
|
// src/react/css.ts
|
|
59
20
|
function buildStyleContent(palette, darkMode, cssPrefix, selector = ":root") {
|
|
60
21
|
const lightEntries = Object.entries(palette.light);
|
|
61
22
|
const darkEntries = Object.entries(palette.dark);
|
|
62
|
-
const toVars = (entries, lineIndent) => entries.map(([key, value]) => `${lineIndent}${cssPrefix}
|
|
23
|
+
const toVars = (entries, lineIndent) => entries.map(([key, value]) => `${lineIndent}${cssPrefix}--${key}: ${value};`).join("\n");
|
|
63
24
|
const lightVars = toVars(lightEntries, " ");
|
|
64
25
|
const darkVars = toVars(darkEntries, " ");
|
|
65
26
|
const darkVarsInMediaInner = toVars(darkEntries, " ");
|
|
@@ -125,16 +86,58 @@ ${darkVars}
|
|
|
125
86
|
}
|
|
126
87
|
function buildTargetedVars(palette, darkMode, cssPrefix) {
|
|
127
88
|
const prefixEntries = (entries) => entries.map(
|
|
128
|
-
([key, value]) => [`${cssPrefix}
|
|
89
|
+
([key, value]) => [`${cssPrefix}--${key}`, value]
|
|
129
90
|
);
|
|
130
91
|
return {
|
|
131
|
-
light: prefixEntries(
|
|
132
|
-
Object.entries(palette.light)
|
|
133
|
-
),
|
|
92
|
+
light: prefixEntries(Object.entries(palette.light)),
|
|
134
93
|
dark: darkMode !== false ? prefixEntries(Object.entries(palette.dark)) : []
|
|
135
94
|
};
|
|
136
95
|
}
|
|
137
96
|
|
|
97
|
+
// src/react/mode.ts
|
|
98
|
+
import { useSyncExternalStore } from "react";
|
|
99
|
+
var darkQuery = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
100
|
+
function useOsPrefersDark() {
|
|
101
|
+
return useSyncExternalStore(
|
|
102
|
+
(cb) => {
|
|
103
|
+
if (!darkQuery) return () => {
|
|
104
|
+
};
|
|
105
|
+
darkQuery.addEventListener("change", cb);
|
|
106
|
+
return () => darkQuery.removeEventListener("change", cb);
|
|
107
|
+
},
|
|
108
|
+
() => darkQuery?.matches ?? false,
|
|
109
|
+
() => false
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
function applyModeToDOM(mode, darkMode) {
|
|
113
|
+
if (typeof document === "undefined") return;
|
|
114
|
+
if (darkMode === false || darkMode.type === "media") return;
|
|
115
|
+
const root = document.documentElement;
|
|
116
|
+
if (darkMode.type === "data") {
|
|
117
|
+
const attr = darkMode.attribute ?? "data-theme";
|
|
118
|
+
if (mode) {
|
|
119
|
+
root.setAttribute(attr, mode);
|
|
120
|
+
} else {
|
|
121
|
+
root.removeAttribute(attr);
|
|
122
|
+
}
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
const cls = darkMode.className ?? "dark";
|
|
126
|
+
const lightCls = cls === "dark" ? "light" : `not-${cls}`;
|
|
127
|
+
root.classList.remove(cls, lightCls);
|
|
128
|
+
if (mode) {
|
|
129
|
+
root.classList.add(mode === "dark" ? cls : lightCls);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// src/react/types.ts
|
|
134
|
+
function normalizeColorProp(value) {
|
|
135
|
+
if (typeof value === "string") {
|
|
136
|
+
return { light: value, dark: value };
|
|
137
|
+
}
|
|
138
|
+
return { light: value.light, dark: value.dark };
|
|
139
|
+
}
|
|
140
|
+
|
|
138
141
|
// src/react/use-stable-options.ts
|
|
139
142
|
import { useRef } from "react";
|
|
140
143
|
function useStableOptions(options) {
|
|
@@ -158,56 +161,14 @@ function useStableOptions(options) {
|
|
|
158
161
|
return ref.current;
|
|
159
162
|
}
|
|
160
163
|
|
|
161
|
-
// src/react/use-hextimator.ts
|
|
162
|
-
function useHextimator(color, options) {
|
|
163
|
-
const stable = useStableOptions(options);
|
|
164
|
-
const configure = options?.configure;
|
|
165
|
-
const target = options?.target;
|
|
166
|
-
const presets = stable?.presets;
|
|
167
|
-
const palette = useMemo(() => {
|
|
168
|
-
const builder = hextimate(color);
|
|
169
|
-
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
170
|
-
builder.style(stable.style);
|
|
171
|
-
}
|
|
172
|
-
for (const p of presets ?? []) builder.preset(p);
|
|
173
|
-
configure?.(builder);
|
|
174
|
-
return builder.format({
|
|
175
|
-
...stable?.format,
|
|
176
|
-
as: "css"
|
|
177
|
-
});
|
|
178
|
-
}, [color, stable?.style, presets, stable?.format, configure]);
|
|
179
|
-
const darkMode = stable?.darkMode ?? { type: "media" };
|
|
180
|
-
const cssPrefix = stable?.cssPrefix ?? "";
|
|
181
|
-
useEffect(() => {
|
|
182
|
-
const el = target?.current;
|
|
183
|
-
if (el) {
|
|
184
|
-
const vars = buildTargetedVars(palette, darkMode, cssPrefix);
|
|
185
|
-
for (const [key, value] of vars.light) {
|
|
186
|
-
el.style.setProperty(key, value);
|
|
187
|
-
}
|
|
188
|
-
return () => {
|
|
189
|
-
for (const [key] of vars.light) {
|
|
190
|
-
el.style.removeProperty(key);
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
const style = document.createElement("style");
|
|
195
|
-
style.setAttribute("data-hextimator", "");
|
|
196
|
-
style.textContent = buildStyleContent(palette, darkMode, cssPrefix);
|
|
197
|
-
document.head.appendChild(style);
|
|
198
|
-
return () => {
|
|
199
|
-
document.head.removeChild(style);
|
|
200
|
-
};
|
|
201
|
-
}, [palette, darkMode, cssPrefix, target]);
|
|
202
|
-
return palette;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
164
|
// src/react/hextimator-provider.tsx
|
|
206
165
|
import { jsx } from "react/jsx-runtime";
|
|
207
166
|
function HextimatorProvider({
|
|
208
167
|
children,
|
|
209
168
|
defaultColor,
|
|
169
|
+
onColorChange,
|
|
210
170
|
defaultMode: initialMode = "system",
|
|
171
|
+
onModePreferenceChange,
|
|
211
172
|
style: initialStyle,
|
|
212
173
|
presets: initialPresets,
|
|
213
174
|
format: formatOpts,
|
|
@@ -216,48 +177,139 @@ function HextimatorProvider({
|
|
|
216
177
|
cssPrefix,
|
|
217
178
|
target
|
|
218
179
|
}) {
|
|
219
|
-
const [
|
|
220
|
-
|
|
180
|
+
const [lightColor, setLightColorState] = useState(
|
|
181
|
+
() => normalizeColorProp(defaultColor).light
|
|
182
|
+
);
|
|
183
|
+
const [darkColor, setDarkColorState] = useState(
|
|
184
|
+
() => normalizeColorProp(defaultColor).dark
|
|
185
|
+
);
|
|
186
|
+
const [modePreference, setModePreferenceState] = useState(initialMode);
|
|
221
187
|
const [style, setStyle] = useState(initialStyle);
|
|
222
188
|
const [presets, setPresets] = useState(initialPresets);
|
|
223
189
|
const [configure, setConfigureState] = useState(() => initialConfigure);
|
|
224
190
|
const osDark = useOsPrefersDark();
|
|
225
191
|
const mode = modePreference === "system" ? osDark ? "dark" : "light" : modePreference;
|
|
226
192
|
const resolvedDarkMode = darkMode ?? { type: "media" };
|
|
227
|
-
|
|
193
|
+
useEffect(() => {
|
|
228
194
|
applyModeToDOM(
|
|
229
195
|
modePreference === "system" ? null : modePreference,
|
|
230
196
|
resolvedDarkMode
|
|
231
197
|
);
|
|
232
198
|
}, [modePreference, resolvedDarkMode]);
|
|
199
|
+
const setColor = useCallback(
|
|
200
|
+
(c) => {
|
|
201
|
+
setLightColorState(c);
|
|
202
|
+
setDarkColorState(c);
|
|
203
|
+
onColorChange?.({ light: c, dark: c });
|
|
204
|
+
},
|
|
205
|
+
[onColorChange]
|
|
206
|
+
);
|
|
207
|
+
const setLightColor = useCallback(
|
|
208
|
+
(c) => {
|
|
209
|
+
setLightColorState(c);
|
|
210
|
+
onColorChange?.({ light: c, dark: darkColor });
|
|
211
|
+
},
|
|
212
|
+
[darkColor, onColorChange]
|
|
213
|
+
);
|
|
214
|
+
const setDarkColor = useCallback(
|
|
215
|
+
(c) => {
|
|
216
|
+
setDarkColorState(c);
|
|
217
|
+
onColorChange?.({ light: lightColor, dark: c });
|
|
218
|
+
},
|
|
219
|
+
[lightColor, onColorChange]
|
|
220
|
+
);
|
|
221
|
+
const setMode = useCallback(
|
|
222
|
+
(next) => {
|
|
223
|
+
setModePreferenceState(next);
|
|
224
|
+
onModePreferenceChange?.(next);
|
|
225
|
+
},
|
|
226
|
+
[onModePreferenceChange]
|
|
227
|
+
);
|
|
233
228
|
const setConfigure = useCallback(
|
|
234
229
|
(fn) => {
|
|
235
230
|
setConfigureState(() => fn);
|
|
236
231
|
},
|
|
237
232
|
[]
|
|
238
233
|
);
|
|
239
|
-
const
|
|
234
|
+
const stable = useStableOptions({
|
|
240
235
|
style,
|
|
241
236
|
presets,
|
|
242
237
|
format: formatOpts,
|
|
243
|
-
configure,
|
|
244
238
|
darkMode,
|
|
245
|
-
cssPrefix
|
|
246
|
-
target
|
|
239
|
+
cssPrefix
|
|
247
240
|
});
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
241
|
+
const buildOne = useCallback(
|
|
242
|
+
(c) => {
|
|
243
|
+
const b = hextimate(c);
|
|
244
|
+
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
245
|
+
b.style(stable.style);
|
|
246
|
+
}
|
|
247
|
+
for (const p of presets ?? []) b.preset(p);
|
|
248
|
+
configure?.(b);
|
|
249
|
+
return b;
|
|
250
|
+
},
|
|
251
|
+
[stable?.style, presets, configure]
|
|
252
|
+
);
|
|
253
|
+
const lightBuilder = useMemo(
|
|
254
|
+
() => buildOne(lightColor),
|
|
255
|
+
[buildOne, lightColor]
|
|
256
|
+
);
|
|
257
|
+
const darkBuilder = useMemo(
|
|
258
|
+
() => lightColor === darkColor ? lightBuilder : buildOne(darkColor),
|
|
259
|
+
[buildOne, lightColor, darkColor, lightBuilder]
|
|
260
|
+
);
|
|
261
|
+
const palette = useMemo(() => {
|
|
262
|
+
const lightResult = lightBuilder.format({
|
|
263
|
+
...stable?.format,
|
|
264
|
+
as: "object"
|
|
265
|
+
});
|
|
266
|
+
if (darkBuilder === lightBuilder) return lightResult;
|
|
267
|
+
const darkResult = darkBuilder.format({
|
|
268
|
+
...stable?.format,
|
|
269
|
+
as: "object"
|
|
270
|
+
});
|
|
271
|
+
return { light: lightResult.light, dark: darkResult.dark };
|
|
272
|
+
}, [lightBuilder, darkBuilder, stable?.format]);
|
|
273
|
+
useEffect(() => {
|
|
274
|
+
const cssPrefixResolved = stable?.cssPrefix ?? "";
|
|
275
|
+
const darkModeResolved = stable?.darkMode ?? { type: "media" };
|
|
276
|
+
const el = target?.current;
|
|
277
|
+
if (el) {
|
|
278
|
+
const vars = buildTargetedVars(
|
|
279
|
+
palette,
|
|
280
|
+
darkModeResolved,
|
|
281
|
+
cssPrefixResolved
|
|
282
|
+
);
|
|
283
|
+
for (const [key, value2] of vars.light) {
|
|
284
|
+
el.style.setProperty(key, value2);
|
|
285
|
+
}
|
|
286
|
+
return () => {
|
|
287
|
+
for (const [key] of vars.light) {
|
|
288
|
+
el.style.removeProperty(key);
|
|
289
|
+
}
|
|
290
|
+
};
|
|
252
291
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
292
|
+
const styleEl = document.createElement("style");
|
|
293
|
+
styleEl.setAttribute("data-hextimator", "");
|
|
294
|
+
styleEl.textContent = buildStyleContent(
|
|
295
|
+
palette,
|
|
296
|
+
darkModeResolved,
|
|
297
|
+
cssPrefixResolved
|
|
298
|
+
);
|
|
299
|
+
document.head.appendChild(styleEl);
|
|
300
|
+
return () => {
|
|
301
|
+
document.head.removeChild(styleEl);
|
|
302
|
+
};
|
|
303
|
+
}, [palette, stable?.darkMode, stable?.cssPrefix, target]);
|
|
304
|
+
const builder = mode === "dark" ? darkBuilder : lightBuilder;
|
|
305
|
+
const value = useMemo(
|
|
258
306
|
() => ({
|
|
259
|
-
color,
|
|
307
|
+
color: mode === "dark" ? darkColor : lightColor,
|
|
260
308
|
setColor,
|
|
309
|
+
lightColor,
|
|
310
|
+
setLightColor,
|
|
311
|
+
darkColor,
|
|
312
|
+
setDarkColor,
|
|
261
313
|
mode,
|
|
262
314
|
modePreference,
|
|
263
315
|
setMode,
|
|
@@ -271,9 +323,14 @@ function HextimatorProvider({
|
|
|
271
323
|
builder
|
|
272
324
|
}),
|
|
273
325
|
[
|
|
274
|
-
color,
|
|
275
326
|
mode,
|
|
327
|
+
lightColor,
|
|
328
|
+
darkColor,
|
|
329
|
+
setColor,
|
|
330
|
+
setLightColor,
|
|
331
|
+
setDarkColor,
|
|
276
332
|
modePreference,
|
|
333
|
+
setMode,
|
|
277
334
|
style,
|
|
278
335
|
presets,
|
|
279
336
|
configure,
|
|
@@ -289,9 +346,9 @@ function HextimatorProvider({
|
|
|
289
346
|
import {
|
|
290
347
|
useCallback as useCallback2,
|
|
291
348
|
useContext,
|
|
292
|
-
useEffect as
|
|
349
|
+
useEffect as useEffect2,
|
|
293
350
|
useId,
|
|
294
|
-
useMemo as
|
|
351
|
+
useMemo as useMemo2,
|
|
295
352
|
useState as useState2
|
|
296
353
|
} from "react";
|
|
297
354
|
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
@@ -310,13 +367,26 @@ function HextimatorScope({
|
|
|
310
367
|
}) {
|
|
311
368
|
const id = useId();
|
|
312
369
|
const selector = `[data-hextimator-scope="${id}"]`;
|
|
313
|
-
const [
|
|
370
|
+
const [lightColor, setLightColorState] = useState2(
|
|
371
|
+
() => normalizeColorProp(defaultColor).light
|
|
372
|
+
);
|
|
373
|
+
const [darkColor, setDarkColorState] = useState2(
|
|
374
|
+
() => normalizeColorProp(defaultColor).dark
|
|
375
|
+
);
|
|
314
376
|
const [style, setStyle] = useState2(initialStyle);
|
|
315
377
|
const [presets, setPresets] = useState2(initialPresets);
|
|
316
378
|
const [configure, setConfigureState] = useState2(() => initialConfigure);
|
|
317
|
-
|
|
318
|
-
|
|
379
|
+
useEffect2(() => {
|
|
380
|
+
const next = normalizeColorProp(defaultColor);
|
|
381
|
+
setLightColorState(next.light);
|
|
382
|
+
setDarkColorState(next.dark);
|
|
319
383
|
}, [defaultColor]);
|
|
384
|
+
const setLightColor = setLightColorState;
|
|
385
|
+
const setDarkColor = setDarkColorState;
|
|
386
|
+
const setColor = useCallback2((c) => {
|
|
387
|
+
setLightColorState(c);
|
|
388
|
+
setDarkColorState(c);
|
|
389
|
+
}, []);
|
|
320
390
|
const setConfigure = useCallback2(
|
|
321
391
|
(fn) => {
|
|
322
392
|
setConfigureState(() => fn);
|
|
@@ -331,30 +401,46 @@ function HextimatorScope({
|
|
|
331
401
|
darkMode,
|
|
332
402
|
cssPrefix
|
|
333
403
|
});
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
[
|
|
404
|
+
const buildOne = useCallback2(
|
|
405
|
+
(c) => {
|
|
406
|
+
const b = !isolated && parent?.builder ? parent.builder.fork(c) : hextimate(c);
|
|
407
|
+
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
408
|
+
b.style(stable.style);
|
|
409
|
+
}
|
|
410
|
+
for (const p of presets ?? []) b.preset(p);
|
|
411
|
+
configure?.(b);
|
|
412
|
+
return b;
|
|
413
|
+
},
|
|
414
|
+
[parent?.builder, isolated, stable?.style, presets, configure]
|
|
415
|
+
);
|
|
416
|
+
const lightBuilder = useMemo2(
|
|
417
|
+
() => buildOne(lightColor),
|
|
418
|
+
[buildOne, lightColor]
|
|
349
419
|
);
|
|
350
|
-
const
|
|
420
|
+
const darkBuilder = useMemo2(
|
|
421
|
+
() => lightColor === darkColor ? lightBuilder : buildOne(darkColor),
|
|
422
|
+
[buildOne, lightColor, darkColor, lightBuilder]
|
|
423
|
+
);
|
|
424
|
+
const palette = useMemo2(() => {
|
|
425
|
+
const lightResult = lightBuilder.format({
|
|
426
|
+
...stable?.format,
|
|
427
|
+
as: "object"
|
|
428
|
+
});
|
|
429
|
+
if (darkBuilder === lightBuilder) return lightResult;
|
|
430
|
+
const darkResult = darkBuilder.format({
|
|
431
|
+
...stable?.format,
|
|
432
|
+
as: "object"
|
|
433
|
+
});
|
|
434
|
+
return { light: lightResult.light, dark: darkResult.dark };
|
|
435
|
+
}, [lightBuilder, darkBuilder, stable?.format]);
|
|
436
|
+
const css = useMemo2(
|
|
351
437
|
() => buildStyleContent(
|
|
352
438
|
palette,
|
|
353
439
|
stable?.darkMode ?? { type: "media" },
|
|
354
440
|
stable?.cssPrefix ?? "",
|
|
355
441
|
selector
|
|
356
442
|
),
|
|
357
|
-
[palette, stable, selector]
|
|
443
|
+
[palette, stable?.darkMode, stable?.cssPrefix, selector]
|
|
358
444
|
);
|
|
359
445
|
const osDark = useOsPrefersDark();
|
|
360
446
|
const noopSetMode = useCallback2(() => {
|
|
@@ -362,10 +448,16 @@ function HextimatorScope({
|
|
|
362
448
|
const mode = parent?.mode ?? (osDark ? "dark" : "light");
|
|
363
449
|
const modePreference = parent?.modePreference ?? "system";
|
|
364
450
|
const setMode = parent?.setMode ?? noopSetMode;
|
|
365
|
-
const
|
|
451
|
+
const builder = mode === "dark" ? darkBuilder : lightBuilder;
|
|
452
|
+
const activeColor = mode === "dark" ? darkColor : lightColor;
|
|
453
|
+
const value = useMemo2(
|
|
366
454
|
() => ({
|
|
367
|
-
color,
|
|
455
|
+
color: activeColor,
|
|
368
456
|
setColor,
|
|
457
|
+
lightColor,
|
|
458
|
+
setLightColor,
|
|
459
|
+
darkColor,
|
|
460
|
+
setDarkColor,
|
|
369
461
|
mode,
|
|
370
462
|
modePreference,
|
|
371
463
|
setMode,
|
|
@@ -379,7 +471,10 @@ function HextimatorScope({
|
|
|
379
471
|
builder
|
|
380
472
|
}),
|
|
381
473
|
[
|
|
382
|
-
|
|
474
|
+
activeColor,
|
|
475
|
+
setColor,
|
|
476
|
+
lightColor,
|
|
477
|
+
darkColor,
|
|
383
478
|
mode,
|
|
384
479
|
modePreference,
|
|
385
480
|
setMode,
|
|
@@ -406,7 +501,7 @@ function HextimatorScope({
|
|
|
406
501
|
}
|
|
407
502
|
|
|
408
503
|
// src/react/hextimator-style.tsx
|
|
409
|
-
import { useMemo as
|
|
504
|
+
import { useMemo as useMemo3 } from "react";
|
|
410
505
|
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
411
506
|
function HextimatorStyle({
|
|
412
507
|
color,
|
|
@@ -425,7 +520,7 @@ function HextimatorStyle({
|
|
|
425
520
|
darkMode,
|
|
426
521
|
cssPrefix
|
|
427
522
|
});
|
|
428
|
-
const css =
|
|
523
|
+
const css = useMemo3(() => {
|
|
429
524
|
const builder = hextimate(color);
|
|
430
525
|
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
431
526
|
builder.style(stable.style);
|
|
@@ -434,7 +529,7 @@ function HextimatorStyle({
|
|
|
434
529
|
configure?.(builder);
|
|
435
530
|
const palette = builder.format({
|
|
436
531
|
...stable?.format,
|
|
437
|
-
as: "
|
|
532
|
+
as: "object"
|
|
438
533
|
});
|
|
439
534
|
return buildStyleContent(
|
|
440
535
|
palette,
|
|
@@ -446,6 +541,51 @@ function HextimatorStyle({
|
|
|
446
541
|
return /* @__PURE__ */ jsx3("style", { "data-hextimator": "", children: css });
|
|
447
542
|
}
|
|
448
543
|
|
|
544
|
+
// src/react/use-hextimator.ts
|
|
545
|
+
import { useEffect as useEffect3, useMemo as useMemo4 } from "react";
|
|
546
|
+
function useHextimator(color, options) {
|
|
547
|
+
const stable = useStableOptions(options);
|
|
548
|
+
const configure = options?.configure;
|
|
549
|
+
const target = options?.target;
|
|
550
|
+
const presets = stable?.presets;
|
|
551
|
+
const palette = useMemo4(() => {
|
|
552
|
+
const builder = hextimate(color);
|
|
553
|
+
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
554
|
+
builder.style(stable.style);
|
|
555
|
+
}
|
|
556
|
+
for (const p of presets ?? []) builder.preset(p);
|
|
557
|
+
configure?.(builder);
|
|
558
|
+
return builder.format({
|
|
559
|
+
...stable?.format,
|
|
560
|
+
as: "object"
|
|
561
|
+
});
|
|
562
|
+
}, [color, stable?.style, presets, stable?.format, configure]);
|
|
563
|
+
const darkMode = stable?.darkMode ?? { type: "media" };
|
|
564
|
+
const cssPrefix = stable?.cssPrefix ?? "";
|
|
565
|
+
useEffect3(() => {
|
|
566
|
+
const el = target?.current;
|
|
567
|
+
if (el) {
|
|
568
|
+
const vars = buildTargetedVars(palette, darkMode, cssPrefix);
|
|
569
|
+
for (const [key, value] of vars.light) {
|
|
570
|
+
el.style.setProperty(key, value);
|
|
571
|
+
}
|
|
572
|
+
return () => {
|
|
573
|
+
for (const [key] of vars.light) {
|
|
574
|
+
el.style.removeProperty(key);
|
|
575
|
+
}
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
const style = document.createElement("style");
|
|
579
|
+
style.setAttribute("data-hextimator", "");
|
|
580
|
+
style.textContent = buildStyleContent(palette, darkMode, cssPrefix);
|
|
581
|
+
document.head.appendChild(style);
|
|
582
|
+
return () => {
|
|
583
|
+
document.head.removeChild(style);
|
|
584
|
+
};
|
|
585
|
+
}, [palette, darkMode, cssPrefix, target]);
|
|
586
|
+
return palette;
|
|
587
|
+
}
|
|
588
|
+
|
|
449
589
|
// src/react/use-hextimator-theme.ts
|
|
450
590
|
import { useContext as useContext2 } from "react";
|
|
451
591
|
function useHextimatorTheme() {
|
package/llms.txt
CHANGED
|
@@ -17,9 +17,9 @@ import { hextimate } from "hextimator";
|
|
|
17
17
|
const theme = hextimate("#6A5ACD").format();
|
|
18
18
|
// → { light: { accent: "#6a5acd", "accent-strong": "...", ... }, dark: { ... } }
|
|
19
19
|
|
|
20
|
-
// With format options
|
|
20
|
+
// With format options — `as: "css"` returns a ready-to-paste stylesheet string
|
|
21
21
|
const css = hextimate("#6A5ACD").format({ as: "css", colors: "oklch" });
|
|
22
|
-
// →
|
|
22
|
+
// → ":root { --accent: oklch(...); ... }\n@media (prefers-color-scheme: dark) { :root { ... } }"
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## API
|
|
@@ -48,7 +48,7 @@ Tune how the palette is built with `.style(partial)` (`HextimateStyleOptions`, s
|
|
|
48
48
|
| `.fork(color?)` | Clone builder with optional new accent color. Chain `.style()` on the fork to change style options. Original unchanged |
|
|
49
49
|
| `.format(options?)` | Output the palette (see format options) |
|
|
50
50
|
|
|
51
|
-
### `.format(options?)` → `{ light: FormatResult, dark: FormatResult }`
|
|
51
|
+
### `.format(options?)` → `{ light: FormatResult, dark: FormatResult }` or `string`
|
|
52
52
|
|
|
53
53
|
| Option | Values | Default |
|
|
54
54
|
|---|---|---|
|
|
@@ -57,16 +57,20 @@ Tune how the palette is built with `.style(partial)` (`HextimateStyleOptions`, s
|
|
|
57
57
|
| `roleNames` | `Record<string, string>` — rename roles in output keys | built-in names |
|
|
58
58
|
| `variantNames` | `Record<string, string>` — rename variant suffixes | built-in names |
|
|
59
59
|
| `separator` | `string` — between role and variant in keys | `"-"` |
|
|
60
|
+
| `darkMode` | `"media"`, `"class"`, `"data-attribute"`, `false` (stylesheet outputs only) | `"media"` |
|
|
61
|
+
| `selector` | `string` — root selector for `as: "css"` | `":root"` |
|
|
60
62
|
| `excludeRoles` | `string[]` — role keys to omit entirely from output (by internal name, before `roleNames`) | `[]` |
|
|
61
63
|
| `excludeVariants` | `string[]` — variant keys to omit from every role's output (by internal name, before `variantNames`) | `[]` |
|
|
62
64
|
|
|
63
65
|
Output shape depends on `as`:
|
|
64
|
-
- `"object"` → `
|
|
65
|
-
- `"css"` → `
|
|
66
|
-
- `"tailwind"` → nested
|
|
67
|
-
- `"tailwind-css"` → string: `@theme { --color-accent: #...; }`
|
|
68
|
-
- `"scss"` → `
|
|
69
|
-
- `"json"` → JSON
|
|
66
|
+
- `"object"` → `{ light, dark }` flat maps: `{ accent: "#...", "accent-strong": "#..." }`
|
|
67
|
+
- `"css"` → `string` — ready-to-paste CSS stylesheet with `:root {}` and a dark-mode wrapper
|
|
68
|
+
- `"tailwind"` → `{ light, dark }` nested maps: `{ accent: { DEFAULT: "#...", strong: "#..." } }`
|
|
69
|
+
- `"tailwind-css"` → `string` — Tailwind v4 stylesheet: single `@theme { --color-accent: #...; }` block plus dark overrides
|
|
70
|
+
- `"scss"` → `{ light, dark }` flat maps: `{ "$accent": "#...", "$accent-strong": "#..." }`
|
|
71
|
+
- `"json"` → `{ light, dark }` JSON strings
|
|
72
|
+
|
|
73
|
+
Pick the dark wrapper with `darkMode`: `"media"` (default) wraps dark tokens in `@media (prefers-color-scheme: dark)`; `"class"` uses `.dark`; `"data-attribute"` uses `[data-theme="dark"]`; `false` omits dark entirely. For `as: "css"`, change the root selector with `selector` (e.g. `selector: "[data-theme]"`).
|
|
70
74
|
|
|
71
75
|
### Style options (`HextimateStyleOptions`)
|
|
72
76
|
|
|
@@ -291,9 +295,11 @@ Both commands are identical. Use whichever you prefer.
|
|
|
291
295
|
|---|---|---|
|
|
292
296
|
| `-f, --format` | `css`, `object`, `tailwind`, `tailwind-css`, `scss`, `json` | `css` |
|
|
293
297
|
| `-c, --colors` | `hex`, `rgb`, `rgb-raw`, `hsl`, `hsl-raw`, `oklch`, `oklch-raw`, `p3`, `p3-raw` | `hex` |
|
|
294
|
-
| `-t, --theme` | `light`, `dark`, `both` | `both` |
|
|
298
|
+
| `-t, --theme` | `light`, `dark`, `both` (ignored for `css`/`tailwind-css` — both always combine into one string) | `both` |
|
|
295
299
|
| `-p, --preset` | Apply preset (repeatable, applied in order): `shadcn`, `mui`, `muted`, `vibrant`, `tinted` | — |
|
|
296
300
|
| `--separator` | Token separator between role and variant | `-` |
|
|
301
|
+
| `--dark-mode` | `media`, `class`, `data-attribute`, `false` (stylesheet outputs only) | `media` |
|
|
302
|
+
| `--selector` | Root selector for `--format css` | `:root` |
|
|
297
303
|
| `--exclude-role` | Omit a role from output by internal name (repeatable) | — |
|
|
298
304
|
| `--exclude-variant` | Omit a variant from output by internal name (repeatable) | — |
|
|
299
305
|
| `--base-color` | Override base color | — |
|
|
@@ -378,8 +384,10 @@ For app-wide theming with dynamic color changes:
|
|
|
378
384
|
import { HextimatorProvider, useHextimatorTheme } from "hextimator/react";
|
|
379
385
|
|
|
380
386
|
<HextimatorProvider
|
|
381
|
-
defaultColor="#6A5ACD"
|
|
382
|
-
defaultMode="system"
|
|
387
|
+
defaultColor="#6A5ACD" // string OR { light: "#...", dark: "#..." }
|
|
388
|
+
defaultMode="system" // "light" | "dark" | "system"
|
|
389
|
+
onColorChange={(next) => persist(next)} // fires on every color change
|
|
390
|
+
onModePreferenceChange={(mode) => persist(mode)}
|
|
383
391
|
style={styleOpts}
|
|
384
392
|
presets={[presets.muted, presets.shadcn]}
|
|
385
393
|
format={formatOpts}
|
|
@@ -391,13 +399,23 @@ import { HextimatorProvider, useHextimatorTheme } from "hextimator/react";
|
|
|
391
399
|
</HextimatorProvider>
|
|
392
400
|
```
|
|
393
401
|
|
|
402
|
+
**Per-mode colors:** pass `defaultColor={{ light: "#FF6600", dark: "#0088FF" }}` to use a different brand color per mode. Both palettes are built and stitched in CSS, so `@media (prefers-color-scheme: dark)` always serves the dark palette correctly. When `light === dark` (default for a string), only one palette is built.
|
|
403
|
+
|
|
404
|
+
**Persistence:** `onColorChange` and `onModePreferenceChange` always fire on user-driven updates. Wire them to localStorage / cookies / etc. Restore by reading once on mount and passing the result to `defaultColor` / `defaultMode`. For forced overrides (URL-driven, server push), remount with `key={color}`.
|
|
405
|
+
|
|
406
|
+
**FOUC:** the provider injects CSS in `useEffect` (after first paint). To avoid flash on SPAs, read storage synchronously in a lazy initializer so `defaultColor` is already correct on the first render. For SSR (Next.js, Remix), persist in a cookie and render `<HextimatorStyle>` server-side in the layout `<head>`.
|
|
407
|
+
|
|
394
408
|
`useHextimatorTheme()` returns:
|
|
395
409
|
|
|
396
410
|
| Property | Type | Description |
|
|
397
411
|
|---|---|---|
|
|
398
|
-
| `color` | `string` |
|
|
399
|
-
| `setColor` | `(color: string) => void` |
|
|
400
|
-
| `
|
|
412
|
+
| `color` | `string` | Color active for the resolved mode (= `lightColor` in light, `darkColor` in dark) |
|
|
413
|
+
| `setColor` | `(color: string) => void` | Sets BOTH `lightColor` and `darkColor` (one-color-in, whole-theme-out) |
|
|
414
|
+
| `lightColor` | `string` | Color used to generate the light-mode palette |
|
|
415
|
+
| `setLightColor` | `(color: string) => void` | Update only the light-mode color |
|
|
416
|
+
| `darkColor` | `string` | Color used to generate the dark-mode palette |
|
|
417
|
+
| `setDarkColor` | `(color: string) => void` | Update only the dark-mode color |
|
|
418
|
+
| `palette` | `HextimateResult` | The generated `{ light, dark }` palette (light from `lightColor`, dark from `darkColor`) |
|
|
401
419
|
| `mode` | `"light" \| "dark"` | Resolved color mode (accounts for OS preference) |
|
|
402
420
|
| `modePreference` | `"light" \| "dark" \| "system"` | Raw preference before resolution |
|
|
403
421
|
| `setMode` | `(mode: ModePreference) => void` | Change mode preference |
|
|
@@ -449,7 +467,7 @@ import { HextimatorProvider, HextimatorScope } from "hextimator/react";
|
|
|
449
467
|
</HextimatorProvider>
|
|
450
468
|
```
|
|
451
469
|
|
|
452
|
-
Props: `defaultColor
|
|
470
|
+
Props: `defaultColor` (`string \| { light, dark }`), `isolated?`, `style?`, `presets?`, `format?`, `configure?`, `darkMode?`, `cssPrefix?`, `className?`, `wrapperStyle?` (inline styles for the scope element).
|
|
453
471
|
|
|
454
472
|
## `fallback.css`
|
|
455
473
|
|