import { forwardRef, type ButtonHTMLAttributes, type HTMLAttributes, type ReactNode } from "react"; import { cn } from "./cn.js"; export interface SegmentedItem { value: T; label: string; icon?: ReactNode; disabled?: boolean; } export type SegmentedControlSize = "default" | "dense"; export type SegmentedControlProps = { items: Array>; label: string; size?: SegmentedControlSize; className?: string; } & ({ selectionMode?: "single"; value: T; onChange: (value: T) => void } | { selectionMode: "multiple"; value: readonly T[]; onChange: (value: T[]) => void }); export function SegmentedControl(props: SegmentedControlProps) { const { items, label, size = "default", className } = props; const multiple = props.selectionMode === "multiple"; return
{items.map(item => { const active = props.selectionMode === "multiple" ? props.value.includes(item.value) : props.value === item.value; return ; })}
; } /** Shared capsule for independently controlled toggles and menu triggers. */ export function SegmentedGroup({ label, size = "default", className, ...props }: HTMLAttributes & { label: string; size?: SegmentedControlSize }) { return
; } /** Caller supplies aria-pressed for toggles or aria-expanded/haspopup for menu triggers. */ export const SegmentedButton = forwardRef & { active?: boolean } >(function SegmentedButton({ active, className, type = "button", ...props }, ref) { return