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

液态分段滑块

FreeRegistry
components/linyi/segmented-control.tsx
motion
"use client";
 
import * as React from "react";
import { motion } from "motion/react";
import { cn } from "@/lib/cn";
import { SPRING_SNAPPY } from "@/lib/motion";
 
export interface SegmentedControlOption {
value: string;
label: string;
}
 
export interface SegmentedControlProps {
options: (string | SegmentedControlOption)[];
value?: string;
defaultValue?: string;
onValueChange?: (value: string) => void;
pillRadius?: number;
showDot?: boolean;
size?: "sm" | "md";
ariaLabel?: string;
className?: string;
}
 
function normalizeOptions(options: (string | SegmentedControlOption)[]): SegmentedControlOption[] {
return options.map((opt) =>
typeof opt === "string" ? { value: opt, label: opt } : opt
);
}
 
export function SegmentedControl({
options,
value,
defaultValue,
onValueChange,
pillRadius = 32,
showDot = true,
size = "md",
ariaLabel = "液态分段滑块",
className,
}: SegmentedControlProps) {
const normalized = React.useMemo(() => normalizeOptions(options), [options]);
const [internalValue, setInternalValue] = React.useState(
defaultValue ?? normalized[0]?.value ?? ""
);
const tabRefs = React.useRef<(HTMLButtonElement | null)[]>([]);
const uid = React.useId();
 
const isControlled = value !== undefined;
const activeValue = isControlled ? value : internalValue;
const activeIndex = Math.max(
0,
normalized.findIndex((item) => item.value === activeValue)
);
 
const radius = pillRadius >= 30 ? "9999px" : `${pillRadius}px`;
const outerRadius = pillRadius >= 30 ? "9999px" : `${pillRadius + 4}px`;
 
const selectByIndex = (nextIdx: number, moveFocus = false) => {
const target = normalized[nextIdx];
if (!target) return;
if (!isControlled) {
setInternalValue(target.value);
}
onValueChange?.(target.value);
if (moveFocus) {
tabRefs.current[nextIdx]?.focus();
}
};
 
return (
<div
role="tablist"
aria-label={ariaLabel}
className={cn("inset-well p-1 inline-flex items-center relative", className)}
style={{ borderRadius: outerRadius }}
>
{normalized.map((opt, i) => {
const active = i === activeIndex;
return (
<button
key={opt.value}
ref={(el) => {
tabRefs.current[i] = el;
}}
type="button"
role="tab"
aria-selected={active}
tabIndex={active ? 0 : -1}
onClick={() => selectByIndex(i)}
onKeyDown={(e) => {
if (!normalized.length) return;
if (e.key === "ArrowRight") {
e.preventDefault();
selectByIndex((i + 1) % normalized.length, true);
} else if (e.key === "ArrowLeft") {
e.preventDefault();
selectByIndex((i - 1 + normalized.length) % normalized.length, true);
} else if (e.key === "Home") {
e.preventDefault();
selectByIndex(0, true);
} else if (e.key === "End") {
e.preventDefault();
selectByIndex(normalized.length - 1, true);
}
}}
style={{ borderRadius: radius }}
className={cn(
"relative cursor-pointer select-none flex items-center justify-center gap-1.5 outline-none focus-visible:ring-2 focus-visible:ring-accent transition-colors hover:text-fg",
size === "sm" ? "px-2.5 min-h-[30px] text-[11px]" : "px-3.5 min-h-[34px] text-xs"
)}
>
{active && (
<motion.span
layoutId={`seg-pill-${uid}`}
transition={SPRING_SNAPPY}
className="absolute inset-0 pointer-events-none border border-line"
style={{
borderRadius: radius,
background: "var(--og-clear-bg)",
boxShadow: "var(--og-clear-shadow)",
}}
/>
)}
{showDot && (
<span className="relative z-10 w-1.5 h-1.5 flex items-center justify-center flex-shrink-0">
{active && (
<motion.span
layoutId={`seg-dot-${uid}`}
transition={SPRING_SNAPPY}
className="w-1.5 h-1.5 rounded-pill bg-accent"
/>
)}
</span>
)}
<span
className={cn(
"relative z-10 transition-colors whitespace-nowrap",
active ? "text-fg font-medium" : "text-fg-secondary font-normal"
)}
>
{opt.label}
</span>
</button>
);
})}
</div>
);
}