Add shared dock layouts, navigation cube, range bands, progress actions and comparison surfaces. Extend toolbar menus and loading composition, and align tokens, registry entries, documentation and the living catalog.
53 lines
2.6 KiB
TypeScript
53 lines
2.6 KiB
TypeScript
import { forwardRef, type ButtonHTMLAttributes, type HTMLAttributes, type ReactNode } from "react";
|
|
import { cn } from "./cn.js";
|
|
|
|
export interface SegmentedItem<T extends string> {
|
|
value: T;
|
|
label: string;
|
|
icon?: ReactNode;
|
|
disabled?: boolean;
|
|
}
|
|
export type SegmentedControlSize = "default" | "dense";
|
|
export type SegmentedControlProps<T extends string> = {
|
|
items: Array<SegmentedItem<T>>;
|
|
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<T extends string>(props: SegmentedControlProps<T>) {
|
|
const { items, label, size = "default", className } = props;
|
|
const multiple = props.selectionMode === "multiple";
|
|
return <div className={cn("nodedc-segmented", className)}
|
|
role={multiple ? "group" : "tablist"} aria-label={label}
|
|
data-size={size === "default" ? undefined : size}>
|
|
{items.map(item => {
|
|
const active = props.selectionMode === "multiple" ? props.value.includes(item.value) : props.value === item.value;
|
|
return <button key={item.value} type="button" className="nodedc-segmented__item"
|
|
role={multiple ? undefined : "tab"} aria-selected={multiple ? undefined : active}
|
|
aria-pressed={multiple ? active : undefined} data-active={active ? "true" : undefined}
|
|
disabled={item.disabled} onClick={() => {
|
|
if (props.selectionMode === "multiple") {
|
|
props.onChange(active ? props.value.filter(value => value !== item.value) : [...props.value, item.value]);
|
|
} else props.onChange(item.value);
|
|
}}>{item.icon}{item.label}</button>;
|
|
})}
|
|
</div>;
|
|
}
|
|
|
|
/** Shared capsule for independently controlled toggles and menu triggers. */
|
|
export function SegmentedGroup({ label, size = "default", className, ...props }:
|
|
HTMLAttributes<HTMLDivElement> & { label: string; size?: SegmentedControlSize }) {
|
|
return <div {...props} className={cn("nodedc-segmented", className)} role="group" aria-label={label}
|
|
data-size={size === "default" ? undefined : size} />;
|
|
}
|
|
|
|
/** Caller supplies aria-pressed for toggles or aria-expanded/haspopup for menu triggers. */
|
|
export const SegmentedButton = forwardRef<HTMLButtonElement,
|
|
ButtonHTMLAttributes<HTMLButtonElement> & { active?: boolean }
|
|
>(function SegmentedButton({ active, className, type = "button", ...props }, ref) {
|
|
return <button {...props} ref={ref} type={type} className={cn("nodedc-segmented__item", className)}
|
|
data-active={active ? "true" : undefined} />;
|
|
});
|