@motion-proto/live-tokens 0.50.0 → 0.53.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.
Files changed (91) hide show
  1. package/.claude/skills/live-tokens-adjust-shape-space/SKILL.md +7 -1
  2. package/CHANGELOG.md +108 -0
  3. package/bin/adjust.mjs +16 -39
  4. package/bin/generate-theme.mjs +20 -6
  5. package/dist-plugin/adjust/index.cjs +781 -21
  6. package/dist-plugin/adjust/index.d.cts +2 -1
  7. package/dist-plugin/adjust/index.d.ts +2 -1
  8. package/dist-plugin/adjust/index.js +43 -21
  9. package/dist-plugin/{chunk-6OZFXIQI.js → chunk-232GZGQU.js} +207 -12
  10. package/dist-plugin/chunk-NE6N66EE.js +753 -0
  11. package/dist-plugin/{chunk-76TFDTJO.js → chunk-PB2JTK2H.js} +179 -128
  12. package/dist-plugin/{chunk-44RSTAII.js → chunk-TKZBVIW5.js} +33 -5
  13. package/dist-plugin/chunk-YR6GPXW2.js +129 -0
  14. package/dist-plugin/generateColorsAndType/index.cjs +135 -63
  15. package/dist-plugin/generateColorsAndType/index.d.cts +2 -1
  16. package/dist-plugin/generateColorsAndType/index.d.ts +2 -1
  17. package/dist-plugin/generateColorsAndType/index.js +28 -784
  18. package/dist-plugin/{themeTypes-DSwKq-bj.d.ts → index-DpTIRZ2H.d.cts} +67 -26
  19. package/dist-plugin/{themeTypes-DSwKq-bj.d.cts → index-DpTIRZ2H.d.ts} +67 -26
  20. package/dist-plugin/index.cjs +1254 -140
  21. package/dist-plugin/index.js +106 -82
  22. package/dist-plugin/migrateData/index.cjs +956 -14
  23. package/dist-plugin/migrateData/index.js +10 -3
  24. package/dist-plugin/tokensCssMigrations/index.cjs +71 -5
  25. package/dist-plugin/tokensCssMigrations/index.d.cts +20 -9
  26. package/dist-plugin/tokensCssMigrations/index.d.ts +20 -9
  27. package/dist-plugin/tokensCssMigrations/index.js +4 -1
  28. package/package.json +8 -4
  29. package/src/editor/component-editor/NotificationEditor.svelte +6 -0
  30. package/src/editor/component-editor/scaffolding/ComponentFileManager.svelte +9 -23
  31. package/src/editor/component-editor/scaffolding/LinkedBlock.svelte +8 -8
  32. package/src/editor/core/components/adjustAliases.ts +49 -22
  33. package/src/editor/core/components/aliasKinds.ts +7 -2
  34. package/src/editor/core/components/componentConfigService.ts +1 -1
  35. package/src/editor/core/palettes/oklch.ts +3 -137
  36. package/src/editor/core/palettes/paletteDerivation.ts +79 -37
  37. package/src/editor/core/store/editorStore.ts +7 -197
  38. package/src/editor/core/store/editorTypes.ts +9 -30
  39. package/src/editor/core/themes/generateColorsAndType.ts +18 -8
  40. package/src/editor/core/themes/lookSummary.ts +3 -25
  41. package/src/editor/core/themes/migrateComponentConfig.ts +205 -0
  42. package/src/editor/core/themes/migrations/2026-05-19-sectiondivider-rich-gradient.ts +1 -1
  43. package/src/editor/core/themes/migrations/2026-05-20-sectiondivider-slim-variants.ts +1 -1
  44. package/src/editor/core/themes/migrations/2026-05-22-sectiondivider-intrinsics-to-css.ts +1 -1
  45. package/src/editor/core/themes/parsers/gradient.ts +211 -0
  46. package/src/editor/core/themes/slices/gradients.ts +5 -58
  47. package/src/editor/core/themes/themeService.ts +22 -9
  48. package/src/editor/core/themes/themeTypes.ts +89 -27
  49. package/src/editor/docs/content/editing-tokens.md +6 -2
  50. package/src/editor/docs/content/themes-workflow.md +9 -11
  51. package/src/editor/docs/content/where-themes-live.md +2 -2
  52. package/src/editor/docs/content.generated.ts +3 -3
  53. package/src/editor/index.ts +1 -0
  54. package/src/editor/styles/ui-editor.css +5 -2
  55. package/src/editor/ui/BezierCurveEditor.svelte +90 -67
  56. package/src/editor/ui/PaletteEditor.svelte +180 -59
  57. package/src/editor/ui/ThemePanel.svelte +24 -15
  58. package/src/editor/ui/Toggle.svelte +36 -24
  59. package/src/editor/ui/UIPillButton.svelte +1 -1
  60. package/src/editor/ui/UIReveal.svelte +47 -0
  61. package/src/editor/ui/UISquareButton.svelte +1 -1
  62. package/src/editor/ui/colors/ColorStory.svelte +12 -8
  63. package/src/editor/ui/colors/ColorsTab.svelte +4 -5
  64. package/src/editor/ui/curveEngine.ts +101 -21
  65. package/src/editor/ui/palette/OverridesPanel.svelte +23 -8
  66. package/src/editor/ui/palette/PaletteBase.svelte +17 -78
  67. package/src/editor/ui/palette/PaletteJumpButton.svelte +3 -3
  68. package/src/editor/ui/palette/ScaleCurveEditor.svelte +110 -20
  69. package/src/editor/ui/palette/curveSummary.ts +20 -0
  70. package/src/editor/ui/palette/dockMagnify.ts +5 -13
  71. package/src/editor/ui/palette/paletteMath.ts +7 -4
  72. package/src/live-tokens/data/colors-and-type/autumn.json +528 -528
  73. package/src/live-tokens/data/colors-and-type/default.json +247 -247
  74. package/src/live-tokens/data/colors-and-type/halloween.json +532 -532
  75. package/src/live-tokens/data/colors-and-type/midnight-study.json +538 -538
  76. package/src/live-tokens/data/colors-and-type/ocean.json +521 -521
  77. package/src/live-tokens/data/colors-and-type/royal-velvet.json +532 -532
  78. package/src/live-tokens/data/colors-and-type/spring-meadow.json +504 -504
  79. package/src/live-tokens/data/colors-and-type/sunset.json +531 -531
  80. package/src/live-tokens/data/themes/autumn.json +604 -568
  81. package/src/live-tokens/data/themes/halloween.json +584 -583
  82. package/src/live-tokens/data/themes/midnight-study.json +673 -605
  83. package/src/live-tokens/data/themes/ocean.json +597 -561
  84. package/src/live-tokens/data/themes/royal-velvet.json +574 -573
  85. package/src/live-tokens/data/themes/spring-meadow.json +590 -556
  86. package/src/live-tokens/data/themes/sunset.json +632 -571
  87. package/src/live-tokens/data/tokens.generated.css +282 -282
  88. package/src/system/components/FloatingTokenTags.css +0 -1
  89. package/src/system/internal/backgroundContrast.ts +7 -0
  90. package/src/system/internal/oklch.ts +189 -0
  91. package/src/system/styles/tokens.css +283 -283
