hextimator 0.3.1 → 0.4.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 +7 -1
- package/dist/react.d.ts +46 -94
- package/dist/react.js +271 -129
- package/llms.txt +18 -6
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
##
|
|
1
|
+
## 0.4.0 (Latest)
|
|
2
|
+
|
|
3
|
+
- [#106](https://github.com/fgrgic/hextimator/pull/106): add per-theme color for React
|
|
4
|
+
- React provider and scope now accept per-mode brand colors (`defaultColor: string | { light, dark }`)
|
|
5
|
+
- expose `onColorChange` / `onModePreferenceChange` callbacks for persistence.
|
|
6
|
+
|
|
7
|
+
## 0.3.2
|
|
2
8
|
|
|
3
9
|
- [#99](https://github.com/fgrgic/hextimator/pull/99): Adds option to chain presets. Presets are merged, last added wins if both touch the same tokens.
|
|
4
10
|
- For example: `hextimate("#ff6677").preset(shadcn).preset(muted);`
|
package/dist/react.d.ts
CHANGED
|
@@ -16,11 +16,31 @@ type DarkModeStrategy = {
|
|
|
16
16
|
} | false;
|
|
17
17
|
type ModePreference = 'light' | 'dark' | 'system';
|
|
18
18
|
type ResolvedMode = 'light' | 'dark';
|
|
19
|
+
/** A single color (used for both modes) or a per-mode pair. */
|
|
20
|
+
type ColorInputProp = string | {
|
|
21
|
+
light: string;
|
|
22
|
+
dark: string;
|
|
23
|
+
};
|
|
24
|
+
/** A per-mode color pair. */
|
|
25
|
+
interface ModeColors {
|
|
26
|
+
light: string;
|
|
27
|
+
dark: string;
|
|
28
|
+
}
|
|
19
29
|
|
|
20
30
|
interface HextimatorContextValue {
|
|
31
|
+
/** Active brand color for the current mode. */
|
|
21
32
|
color: string;
|
|
33
|
+
/** Set the brand color for both modes at once. */
|
|
22
34
|
setColor: (color: string) => void;
|
|
35
|
+
/** Brand color used in light mode. */
|
|
36
|
+
lightColor: string;
|
|
37
|
+
setLightColor: (color: string) => void;
|
|
38
|
+
/** Brand color used in dark mode. */
|
|
39
|
+
darkColor: string;
|
|
40
|
+
setDarkColor: (color: string) => void;
|
|
41
|
+
/** Resolved mode (`'light'` or `'dark'`). */
|
|
23
42
|
mode: ResolvedMode;
|
|
43
|
+
/** Raw preference (`'light'`, `'dark'`, or `'system'`). */
|
|
24
44
|
modePreference: ModePreference;
|
|
25
45
|
setMode: (mode: ModePreference) => void;
|
|
26
46
|
style: HextimateStyleOptions | undefined;
|
|
@@ -29,28 +49,21 @@ interface HextimatorContextValue {
|
|
|
29
49
|
setPresets: (presets: HextimatePreset[] | undefined) => void;
|
|
30
50
|
configure: ((builder: HextimatePaletteBuilder) => void) | undefined;
|
|
31
51
|
setConfigure: (fn: ((builder: HextimatePaletteBuilder) => void) | undefined) => void;
|
|
52
|
+
/** Generated `{ light, dark }` palette. */
|
|
32
53
|
palette: HextimateResult;
|
|
33
|
-
/**
|
|
34
|
-
* The palette builder backing this provider or scope. Nested
|
|
35
|
-
* `HextimatorScope`s fork from this builder so they inherit any custom
|
|
36
|
-
* roles, variants, tokens, or presets without re-declaring them.
|
|
37
|
-
*/
|
|
54
|
+
/** Underlying palette builder. Nested scopes fork from it. */
|
|
38
55
|
builder: HextimatePaletteBuilder;
|
|
39
56
|
}
|
|
40
57
|
|
|
41
|
-
/**
|
|
42
|
-
* Props for the `HextimatorProvider` component, which manages the state of a Hextimator-generated color palette and injects corresponding CSS variables into the document.
|
|
43
|
-
* - `defaultColor`: The initial base color to generate the palette from.
|
|
44
|
-
* - `style`: Initial options for how the palette is generated (e.g., light/dark settings, contrast requirements).
|
|
45
|
-
* - `format`: Initial options for how the generated palette is formatted (e.g., output format).
|
|
46
|
-
* - `configure`: An initial callback to further customize the palette builder before formatting.
|
|
47
|
-
* - `darkMode`: Strategy for handling dark mode variants in CSS variable injection.
|
|
48
|
-
* - `cssPrefix`: A prefix to apply to all generated CSS variable names.
|
|
49
|
-
* - `target`: An optional ref to a specific DOM element where CSS variables should be injected instead of the document root.
|
|
50
|
-
*/
|
|
51
58
|
interface HextimatorProviderProps {
|
|
52
|
-
|
|
59
|
+
/** Brand color. String sets the same color for both modes; object sets a different color for each mode. */
|
|
60
|
+
defaultColor: ColorInputProp;
|
|
61
|
+
/** Called when the color changes. */
|
|
62
|
+
onColorChange?: (next: ModeColors) => void;
|
|
63
|
+
/** Initial mode. Defaults to `'system'`. */
|
|
53
64
|
defaultMode?: ModePreference;
|
|
65
|
+
/** Called when the mode changes. */
|
|
66
|
+
onModePreferenceChange?: (mode: ModePreference) => void;
|
|
54
67
|
style?: HextimateStyleOptions;
|
|
55
68
|
presets?: HextimatePreset[];
|
|
56
69
|
format?: Omit<HextimateFormatOptions, 'as'>;
|
|
@@ -60,49 +73,21 @@ interface HextimatorProviderProps {
|
|
|
60
73
|
target?: RefObject<HTMLElement | null>;
|
|
61
74
|
}
|
|
62
75
|
/**
|
|
76
|
+
* Provides a hextimator theme to its children.
|
|
63
77
|
*
|
|
64
|
-
*
|
|
65
|
-
* a Hextimator-generated color palette and injects corresponding CSS variables into the document.
|
|
66
|
-
* It allows child components to access and update the
|
|
67
|
-
* base color, style options, and configuration callback, automatically
|
|
68
|
-
* regenerating the palette and updating CSS variables as needed.
|
|
69
|
-
|
|
70
|
-
* The Provider accepts props for the default color, style options, formatting options,
|
|
71
|
-
* dark mode strategy, CSS variable prefix, and an optional target element for CSS variable injection.
|
|
72
|
-
* It uses the `useHextimator` hook to generate the palette and handles the injection of CSS variables based on the current state.
|
|
73
|
-
*
|
|
74
|
-
* Example usage:
|
|
78
|
+
* @example
|
|
75
79
|
* ```tsx
|
|
76
|
-
* <HextimatorProvider defaultColor="#ff6600"
|
|
77
|
-
*
|
|
80
|
+
* <HextimatorProvider defaultColor="#ff6600">
|
|
81
|
+
* <App />
|
|
78
82
|
* </HextimatorProvider>
|
|
79
83
|
* ```
|
|
80
84
|
*/
|
|
81
|
-
declare function HextimatorProvider({ children, defaultColor, defaultMode: initialMode, style: initialStyle, presets: initialPresets, format: formatOpts, configure: initialConfigure, darkMode, cssPrefix, target, }: PropsWithChildren<HextimatorProviderProps>): react_jsx_runtime.JSX.Element;
|
|
85
|
+
declare function HextimatorProvider({ children, defaultColor, onColorChange, defaultMode: initialMode, onModePreferenceChange, style: initialStyle, presets: initialPresets, format: formatOpts, configure: initialConfigure, darkMode, cssPrefix, target, }: PropsWithChildren<HextimatorProviderProps>): react_jsx_runtime.JSX.Element;
|
|
82
86
|
|
|
83
|
-
/**
|
|
84
|
-
* Props for `HextimatorScope`, a wrapper that creates a scoped theme for its
|
|
85
|
-
* descendants via CSS cascade and React context.
|
|
86
|
-
*
|
|
87
|
-
* Unlike `HextimatorProvider` (which themes the whole document root),
|
|
88
|
-
* `HextimatorScope` themes only its subtree. Scopes can nest freely, and the
|
|
89
|
-
* nearest `HextimatorScope` or `HextimatorProvider` wins for
|
|
90
|
-
* `useHextimatorTheme()`.
|
|
91
|
-
*
|
|
92
|
-
* Mode (light/dark) is inherited from the nearest parent context when present,
|
|
93
|
-
* so a scope automatically follows the app's global dark-mode toggle. If you
|
|
94
|
-
* pass `darkMode` explicitly, make sure it matches the strategy your root
|
|
95
|
-
* `HextimatorProvider` uses (class, data, media, etc.) so the dark selectors
|
|
96
|
-
* line up.
|
|
97
|
-
*/
|
|
98
87
|
interface HextimatorScopeProps {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
* `hextimate`), without forking the parent builder. Use for previews or
|
|
103
|
-
* widgets where each scope must match exactly its presets and must not
|
|
104
|
-
* inherit parent roles, variants, or preset merges.
|
|
105
|
-
*/
|
|
88
|
+
/** Brand color for this subtree. String sets the same color for both modes; object sets a different color for each mode. */
|
|
89
|
+
defaultColor: ColorInputProp;
|
|
90
|
+
/** When true, ignores any parent provider/scope and builds the theme from scratch. */
|
|
106
91
|
isolated?: boolean;
|
|
107
92
|
style?: HextimateStyleOptions;
|
|
108
93
|
presets?: HextimatePreset[];
|
|
@@ -111,40 +96,19 @@ interface HextimatorScopeProps {
|
|
|
111
96
|
darkMode?: DarkModeStrategy;
|
|
112
97
|
cssPrefix?: string;
|
|
113
98
|
className?: string;
|
|
114
|
-
/** Inline styles for the
|
|
99
|
+
/** Inline styles for the wrapper `<div>`. */
|
|
115
100
|
wrapperStyle?: CSSProperties;
|
|
116
101
|
children?: ReactNode;
|
|
117
102
|
}
|
|
118
103
|
/**
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
* `<style>` with the generated palette. Also pushes a nested
|
|
122
|
-
* `HextimatorContext` so `useHextimatorTheme()` called from inside the wrapper
|
|
123
|
-
* returns the scope's palette and color state, not the root provider's.
|
|
124
|
-
*
|
|
125
|
-
* When nested inside a `HextimatorProvider` or another `HextimatorScope`, the
|
|
126
|
-
* scope inherits all custom roles, variants, tokens, and presets from its
|
|
127
|
-
* parent by forking the parent's builder — so you only pass the new color.
|
|
128
|
-
* Any per-scope `configure` callback is applied on top of the inherited shape.
|
|
104
|
+
* Themes a single subtree with its own brand color. Inherits the shape (roles,
|
|
105
|
+
* variants, presets) of any parent provider or scope unless `isolated` is set.
|
|
129
106
|
*
|
|
130
107
|
* @example
|
|
131
108
|
* ```tsx
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
135
|
-
* darkMode={{ type: 'class' }}
|
|
136
|
-
* configure={(b) => b
|
|
137
|
-
* .addRole('cta', '#ff0066')
|
|
138
|
-
* .addVariant('hover', { from: 'strong' })
|
|
139
|
-
* }
|
|
140
|
-
* >
|
|
141
|
-
* <App>
|
|
142
|
-
* // Scope inherits cta + hover, just swaps the base color
|
|
143
|
-
* <HextimatorScope defaultColor="#0066ff" darkMode={{ type: 'class' }}>
|
|
144
|
-
* <Card />
|
|
145
|
-
* </HextimatorScope>
|
|
146
|
-
* </App>
|
|
147
|
-
* </HextimatorProvider>
|
|
109
|
+
* <HextimatorScope defaultColor="#0066ff">
|
|
110
|
+
* <Card />
|
|
111
|
+
* </HextimatorScope>
|
|
148
112
|
* ```
|
|
149
113
|
*/
|
|
150
114
|
declare function HextimatorScope({ defaultColor, isolated, style: initialStyle, presets: initialPresets, format: formatOpts, configure: initialConfigure, darkMode, cssPrefix, className, wrapperStyle, children, }: HextimatorScopeProps): react_jsx_runtime.JSX.Element;
|
|
@@ -235,24 +199,12 @@ interface UseHextimatorOptions {
|
|
|
235
199
|
declare function useHextimator(color: string, options?: UseHextimatorOptions): HextimateResult<FlatTokenMap>;
|
|
236
200
|
|
|
237
201
|
/**
|
|
238
|
-
*
|
|
239
|
-
*
|
|
240
|
-
* `HextimatorScope`, returns the scope's palette and `color`/`setColor` state;
|
|
241
|
-
* `mode` and `setMode` are inherited from the nearest enclosing provider.
|
|
242
|
-
*
|
|
243
|
-
* Returned properties:
|
|
244
|
-
* - `color` / `setColor` — the current base color for this scope or provider.
|
|
245
|
-
* - `mode` — the resolved color mode (`'light'` or `'dark'`), accounting for OS preference when set to `'system'`.
|
|
246
|
-
* - `modePreference` — the raw preference (`'light'`, `'dark'`, or `'system'`).
|
|
247
|
-
* - `setMode` — update the mode preference. Pass `'system'` to follow the OS.
|
|
248
|
-
* - `style` / `setStyle` — palette style options.
|
|
249
|
-
* - `configure` / `setConfigure` — builder configuration callback.
|
|
250
|
-
* - `palette` — the generated `HextimateResult`.
|
|
202
|
+
* Reads the current theme from the nearest `HextimatorProvider` or
|
|
203
|
+
* `HextimatorScope`. See `HextimatorContextValue` for the returned fields.
|
|
251
204
|
*
|
|
252
205
|
* @example
|
|
253
206
|
* ```tsx
|
|
254
207
|
* const { mode, setMode } = useHextimatorTheme();
|
|
255
|
-
*
|
|
256
208
|
* <button onClick={() => setMode(mode === 'dark' ? 'light' : 'dark')}>
|
|
257
209
|
* Toggle dark mode
|
|
258
210
|
* </button>
|
|
@@ -262,4 +214,4 @@ declare function useHextimator(color: string, options?: UseHextimatorOptions): H
|
|
|
262
214
|
*/
|
|
263
215
|
declare function useHextimatorTheme(): HextimatorContextValue;
|
|
264
216
|
|
|
265
|
-
export { type HextimatorContextValue, HextimatorProvider, type HextimatorProviderProps, HextimatorScope, type HextimatorScopeProps, HextimatorStyle, type HextimatorStyleProps, type ModePreference, type ResolvedMode, type UseHextimatorOptions, useHextimator, useHextimatorTheme };
|
|
217
|
+
export { type ColorInputProp, type HextimatorContextValue, HextimatorProvider, type HextimatorProviderProps, HextimatorScope, type HextimatorScopeProps, HextimatorStyle, type HextimatorStyleProps, type ModeColors, type ModePreference, type ResolvedMode, type UseHextimatorOptions, useHextimator, useHextimatorTheme };
|
package/dist/react.js
CHANGED
|
@@ -5,8 +5,8 @@ import {
|
|
|
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,45 +16,6 @@ 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);
|
|
@@ -135,6 +96,50 @@ function buildTargetedVars(palette, darkMode, cssPrefix) {
|
|
|
135
96
|
};
|
|
136
97
|
}
|
|
137
98
|
|
|
99
|
+
// src/react/mode.ts
|
|
100
|
+
import { useSyncExternalStore } from "react";
|
|
101
|
+
var darkQuery = typeof window !== "undefined" ? window.matchMedia("(prefers-color-scheme: dark)") : null;
|
|
102
|
+
function useOsPrefersDark() {
|
|
103
|
+
return useSyncExternalStore(
|
|
104
|
+
(cb) => {
|
|
105
|
+
if (!darkQuery) return () => {
|
|
106
|
+
};
|
|
107
|
+
darkQuery.addEventListener("change", cb);
|
|
108
|
+
return () => darkQuery.removeEventListener("change", cb);
|
|
109
|
+
},
|
|
110
|
+
() => darkQuery?.matches ?? false,
|
|
111
|
+
() => false
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
function applyModeToDOM(mode, darkMode) {
|
|
115
|
+
if (typeof document === "undefined") return;
|
|
116
|
+
if (darkMode === false || darkMode.type === "media") return;
|
|
117
|
+
const root = document.documentElement;
|
|
118
|
+
if (darkMode.type === "data") {
|
|
119
|
+
const attr = darkMode.attribute ?? "data-theme";
|
|
120
|
+
if (mode) {
|
|
121
|
+
root.setAttribute(attr, mode);
|
|
122
|
+
} else {
|
|
123
|
+
root.removeAttribute(attr);
|
|
124
|
+
}
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
const cls = darkMode.className ?? "dark";
|
|
128
|
+
const lightCls = cls === "dark" ? "light" : `not-${cls}`;
|
|
129
|
+
root.classList.remove(cls, lightCls);
|
|
130
|
+
if (mode) {
|
|
131
|
+
root.classList.add(mode === "dark" ? cls : lightCls);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// src/react/types.ts
|
|
136
|
+
function normalizeColorProp(value) {
|
|
137
|
+
if (typeof value === "string") {
|
|
138
|
+
return { light: value, dark: value };
|
|
139
|
+
}
|
|
140
|
+
return { light: value.light, dark: value.dark };
|
|
141
|
+
}
|
|
142
|
+
|
|
138
143
|
// src/react/use-stable-options.ts
|
|
139
144
|
import { useRef } from "react";
|
|
140
145
|
function useStableOptions(options) {
|
|
@@ -158,56 +163,14 @@ function useStableOptions(options) {
|
|
|
158
163
|
return ref.current;
|
|
159
164
|
}
|
|
160
165
|
|
|
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
166
|
// src/react/hextimator-provider.tsx
|
|
206
167
|
import { jsx } from "react/jsx-runtime";
|
|
207
168
|
function HextimatorProvider({
|
|
208
169
|
children,
|
|
209
170
|
defaultColor,
|
|
171
|
+
onColorChange,
|
|
210
172
|
defaultMode: initialMode = "system",
|
|
173
|
+
onModePreferenceChange,
|
|
211
174
|
style: initialStyle,
|
|
212
175
|
presets: initialPresets,
|
|
213
176
|
format: formatOpts,
|
|
@@ -216,48 +179,139 @@ function HextimatorProvider({
|
|
|
216
179
|
cssPrefix,
|
|
217
180
|
target
|
|
218
181
|
}) {
|
|
219
|
-
const [
|
|
220
|
-
|
|
182
|
+
const [lightColor, setLightColorState] = useState(
|
|
183
|
+
() => normalizeColorProp(defaultColor).light
|
|
184
|
+
);
|
|
185
|
+
const [darkColor, setDarkColorState] = useState(
|
|
186
|
+
() => normalizeColorProp(defaultColor).dark
|
|
187
|
+
);
|
|
188
|
+
const [modePreference, setModePreferenceState] = useState(initialMode);
|
|
221
189
|
const [style, setStyle] = useState(initialStyle);
|
|
222
190
|
const [presets, setPresets] = useState(initialPresets);
|
|
223
191
|
const [configure, setConfigureState] = useState(() => initialConfigure);
|
|
224
192
|
const osDark = useOsPrefersDark();
|
|
225
193
|
const mode = modePreference === "system" ? osDark ? "dark" : "light" : modePreference;
|
|
226
194
|
const resolvedDarkMode = darkMode ?? { type: "media" };
|
|
227
|
-
|
|
195
|
+
useEffect(() => {
|
|
228
196
|
applyModeToDOM(
|
|
229
197
|
modePreference === "system" ? null : modePreference,
|
|
230
198
|
resolvedDarkMode
|
|
231
199
|
);
|
|
232
200
|
}, [modePreference, resolvedDarkMode]);
|
|
201
|
+
const setColor = useCallback(
|
|
202
|
+
(c) => {
|
|
203
|
+
setLightColorState(c);
|
|
204
|
+
setDarkColorState(c);
|
|
205
|
+
onColorChange?.({ light: c, dark: c });
|
|
206
|
+
},
|
|
207
|
+
[onColorChange]
|
|
208
|
+
);
|
|
209
|
+
const setLightColor = useCallback(
|
|
210
|
+
(c) => {
|
|
211
|
+
setLightColorState(c);
|
|
212
|
+
onColorChange?.({ light: c, dark: darkColor });
|
|
213
|
+
},
|
|
214
|
+
[darkColor, onColorChange]
|
|
215
|
+
);
|
|
216
|
+
const setDarkColor = useCallback(
|
|
217
|
+
(c) => {
|
|
218
|
+
setDarkColorState(c);
|
|
219
|
+
onColorChange?.({ light: lightColor, dark: c });
|
|
220
|
+
},
|
|
221
|
+
[lightColor, onColorChange]
|
|
222
|
+
);
|
|
223
|
+
const setMode = useCallback(
|
|
224
|
+
(next) => {
|
|
225
|
+
setModePreferenceState(next);
|
|
226
|
+
onModePreferenceChange?.(next);
|
|
227
|
+
},
|
|
228
|
+
[onModePreferenceChange]
|
|
229
|
+
);
|
|
233
230
|
const setConfigure = useCallback(
|
|
234
231
|
(fn) => {
|
|
235
232
|
setConfigureState(() => fn);
|
|
236
233
|
},
|
|
237
234
|
[]
|
|
238
235
|
);
|
|
239
|
-
const
|
|
236
|
+
const stable = useStableOptions({
|
|
240
237
|
style,
|
|
241
238
|
presets,
|
|
242
239
|
format: formatOpts,
|
|
243
|
-
configure,
|
|
244
240
|
darkMode,
|
|
245
|
-
cssPrefix
|
|
246
|
-
target
|
|
241
|
+
cssPrefix
|
|
247
242
|
});
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
243
|
+
const buildOne = useCallback(
|
|
244
|
+
(c) => {
|
|
245
|
+
const b = hextimate(c);
|
|
246
|
+
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
247
|
+
b.style(stable.style);
|
|
248
|
+
}
|
|
249
|
+
for (const p of presets ?? []) b.preset(p);
|
|
250
|
+
configure?.(b);
|
|
251
|
+
return b;
|
|
252
|
+
},
|
|
253
|
+
[stable?.style, presets, configure]
|
|
254
|
+
);
|
|
255
|
+
const lightBuilder = useMemo(
|
|
256
|
+
() => buildOne(lightColor),
|
|
257
|
+
[buildOne, lightColor]
|
|
258
|
+
);
|
|
259
|
+
const darkBuilder = useMemo(
|
|
260
|
+
() => lightColor === darkColor ? lightBuilder : buildOne(darkColor),
|
|
261
|
+
[buildOne, lightColor, darkColor, lightBuilder]
|
|
262
|
+
);
|
|
263
|
+
const palette = useMemo(() => {
|
|
264
|
+
const lightResult = lightBuilder.format({
|
|
265
|
+
...stable?.format,
|
|
266
|
+
as: "css"
|
|
267
|
+
});
|
|
268
|
+
if (darkBuilder === lightBuilder) return lightResult;
|
|
269
|
+
const darkResult = darkBuilder.format({
|
|
270
|
+
...stable?.format,
|
|
271
|
+
as: "css"
|
|
272
|
+
});
|
|
273
|
+
return { light: lightResult.light, dark: darkResult.dark };
|
|
274
|
+
}, [lightBuilder, darkBuilder, stable?.format]);
|
|
275
|
+
useEffect(() => {
|
|
276
|
+
const cssPrefixResolved = stable?.cssPrefix ?? "";
|
|
277
|
+
const darkModeResolved = stable?.darkMode ?? { type: "media" };
|
|
278
|
+
const el = target?.current;
|
|
279
|
+
if (el) {
|
|
280
|
+
const vars = buildTargetedVars(
|
|
281
|
+
palette,
|
|
282
|
+
darkModeResolved,
|
|
283
|
+
cssPrefixResolved
|
|
284
|
+
);
|
|
285
|
+
for (const [key, value2] of vars.light) {
|
|
286
|
+
el.style.setProperty(key, value2);
|
|
287
|
+
}
|
|
288
|
+
return () => {
|
|
289
|
+
for (const [key] of vars.light) {
|
|
290
|
+
el.style.removeProperty(key);
|
|
291
|
+
}
|
|
292
|
+
};
|
|
252
293
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
294
|
+
const styleEl = document.createElement("style");
|
|
295
|
+
styleEl.setAttribute("data-hextimator", "");
|
|
296
|
+
styleEl.textContent = buildStyleContent(
|
|
297
|
+
palette,
|
|
298
|
+
darkModeResolved,
|
|
299
|
+
cssPrefixResolved
|
|
300
|
+
);
|
|
301
|
+
document.head.appendChild(styleEl);
|
|
302
|
+
return () => {
|
|
303
|
+
document.head.removeChild(styleEl);
|
|
304
|
+
};
|
|
305
|
+
}, [palette, stable?.darkMode, stable?.cssPrefix, target]);
|
|
306
|
+
const builder = mode === "dark" ? darkBuilder : lightBuilder;
|
|
307
|
+
const value = useMemo(
|
|
258
308
|
() => ({
|
|
259
|
-
color,
|
|
309
|
+
color: mode === "dark" ? darkColor : lightColor,
|
|
260
310
|
setColor,
|
|
311
|
+
lightColor,
|
|
312
|
+
setLightColor,
|
|
313
|
+
darkColor,
|
|
314
|
+
setDarkColor,
|
|
261
315
|
mode,
|
|
262
316
|
modePreference,
|
|
263
317
|
setMode,
|
|
@@ -271,9 +325,14 @@ function HextimatorProvider({
|
|
|
271
325
|
builder
|
|
272
326
|
}),
|
|
273
327
|
[
|
|
274
|
-
color,
|
|
275
328
|
mode,
|
|
329
|
+
lightColor,
|
|
330
|
+
darkColor,
|
|
331
|
+
setColor,
|
|
332
|
+
setLightColor,
|
|
333
|
+
setDarkColor,
|
|
276
334
|
modePreference,
|
|
335
|
+
setMode,
|
|
277
336
|
style,
|
|
278
337
|
presets,
|
|
279
338
|
configure,
|
|
@@ -289,9 +348,9 @@ function HextimatorProvider({
|
|
|
289
348
|
import {
|
|
290
349
|
useCallback as useCallback2,
|
|
291
350
|
useContext,
|
|
292
|
-
useEffect as
|
|
351
|
+
useEffect as useEffect2,
|
|
293
352
|
useId,
|
|
294
|
-
useMemo as
|
|
353
|
+
useMemo as useMemo2,
|
|
295
354
|
useState as useState2
|
|
296
355
|
} from "react";
|
|
297
356
|
import { jsx as jsx2, jsxs } from "react/jsx-runtime";
|
|
@@ -310,13 +369,26 @@ function HextimatorScope({
|
|
|
310
369
|
}) {
|
|
311
370
|
const id = useId();
|
|
312
371
|
const selector = `[data-hextimator-scope="${id}"]`;
|
|
313
|
-
const [
|
|
372
|
+
const [lightColor, setLightColorState] = useState2(
|
|
373
|
+
() => normalizeColorProp(defaultColor).light
|
|
374
|
+
);
|
|
375
|
+
const [darkColor, setDarkColorState] = useState2(
|
|
376
|
+
() => normalizeColorProp(defaultColor).dark
|
|
377
|
+
);
|
|
314
378
|
const [style, setStyle] = useState2(initialStyle);
|
|
315
379
|
const [presets, setPresets] = useState2(initialPresets);
|
|
316
380
|
const [configure, setConfigureState] = useState2(() => initialConfigure);
|
|
317
|
-
|
|
318
|
-
|
|
381
|
+
useEffect2(() => {
|
|
382
|
+
const next = normalizeColorProp(defaultColor);
|
|
383
|
+
setLightColorState(next.light);
|
|
384
|
+
setDarkColorState(next.dark);
|
|
319
385
|
}, [defaultColor]);
|
|
386
|
+
const setLightColor = setLightColorState;
|
|
387
|
+
const setDarkColor = setDarkColorState;
|
|
388
|
+
const setColor = useCallback2((c) => {
|
|
389
|
+
setLightColorState(c);
|
|
390
|
+
setDarkColorState(c);
|
|
391
|
+
}, []);
|
|
320
392
|
const setConfigure = useCallback2(
|
|
321
393
|
(fn) => {
|
|
322
394
|
setConfigureState(() => fn);
|
|
@@ -331,30 +403,46 @@ function HextimatorScope({
|
|
|
331
403
|
darkMode,
|
|
332
404
|
cssPrefix
|
|
333
405
|
});
|
|
334
|
-
const
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
406
|
+
const buildOne = useCallback2(
|
|
407
|
+
(c) => {
|
|
408
|
+
const b = !isolated && parent?.builder ? parent.builder.fork(c) : hextimate(c);
|
|
409
|
+
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
410
|
+
b.style(stable.style);
|
|
411
|
+
}
|
|
412
|
+
for (const p of presets ?? []) b.preset(p);
|
|
413
|
+
configure?.(b);
|
|
414
|
+
return b;
|
|
415
|
+
},
|
|
416
|
+
[parent?.builder, isolated, stable?.style, presets, configure]
|
|
417
|
+
);
|
|
418
|
+
const lightBuilder = useMemo2(
|
|
419
|
+
() => buildOne(lightColor),
|
|
420
|
+
[buildOne, lightColor]
|
|
421
|
+
);
|
|
422
|
+
const darkBuilder = useMemo2(
|
|
423
|
+
() => lightColor === darkColor ? lightBuilder : buildOne(darkColor),
|
|
424
|
+
[buildOne, lightColor, darkColor, lightBuilder]
|
|
425
|
+
);
|
|
426
|
+
const palette = useMemo2(() => {
|
|
427
|
+
const lightResult = lightBuilder.format({
|
|
345
428
|
...stable?.format,
|
|
346
429
|
as: "css"
|
|
347
|
-
})
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
430
|
+
});
|
|
431
|
+
if (darkBuilder === lightBuilder) return lightResult;
|
|
432
|
+
const darkResult = darkBuilder.format({
|
|
433
|
+
...stable?.format,
|
|
434
|
+
as: "css"
|
|
435
|
+
});
|
|
436
|
+
return { light: lightResult.light, dark: darkResult.dark };
|
|
437
|
+
}, [lightBuilder, darkBuilder, stable?.format]);
|
|
438
|
+
const css = useMemo2(
|
|
351
439
|
() => buildStyleContent(
|
|
352
440
|
palette,
|
|
353
441
|
stable?.darkMode ?? { type: "media" },
|
|
354
442
|
stable?.cssPrefix ?? "",
|
|
355
443
|
selector
|
|
356
444
|
),
|
|
357
|
-
[palette, stable, selector]
|
|
445
|
+
[palette, stable?.darkMode, stable?.cssPrefix, selector]
|
|
358
446
|
);
|
|
359
447
|
const osDark = useOsPrefersDark();
|
|
360
448
|
const noopSetMode = useCallback2(() => {
|
|
@@ -362,10 +450,16 @@ function HextimatorScope({
|
|
|
362
450
|
const mode = parent?.mode ?? (osDark ? "dark" : "light");
|
|
363
451
|
const modePreference = parent?.modePreference ?? "system";
|
|
364
452
|
const setMode = parent?.setMode ?? noopSetMode;
|
|
365
|
-
const
|
|
453
|
+
const builder = mode === "dark" ? darkBuilder : lightBuilder;
|
|
454
|
+
const activeColor = mode === "dark" ? darkColor : lightColor;
|
|
455
|
+
const value = useMemo2(
|
|
366
456
|
() => ({
|
|
367
|
-
color,
|
|
457
|
+
color: activeColor,
|
|
368
458
|
setColor,
|
|
459
|
+
lightColor,
|
|
460
|
+
setLightColor,
|
|
461
|
+
darkColor,
|
|
462
|
+
setDarkColor,
|
|
369
463
|
mode,
|
|
370
464
|
modePreference,
|
|
371
465
|
setMode,
|
|
@@ -379,7 +473,10 @@ function HextimatorScope({
|
|
|
379
473
|
builder
|
|
380
474
|
}),
|
|
381
475
|
[
|
|
382
|
-
|
|
476
|
+
activeColor,
|
|
477
|
+
setColor,
|
|
478
|
+
lightColor,
|
|
479
|
+
darkColor,
|
|
383
480
|
mode,
|
|
384
481
|
modePreference,
|
|
385
482
|
setMode,
|
|
@@ -406,7 +503,7 @@ function HextimatorScope({
|
|
|
406
503
|
}
|
|
407
504
|
|
|
408
505
|
// src/react/hextimator-style.tsx
|
|
409
|
-
import { useMemo as
|
|
506
|
+
import { useMemo as useMemo3 } from "react";
|
|
410
507
|
import { jsx as jsx3 } from "react/jsx-runtime";
|
|
411
508
|
function HextimatorStyle({
|
|
412
509
|
color,
|
|
@@ -425,7 +522,7 @@ function HextimatorStyle({
|
|
|
425
522
|
darkMode,
|
|
426
523
|
cssPrefix
|
|
427
524
|
});
|
|
428
|
-
const css =
|
|
525
|
+
const css = useMemo3(() => {
|
|
429
526
|
const builder = hextimate(color);
|
|
430
527
|
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
431
528
|
builder.style(stable.style);
|
|
@@ -446,6 +543,51 @@ function HextimatorStyle({
|
|
|
446
543
|
return /* @__PURE__ */ jsx3("style", { "data-hextimator": "", children: css });
|
|
447
544
|
}
|
|
448
545
|
|
|
546
|
+
// src/react/use-hextimator.ts
|
|
547
|
+
import { useEffect as useEffect3, useMemo as useMemo4 } from "react";
|
|
548
|
+
function useHextimator(color, options) {
|
|
549
|
+
const stable = useStableOptions(options);
|
|
550
|
+
const configure = options?.configure;
|
|
551
|
+
const target = options?.target;
|
|
552
|
+
const presets = stable?.presets;
|
|
553
|
+
const palette = useMemo4(() => {
|
|
554
|
+
const builder = hextimate(color);
|
|
555
|
+
if (stable?.style && Object.keys(stable.style).length > 0) {
|
|
556
|
+
builder.style(stable.style);
|
|
557
|
+
}
|
|
558
|
+
for (const p of presets ?? []) builder.preset(p);
|
|
559
|
+
configure?.(builder);
|
|
560
|
+
return builder.format({
|
|
561
|
+
...stable?.format,
|
|
562
|
+
as: "css"
|
|
563
|
+
});
|
|
564
|
+
}, [color, stable?.style, presets, stable?.format, configure]);
|
|
565
|
+
const darkMode = stable?.darkMode ?? { type: "media" };
|
|
566
|
+
const cssPrefix = stable?.cssPrefix ?? "";
|
|
567
|
+
useEffect3(() => {
|
|
568
|
+
const el = target?.current;
|
|
569
|
+
if (el) {
|
|
570
|
+
const vars = buildTargetedVars(palette, darkMode, cssPrefix);
|
|
571
|
+
for (const [key, value] of vars.light) {
|
|
572
|
+
el.style.setProperty(key, value);
|
|
573
|
+
}
|
|
574
|
+
return () => {
|
|
575
|
+
for (const [key] of vars.light) {
|
|
576
|
+
el.style.removeProperty(key);
|
|
577
|
+
}
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
const style = document.createElement("style");
|
|
581
|
+
style.setAttribute("data-hextimator", "");
|
|
582
|
+
style.textContent = buildStyleContent(palette, darkMode, cssPrefix);
|
|
583
|
+
document.head.appendChild(style);
|
|
584
|
+
return () => {
|
|
585
|
+
document.head.removeChild(style);
|
|
586
|
+
};
|
|
587
|
+
}, [palette, darkMode, cssPrefix, target]);
|
|
588
|
+
return palette;
|
|
589
|
+
}
|
|
590
|
+
|
|
449
591
|
// src/react/use-hextimator-theme.ts
|
|
450
592
|
import { useContext as useContext2 } from "react";
|
|
451
593
|
function useHextimatorTheme() {
|
package/llms.txt
CHANGED
|
@@ -378,8 +378,10 @@ For app-wide theming with dynamic color changes:
|
|
|
378
378
|
import { HextimatorProvider, useHextimatorTheme } from "hextimator/react";
|
|
379
379
|
|
|
380
380
|
<HextimatorProvider
|
|
381
|
-
defaultColor="#6A5ACD"
|
|
382
|
-
defaultMode="system"
|
|
381
|
+
defaultColor="#6A5ACD" // string OR { light: "#...", dark: "#..." }
|
|
382
|
+
defaultMode="system" // "light" | "dark" | "system"
|
|
383
|
+
onColorChange={(next) => persist(next)} // fires on every color change
|
|
384
|
+
onModePreferenceChange={(mode) => persist(mode)}
|
|
383
385
|
style={styleOpts}
|
|
384
386
|
presets={[presets.muted, presets.shadcn]}
|
|
385
387
|
format={formatOpts}
|
|
@@ -391,13 +393,23 @@ import { HextimatorProvider, useHextimatorTheme } from "hextimator/react";
|
|
|
391
393
|
</HextimatorProvider>
|
|
392
394
|
```
|
|
393
395
|
|
|
396
|
+
**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.
|
|
397
|
+
|
|
398
|
+
**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}`.
|
|
399
|
+
|
|
400
|
+
**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>`.
|
|
401
|
+
|
|
394
402
|
`useHextimatorTheme()` returns:
|
|
395
403
|
|
|
396
404
|
| Property | Type | Description |
|
|
397
405
|
|---|---|---|
|
|
398
|
-
| `color` | `string` |
|
|
399
|
-
| `setColor` | `(color: string) => void` |
|
|
400
|
-
| `
|
|
406
|
+
| `color` | `string` | Color active for the resolved mode (= `lightColor` in light, `darkColor` in dark) |
|
|
407
|
+
| `setColor` | `(color: string) => void` | Sets BOTH `lightColor` and `darkColor` (one-color-in, whole-theme-out) |
|
|
408
|
+
| `lightColor` | `string` | Color used to generate the light-mode palette |
|
|
409
|
+
| `setLightColor` | `(color: string) => void` | Update only the light-mode color |
|
|
410
|
+
| `darkColor` | `string` | Color used to generate the dark-mode palette |
|
|
411
|
+
| `setDarkColor` | `(color: string) => void` | Update only the dark-mode color |
|
|
412
|
+
| `palette` | `HextimateResult` | The generated `{ light, dark }` palette (light from `lightColor`, dark from `darkColor`) |
|
|
401
413
|
| `mode` | `"light" \| "dark"` | Resolved color mode (accounts for OS preference) |
|
|
402
414
|
| `modePreference` | `"light" \| "dark" \| "system"` | Raw preference before resolution |
|
|
403
415
|
| `setMode` | `(mode: ModePreference) => void` | Change mode preference |
|
|
@@ -449,7 +461,7 @@ import { HextimatorProvider, HextimatorScope } from "hextimator/react";
|
|
|
449
461
|
</HextimatorProvider>
|
|
450
462
|
```
|
|
451
463
|
|
|
452
|
-
Props: `defaultColor
|
|
464
|
+
Props: `defaultColor` (`string \| { light, dark }`), `isolated?`, `style?`, `presets?`, `format?`, `configure?`, `darkMode?`, `cssPrefix?`, `className?`, `wrapperStyle?` (inline styles for the scope element).
|
|
453
465
|
|
|
454
466
|
## `fallback.css`
|
|
455
467
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hextimator",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.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",
|