4c5237ca4a
## What this does Rounds out the theme work behind the existing `hasThemeSwitcher` flag. **Two new themes.** Black and White sit alongside Dark and Light. They inherit their neighbour's whole token set and only pin their surfaces flat, so sections are separated by grid lines rather than layered fills. **`System` is now configurable at both ends.** You choose which theme the OS light setting lands on (Light or White) and which the dark setting lands on (Dark or Black). **Two accessibility toggles.** - *Stronger colors* — swaps tinted status chips for solid fills, drops decorative icon accents to monochrome, and darkens chart series that didn't clear 3:1 on a white plot. - *Underline links* — underlines body-text links, so an underline always means the preference is on rather than being a hover style. **Contrast slider.** Stores a 0–100 position within the active theme's own range rather than a shared scale, so 35% stays 35% when you switch themes. Each theme maps it in CSS, which keeps `system` working before hydration. **Appearance in the account popover.** A submenu listing the themes with a check against the current one, plus a link through to the full set on your profile. Picking one applies immediately rather than waiting for the write to round-trip. **Profile page.** Each row now saves on its own — no submit button. Name and email show their value inline with an edit button; the email row is read-only when an identity provider owns the address. **A `/storybook/colors` audit page.** Renders every colour-carrying pattern in the app once per theme plus once under Stronger colors, and measures contrast ratios off the live DOM rather than a hard-coded table, so it can't go stale. --- ## Demo https://github.com/user-attachments/assets/d56cd4d8-719f-4ec5-a990-e04cdb98def1 --- ## Compatibility The stored preference shape is unchanged (`version: "1"`), and the four new fields are all optional. The retired `classic` theme falls back to Dark, whose palette at contrast 0 is what Classic shipped. One deliberate change worth knowing: the default contrast moves from 50 to 0, so existing users who never touched the slider will see slightly less contrast than before. That's what makes 0 mean "the base palette". --- ## Testing Switched between every theme from both the account popover and the profile page, in the expanded and collapsed rail, checking `data-theme` follows and survives a reload. Dragged the contrast slider in each theme and confirmed the percentage label tracks the handle and resnaps if a save fails. Checked both accessibility toggles across the `/storybook/colors` page, which is also where the contrast ratios were read from. Confirmed the Appearance entry stays hidden for a non-admin while the flag is off. <!-- conductor-workspace-link --> --- [Open workspace in Conductor](https://app.conductor.build/workspace/fee50611-7623-4422-bada-ed1cba317ed1) --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
52 lines
2.0 KiB
TypeScript
52 lines
2.0 KiB
TypeScript
import { z } from "zod";
|
|
|
|
// Shared with the client, so no server-only imports.
|
|
export const ThemePreference = z.enum(["system", "dark", "light", "black", "white"]);
|
|
export type ThemePreference = z.infer<typeof ThemePreference>;
|
|
|
|
/* Which theme `system` resolves to at each end of the OS setting. */
|
|
export const SystemLightTheme = z.enum(["light", "white"]);
|
|
export type SystemLightTheme = z.infer<typeof SystemLightTheme>;
|
|
export const SystemDarkTheme = z.enum(["dark", "black"]);
|
|
export type SystemDarkTheme = z.infer<typeof SystemDarkTheme>;
|
|
|
|
export function normalizeSystemLightTheme(value: unknown): SystemLightTheme {
|
|
const result = SystemLightTheme.safeParse(value);
|
|
return result.success ? result.data : "light";
|
|
}
|
|
|
|
export function normalizeSystemDarkTheme(value: unknown): SystemDarkTheme {
|
|
const result = SystemDarkTheme.safeParse(value);
|
|
return result.success ? result.data : "dark";
|
|
}
|
|
|
|
/** Missing, unknown and legacy values (including the removed `classic`) fall
|
|
* back to `dark`. */
|
|
export function normalizeThemePreference(value: unknown): ThemePreference {
|
|
const result = ThemePreference.safeParse(value);
|
|
return result.success ? result.data : "dark";
|
|
}
|
|
|
|
/** 0 is the base palette; the slider only ever adds contrast on top. */
|
|
const DEFAULT_THEME_CONTRAST = 0;
|
|
|
|
/** The "Stronger colors" preference. Stored as `iconContrast`, which predates it
|
|
* covering charts and shapes too. */
|
|
export function normalizeIconContrast(value: unknown): boolean {
|
|
return value === true;
|
|
}
|
|
|
|
export function normalizeUnderlineLinks(value: unknown): boolean {
|
|
return value === true;
|
|
}
|
|
|
|
/**
|
|
* A 0-100 position within the active theme's own range, not a shared scale, so
|
|
* 35% stays 35% across themes. Each theme maps it in tailwind.css.
|
|
*/
|
|
export function normalizeThemeContrast(value: unknown): number {
|
|
const num = typeof value === "string" ? Number(value) : value;
|
|
if (typeof num !== "number" || !Number.isFinite(num)) return DEFAULT_THEME_CONTRAST;
|
|
return Math.min(100, Math.max(0, Math.round(num)));
|
|
}
|