@@ -1,137 +1,3 @@
1
- // OKLCH color space conversions — hand-rolled, no dependencies
2
- // Pipeline: sRGB hex → linear RGB → OKLab (via M1×M2 + cube root) → OKLCH (polar)
3
-
4
- export interface Oklch {
5
- l: number; // 0..1
6
- c: number; // 0..~0.4
7
- h: number; // 0..360
8
- }
9
-
10
- // --- sRGB gamma ---
11
-
12
- function srgbToLinear(c: number): number {
13
- return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
14
- }
15
-
16
- function linearToSrgb(c: number): number {
17
- return c <= 0.0031308 ? 12.92 * c : 1.055 * Math.pow(c, 1 / 2.4) - 0.055;
18
- }
19
-
20
- // --- Hex ↔ linear RGB ---
21
-
22
- function hexToLinearRgb(hex: string): [number, number, number] {
23
- const r = parseInt(hex.slice(1, 3), 16) / 255;
24
- const g = parseInt(hex.slice(3, 5), 16) / 255;
25
- const b = parseInt(hex.slice(5, 7), 16) / 255;
26
- return [srgbToLinear(r), srgbToLinear(g), srgbToLinear(b)];
27
- }
28
-
29
- function linearRgbToHex(r: number, g: number, b: number): string {
30
- const toHex = (c: number) => {
31
- const v = Math.round(Math.max(0, Math.min(1, linearToSrgb(c))) * 255);
32
- return v.toString(16).padStart(2, '0');
33
- };
34
- return `#${toHex(r)}${toHex(g)}${toHex(b)}`;
35
- }
36
-
37
- // --- Linear RGB ↔ OKLab ---
38
- // Uses the two-matrix approach from Björn Ottosson's blog
39
-
40
- function linearRgbToOklab(r: number, g: number, b: number): [number, number, number] {
41
- const l_ = 0.4122214708 * r + 0.5363325363 * g + 0.0514459929 * b;
42
- const m_ = 0.2119034982 * r + 0.6806995451 * g + 0.1073969566 * b;
43
- const s_ = 0.0883024619 * r + 0.2817188376 * g + 0.6299787005 * b;
44
-
45
- const l = Math.cbrt(l_);
46
- const m = Math.cbrt(m_);
47
- const s = Math.cbrt(s_);
48
-
49
- return [
50
- 0.2104542553 * l + 0.7936177850 * m - 0.0040720468 * s,
51
- 1.9779984951 * l - 2.4285922050 * m + 0.4505937099 * s,
52
- 0.0259040371 * l + 0.7827717662 * m - 0.8086757660 * s,
53
- ];
54
- }
55
-
56
- function oklabToLinearRgb(L: number, a: number, b: number): [number, number, number] {
57
- const l_ = L + 0.3963377774 * a + 0.2158037573 * b;
58
- const m_ = L - 0.1055613458 * a - 0.0638541728 * b;
59
- const s_ = L - 0.0894841775 * a - 1.2914855480 * b;
60
-
61
- const l = l_ * l_ * l_;
62
- const m = m_ * m_ * m_;
63
- const s = s_ * s_ * s_;
64
-
65
- return [
66
- +4.0767416621 * l - 3.3077115913 * m + 0.2309699292 * s,
67
- -1.2684380046 * l + 2.6097574011 * m - 0.3413193965 * s,
68
- -0.0041960863 * l - 0.7034186147 * m + 1.7076147010 * s,
69
- ];
70
- }
71
-
72
- // --- OKLab ↔ OKLCH ---
73
-
74
- function oklabToOklch(L: number, a: number, b: number): Oklch {
75
- const c = Math.sqrt(a * a + b * b);
76
- let h = (Math.atan2(b, a) * 180) / Math.PI;
77
- if (h < 0) h += 360;
78
- return { l: L, c, h };
79
- }
80
-
81
- function oklchToOklab(l: number, c: number, h: number): [number, number, number] {
82
- const hRad = (h * Math.PI) / 180;
83
- return [l, c * Math.cos(hRad), c * Math.sin(hRad)];
84
- }
85
-
86
- // --- Public API ---
87
-
88
- export function hexToOklch(hex: string): Oklch {
89
- const [r, g, b] = hexToLinearRgb(hex);
90
- const [L, a, bVal] = linearRgbToOklab(r, g, b);
91
- return oklabToOklch(L, a, bVal);
92
- }
93
-
94
- export function oklchToHex(l: number, c: number, h: number): string {
95
- const [L, a, b] = oklchToOklab(l, c, h);
96
- const [r, g, bVal] = oklabToLinearRgb(L, a, b);
97
- return linearRgbToHex(r, g, bVal);
98
- }
99
-
100
- function isInGamut(r: number, g: number, b: number): boolean {
101
- const eps = 0.0001;
102
- return r >= -eps && r <= 1 + eps && g >= -eps && g <= 1 + eps && b >= -eps && b <= 1 + eps;
103
- }
104
-
105
- /** The clamped sRGB projection: reduce chroma into gamut, then serialize to hex.
106
- * This is the correct projection for unclamped stored intent — naive
107
- * `oklchToHex` per-channel clips (shifting hue), this preserves hue and L. */
108
- export function oklchToHexClamped(l: number, c: number, h: number): string {
109
- const g = gamutClamp(l, c, h);
110
- return oklchToHex(g.l, g.c, g.h);
111
- }
112
-
113
- export function gamutClamp(l: number, c: number, h: number): Oklch {
114
- // Clamp lightness
115
- if (l <= 0) return { l: 0, c: 0, h };
116
- if (l >= 1) return { l: 1, c: 0, h };
117
-
118
- // Binary search: reduce chroma until in sRGB gamut
119
- const [L, a, b] = oklchToOklab(l, c, h);
120
- const [r, g, bVal] = oklabToLinearRgb(L, a, b);
121
-
122
- if (isInGamut(r, g, bVal)) return { l, c, h };
123
-
124
- let lo = 0;
125
- let hi = c;
126
- for (let i = 0; i < 20; i++) {
127
- const mid = (lo + hi) / 2;
128
- const [La, aa, ba] = oklchToOklab(l, mid, h);
129
- const [rr, gg, bb] = oklabToLinearRgb(La, aa, ba);
130
- if (isInGamut(rr, gg, bb)) {
131
- lo = mid;
132
- } else {
133
- hi = mid;
134
- }
135
- }
136
- return { l, c: lo, h };
137
- }
1
+ // The math lives in the system layer because `src/system` ships to consumers
2
+ // and cannot import from `src/editor`; `backgroundContrast.ts` needs it too.
3
+ export * from '../../../system/internal/oklch';
@@ -13,8 +13,8 @@
13
13
  * derives the same CSS vars from the persisted config.
