hextimator 0.14.4 → 0.15.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 CHANGED
@@ -1,3 +1,17 @@
1
+ ## 0.15.0
2
+
3
+ ### Minor Changes
4
+
5
+ - [#200](https://github.com/fgrgic/hextimator/pull/200) [`b220fdf`](https://github.com/fgrgic/hextimator/commit/b220fdf0e113e883afa25673e2e7f1634ffd9dcc) Thanks [@fgrgic](https://github.com/fgrgic)! - Deprecate `fromConfig()`, `HextimateConfig`, `.preset()`, `HextimatePreset`, `.style()`, `.addRole()`, `.addVariant()`, `.addToken()`, and `format.roleNames` in favor of `hextimate(accent, theme)`. They still work. The migration guide shows the theme equivalent of each.
6
+
7
+ - [#202](https://github.com/fgrgic/hextimator/pull/202) [`ad90f31`](https://github.com/fgrgic/hextimator/commit/ad90f31000bd9d53a4138f8beb11ecbd06c86aed) Thanks [@fgrgic](https://github.com/fgrgic)! - Add a `theme` option to `useHextimator`, `HextimatorProvider`, `HextimatorScope`, and `HextimatorStyle`. It builds with `hextimate(color, theme)`, and `configure` runs after it. A scope with its own `theme` builds fresh instead of inheriting the parent. Deprecate the `style` and `presets` options in favor of `theme`. They still work.
8
+
9
+ - [#187](https://github.com/fgrgic/hextimator/pull/187) [`616f4bd`](https://github.com/fgrgic/hextimator/commit/616f4bdb2cd51abb915086635059443bb9499113) Thanks [@fgrgic](https://github.com/fgrgic)! - Add `surface-foreground-accent`, `surface-foreground-positive`, `surface-foreground-negative`, and `surface-foreground-caution`. Each is text in the role's color that meets `minContrastRatio` on surface DEFAULT, weak, and strong. Use `text-surface-foreground-negative` for an error message on the page. Existing tokens are unchanged.
10
+
11
+ - [#199](https://github.com/fgrgic/hextimator/pull/199) [`4ecb246`](https://github.com/fgrgic/hextimator/commit/4ecb246f41eff44e6bc9b875361da5976fadb344) Thanks [@fgrgic](https://github.com/fgrgic)! - Add `tailwindAliases(theme)` to generate Tailwind v4 aliases for a custom theme's runtime CSS variables. Pass `{ inverted: true }` to generate aliases for inverted tokens. The package build now generates both shipped Tailwind stylesheets from `baseTheme`.
12
+
13
+ - [#198](https://github.com/fgrgic/hextimator/pull/198) [`9bd8db7`](https://github.com/fgrgic/hextimator/commit/9bd8db73a221dd3b2e5cb467d65ddcec917e1fa1) Thanks [@fgrgic](https://github.com/fgrgic)! - Add `hextimate(accent, theme)` to generate light and dark palettes from a reusable `ThemeDefinition`. Extend `baseTheme` to define your own, or use one of the six built-ins from `themes`.
14
+
1
15
  ## 0.14.4
2
16
 
3
17
  ### Patch Changes
package/README.md CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  Runtime theming for multi-tenant apps.
14
14
 
15
- Your customers pick a brand color. Your app looks good every time. No per-customer design reviews. No manual tuning. Yellow does not break your UI.
15
+ Your customers pick a brand color; your app always looks good. No per-customer design reviews. No manual tuning. Yellow does not break your UI.
16
16
 
17
17
  Each call takes under a millisecond (~0.4ms [^perf]). Same color and options always produce the same tokens. Run it on the request path. Cache it if you want.
18
18
 
@@ -23,7 +23,7 @@ Try it: **[hextimator.com](https://hextimator.com)**
23
23
  ## Why `hextimator` exists
24
24
 
25
25
  <p align="center">
26
- <video src="https://github.com/user-attachments/assets/245f0728-38e3-425a-8d0c-87bff6f48b0f" width="100%" controls playsinline></video>
26
+ <video src="https://github.com/user-attachments/assets/fec967e9-9e5d-4bb1-8e02-15fbc02afb0b" width="100%" controls playsinline></video>
27
27
  </p>
28
28
 
29
29
  You ship a B2B, B2B2C, or white-label app. Every tenant brings a brand color. Your choices today:
@@ -45,72 +45,117 @@ hextimator is not the first palette generator. It targets one job the others ski
45
45
 
46
46
  Hand-crafting one brand's design system? Several of these beat hextimator. Color comes from user input (tenants, white-label customers, CMS authors)? That is hextimator's job.
47
47
 
48
- ## Installation
48
+ ## 30-second start
49
49
 
50
50
  ```bash
51
51
  npm i hextimator
52
52
  ```
53
53
 
54
- **Tailwind v4:** import `hextimator/tailwind.css` in your CSS entry (see [Tailwind](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/tailwind.md)).
55
-
56
- ## 30-second integration
57
-
58
54
  ```ts
59
55
  import { hextimate } from "hextimator";
60
56
 
61
57
  const css = hextimate("#C0FFEE").format({ as: "css" });
62
58
  ```
63
59
 
64
- `hextimate` turns one color into a theme. You choose how to ship it: inline `<style>`, a `.css` file, a template partial, an edge cache, or something else. For tokens instead of a stylesheet, use `format({ as: 'object' })`.
60
+ One color in, light and dark CSS variables out. Ship them however you like: inline `<style>`, a `.css` file, an edge cache. No DOM, so it works with SSR.
65
61
 
66
- No DOM. Pure computation. Works with SSR.
67
-
68
- ### React (SSR-safe)
62
+ React:
69
63
 
70
64
  ```tsx
71
65
  import { HextimatorStyle } from "hextimator/react";
72
66
 
73
- <HextimatorStyle color={"#0FF1CE"} />;
67
+ <HextimatorStyle color="#0FF1CE" />;
74
68
  ```
75
69
 
76
- Put it in your layout `<head>`. Server render writes a `<style>` node. No `useEffect`. No FOUC when you pair it with static HTML. API and dark mode: [React](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/react.md).
70
+ CLI: `npx hextimator '#BADA55'`
71
+
72
+ ## Tailwind
77
73
 
78
- ### CLI (and AI agents)
74
+ ```css
75
+ @import "tailwindcss";
76
+ @import "hextimator/tailwind.css";
77
+ ```
78
+
79
+ ```html
80
+ <button class="bg-accent text-accent-foreground">Save</button>
81
+ <p class="text-surface-foreground-negative">Payment failed</p>
82
+ ```
79
83
 
80
- Same engine as the library. Flags for presets and format.
84
+ Every class uses your brand color, run through the theme. Change the color at runtime and the classes follow.
81
85
 
82
- ```bash
83
- npx hextimator '#BADA55' --preset shadcn # framework-shaped tokens
84
- npx hextimator '#BADA55' --preset muted # style preset
85
- npx hextimator '#BADA55' --preset vibrant
86
- npx hextimator '#BADA55' --preset bold
86
+ ## Theme
87
+
88
+ The theme decides what your brand color turns into. You can use it as is, add to it, or replace it.
89
+
90
+ ### Use it as is
91
+
92
+ ```ts
93
+ hextimate(accent).format();
94
+ ```
95
+
96
+ You get five roles: `surface`, `accent`, `positive`, `negative`, and `caution`. Each has `DEFAULT`, `strong`, `weak`, `foreground`, and `foreground-weak`. The surface also gets readable colored text for each role, such as `surface-foreground-negative`. Plus `brand-exact`, your input color unchanged.
97
+
98
+ Every foreground meets WCAG AAA contrast against its background.
99
+
100
+ ### Add to it
101
+
102
+ Spread `baseTheme` and add what you need:
103
+
104
+ ```ts
105
+ import { baseTheme, hextimate, type ThemeDefinition } from "hextimator";
106
+
107
+ const theme = {
108
+ ...baseTheme,
109
+ roles: { ...baseTheme.roles, cta: { color: "#ee2244" } },
110
+ tokens: [...baseTheme.tokens, { name: "ring", value: { from: "accent" } }],
111
+ } satisfies ThemeDefinition;
112
+
113
+ hextimate(accent, theme).format();
87
114
  ```
88
115
 
89
- ## What is in every theme
116
+ With Tailwind, keep `hextimator/tailwind.css` and register the new names:
117
+
118
+ ```css
119
+ @theme inline {
120
+ --color-cta: var(--cta);
121
+ --color-cta-foreground: var(--cta-foreground);
122
+ --color-ring: var(--ring);
123
+ }
124
+ ```
90
125
 
91
- | Role | Variants |
92
- | -------- | ------------------------------------------------- |
93
- | surface | DEFAULT, strong, weak, foreground, foreground-weak |
94
- | accent | DEFAULT, strong, weak, foreground, foreground-weak |
95
- | positive | DEFAULT, strong, weak, foreground, foreground-weak |
96
- | negative | DEFAULT, strong, weak, foreground, foreground-weak |
97
- | caution | DEFAULT, strong, weak, foreground, foreground-weak |
126
+ ### Replace it
98
127
 
99
- `foreground-weak` is the softest foreground that still meets your contrast target against the role's DEFAULT background - use it for secondary or muted text.
128
+ Declare only what you want:
100
129
 
101
- Plus **`brand-exact`** (your input color, unchanged) and **`brand-exact-foreground`**.
130
+ ```ts
131
+ const theme = {
132
+ roles: {
133
+ page: { generate: "surface" },
134
+ brand: { generate: "accent" },
135
+ danger: { generate: "negative" },
136
+ },
137
+ } satisfies ThemeDefinition;
138
+
139
+ hextimate(accent, theme).format(); // --page, --brand, --danger, ...
140
+ ```
141
+
142
+ With Tailwind, generate the stylesheet from your theme in a build script and import it instead of `hextimator/tailwind.css`:
143
+
144
+ ```ts
145
+ import { tailwindAliases } from "hextimator";
146
+
147
+ writeFileSync("src/hextimator-theme.css", tailwindAliases(theme));
148
+ ```
102
149
 
103
- ## Customization and reference
150
+ Rerun it when the theme changes. Accent changes need no rebuild.
104
151
 
105
- - [Customization](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/customization.md): style and format options
106
- - [Extending the palette](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/extending-the-palette.md): `addRole`, `addVariant`, `addToken`
107
- - [Presets](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/presets.md): shadcn/ui configs and custom presets
108
- - [Multiple themes](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/multiple-themes.md): dynamic theming and `.fork()`
109
- - [React](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/react.md): hook, `HextimatorStyle`, provider, scoped themes, dark mode
110
- - [Tailwind CSS v4](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/tailwind.md): setup and usage
152
+ Ready-made themes for shadcn/ui, MUI, and a few styles live in `themes`: `hextimate(accent, themes.shadcn)`.
111
153
 
112
- ### Also
154
+ ## Docs
113
155
 
156
+ - [Themes](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/themes.md): every field, style, and format option
157
+ - [Built-in themes](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/presets.md)
158
+ - [React](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/react.md)
114
159
  - [Color vision deficiency](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/color-vision-deficiency.md)
115
160
  - [Examples](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/examples.md)
116
161
  - [Migration](https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md)
@@ -123,7 +168,7 @@ APCA is the perceptual model in draft WCAG 3. Better for a lot of UI. Not what p
123
168
 
124
169
  ## Stability
125
170
 
126
- The API follows strict semver. Builder methods, options, formats, CLI flags, and token names only break in majors, with notes in migration.md.
171
+ The API follows strict semver. Theme fields, builder methods, options, formats, CLI flags, and token names only break in majors, with notes in migration.md.
127
172
 
128
173
  Generated color values are a separate contract. Output is fully deterministic: same version, same color, same options, same tokens. The algorithm itself may improve in minors (wider hue ranges, better anchors), which changes the exact values a theme produces. Every such change is flagged in the changelog with how to opt back into the old behavior.
129
174
 
@@ -100,7 +100,8 @@ interface ThemeAdjustments {
100
100
  */
101
101
  baseLightnessRange?: readonly [number, number];
102
102
  /**
103
- * Minimum contrast between non-foreground variants and foreground.
103
+ * Minimum contrast between non-foreground variants and foreground, and
104
+ * between `surface-foreground-<role>` and every surface variant.
104
105
  * `"AAA"` → 7, `"AA"` → 4.5, or a number.
105
106
  */
106
107
  minContrastRatio?: 'AAA' | 'AA' | number;
@@ -178,6 +179,9 @@ interface HextimateFormatOptions {
178
179
  * - positive: "positive"
179
180
  * - negative: "negative"
180
181
  * - caution: "caution"
182
+ *
183
+ * @deprecated Rename roles with `ThemeDefinition` role keys: `roles: { brand: { generate: 'accent' } }`.
184
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
181
185
  */
182
186
  roleNames?: Record<string, string>;
183
187
  /**
@@ -322,6 +326,9 @@ type FormatResult = FlatTokenMap | NestedTokenMap | string;
322
326
  * const theme = hextimate('#6366F1')
323
327
  * .preset(presets.shadcn)
324
328
  * .format();
329
+ *
330
+ * @deprecated Use a `ThemeDefinition` with `hextimate(accent, theme)`.
331
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
325
332
  */
326
333
  interface HextimatePreset {
327
334
  /** Style options (contrast, hue shifts, lightness, chroma). Applied before roles/variants/tokens. */
@@ -345,6 +352,70 @@ interface HextimatePreset {
345
352
  format?: HextimateFormatOptions;
346
353
  }
347
354
 
355
+ type RoleGenerator = 'surface' | 'accent' | 'positive' | 'negative' | 'caution';
356
+ type TokenGenerator = 'brand-exact' | 'brand-exact-foreground';
357
+ type RoleDefinition = {
358
+ generate: RoleGenerator;
359
+ } | {
360
+ color: ColorInput;
361
+ } | DerivedToken;
362
+ type ThemeTokenValue = TokenValue | {
363
+ generate: TokenGenerator;
364
+ };
365
+ /**
366
+ * A palette structure that turns any accent into light and dark palettes.
367
+ * Plain data, so it survives a JSON round trip.
368
+ */
369
+ interface ThemeDefinition {
370
+ /** Accent for tools that preview a theme on its own. `hextimate(accent, theme)` ignores it. */
371
+ defaultAccent?: ColorInput;
372
+ style?: HextimateStyleOptions;
373
+ /** The key sets the output role name. */
374
+ roles?: Record<string, RoleDefinition>;
375
+ variants?: Array<{
376
+ name: string;
377
+ placement: VariantPlacement;
378
+ }>;
379
+ tokens?: Array<{
380
+ name: string;
381
+ value: ThemeTokenValue;
382
+ }>;
383
+ /** Renames variant suffixes in the output, such as `{ foreground: 'contrastText' }`. */
384
+ variantNames?: Record<string, string>;
385
+ /** Default output options. `.format()` options override them. */
386
+ format?: HextimateFormatOptions;
387
+ }
388
+ declare const baseTheme: {
389
+ roles: {
390
+ surface: {
391
+ generate: "surface";
392
+ };
393
+ accent: {
394
+ generate: "accent";
395
+ };
396
+ positive: {
397
+ generate: "positive";
398
+ };
399
+ negative: {
400
+ generate: "negative";
401
+ };
402
+ caution: {
403
+ generate: "caution";
404
+ };
405
+ };
406
+ tokens: ({
407
+ name: string;
408
+ value: {
409
+ generate: "brand-exact";
410
+ };
411
+ } | {
412
+ name: string;
413
+ value: {
414
+ generate: "brand-exact-foreground";
415
+ };
416
+ })[];
417
+ };
418
+
348
419
  /** The result of formatting a palette, containing both light and dark theme tokens. */
349
420
  interface HextimateResult<F = FormatResult> {
350
421
  light: F;
@@ -403,21 +474,31 @@ declare class HextimatePaletteBuilder {
403
474
  private lightPalette;
404
475
  private darkPalette;
405
476
  private readonly inputColor;
477
+ private readonly theme;
406
478
  private options;
407
479
  private readonly operations;
408
480
  private readonly standaloneTokens;
481
+ private readonly foregroundOverrides;
409
482
  private readonly weakSideVariants;
410
483
  private readonly strongSideVariants;
411
484
  private readonly foregroundAnchoredVariants;
412
485
  private readonly betweenVariants;
413
486
  private presetFormatDefaults?;
414
- constructor(color: Color);
487
+ constructor(color: Color, theme?: ThemeDefinition);
415
488
  /**
416
489
  * Merges style options into the builder and regenerates the palettes,
417
490
  * then replays recorded operations so roles and variants reflect the new style.
491
+ *
492
+ * @deprecated Set `style` on a `ThemeDefinition`: `hextimate(accent, { ...theme, style })`.
493
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
418
494
  */
419
495
  style(partial: Partial<HextimateStyleOptions>): this;
420
496
  private seedOptionsForFork;
497
+ /** Custom roles, variants, tokens, and format defaults from the theme. Generated roles already exist. */
498
+ private applyTheme;
499
+ private resolveThemeToken;
500
+ /** Every role the theme declares gets colored text on its surface role. */
501
+ private surfaceForegroundRoles;
421
502
  private rebuildFromOperations;
422
503
  private replayOperationInPlace;
423
504
  /**
@@ -431,6 +512,8 @@ declare class HextimatePaletteBuilder {
431
512
  *
432
513
  * @param name Role name (e.g. "cta", "banner")
433
514
  * @param color Base color or derived token for the role
515
+ * @deprecated Declare the role in a `ThemeDefinition`: `roles: { cta: { color: '#ff0066' } }`.
516
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
434
517
  */
435
518
  addRole(name: string, color: ColorInput | DerivedToken): this;
436
519
  /**
@@ -441,6 +524,8 @@ declare class HextimatePaletteBuilder {
441
524
  *
442
525
  * @param name Variant name (e.g. "placeholder", "highlight")
443
526
  * @param placement Placement of the variant, either `{ from: 'weak' }` or `{ between: ['DEFAULT', 'strong'] }`
527
+ * @deprecated Declare the variant in a `ThemeDefinition`: `variants: [{ name, placement }]`.
528
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
444
529
  */
445
530
  addVariant(name: string, placement: VariantPlacement): this;
446
531
  /**
@@ -448,6 +533,9 @@ declare class HextimatePaletteBuilder {
448
533
  *
449
534
  * Override with a dotted key: `surface.weak`, `surface.DEFAULT` (not `surface-weak`).
450
535
  * A kebab key that matches a generated label throws at format time.
536
+ *
537
+ * @deprecated Declare the token in a `ThemeDefinition`: `tokens: [{ name, value }]`.
538
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
451
539
  */
452
540
  addToken(name: string, value: TokenValue): this;
453
541
  /**
@@ -468,6 +556,9 @@ declare class HextimatePaletteBuilder {
468
556
  * const theme = hextimate('#6366F1')
469
557
  * .preset(presets.shadcn)
470
558
  * .format({ colors: 'hsl-raw' });
559
+ *
560
+ * @deprecated Pass a `ThemeDefinition`: `hextimate(accent, themes.shadcn)`.
561
+ * See the {@link https://github.com/fgrgic/hextimator/blob/main/packages/hextimator/docs/migration.md#014x--0150-themes | Migration guide}.
471
562
  */
472
563
  preset(preset: HextimatePreset): this;
473
564
  /**
@@ -538,6 +629,7 @@ declare class HextimatePaletteBuilder {
538
629
  }): HextimateResult<FlatTokenMap>;
539
630
  format(options?: HextimateFormatOptions): HextimateResult | string;
540
631
  private buildInvertedEntries;
632
+ private outputPalette;
541
633
  private applyRole;
542
634
  private applyVariant;
543
635
  private applyAnchoredVariant;
@@ -569,4 +661,4 @@ declare class HextimatePaletteBuilder {
569
661
  private resolvedOptions;
570
662
  }
571
663
 
572
- export { type Color as C, type DerivedToken as D, type FlatTokenMap as F, HextimatePaletteBuilder as H, type NestedTokenMap as N, type OKLCH as O, type ThemeAdjustments as T, type VariantPlacement as V, type CVDType as a, type ColorSpace as b, type ColorInSpace as c, type ColorInput as d, type FormatResult as e, type HextimateFormatOptions as f, type HextimatePreset as g, type HextimateResult as h, type HextimateStyleOptions as i, type TokenValue as j };
664
+ export { type Color as C, type DerivedToken as D, type FlatTokenMap as F, HextimatePaletteBuilder as H, type NestedTokenMap as N, type OKLCH as O, type RoleDefinition as R, type ThemeDefinition as T, type VariantPlacement as V, type CVDType as a, type ColorSpace as b, type ColorInSpace as c, type ColorInput as d, type HextimateFormatOptions as e, type FormatResult as f, type HextimatePreset as g, type HextimateResult as h, type HextimateStyleOptions as i, type RoleGenerator as j, type ThemeAdjustments as k, type ThemeTokenValue as l, type TokenGenerator as m, type TokenValue as n, baseTheme as o };