跳至正文
获取 Pro
动效组件M.24

三态主题切换

FreeRegistry
components/linyi/theme-toggle.tsx
motionlucide-react
"use client";
 
import * as React from "react";
import { motion } from "motion/react";
import { Monitor, Moon, Sun } from "lucide-react";
import { cn } from "@/lib/cn";
import { SPRING_SNAPPY } from "@/lib/motion";
 
export type ThemeToggleValue = "paper" | "obsidian" | "system";
 
const THEME_OPTIONS: Array<{
id: ThemeToggleValue;
label: string;
Icon: React.ComponentType<{ className?: string }>;
}> = [
{ id: "paper", label: "浅色", Icon: Sun },
{ id: "obsidian", label: "深色", Icon: Moon },
{ id: "system", label: "系统", Icon: Monitor },
];
 
export interface ThemeToggleProps {
value?: ThemeToggleValue;
defaultValue?: ThemeToggleValue;
onChange?: (value: ThemeToggleValue) => void;
size?: "sm" | "md";
showLabels?: boolean;
ariaLabel?: string;
className?: string;
}
 
export function ThemeToggle({
value,
defaultValue = "paper",
onChange,
size = "md",
showLabels = false,
ariaLabel = "主题切换",
className,
}: ThemeToggleProps) {
const [internalVal, setInternalVal] = React.useState<ThemeToggleValue>(defaultValue);
const uid = React.useId();
const buttonRefs = React.useRef<Array<HTMLButtonElement | null>>([]);
 
const current = value ?? internalVal;
 
const selectIndex = (idx: number, focus = false) => {
const opt = THEME_OPTIONS[idx];
if (!opt) return;
if (value === undefined) {
setInternalVal(opt.id);
}
onChange?.(opt.id);
if (focus) {
buttonRefs.current[idx]?.focus();
}
};
 
const handleKeyDown = (e: React.KeyboardEvent<HTMLButtonElement>, idx: number) => {
const count = THEME_OPTIONS.length;
if (e.key === "ArrowRight" || e.key === "ArrowDown") {
e.preventDefault();
selectIndex((idx + 1) % count, true);
} else if (e.key === "ArrowLeft" || e.key === "ArrowUp") {
e.preventDefault();
selectIndex((idx - 1 + count) % count, true);
} else if (e.key === "Home") {
e.preventDefault();
selectIndex(0, true);
} else if (e.key === "End") {
e.preventDefault();
selectIndex(count - 1, true);
}
};
 
const isSmall = size === "sm";
 
return (
<div
role="radiogroup"
aria-label={ariaLabel}
className={cn(
"inline-flex items-center rounded-pill border border-line bg-well p-1",
className
)}
>
{THEME_OPTIONS.map(({ id, label, Icon }, idx) => {
const active = id === current;
 
return (
<button
key={id}
ref={(el) => {
buttonRefs.current[idx] = el;
}}
type="button"
role="radio"
aria-checked={active}
aria-label={label}
tabIndex={active ? 0 : -1}
onClick={() => selectIndex(idx)}
onKeyDown={(e) => handleKeyDown(e, idx)}
className={cn(
"relative inline-flex items-center justify-center gap-1.5 rounded-pill cursor-pointer select-none transition-colors",
showLabels
? isSmall
? "h-7 px-2.5 text-[11px]"
: "h-8 px-3 text-xs"
: isSmall
? "h-7 w-7"
: "h-8 w-8",
active ? "text-fg font-medium" : "text-fg-muted hover:text-fg-secondary"
)}
>
{active && (
<motion.span
layoutId={`theme-toggle-pill-${uid}`}
transition={SPRING_SNAPPY}
aria-hidden="true"
className="absolute inset-0 rounded-pill border border-line bg-surface shadow-[var(--tab-pill-shadow)]"
/>
)}
<Icon
className={cn(
"relative z-10",
isSmall ? "h-3.5 w-3.5" : "h-3.5 w-3.5"
)}
/>
{showLabels && <span className="relative z-10">{label}</span>}
</button>
);
})}
</div>
);
}