14
14
  */
15
15
 
16
- import { hexToOklch, oklchToHexClamped, gamutClamp, type Oklch } from './oklch';
17
- import { type CurveAnchor, sampleCurve, makeAnchor, tangentAnchor } from '../../ui/curveEngine';
16
+ import { cssColorToOklch, oklchToCssClamped, gamutClamp, type Oklch } from './oklch';
17
+ import { type CurveAnchor, sampleCurve, makeAnchor, tangentAnchor, resmoothAutoCurve } from '../../ui/curveEngine';
18
18
  import type { PaletteConfig } from '../themes/themeTypes';
19
19
 
20
20
  export interface PaletteSpec {
@@ -108,8 +108,18 @@ export const SCALES: readonly Scale[] = [
108
108
  export const BW_GUARD_MIN_L = 0.17;
109
109
  export const BW_GUARD_MAX_L = 0.93;
110
110
 
111
- export const DEFAULT_PALETTE_LIGHTNESS = (): CurveAnchor[] => [makeAnchor(0, 95, 5), makeAnchor(100, 8, 5)];
112
- export const DEFAULT_PALETTE_SATURATION = (): CurveAnchor[] => [makeAnchor(0, 100, 30), makeAnchor(100, 100, 30)];
111
+ /** Defaults ship auto-shaped, so a curve nobody has touched is the one the editor
112
+ * draws with Auto smooth on. Handle lengths come from the points, which is why
113
+ * `makeAnchor`'s tangent argument is left off throughout. */
114
+ const autoCurve = (...anchors: CurveAnchor[]): CurveAnchor[] => resmoothAutoCurve(anchors);
115
+
116
+ export const DEFAULT_PALETTE_LIGHTNESS = (): CurveAnchor[] => autoCurve(makeAnchor(0, 95), makeAnchor(100, 8));
117
+ export const DEFAULT_PALETTE_SATURATION = (): CurveAnchor[] => autoCurve(makeAnchor(0, 100), makeAnchor(100, 100));
118
+ export const DEFAULT_PALETTE_HUE = (): CurveAnchor[] => autoCurve(makeAnchor(0, 0), makeAnchor(100, 0));
119
+
120
+ /** Hue is cyclic, so derivation wraps rather than clamps. The trig accepts
121
+ * any angle; `Oklch.h` is documented 0..360 and readouts print it. */
122
+ const wrapHue = (h: number): number => ((h % 360) + 360) % 360;
113
123
 
114
124
  export type SchemeDirection = 'light' | 'dark';
115
125
 
@@ -133,31 +143,31 @@ export function scaleCurveDefaults(scheme: SchemeDirection = 'dark'): ScaleCurve
133
143
  if (scheme === 'light') {
134
144
  return {
135
145
  Surfaces: {
136
- lightness: () => [makeAnchor(0, 98, 5), makeAnchor(100, 82, 5)],
137
- saturation: () => [makeAnchor(0, 100, 30), makeAnchor(100, 100, 30)],
146
+ lightness: () => autoCurve(makeAnchor(0, 98), makeAnchor(100, 82)),
147
+ saturation: () => autoCurve(makeAnchor(0, 100), makeAnchor(100, 100)),
138
148
  },
139
149
  Borders: {
140
- lightness: () => [makeAnchor(0, 92, 5), makeAnchor(100, 45, 5)],
141
- saturation: () => [makeAnchor(0, 100, 30), makeAnchor(100, 100, 30)],
150
+ lightness: () => autoCurve(makeAnchor(0, 92), makeAnchor(100, 45)),
151
+ saturation: () => autoCurve(makeAnchor(0, 100), makeAnchor(100, 100)),
142
152
  },
143
153
  Text: {
144
- lightness: () => [makeAnchor(0, 30, 30), makeAnchor(100, 120, 30)],
145
- saturation: () => [makeAnchor(0, 100, 30), makeAnchor(100, 15, 30)],
154
+ lightness: () => autoCurve(makeAnchor(0, 30), makeAnchor(100, 120)),
155
+ saturation: () => autoCurve(makeAnchor(0, 100), makeAnchor(100, 15)),
146
156
  },
147
157
  };
148
158
  }
149
159
  return {
150
160
  Surfaces: {
151
- lightness: () => [makeAnchor(0, 15, 5), makeAnchor(100, 47, 5)],
152
- saturation: () => [makeAnchor(0, 100, 30), makeAnchor(100, 100, 30)],
161
+ lightness: () => autoCurve(makeAnchor(0, 15), makeAnchor(100, 47)),
162
+ saturation: () => autoCurve(makeAnchor(0, 100), makeAnchor(100, 100)),
153
163
  },
154
164
  Borders: {
155
- lightness: () => [makeAnchor(0, 25, 5), makeAnchor(100, 80, 5)],
156
- saturation: () => [makeAnchor(0, 100, 30), makeAnchor(100, 100, 30)],
165
+ lightness: () => autoCurve(makeAnchor(0, 25), makeAnchor(100, 80)),
166
+ saturation: () => autoCurve(makeAnchor(0, 100), makeAnchor(100, 100)),
157
167
  },
158
168
  Text: {
159
- lightness: () => [makeAnchor(0, 120, 30), makeAnchor(100, 55, 30)],
160
- saturation: () => [makeAnchor(0, 100, 30), makeAnchor(100, 15, 30)],
169
+ lightness: () => autoCurve(makeAnchor(0, 120), makeAnchor(100, 55)),
170
+ saturation: () => autoCurve(makeAnchor(0, 100), makeAnchor(100, 15)),
161
171
  },
162
172
  };
163
173
  }
@@ -191,10 +201,10 @@ export type DerivedValue =
191
201
 
192
202
  /** The single color → CSS-string projection, and the single clamp point for
193
203
  * derived color: unclamped stored intent is chroma-reduced into sRGB gamut,
194
- * then serialized (hex this wave; `oklch()` after Part B). Idempotent on the
195
- * already-in-gamut per-step derivation output. `raw` passes through untouched. */
204
+ * then serialized as `oklch()`. Idempotent on the already-in-gamut per-step
205
+ * derivation output. `raw` passes through untouched. */
196
206
  export function serializeDerivedValue(value: DerivedValue): string {
197
- return value.kind === 'raw' ? value.css : oklchToHexClamped(value.l, value.c, value.h);
207
+ return value.kind === 'raw' ? value.css : oklchToCssClamped(value.l, value.c, value.h);
198
208
  }
199
209
 
200
210
  export function computePaletteOklch(
@@ -203,12 +213,18 @@ export function computePaletteOklch(
203
213
  lightnessCurve: CurveAnchor[],
204
214
  saturationCurve: CurveAnchor[],
205
215
  curveOffset: Record<string, number>,
216
+ hueCurve?: CurveAnchor[],
206
217
  ): Oklch {
207
- const { c: baseC, h } = base;
218
+ const { c: baseC } = base;
208
219
  const xPos = stepIndexToX(index);
209
220
  const targetL = Math.max(0, Math.min(100, sampleCurve(lightnessCurve, xPos) + (curveOffset.lightness ?? 0))) / 100;
210
221
  const satMul = Math.max(0, Math.min(2, (sampleCurve(saturationCurve, xPos) + (curveOffset.saturation ?? 0)) / 100));
211
222
  const targetC = baseC * satMul;
223
+ // Zero delta skips the wrap rather than folding through it: `%360` round-trips
224
+ // introduce a one-ULP drift for most floats already in range, which would break
225
+ // byte-identity (Global invariant 1) for every palette with no hue curve.
226
+ const hueDelta = (hueCurve ? sampleCurve(hueCurve, xPos) : 0) + (curveOffset.hue ?? 0);
227
+ const h = hueDelta === 0 ? base.h : wrapHue(base.h + hueDelta);
212
228
  return gamutClamp(targetL, targetC, h);
213
229
  }
214
230
 
@@ -348,9 +364,10 @@ export function liftCurveAnchor(
348
364
  }
349
365
 
350
366
  /**
351
- * Place the base color in the ramp: pin a lightness anchor (base L) and a
352
- * saturation anchor (multiplier 100) at the step whose lightness is nearest
353
- * the base L, so the picked color renders verbatim at that step. Placement
367
+ * Place the base color in the ramp: pin a lightness anchor (base L), a
368
+ * saturation anchor (multiplier 100), and, when a hue curve exists, a hue
369
+ * anchor (delta 0), all at the step whose lightness is nearest the base L,
370
+ * so the picked color renders verbatim at that step. Placement
354
371
  * is measured against the curve with the previous placement lifted out —
355
372
  * the pinned anchor equals base L at its own x by construction, so measuring
356
373
  * against it would be circular and the anchor could never move. Runs inside
@@ -362,6 +379,9 @@ export function syncBaseAnchor(cfg: PaletteConfig): void {
362
379
  const fresh = prev === undefined;
363
380
  let lCurve = cfg.lightnessCurve;
364
381
  let sCurve = cfg.saturationCurve;
382
+ // Hue is pinned only once a curve exists (RJC 5): an absent curve is flat
383
+ // zero already, so there is nothing to lift or re-place.
384
+ let hCurve = cfg.hueCurve;
365
385
  if (prev) {
366
386
  // Re-placing an existing anchor (a hue move, typically) must preserve
367
387
  // whatever is currently on the curve, edited or not — never restore the
@@ -370,6 +390,7 @@ export function syncBaseAnchor(cfg: PaletteConfig): void {
370
390
  const prevX = stepIndexToX(prev.step);
371
391
  lCurve = liftCurveAnchor(lCurve, prevX, prev.displacedL);
372
392
  sCurve = liftCurveAnchor(sCurve, prevX, prev.displacedS);
393
+ if (hCurve) hCurve = liftCurveAnchor(hCurve, prevX, prev.displacedH);
373
394
  }
374
395
  const step = nearestPaletteStep(lCurve, cfg.baseColor.l);
375
396
  const x = stepIndexToX(step);
@@ -377,6 +398,8 @@ export function syncBaseAnchor(cfg: PaletteConfig): void {
377
398
  const s = setCurveAnchor(sCurve, x, 100, fresh);
378
399
  cfg.lightnessCurve = l.curve;
379
400
  cfg.saturationCurve = s.curve;
401
+ const h = hCurve ? setCurveAnchor(hCurve, x, 0, fresh) : undefined;
402
+ if (h) cfg.hueCurve = h.curve;
380
403
  // priorEndpoints is captured once, on the curve's first-ever placement,
381
404
  // and carried forward untouched thereafter so a later clear can still
382
405
  // restore the true original regardless of how many times the anchor moved.
@@ -384,18 +407,27 @@ export function syncBaseAnchor(cfg: PaletteConfig): void {
384
407
  step,
385
408
  displacedL: l.displacedY,
386
409
  displacedS: s.displacedY,
410
+ displacedH: h?.displacedY,
387
411
  priorLightnessEndpoints: prev?.priorLightnessEndpoints ?? l.priorEndpoints,
388
412
  priorSaturationEndpoints: prev?.priorSaturationEndpoints ?? s.priorEndpoints,
413
+ // No else-branch reading `prev?.priorHueEndpoints`: that could only fire
414
+ // if a placed hue curve later went absent, which RJC 7 (reset never
415
+ // deletes) never lets happen.
416
+ priorHueEndpoints: h ? (prev?.priorHueEndpoints ?? h.priorEndpoints) : undefined,
389
417
  };
390
418
  }
391
419
 
392
- /** Toggle-off: lift the placement out of both curves and drop the flag. */
420
+ /** Toggle-off: lift the placement out of every curve it touched (hue only
421
+ * if present) and drop the flag. */
393
422
  export function clearBaseAnchor(cfg: PaletteConfig): void {
394
423
  const prev = cfg.anchorPlacement;
395
424
  if (prev) {
396
425
  const x = stepIndexToX(prev.step);
397
426
  cfg.lightnessCurve = liftCurveAnchor(cfg.lightnessCurve, x, prev.displacedL, prev.priorLightnessEndpoints);
398
427
  cfg.saturationCurve = liftCurveAnchor(cfg.saturationCurve, x, prev.displacedS, prev.priorSaturationEndpoints);
428
+ if (cfg.hueCurve) {
429
+ cfg.hueCurve = liftCurveAnchor(cfg.hueCurve, x, prev.displacedH, prev.priorHueEndpoints);
430
+ }
399
431
  cfg.anchorPlacement = undefined;
400
432
  }
401
433
  cfg.anchorToBase = false;
@@ -405,7 +437,7 @@ export function computeDerivedOklch(
405
437
  step: Step,
406
438
  base: Oklch,
407
439
  scaleTitle: string,
408
- scaleCurves: Record<string, { lightness: CurveAnchor[]; saturation: CurveAnchor[] }>,
440
+ scaleCurves: Record<string, { lightness: CurveAnchor[]; saturation: CurveAnchor[]; hue?: CurveAnchor[] }>,
409
441
  curveOffset: Record<string, number>,
410
442
  ): Oklch {
411
443
  const scale = SCALES.find((s) => s.title === scaleTitle)!;
@@ -413,8 +445,10 @@ export function computeDerivedOklch(
413
445
  const defs = defaultScaleCurves[scaleTitle];
414
446
  const lCurve = scaleCurves[scaleTitle]?.lightness ?? defs.lightness();
415
447
  const sCurve = scaleCurves[scaleTitle]?.saturation ?? defs.saturation();
448
+ const hCurve = scaleCurves[scaleTitle]?.hue;
416
449
  const lOff = curveOffset[`${scaleTitle}-lightness`] ?? 0;
417
450
  const sOff = curveOffset[`${scaleTitle}-saturation`] ?? 0;
451
+ const hOff = curveOffset[`${scaleTitle}-hue`] ?? 0;
418
452
  const { l: baseL, c: baseC, h: baseH } = base;
419
453
  let targetL: number;
420
454
  if (scale.isText) {
@@ -425,7 +459,10 @@ export function computeDerivedOklch(
425
459
  }
426
460
  const satMul = Math.max(0, Math.min(2, (sampleCurve(sCurve, xPos) + sOff) / 100));
427
461
  const targetC = baseC * satMul;
428
- return gamutClamp(targetL, targetC, baseH);
462
+ // See computePaletteOklch: zero delta skips the wrap to preserve byte-identity.
463
+ const hueDelta = (hCurve ? sampleCurve(hCurve, xPos) : 0) + hOff;
464
+ const targetH = hueDelta === 0 ? baseH : wrapHue(baseH + hueDelta);
465
+ return gamutClamp(targetL, targetC, targetH);
429
466
  }
430
467
 
431
468
  export function scaleToCssVar(scaleTitle: string, stepName: string, cssNamespace: string | null): string | null {
@@ -461,7 +498,7 @@ export function derivePaletteValues(spec: PaletteSpec, config: PaletteConfig | u
461
498
  // Overrides are numeric OKLCH intent; pass straight through as color-kind.
462
499
  const value: DerivedValue = (k in overrides)
463
500
  ? { kind: 'color', ...overrides[k] }
464
- : { kind: 'color', ...computePaletteOklch(index, baseColor, lightnessCurve, saturationCurve, curveOffset) };
501
+ : { kind: 'color', ...computePaletteOklch(index, baseColor, lightnessCurve, saturationCurve, curveOffset, config.hueCurve) };
465
502
  out[`--color-${spec.cssNamespace}-${ps.label}`] = value;
466
503
  });
467
504
 
@@ -512,8 +549,8 @@ export function derivePaletteValues(spec: PaletteSpec, config: PaletteConfig | u
512
549
  const stopsCss = sortedStops
513
550
  .map((s) => {
514
551
  const stopValue = out[`--color-${spec.cssNamespace}-${s.paletteLabel}`];
515
- const hex = stopValue ? serializeDerivedValue(stopValue) : '#000000';
516
- return `${hex} ${s.position}%`;
552
+ const css = stopValue ? serializeDerivedValue(stopValue) : 'oklch(0 0 0)';
553
+ return `${css} ${s.position}%`;
517
554
  })
518
555
  .join(', ');
519
556
  let gradient: string;
@@ -554,8 +591,6 @@ export function palettesToVars(palettes: Record<string, PaletteConfig>): Record<
554
591
  return out;
555
592
  }
556
593
 
557
- const HEX_RE = /^#[0-9a-f]{6}$/i;
558
-
559
594
  /**
560
595
  * Reconcile palette typed state against the catch-all `cssVariables` bag and
561
596
  * report the set of variable names the typed slice now owns. Two operations:
@@ -596,11 +631,13 @@ export function reconcilePalettesFromCssVars(
596
631
  if (current === undefined) continue;
597
632
 
598
633
  if (current._imported === true) {
599
- const anchorHex = cssVars[`--color-${spec.cssNamespace}-500`];
600
- if (anchorHex && HEX_RE.test(anchorHex.trim())) {
601
- // Input boundary: the imported cssVariables anchor is a hex string;
602
- // parse it to the OKLCH basis exactly once, here.
603
- next[spec.label] = { ...current, baseColor: hexToOklch(anchorHex.trim()), _imported: false };
634
+ const anchor = cssVars[`--color-${spec.cssNamespace}-500`];
635
+ const anchorOklch = anchor ? cssColorToOklch(anchor) : null;
636
+ if (anchorOklch) {
637
+ // Input boundary: the imported cssVariables anchor is a CSS string in
638
+ // either serialization this system has shipped (hex before the OKLCH
639
+ // flip, `oklch()` after); parse it to the basis exactly once, here.
640
+ next[spec.label] = { ...current, baseColor: anchorOklch, _imported: false };
604
641
  syncBaseAnchor(next[spec.label]);
605
642
  snapped.add(spec.label);
606
643
  } else {
@@ -611,7 +648,12 @@ export function reconcilePalettesFromCssVars(
611
648
  }
612
649
  }
613
650
 
614
- for (const k of Object.keys(derivePaletteVars(spec, next[spec.label]))) {
651
+ // Key names only: which variables this palette owns is a structural
652
+ // question, so read the IR rather than serializing values that are
653
+ // discarded. Callers reach here with pre-migration configs whose
654
+ // `baseColor` is still a hex string; serializing those derived NaNs used
655
+ // to produce junk strings nobody read.
656
+ for (const k of Object.keys(derivePaletteValues(spec, next[spec.label]))) {
615
657
  consumed.add(k);
616
658
  }
617
659
  }
@@ -28,6 +28,7 @@ import {
28
28
  CURRENT_COMPONENT_SCHEMA_VERSION,
29
29
  runMigrations,
30
30
  } from '../themes/migrations';
31
+ import { migrateComponentConfig } from '../themes/migrateComponentConfig';
31
32
  import { renamePrimaryPaletteKey } from '../themes/migrations/2026-05-13-primary-to-brand';
32
33
  import {
33
34
  renameBackgroundPaletteKey,
@@ -220,202 +221,11 @@ export {
220
221
  // `fromVersion >= file.schemaVersion`, then `splitAliasesAndConfig` routes
221
222
  // literal-valued knobs (per `KNOWN_COMPONENT_CONFIG_KEYS`) into the config
222
223
  // bucket and wraps the remainder as `CssVarRef` discriminated unions.
223
-
224
- /**
225
- * Migrate a disk-shape alias bag through the registered migration runner.
226
- *
227
- * The runner is string-typed by contract (most migrations only do
228
- * key/value string rewrites). Object-valued entries — currently just
229
- * inline `{ kind: 'gradient', value: ... }` payloads — pass through
230
- * untouched: we split the bag into string/object subsets, run the runner
231
- * on the string subset, then merge.
232
- *
233
- * For sectiondivider's legacy 7-flat-token gradients we run a synthesis
234
- * step BEFORE the migration runner that would strip those tokens. The
235
- * synthesized gradient object lands in the object subset and survives
236
- * the stripping pass.
237
- */
238
- function migrateComponentAliases(
239
- component: string,
240
- aliasesIn: Record<string, AliasDiskValue>,
241
- rawConfig: Record<string, unknown> | undefined,
242
- fileVersion: number,
243
- ): { aliases: Record<string, AliasDiskValue>; config: Record<string, unknown> } {
244
- const stringPart: Record<string, string> = {};
245
- const objectPart: Record<string, AliasDiskValue> = {};
246
- for (const [k, v] of Object.entries(aliasesIn)) {
247
- if (typeof v === 'string') stringPart[k] = v;
248
- else objectPart[k] = v;
249
- }
250
- // String-valued config entries join the migration input so a migration can
251
- // rename or relocate them. After migration the bag flows through
252
- // `splitAliasesAndConfig` which routes KNOWN keys back to config and the
253
- // rest to aliases — this is what lets a migration *move* a key from the
254
- // config bucket to the aliases bucket by also removing it from KNOWN.
255
- // Config wins over an aliases-bucket collision on the same key: the legacy
256
- // single-bucket → split-bucket move preserved "explicit config field beats
257
- // legacy alias-bucketed value", and that contract still holds.
258
- // Non-string config values (rare; reserved for future structured payloads)
259
- // pass through untouched.
260
- const configOut: Record<string, unknown> = {};
261
- for (const [k, v] of Object.entries(rawConfig ?? {})) {
262
- if (typeof v === 'string') stringPart[k] = v;
263
- else configOut[k] = v;
264
- }
265
- if (component === 'sectiondivider') {
266
- synthesizeSectionDividerGradients(stringPart, objectPart);
267
- }
268
- const migratedString = runMigrations('component-config', fileVersion, stringPart, { component });
269
- const migratedObject = component === 'sectiondivider'
270
- ? renameSectionDividerObjectSlots(objectPart)
271
- : objectPart;
272
- return {
273
- aliases: { ...migratedString, ...migratedObject },
274
- config: configOut,
275
- };
276
- }
277
-
278
- /**
279
- * v8→v9 companion to `componentMigration_2026_05_20_sectiondividerSlimVariants`:
280
- * the structured background payload is reshaped from six color families →
281
- * three size variants (lg/md/sm). Canvas's gradient is taken as the canonical
282
- * seed and fanned across all three new variants; other families' gradients
283
- * are dropped. Idempotent — keys already at the final shape pass through.
284
- *
285
- * Sources accepted (in priority order, first-found wins for canvas's slot):
286
- * --sectiondivider-canvas-background (current/in-progress shape)
287
- * --sectiondivider-canvas-gradient (v7-era shape)
288
- * --sectiondivider-color-canvas-background (intermediate shape that landed
289
- * briefly during the color-prop pivot)
290
- *
291
- * The migration runner only sees string keys, so this object-shape rename
292
- * has to live alongside the runner call.
293
- */
294
- const FAMILIES = ['canvas', 'neutral', 'alternate', 'primary', 'accent', 'special'] as const;
295
- const SIZE_VARIANTS = ['lg', 'md', 'sm'] as const;
296
- function renameSectionDividerObjectSlots(
297
- objectPart: Record<string, AliasDiskValue>,
298
- ): Record<string, AliasDiskValue> {
299
- // Find canvas's gradient under any historical key shape.
300
- const canvasSourceKeys = [
301
- '--sectiondivider-canvas-background',
302
- '--sectiondivider-canvas-gradient',
303
- '--sectiondivider-color-canvas-background',
304
- ];
305
- let canvasGradient: AliasDiskValue | undefined;
306
- for (const key of canvasSourceKeys) {
307
- if (objectPart[key] !== undefined) {
308
- canvasGradient = objectPart[key];
309
- break;
310
- }
311
- }
312
-
313
- // If we found a canvas gradient (any era), fan it into the three new
314
- // per-variant background slots and drop every other family's gradient.
315
- // If no canvas gradient is found, pass through non-family keys verbatim
316
- // so non-migration data (theme tokens, other components) isn't touched.
317
- const out: Record<string, AliasDiskValue> = {};
318
- for (const [key, value] of Object.entries(objectPart)) {
319
- // Skip any sectiondivider per-family gradient; the canvas fan-out emits
320
- // the canonical set below.
321
- let isFamilyGradient = false;
322
- for (const f of FAMILIES) {
323
- if (
324
- key === `--sectiondivider-${f}-background`
325
- || key === `--sectiondivider-${f}-gradient`
326
- || key === `--sectiondivider-color-${f}-background`
327
- ) {
328
- isFamilyGradient = true;
329
- break;
330
- }
331
- }
332
- // Pass through anything that's already at the final lg/md/sm shape so
333
- // re-running the migration is idempotent.
334
- if (isFamilyGradient) continue;
335
- out[key] = value;
336
- }
337
- if (canvasGradient !== undefined) {
338
- for (const v of SIZE_VARIANTS) {
339
- out[`--sectiondivider-${v}-background`] = canvasGradient;
340
- }
341
- }
342
- return out;
343
- }
344
-
345
- /** SectionDivider variants that ship gradient aliases. */
346
- const SECTIONDIVIDER_VARIANTS = ['canvas', 'neutral', 'alternate', 'primary', 'accent', 'special'] as const;
347
-
348
- /**
349
- * Collapse legacy 7-flat-token gradients (`{angle, stop-{1,2,3}-{color,position}}`)
350
- * for each sectiondivider variant into one structured gradient alias at
351
- * `--sectiondivider-{v}-gradient`. The flat tokens stay in `stringPart`
352
- * for the migration runner to strip; the structured payload joins
353
- * `objectPart` so the merge produces the new shape.
354
- *
355
- * Skips variants that already declare a gradient alias (object form) so
356
- * round-tripping doesn't overwrite user edits.
357
- */
358
- function synthesizeSectionDividerGradients(
359
- stringPart: Record<string, string>,
360
- objectPart: Record<string, AliasDiskValue>,
361
- ): void {
362
- const GRADIENT_ANGLE_TOKENS: Record<string, number> = {
363
- '--gradient-angle-horizontal': 90,
364
- '--gradient-angle-vertical': 180,
365
- '--gradient-angle-diagonal': 135,
366
- '--gradient-angle-counter-diagonal': 45,
367
- };
368
- const GRADIENT_STOP_POSITIONS: Record<string, number> = {
369
- '--gradient-stop-start': 0,
370
- '--gradient-stop-mid': 50,
371
- '--gradient-stop-end': 100,
372
- };
373
- const parseAngle = (raw: string | undefined): number => {
374
- if (!raw) return 135;
375
- if (raw.startsWith('--')) return GRADIENT_ANGLE_TOKENS[raw] ?? 135;
376
- const m = raw.match(/^(-?\d+(?:\.\d+)?)\s*deg$/i);
377
- return m ? parseFloat(m[1]) : 135;
378
- };
379
- const parsePosition = (raw: string | undefined, fallback: number): number => {
380
- if (!raw) return fallback;
381
- if (raw.startsWith('--')) return GRADIENT_STOP_POSITIONS[raw] ?? fallback;
382
- const m = raw.match(/^(-?\d+(?:\.\d+)?)\s*%$/);
383
- return m ? parseFloat(m[1]) : fallback;
384
- };
385
- const parseStopColor = (raw: string | undefined): { color: string; opacity?: number } | null => {
386
- if (!raw) return null;
387
- if (raw.startsWith('--')) return { color: raw };
388
- const mix = raw.match(/^color-mix\(\s*in\s+srgb\s*,\s*var\((--[a-z0-9-]+)\)\s+([\d.]+)%\s*,\s*transparent\s*\)$/i);
389
- if (mix) return { color: mix[1], opacity: parseFloat(mix[2]) };
390
- return null;
391
- };
392
- for (const v of SECTIONDIVIDER_VARIANTS) {
393
- const slot = `--sectiondivider-${v}-gradient`;
394
- if (slot in objectPart) continue;
395
- const angleKey = `${slot}-angle`;
396
- const has1 = stringPart[`${slot}-stop-1-color`] !== undefined;
397
- const has2 = stringPart[`${slot}-stop-2-color`] !== undefined;
398
- const has3 = stringPart[`${slot}-stop-3-color`] !== undefined;
399
- if (!has1 && !has2 && !has3) continue;
400
- const stops: { position: number; color: string; opacity?: number }[] = [];
401
- const positionDefaults = [0, 50, 100];
402
- for (let i = 1; i <= 3; i++) {
403
- const c = parseStopColor(stringPart[`${slot}-stop-${i}-color`]);
404
- if (!c) continue;
405
- const pos = parsePosition(stringPart[`${slot}-stop-${i}-position`], positionDefaults[i - 1]);
406
- stops.push({ position: pos, color: c.color, ...(c.opacity !== undefined ? { opacity: c.opacity } : {}) });
407
- }
408
- if (stops.length < 2) continue;
409
- objectPart[slot] = {
410
- kind: 'gradient',
411
- value: {
412
- type: 'linear',
413
- angle: parseAngle(stringPart[angleKey]),
414
- stops,
415
- },
416
- };
417
- }
418
- }
224
+ //
225
+ // The migration itself — `migrateComponentConfig`, plus its sectiondivider
226
+ // gradient helpers — lives in `../themes/migrateComponentConfig` so the
227
+ // server (`normalizeTheme`) can run the same pass over an embedded theme
228
+ // config instead of reading it raw.
419
229
 
420
230
  /**
421
231
  * Disk-shape → in-memory split. Routes legacy single-bucket aliases that
@@ -459,7 +269,7 @@ export function toComponentSlice(
459
269
  config?: Record<string, unknown>,
460
270
  schemaVersion: number = 0,
461
271
  ): { aliases: Record<string, CssVarRef>; config: Record<string, unknown> } {
462
- const migrated = migrateComponentAliases(component, aliases, config, schemaVersion);
272
+ const migrated = migrateComponentConfig(component, aliases, config, schemaVersion);
463
273
  return splitAliasesAndConfig(migrated.aliases, migrated.config);
464
274
  }
465
275