'use client' import * as ToggleGroupPrimitive from '@radix-ui/react-toggle-group' import * as React from 'react' import { cn } from '../../utils' import { segmentButtonVariants, type VariantProps } from '../SegmentButton' const SegmentControllerContext = React.createContext<{ size?: VariantProps['size'] } | null>(null) interface SegmentControllerProps extends Omit< React.ComponentPropsWithoutRef, 'type' | 'value' | 'defaultValue' | 'onValueChange' > { className?: string children?: React.ReactNode value?: string defaultValue?: string onValueChange?: (value: string) => void size?: VariantProps['size'] ref?: React.Ref> } const SegmentController = ({ className, size, children, value, defaultValue, onValueChange, ref, ...props }: SegmentControllerProps) => { const [internalValue, setInternalValue] = React.useState( value ?? defaultValue ?? '', ) const isControlled = value !== undefined const currentValue = isControlled ? value : internalValue const handleValueChange = React.useCallback( (newValue: string | undefined) => { if (!newValue) return if (!isControlled) { setInternalValue(newValue) } onValueChange?.(newValue) }, [isControlled, onValueChange], ) const contextValue = React.useMemo(() => ({ size }), [size]) return ( {children} ) } SegmentController.displayName = 'SegmentController' type SegmentControllerItemProps = React.ComponentPropsWithoutRef< typeof ToggleGroupPrimitive.Item > & VariantProps & { ref?: React.Ref> } const SegmentControllerItem = ({ className, children, variant, size, ref, ...props }: SegmentControllerItemProps) => { const context = React.useContext(SegmentControllerContext) const itemVariant = variant ?? 'default' const itemSize = context?.size ?? size return ( {children} ) } SegmentControllerItem.displayName = 'SegmentControllerItem' export { SegmentController, SegmentControllerItem }