Files
EpicNext-Cms/src/lib/theme-css.ts
T
Simo f0b4bc1630
Local Build and Deploy / deploy (push) Successful in 44s
fix: enforce semantic contrast across admin
2026-07-12 15:14:15 +02:00

59 lines
3.8 KiB
TypeScript

import { derivePublicForegrounds, readableColor } from "@/lib/theme-contrast";
import type { ThemePalette } from "@/lib/theme-presets";
const CSS_VARIABLES: Record<keyof ThemePalette, string> = {
color_primary: "color-primary", color_background: "color-background",
color_surface: "color-surface", color_dropdown: "color-dropdown",
color_navbar: "color-navbar", color_navbar_text: "color-navbar-text",
color_text: "color-text", color_text_muted: "color-text-muted",
color_accent: "color-accent", color_success: "color-success",
color_warning: "color-warning", color_error: "color-error", color_info: "color-info",
button_primary_color: "button-color", button_text_color: "button-text-color",
button_secondary_color: "button-secondary-color",
button_secondary_text_color: "button-secondary-text-color",
button_danger_color: "button-danger-color", button_danger_text_color: "button-danger-text-color",
button_outline_color: "button-outline-color", button_outline_text_color: "button-outline-text-color",
link_color: "link-color", link_hover_color: "link-hover-color",
border_color: "border-color", gradient_from: "gradient-from", gradient_to: "gradient-to",
};
export function themePaletteCss(selector: string, palette: ThemePalette): string {
const semantic = derivePublicForegrounds(palette);
const declarations = Object.entries(CSS_VARIABLES).map(
([key, cssName]) => `--${cssName}:${palette[key as keyof ThemePalette]};`,
);
declarations.push(
`--color-text-readable:${readableColor(palette.color_text, [palette.color_background, palette.color_surface])};`,
`--color-text-muted-readable:${readableColor(palette.color_text_muted, [palette.color_background, palette.color_surface])};`,
`--color-primary-readable:${readableColor(palette.color_primary, [palette.color_background, palette.color_surface])};`,
`--color-accent-readable:${readableColor(palette.color_accent, [palette.color_background, palette.color_surface])};`,
`--color-primary-foreground-readable:${semantic.byName.primary};`,
`--color-accent-foreground-readable:${semantic.byName.accent};`,
`--color-navbar-text-readable:${semantic.byName.navbar};`,
`--button-text-color-readable:${semantic.byName["button-primary"]};`,
`--button-secondary-text-color-readable:${semantic.byName["button-secondary"]};`,
`--button-danger-text-color-readable:${semantic.byName["button-danger"]};`,
`--button-outline-text-color-readable:${semantic.byName["button-outline"]};`,
`--link-color-readable:${semantic.byName.link};`,
`--link-hover-color-readable:${semantic.byName["link-hover"]};`,
`--color-surface-text-readable:${semantic.byName.surface};`,
`--color-dropdown-text-readable:${semantic.byName.dropdown};`,
`--color-pill-text-readable:${semantic.byName.pill};`,
`--color-badge-text-readable:${semantic.byName.badge};`,
`--color-overlay-text-readable:${semantic.byName.overlay};`,
`--color-text-subtle-readable:${semantic.byName.subtle};`,
`--color-text-disabled-readable:${semantic.byName.disabled};`,
`--admin-sidebar-background:${palette.color_navbar};`,
`--admin-sidebar-text-readable:${semantic.byName.sidebar};`,
`--admin-sidebar-muted-readable:${semantic.byName.sidebar};`,
`--admin-input-text-readable:${semantic.byName.input};`,
...(["success", "warning", "error", "info"] as const).flatMap((status) => [
`--color-${status}-text-readable:${semantic.byName[`${status}-text`]};`,
`--color-${status}-foreground-readable:${semantic.byName[`${status}-solid`]};`,
`--color-${status}-subtle:color-mix(in srgb, ${palette[`color_${status}`]} 14%, ${palette.color_surface});`,
`--color-${status}-border:color-mix(in srgb, ${palette[`color_${status}`]} 38%, transparent);`,
]),
);
return `${selector}{${declarations.join("")}}`;
}