components/linyi/liquid-switch.tsx
"use client"; import * as React from "react";import { cn } from "@/lib/cn";import { playSound } from "@/lib/sound"; export interface LiquidSwitchProps { checked?: boolean; defaultChecked?: boolean; onCheckedChange?: (checked: boolean) => void; disabled?: boolean; label?: string; size?: "sm" | "md"; id?: string; className?: string;} export function LiquidSwitch({ checked, defaultChecked = false, onCheckedChange, disabled = false, label = "切换开关", size = "md", id, className,}: LiquidSwitchProps) { const [internalChecked, setInternalChecked] = React.useState(defaultChecked); const isOn = checked ?? internalChecked; const toggle = () => { if (disabled) return; const next = !isOn; playSound("toggle", { on: next }); if (checked === undefined) { setInternalChecked(next); } onCheckedChange?.(next); }; const isSmall = size === "sm"; return ( <button id={id} type="button" role="switch" aria-checked={isOn} aria-label={label} disabled={disabled} data-on={String(isOn)} onClick={toggle} className={cn( "group relative flex-shrink-0 rounded-pill border cursor-pointer transition-[background-color,border-color,opacity] duration-200 ease-out shadow-[inset_0_1px_2px_rgba(0,0,0,0.12)] disabled:opacity-45 disabled:cursor-not-allowed", isSmall ? "h-[22px] w-[38px]" : "h-[26px] w-[46px]", isOn ? "border-accent bg-accent" : "border-line-strong bg-well", className )} > <span aria-hidden="true" className={cn( "pointer-events-none absolute top-[2px] left-[2px] block rounded-pill bg-white/95 shadow-[0_2px_6px_rgba(0,0,0,0.22),inset_0_1.5px_1px_rgba(255,255,255,0.98)] transition-[transform,width] duration-200 ease-[cubic-bezier(0.34,1.56,0.64,1)] motion-reduce:transition-none", isSmall ? cn( "h-[16px] w-[18px]", !disabled && "group-active:w-[22px]", isOn && "translate-x-[14px]", isOn && !disabled && "group-active:translate-x-[10px]" ) : cn( "h-[20px] w-[22px]", !disabled && "group-active:w-[27px]", isOn && "translate-x-[18px]", isOn && !disabled && "group-active:translate-x-[13px]" ) )} /> </button> );}