import { Slider as SliderPrimitive } from '@base-ui/react/slider'; import * as React from 'react'; import { cn } from '@/lib/utils'; function Slider({ className, defaultValue, value, min = 0, max = 100, onValueChange, ...props }: SliderPrimitive.Root.Props) { const _values = React.useMemo( () => (Array.isArray(value) ? value : Array.isArray(defaultValue) ? defaultValue : [min, max]), [value, defaultValue, min, max] ); // base-ui decides the value's shape in two places that disagree for a // one-element array: the root treats `[n]` as a range and hands the keyboard // path an array, but the control counts thumbs (`values.length > 1`) and hands // pointer drags and track presses a bare number. Callers that pass an array // naturally expect an array back, so normalize the shape to match the input. const arrayShaped = Array.isArray(value ?? defaultValue); const handleValueChange = React.useCallback< NonNullable >( (next, details) => { onValueChange?.(arrayShaped && !Array.isArray(next) ? [next as number] : next, details); }, [onValueChange, arrayShaped] ); return ( {/* Key by position, never by value: a value key remounts the thumb on every change, dropping its focused input mid-interaction. */} {_values.map((_, index) => ( ))} ); } export { Slider };