import { cva, VariantProps } from "class-variance-authority"; import * as React from "react"; import { Button } from "@sparkle/components/Button"; import { cn } from "@sparkle/lib/utils"; type ButtonSize = Extract< React.ComponentProps["size"], "xs" | "sm" | "md" >; type ButtonsSwitchContextType = { value?: string; onValueChange?: (value: string) => void; size: ButtonSize; disabled?: boolean; }; const ButtonsSwitchContext = React.createContext(null); const useButtonsSwitch = () => { const ctx = React.useContext(ButtonsSwitchContext); if (!ctx) { throw new Error( "ButtonsSwitch must be used within a ButtonsSwitchList component" ); } return ctx; }; const listStyles = cva( cn( "s-inline-flex s-items-center s-gap-1", "s-bg-primary-100 dark:s-bg-primary-900" ), { variants: { fullWidth: { true: "s-w-full", false: "", }, size: { xs: "s-rounded-lg s-p-0.5", sm: "s-rounded-xl s-p-1", md: "s-rounded-2xl s-p-1.5", }, }, defaultVariants: { fullWidth: false, size: "sm", }, } ); export interface ButtonsSwitchListProps extends React.HTMLAttributes, VariantProps { size?: ButtonSize; disabled?: boolean; defaultValue?: string; onValueChange?: (value: string) => void; } export const ButtonsSwitchList = React.forwardRef< HTMLDivElement, ButtonsSwitchListProps >( ( { className, children, size = "sm", defaultValue, onValueChange, disabled, fullWidth, ...props }, ref ) => { const [internalValue, setInternalValue] = React.useState< string | undefined >(defaultValue); const selected = internalValue; const handleChange = React.useCallback( (next: string) => { setInternalValue(next); onValueChange?.(next); }, [onValueChange] ); const context: ButtonsSwitchContextType = React.useMemo( () => ({ value: selected, onValueChange: handleChange, size, disabled }), [selected, handleChange, size, disabled] ); return (
{children}
); } ); ButtonsSwitchList.displayName = "ButtonsSwitchList"; interface ButtonsSwitchProps extends Omit< React.ComponentProps, "size" | "variant" > { value: string; label?: string; icon?: React.ComponentProps["icon"]; } export const ButtonsSwitch = React.forwardRef< HTMLButtonElement, ButtonsSwitchProps >(({ className, value, label, icon, disabled, onClick, ...props }, ref) => { const { value: selected, onValueChange, size, disabled: groupDisabled, } = useButtonsSwitch(); const isActive = selected === value; const isDisabled = disabled || groupDisabled; const handleClick: React.MouseEventHandler = (e) => { if (isDisabled) { return; } onValueChange?.(value); onClick?.(e); }; return (