hextimator 0.1.2 → 0.2.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/dist/react.js CHANGED
@@ -2,36 +2,79 @@ import {
2
2
  hextimate
3
3
  } from "./chunk-MBVLFPTG.js";
4
4
 
5
- // src/react.tsx
5
+ // src/react/hextimator-provider.tsx
6
6
  import {
7
- createContext,
8
7
  useCallback,
9
- useContext,
10
- useEffect,
11
- useMemo,
12
- useRef,
13
- useState,
14
- useSyncExternalStore
8
+ useEffect as useEffect2,
9
+ useMemo as useMemo2,
10
+ useState
15
11
  } from "react";
16
- import { jsx } from "react/jsx-runtime";
17
- function buildStyleContent(palette, darkMode, cssPrefix) {
12
+
13
+ // src/react/context.ts
14
+ import { createContext } from "react";
15
+ var HextimatorContext = createContext(
16
+ null
17
+ );
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
+ // src/react/css.ts
59
+ function buildStyleContent(palette, darkMode, cssPrefix, selector = ":root") {
18
60
  const lightEntries = Object.entries(palette.light);
19
61
  const darkEntries = Object.entries(palette.dark);
20
62
  const toVars = (entries) => entries.map(([key, value]) => `${cssPrefix}${key}: ${value};`).join("\n ");
21
63
  const lightVars = toVars(lightEntries);
22
64
  const darkVars = toVars(darkEntries);
65
+ const isRoot = selector === ":root";
23
66
  if (darkMode === false) {
24
- return `:root {
67
+ return `${selector} {
25
68
  ${lightVars}
26
69
  }`;
27
70
  }
28
71
  if (darkMode.type === "media") {
29
72
  return [
30
- `:root {
73
+ `${selector} {
31
74
  ${lightVars}
32
75
  }`,
33
76
  `@media (prefers-color-scheme: dark) {
34
- :root {
77
+ ${selector} {
35
78
  ${darkVars}
36
79
  }
37
80
  }`
@@ -40,36 +83,41 @@ function buildStyleContent(palette, darkMode, cssPrefix) {
40
83
  if (darkMode.type === "media-or-class") {
41
84
  const cls = darkMode.className ?? "dark";
42
85
  const lightCls = cls === "dark" ? "light" : `not-${cls}`;
86
+ const darkClassSelector = isRoot ? `:root.${cls}` : `:root.${cls} ${selector}`;
87
+ const mediaDarkSelector = isRoot ? `:root:not(.${lightCls})` : `:root:not(.${lightCls}) ${selector}`;
43
88
  return [
44
- `:root {
89
+ `${selector} {
45
90
  ${lightVars}
46
91
  }`,
47
92
  `@media (prefers-color-scheme: dark) {
48
- :root:not(.${lightCls}) {
93
+ ${mediaDarkSelector} {
49
94
  ${darkVars}
50
95
  }
51
96
  }`,
52
- `:root.${cls} {
97
+ `${darkClassSelector} {
53
98
  ${darkVars}
54
99
  }`
55
100
  ].join("\n");
56
101
  }
57
102
  if (darkMode.type === "class") {
58
103
  const cls = darkMode.className ?? "dark";
59
- return [`:root {
104
+ const darkSelector2 = isRoot ? `.${cls}` : `.${cls} ${selector}`;
105
+ return [
106
+ `${selector} {
60
107
  ${lightVars}
61
- }`, `.${cls} {
108
+ }`,
109
+ `${darkSelector2} {
62
110
  ${darkVars}
63
- }`].join(
64
- "\n"
65
- );
111
+ }`
112
+ ].join("\n");
66
113
  }
67
114
  const attr = darkMode.attribute ?? "data-theme";
115
+ const darkSelector = isRoot ? `[${attr}="dark"]` : `[${attr}="dark"] ${selector}`;
68
116
  return [
69
- `:root {
117
+ `${selector} {
70
118
  ${lightVars}
71
119
  }`,
72
- `[${attr}="dark"] {
120
+ `${darkSelector} {
73
121
  ${darkVars}
74
122
  }`
75
123
  ].join("\n");
@@ -85,6 +133,9 @@ function buildTargetedVars(palette, darkMode, cssPrefix) {
85
133
  dark: darkMode !== false ? prefixEntries(Object.entries(palette.dark)) : []
86
134
  };
87
135
  }
136
+
137
+ // src/react/use-stable-options.ts
138
+ import { useRef } from "react";
88
139
  function useStableOptions(options) {
89
140
  const ref = useRef(options);
90
141
  const serialized = JSON.stringify({
@@ -103,6 +154,8 @@ function useStableOptions(options) {
103
154
  }
104
155
  return ref.current;
105
156
  }
157
+
158
+ // src/react/use-hextimator.ts
106
159
  function useHextimator(color, options) {
107
160
  const stable = useStableOptions(options);
108
161
  const configure = options?.configure;
@@ -140,40 +193,9 @@ function useHextimator(color, options) {
140
193
  }, [palette, darkMode, cssPrefix, target]);
141
194
  return palette;
142
195
  }
143
- var darkQuery = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
144
- function useOsPrefersDark() {
145
- return useSyncExternalStore(
146
- (cb) => {
147
- if (!darkQuery) return () => {
148
- };
149
- darkQuery.addEventListener("change", cb);
150
- return () => darkQuery.removeEventListener("change", cb);
151
- },
152
- () => darkQuery?.matches ?? false,
153
- () => false
154
- );
155
- }
156
- function applyModeToDOM(mode, darkMode) {
157
- if (typeof document === "undefined") return;
158
- if (darkMode === false || darkMode.type === "media") return;
159
- const root = document.documentElement;
160
- if (darkMode.type === "data") {
161
- const attr = darkMode.attribute ?? "data-theme";
162
- if (mode) {
163
- root.setAttribute(attr, mode);
164
- } else {
165
- root.removeAttribute(attr);
166
- }
167
- return;
168
- }
169
- const cls = darkMode.className ?? "dark";
170
- const lightCls = cls === "dark" ? "light" : `not-${cls}`;
171
- root.classList.remove(cls, lightCls);
172
- if (mode) {
173
- root.classList.add(mode === "dark" ? cls : lightCls);
174
- }
175
- }
176
- var HextimatorContext = createContext(null);
196
+
197
+ // src/react/hextimator-provider.tsx
198
+ import { jsx } from "react/jsx-runtime";
177
199
  function HextimatorProvider({
178
200
  children,
179
201
  defaultColor,
@@ -192,7 +214,7 @@ function HextimatorProvider({
192
214
  const osDark = useOsPrefersDark();
193
215
  const mode = modePreference === "system" ? osDark ? "dark" : "light" : modePreference;
194
216
  const resolvedDarkMode = darkMode ?? { type: "media" };
195
- useEffect(() => {
217
+ useEffect2(() => {
196
218
  applyModeToDOM(
197
219
  modePreference === "system" ? null : modePreference,
198
220
  resolvedDarkMode
@@ -212,7 +234,12 @@ function HextimatorProvider({
212
234
  cssPrefix,
213
235
  target
214
236
  });
215
- const value = useMemo(
237
+ const builder = useMemo2(() => {
238
+ const b = hextimate(color, generation);
239
+ configure?.(b);
240
+ return b;
241
+ }, [color, generation, configure]);
242
+ const value = useMemo2(
216
243
  () => ({
217
244
  color,
218
245
  setColor,
@@ -223,23 +250,170 @@ function HextimatorProvider({
223
250
  setGeneration,
224
251
  configure,
225
252
  setConfigure,
226
- palette
253
+ palette,
254
+ builder
227
255
  }),
228
- [color, mode, modePreference, generation, configure, setConfigure, palette]
256
+ [
257
+ color,
258
+ mode,
259
+ modePreference,
260
+ generation,
261
+ configure,
262
+ setConfigure,
263
+ palette,
264
+ builder
265
+ ]
229
266
  );
230
267
  return /* @__PURE__ */ jsx(HextimatorContext.Provider, { value, children });
231
268
  }
269
+
270
+ // src/react/hextimator-scope.tsx
271
+ import {
272
+ useCallback as useCallback2,
273
+ useContext,
274
+ useId,
275
+ useMemo as useMemo3,
276
+ useState as useState2
277
+ } from "react";
278
+ import { jsx as jsx2, jsxs } from "react/jsx-runtime";
279
+ function HextimatorScope({
280
+ defaultColor,
281
+ generation: initialGeneration,
282
+ format: formatOpts,
283
+ configure: initialConfigure,
284
+ darkMode,
285
+ cssPrefix,
286
+ className,
287
+ style,
288
+ children
289
+ }) {
290
+ const id = useId();
291
+ const selector = `[data-hextimator-scope="${id}"]`;
292
+ const [color, setColor] = useState2(defaultColor);
293
+ const [generation, setGeneration] = useState2(initialGeneration);
294
+ const [configure, setConfigureState] = useState2(() => initialConfigure);
295
+ const setConfigure = useCallback2(
296
+ (fn) => {
297
+ setConfigureState(() => fn);
298
+ },
299
+ []
300
+ );
301
+ const parent = useContext(HextimatorContext);
302
+ const stable = useStableOptions({
303
+ generation,
304
+ format: formatOpts,
305
+ darkMode,
306
+ cssPrefix
307
+ });
308
+ const builder = useMemo3(() => {
309
+ const b = parent?.builder ? parent.builder.fork(color, stable?.generation) : hextimate(color, stable?.generation);
310
+ configure?.(b);
311
+ return b;
312
+ }, [parent?.builder, color, stable, configure]);
313
+ const palette = useMemo3(
314
+ () => builder.format({
315
+ ...stable?.format,
316
+ as: "css"
317
+ }),
318
+ [builder, stable]
319
+ );
320
+ const css = useMemo3(
321
+ () => buildStyleContent(
322
+ palette,
323
+ stable?.darkMode ?? { type: "media" },
324
+ stable?.cssPrefix ?? "",
325
+ selector
326
+ ),
327
+ [palette, stable, selector]
328
+ );
329
+ const osDark = useOsPrefersDark();
330
+ const noopSetMode = useCallback2(() => {
331
+ }, []);
332
+ const mode = parent?.mode ?? (osDark ? "dark" : "light");
333
+ const modePreference = parent?.modePreference ?? "system";
334
+ const setMode = parent?.setMode ?? noopSetMode;
335
+ const value = useMemo3(
336
+ () => ({
337
+ color,
338
+ setColor,
339
+ mode,
340
+ modePreference,
341
+ setMode,
342
+ generation,
343
+ setGeneration,
344
+ configure,
345
+ setConfigure,
346
+ palette,
347
+ builder
348
+ }),
349
+ [
350
+ color,
351
+ mode,
352
+ modePreference,
353
+ setMode,
354
+ generation,
355
+ configure,
356
+ setConfigure,
357
+ palette,
358
+ builder
359
+ ]
360
+ );
361
+ return /* @__PURE__ */ jsx2(HextimatorContext.Provider, { value, children: /* @__PURE__ */ jsxs("div", { "data-hextimator-scope": id, className, style, children: [
362
+ /* @__PURE__ */ jsx2("style", { "data-hextimator": "", children: css }),
363
+ children
364
+ ] }) });
365
+ }
366
+
367
+ // src/react/hextimator-style.tsx
368
+ import { useMemo as useMemo4 } from "react";
369
+ import { jsx as jsx3 } from "react/jsx-runtime";
370
+ function HextimatorStyle({
371
+ color,
372
+ generation,
373
+ format: formatOpts,
374
+ configure,
375
+ darkMode,
376
+ cssPrefix,
377
+ selector
378
+ }) {
379
+ const stable = useStableOptions({
380
+ generation,
381
+ format: formatOpts,
382
+ darkMode,
383
+ cssPrefix
384
+ });
385
+ const css = useMemo4(() => {
386
+ const builder = hextimate(color, stable?.generation);
387
+ configure?.(builder);
388
+ const palette = builder.format({
389
+ ...stable?.format,
390
+ as: "css"
391
+ });
392
+ return buildStyleContent(
393
+ palette,
394
+ stable?.darkMode ?? { type: "media" },
395
+ stable?.cssPrefix ?? "",
396
+ selector
397
+ );
398
+ }, [color, stable, configure, selector]);
399
+ return /* @__PURE__ */ jsx3("style", { "data-hextimator": "", children: css });
400
+ }
401
+
402
+ // src/react/use-hextimator-theme.ts
403
+ import { useContext as useContext2 } from "react";
232
404
  function useHextimatorTheme() {
233
- const ctx = useContext(HextimatorContext);
405
+ const ctx = useContext2(HextimatorContext);
234
406
  if (!ctx) {
235
407
  throw new Error(
236
- "useHextimatorTheme must be used within a <HextimatorProvider>"
408
+ "useHextimatorTheme must be used within a <HextimatorProvider> or <HextimatorScope>"
237
409
  );
238
410
  }
239
411
  return ctx;
240
412
  }
241
413
  export {
242
414
  HextimatorProvider,
415
+ HextimatorScope,
416
+ HextimatorStyle,
243
417
  useHextimator,
244
418
  useHextimatorTheme
245
419
  };
package/fallback.css ADDED
@@ -0,0 +1,86 @@
1
+ /**
2
+ * Neutral placeholder theme for the pre-JavaScript paint (and before Hextimator injects CSS).
3
+ * Same token names as default hextimate() + hextimator/tailwind.css. Dark mode matches
4
+ * useHextimator / HextimatorStyle with darkMode: { type: "media-or-class" } (also fine for
5
+ * { type: "media" } when :root has no .light class).
6
+ *
7
+ * Does not include tokens from presets (e.g. shadcn). For those, generate CSS with the CLI
8
+ * and ship it alongside this file or instead of it.
9
+ *
10
+ * Regenerated at package build time (see package.json "build").
11
+ */
12
+
13
+ :root {
14
+ --base: #f5f5f5;
15
+ --base-strong: #fcfcfc;
16
+ --base-weak: #ebebeb;
17
+ --base-foreground: #030303;
18
+ --accent: #aeaeae;
19
+ --accent-strong: #989898;
20
+ --accent-weak: #cecece;
21
+ --accent-foreground: #030303;
22
+ --positive: #aeaeae;
23
+ --positive-strong: #989898;
24
+ --positive-weak: #cecece;
25
+ --positive-foreground: #030303;
26
+ --negative: #aeaeae;
27
+ --negative-strong: #989898;
28
+ --negative-weak: #cecece;
29
+ --negative-foreground: #030303;
30
+ --warning: #aeaeae;
31
+ --warning-strong: #989898;
32
+ --warning-weak: #cecece;
33
+ --warning-foreground: #030303;
34
+ --brand-exact: #737373;
35
+ --brand-exact-foreground: #f5f5f5;
36
+ }
37
+ @media (prefers-color-scheme: dark) {
38
+ :root:not(.light) {
39
+ --base: #161616;
40
+ --base-strong: #030303;
41
+ --base-weak: #2e2e2e;
42
+ --base-foreground: #f5f5f5;
43
+ --accent: #a8a8a8;
44
+ --accent-strong: #b7b7b7;
45
+ --accent-weak: #989898;
46
+ --accent-foreground: #030303;
47
+ --positive: #a8a8a8;
48
+ --positive-strong: #b7b7b7;
49
+ --positive-weak: #989898;
50
+ --positive-foreground: #030303;
51
+ --negative: #a8a8a8;
52
+ --negative-strong: #b7b7b7;
53
+ --negative-weak: #989898;
54
+ --negative-foreground: #030303;
55
+ --warning: #a8a8a8;
56
+ --warning-strong: #b7b7b7;
57
+ --warning-weak: #989898;
58
+ --warning-foreground: #030303;
59
+ --brand-exact: #737373;
60
+ --brand-exact-foreground: #f5f5f5;
61
+ }
62
+ }
63
+ :root.dark {
64
+ --base: #161616;
65
+ --base-strong: #030303;
66
+ --base-weak: #2e2e2e;
67
+ --base-foreground: #f5f5f5;
68
+ --accent: #a8a8a8;
69
+ --accent-strong: #b7b7b7;
70
+ --accent-weak: #989898;
71
+ --accent-foreground: #030303;
72
+ --positive: #a8a8a8;
73
+ --positive-strong: #b7b7b7;
74
+ --positive-weak: #989898;
75
+ --positive-foreground: #030303;
76
+ --negative: #a8a8a8;
77
+ --negative-strong: #b7b7b7;
78
+ --negative-weak: #989898;
79
+ --negative-foreground: #030303;
80
+ --warning: #a8a8a8;
81
+ --warning-strong: #b7b7b7;
82
+ --warning-weak: #989898;
83
+ --warning-foreground: #030303;
84
+ --brand-exact: #737373;
85
+ --brand-exact-foreground: #f5f5f5;
86
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hextimator",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Generate perceptually uniform color palettes with light/dark themes and WCAG contrast guarantees from a single brand color. Outputs CSS vars, Tailwind, SCSS, or JSON.",
5
5
  "keywords": [
6
6
  "color",
@@ -52,17 +52,20 @@
52
52
  "import": "./dist/react.js"
53
53
  },
54
54
  "./cli": "./dist/cli.js",
55
- "./tailwind.css": "./tailwind.css"
55
+ "./tailwind.css": "./tailwind.css",
56
+ "./fallback.css": "./fallback.css"
56
57
  },
57
58
  "files": [
58
59
  "dist",
59
60
  "tailwind.css",
61
+ "fallback.css",
60
62
  "llms.txt",
61
63
  "CHANGELOG.md"
62
64
  ],
63
65
  "scripts": {
64
- "build": "tsup",
66
+ "build": "bun scripts/generate-fallback-css.ts && tsup",
65
67
  "dev": "tsup --watch",
68
+ "generate:fallback": "bun scripts/generate-fallback-css.ts",
66
69
  "test": "bun test"
67
70
  },
68
71
  "peerDependencies": {
@@ -75,7 +78,9 @@
75
78
  },
76
79
  "devDependencies": {
77
80
  "@types/react": "^19.2.7",
78
- "react": "^19.2.0",
81
+ "@types/react-dom": "^19.2.3",
82
+ "react": "19.2.5",
83
+ "react-dom": "^19.2.5",
79
84
  "tsup": "^8.0.0",
80
85
  "typescript": "^5.0.0"
81
86
  }