Skip to content

Open-source, local-first AI agent for real work.

2.9 KB
Raw Blame
import { motion } from "motion/react";
import { useId, type JSX } from "react";

function cn(...parts: Array<string | false | null | undefined>): string {
	return parts.filter(Boolean).join(" ");
}

export interface SegmentedControlItem<T extends string> {
	key: T;
	label: string;
	icon?: string;
	/** 可选未读小红点(>0 显示)。表达「有待处理的新内容」,与 count 的语义不同。 */
	badge?: number;
	/**
	 * 可选计数(0 不显示):跟在标题后面的灰字小胶囊,用于「这一栏里有多少条」这类
	 * 中性的量,例如变更数、任务数。需要告警语气时用 badge。
	 */
	count?: number;
}

export interface SegmentedControlProps<T extends string> {
	items: SegmentedControlItem<T>[];
	value: T;
	onChange: (value: T) => void;
	className?: string;
	/** 容器尺寸正在变化时设为 true,禁用指示器的 layout 动画,避免抖动 */
	suppressLayoutAnimation?: boolean;
}

export function SegmentedControl<T extends string>({
	items,
	value,
	onChange,
	className,
	suppressLayoutAnimation = false,
}: SegmentedControlProps<T>): JSX.Element {
	const layoutId = useId();

	return (
		<div
			className={cn("relative inline-flex shrink-0 gap-1", className)}
		>
			{items.map(({ key, label, icon, badge, count }) => {
				const active = value === key;
				return (
					<button
						key={key}
						type="button"
						onClick={() => onChange(key)}
						className={cn(
							"relative flex h-7 shrink-0 items-center justify-center whitespace-nowrap rounded-full px-2.5 text-[0.8rem] font-medium outline-none transition-colors duration-150 select-none focus-visible:ring-2 focus-visible:ring-ring",
							active ? "text-foreground" : "text-muted-foreground hover:bg-accent/50 hover:text-foreground",
						)}
					>
						{active && (
							<motion.span
								layoutId={`seg-indicator-${layoutId}`}
								className="absolute inset-0 rounded-full bg-secondary"
								transition={
									suppressLayoutAnimation
										? { duration: 0 }
										: { type: "spring", stiffness: 480, damping: 32, mass: 0.8 }
								}
							/>
						)}
						<motion.span
							className="relative z-10 flex items-center gap-1.5"
							whileTap={{ scale: 0.93 }}
							transition={{ type: "spring", stiffness: 500, damping: 24 }}
						>
							{icon && <span className={cn(icon, "h-3.5 w-3.5")} />}
							{label}
							{count && count > 0 ? (
								<span className="rounded-full bg-background/60 px-1.5 text-[11px] leading-4 tabular-nums text-muted-foreground">
									{count}
								</span>
							) : null}
							{badge && badge > 0 ? (
								<span className="ml-0.5 inline-flex h-3.5 min-w-3.5 items-center justify-center rounded-full bg-red-500 px-1 text-[9px] font-semibold leading-none text-white">
									{badge > 99 ? "99+" : badge}
								</span>
							) : null}
						</motion.span>
					</button>
				);
			})}
		</div>
	);
}