Skip to content

Commit 0065715

Browse files
committed
fix(configurator): align Auto dark derivation with CSS formula
The JS deriveDarkFromLight formula (l + 0.27) didn't match the CSS formula in themes.css (clamp(0.65, 0.95 - l * 0.5, 0.88)), and the base color used a completely different formula that wasn't handled at all. In Auto dark mode the configurator was also baking the derived dark value into the exported CSS override, bypassing the runtime CSS formula entirely. - Fix deriveDarkFromLight to mirror the exact CSS formulas (including the base-specific clamp(0.16, 1.18 - l, 0.24) / c * 0.5 variant) - In Auto dark mode, only write the light token to overrides — the CSS formula in themes.css handles source-dark at runtime - Switching Auto → Manual now removes the dark override; switching Manual → Auto populates dark as a pre-filled starting point Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Gjdz9WEvMA8M1ve3Z1Xsoc
1 parent 5449064 commit 0065715

1 file changed

Lines changed: 17 additions & 20 deletions

File tree

configurator/src/components/panels/ColorsPanel.svelte

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -146,30 +146,33 @@
146146
STATUS_PAIRS.push([STATUS_SOURCES[i], STATUS_SOURCES[i + 1]]);
147147
}
148148
149-
function deriveDarkFromLight(lightVal: string): string {
149+
function deriveDarkFromLight(lightVal: string, colorKey: string): string {
150150
const { l, c, h, valid } = parseOklch(lightVal);
151151
if (!valid) return lightVal;
152-
const darkL = Math.min(l + 0.27, 0.95);
153-
const darkC = c * 0.9;
152+
let darkL: number, darkC: number;
153+
if (colorKey === 'base') {
154+
darkL = Math.max(0.16, Math.min(1.18 - l, 0.24));
155+
darkC = c * 0.5;
156+
} else {
157+
darkL = Math.max(0.65, Math.min(0.95 - l * 0.5, 0.88));
158+
darkC = c * 0.9;
159+
}
154160
return stringifyOklch(darkL, darkC, h);
155161
}
156162
157163
function handleLightChange(light: ColorSource, dark: ColorSource | undefined, newVal: string) {
158-
if (dark && autoDarkSet.has(light.colorKey)) {
159-
onBulkChange({ [light.name]: newVal, [dark.name]: deriveDarkFromLight(newVal) });
160-
} else {
161-
onSet(light.name, newVal);
162-
}
164+
onSet(light.name, newVal);
163165
}
164166
165167
function toggleDarkMode(colorKey: string, dark: ColorSource | undefined, lightName: string) {
166168
if (autoDarkSet.has(colorKey)) {
167-
// Switch to manual — just remove from auto set, keep current dark value
169+
// Switch to manual — populate dark with the derived value as a starting point
170+
const lightVal = overrides[lightName] ?? BRAND_SOURCES.find(s => s.name === lightName)?.default ?? "";
171+
if (dark) onSet(dark.name, deriveDarkFromLight(lightVal, colorKey));
168172
autoDarkSet = new Set([...autoDarkSet].filter(k => k !== colorKey));
169173
} else {
170-
// Switch to auto — derive dark from current light and apply
171-
const lightVal = overrides[lightName] ?? BRAND_SOURCES.find(s => s.name === lightName)?.default ?? "";
172-
if (dark) onSet(dark.name, deriveDarkFromLight(lightVal));
174+
// Switch to auto — remove dark override so the CSS formula handles it at runtime
175+
if (dark) onBulkChange({ [dark.name]: null });
173176
autoDarkSet = new Set([...autoDarkSet, colorKey]);
174177
}
175178
}
@@ -221,13 +224,7 @@
221224
value={overrides[light.name] ?? sourceTokenMap()[light.name]?.value ?? light.default}
222225
overridden={light.name in overrides}
223226
onChange={(v) => handleLightChange(light, dark, v)}
224-
onReset={() => {
225-
if (dark && isAutoMode) {
226-
onBulkChange({ [light.name]: null, [dark.name]: null });
227-
} else {
228-
onReset(light.name);
229-
}
230-
}}
227+
onReset={() => onReset(light.name)}
231228
/>
232229
<!-- Palette swatch strip for brand colors -->
233230
{#if BRAND_COLOR_KEYS.includes(light.colorKey)}
@@ -252,7 +249,7 @@
252249
/>
253250
{:else if dark && isAutoMode}
254251
<div class="text-[9px] text-slate-600 pl-1">
255-
Dark: auto-derived ({deriveDarkFromLight(overrides[light.name] ?? light.default)})
252+
Dark: auto-derived ({deriveDarkFromLight(overrides[light.name] ?? light.default, light.colorKey)})
256253
</div>
257254
{/if}
258255
</div>

0 commit comments

Comments
 (0)