sanamare/scripts/build-tokens.mjs
Claude 813f6e3cc9 Darkmode für Shop und Admin mit animiertem Umschalten
- Schalter (Sonne/Mond) im Kopf des Shops, in der Admin-Seitenleiste und oben in der mobilen Admin-Ansicht
- Ohne eigene Wahl folgt das Design der Systemeinstellung; die Wahl wird im Browser gemerkt, kein Aufblitzen beim Laden
- Umschalten: neues Farbschema breitet sich als Kreis vom Schalter aus (View Transitions), sonst sanfte Überblendung; reduzierte Bewegung wird beachtet
- Gold-Logo im dunklen Admin und Login; Rechnung bleibt immer hell ([data-theme="light"] aus den Tokens)

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
2026-10-19 22:12:24 +02:00

49 lines
2.5 KiB
JavaScript

// Erzeugt src/styles/tokens.css aus design/tokens.json (Sanamare Design System).
import { readFileSync, writeFileSync } from "node:fs";
const t = JSON.parse(readFileSync(new URL("../design/tokens.json", import.meta.url)));
const themes = t.color.themes.map((x) => x.id);
const first = themes[0];
const colorValue = (v, theme) => {
const raw = typeof v === "string" ? v : v[theme] ?? v[first];
return raw.startsWith("{") ? `var(--${raw.slice(1, -1)})` : raw;
};
const themed = (v, theme) => (typeof v === "string" ? v : v[theme] ?? v[first]);
const block = (theme) => {
const lines = [];
for (const tok of t.color.tokens) lines.push(` --${tok.name}: ${colorValue(tok.value, theme)};`);
for (const tok of t.shadow?.tokens ?? []) lines.push(` --${tok.name}: ${themed(tok.value, theme)};`);
return lines.join("\n");
};
let css = `/* ${t.name} — generated from design/tokens.json (npm run tokens) */\n:root {\n`;
for (const [k, v] of Object.entries(t.type.families)) css += ` --font-${k}: ${v};\n`;
for (const fam of ["spacing", "radius", "layout", "breakpoint"])
for (const tok of t[fam]?.tokens ?? []) css += ` --${tok.name}: ${tok.value};\n`;
css += block(first) + "\n color-scheme: light;\n}\n";
// Für Bereiche, die im Darkmode hell bleiben sollen (z. B. Rechnung)
css += `[data-theme="${first}"] {\n${block(first)}\n color-scheme: light;\n background-color: var(--surface);\n color: var(--ink);\n}\n`;
for (const th of themes.slice(1)) {
css += `[data-theme="${th}"] {\n${block(th)}\n color-scheme: ${th};\n background-color: var(--surface);\n color: var(--ink);\n}\n`;
}
for (const g of t.type.groups) {
for (const s of g.styles) {
const props = [
`font-family: var(--font-${g.family})`,
`font-size: ${s.fontSize}`,
`line-height: ${s.lineHeight}`,
`font-weight: ${s.fontWeight}`,
];
if (s.letterSpacing) props.push(`letter-spacing: ${s.letterSpacing}`);
if (s.fontStyle) props.push(`font-style: ${s.fontStyle}`);
if (["button", "nav", "eyebrow", "wordmark"].includes(s.name)) props.push("text-transform: uppercase");
if (s.name === "eyebrow") props.push("color: var(--gold-deep)");
if (s.name.startsWith("price")) props.push("font-variant-numeric: tabular-nums");
css += `.${s.name} { ${props.join("; ")}; }\n`;
}
}
css += `@media (max-width: 767px) { .display-xl { font-size: 44px; line-height: 1.05; } .display-lg { font-size: 34px; } }\n`;
writeFileSync(new URL("../src/styles/tokens.css", import.meta.url), css);
console.log("tokens.css geschrieben");