- 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
100 lines
4.3 KiB
TypeScript
100 lines
4.3 KiB
TypeScript
"use client";
|
|
import { useEffect, useState } from "react";
|
|
|
|
export const THEME_KEY = "sm-theme";
|
|
|
|
type Theme = "light" | "dark";
|
|
|
|
const current = (): Theme => (document.documentElement.dataset.theme === "dark" ? "dark" : "light");
|
|
|
|
function apply(theme: Theme) {
|
|
document.documentElement.dataset.theme = theme;
|
|
document.querySelector('meta[name="theme-color"]')?.setAttribute("content", theme === "dark" ? "#0d0d0f" : "#f7f4ee");
|
|
}
|
|
|
|
/**
|
|
* Hell/Dunkel umschalten. Ohne eigene Wahl folgt der Shop der Systemeinstellung.
|
|
* Animation: Das neue Farbschema breitet sich als Kreis vom Schalter aus (View Transitions), sonst sanfte Überblendung.
|
|
*/
|
|
export function ThemeToggle({ className = "sm-header__btn" }: { className?: string }) {
|
|
const [theme, setTheme] = useState<Theme | null>(null);
|
|
|
|
useEffect(() => {
|
|
setTheme(current());
|
|
// Systemwechsel übernehmen, solange niemand selbst gewählt hat
|
|
const mq = window.matchMedia("(prefers-color-scheme: dark)");
|
|
const onChange = () => {
|
|
let stored: string | null = null;
|
|
try {
|
|
stored = localStorage.getItem(THEME_KEY);
|
|
} catch {}
|
|
if (stored) return;
|
|
const next = mq.matches ? "dark" : "light";
|
|
apply(next);
|
|
setTheme(next);
|
|
};
|
|
mq.addEventListener("change", onChange);
|
|
return () => mq.removeEventListener("change", onChange);
|
|
}, []);
|
|
|
|
const toggle = (e: React.MouseEvent<HTMLButtonElement>) => {
|
|
const next: Theme = current() === "dark" ? "light" : "dark";
|
|
try {
|
|
localStorage.setItem(THEME_KEY, next);
|
|
} catch {}
|
|
const root = document.documentElement;
|
|
const reduce = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
const doc = document as Document & { startViewTransition?: (cb: () => void) => { ready: Promise<void> } };
|
|
if (reduce || !doc.startViewTransition) {
|
|
root.classList.add("theme-fading");
|
|
apply(next);
|
|
setTheme(next);
|
|
setTimeout(() => root.classList.remove("theme-fading"), 500);
|
|
return;
|
|
}
|
|
const rect = e.currentTarget.getBoundingClientRect();
|
|
const x = rect.left + rect.width / 2;
|
|
const y = rect.top + rect.height / 2;
|
|
const radius = Math.hypot(Math.max(x, innerWidth - x), Math.max(y, innerHeight - y));
|
|
root.classList.add("theme-switching");
|
|
const t = doc.startViewTransition(() => {
|
|
apply(next);
|
|
setTheme(next);
|
|
});
|
|
t.ready
|
|
.then(() =>
|
|
root.animate(
|
|
{ clipPath: [`circle(0px at ${x}px ${y}px)`, `circle(${radius}px at ${x}px ${y}px)`] },
|
|
{ duration: 700, easing: "cubic-bezier(0.65, 0, 0.35, 1)", pseudoElement: "::view-transition-new(root)" },
|
|
).finished,
|
|
)
|
|
.catch(() => {})
|
|
.finally(() => root.classList.remove("theme-switching"));
|
|
};
|
|
|
|
const label = theme === null ? "Farbschema wechseln" : theme === "dark" ? "Helles Design einschalten" : "Dunkles Design einschalten";
|
|
return (
|
|
<button type="button" className={`${className} sm-theme-toggle`} onClick={toggle} aria-label={label} title={label}>
|
|
<svg viewBox="0 0 24 24" width="22" height="22" aria-hidden="true" className="sm-theme-toggle__icon">
|
|
<mask id="sm-moon-mask">
|
|
<rect x="0" y="0" width="24" height="24" fill="#fff" />
|
|
<circle className="sm-theme-toggle__cut" cx="24" cy="4" r="7" fill="#000" />
|
|
</mask>
|
|
<circle className="sm-theme-toggle__core" cx="12" cy="12" r="5" mask="url(#sm-moon-mask)" fill="currentColor" />
|
|
<g className="sm-theme-toggle__rays" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round">
|
|
<line x1="12" y1="1.5" x2="12" y2="3.5" />
|
|
<line x1="12" y1="20.5" x2="12" y2="22.5" />
|
|
<line x1="1.5" y1="12" x2="3.5" y2="12" />
|
|
<line x1="20.5" y1="12" x2="22.5" y2="12" />
|
|
<line x1="4.6" y1="4.6" x2="6" y2="6" />
|
|
<line x1="18" y1="18" x2="19.4" y2="19.4" />
|
|
<line x1="4.6" y1="19.4" x2="6" y2="18" />
|
|
<line x1="18" y1="6" x2="19.4" y2="4.6" />
|
|
</g>
|
|
</svg>
|
|
</button>
|
|
);
|
|
}
|
|
|
|
/** Läuft vor dem ersten Zeichnen, damit nichts hell aufblitzt. */
|
|
export const themeScript = `(function(){try{var t=localStorage.getItem("${THEME_KEY}");if(t!=="light"&&t!=="dark")t=matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light";document.documentElement.dataset.theme=t}catch(e){}})()`;
|