@@ -5,6 +5,7 @@ import { areArraysEqual } from '../../utils/areArraysEqual';
5
5
import { clamp } from '../../utils/clamp' ;
6
6
import { mergeReactProps } from '../../utils/mergeReactProps' ;
7
7
import { ownerDocument } from '../../utils/owner' ;
8
+ import type { GenericHTMLProps } from '../../utils/types' ;
8
9
import { useControlled } from '../../utils/useControlled' ;
9
10
import { useEnhancedEffect } from '../../utils/useEnhancedEffect' ;
10
11
import { useEventCallback } from '../../utils/useEventCallback' ;
@@ -233,8 +234,22 @@ export function useSliderRoot(parameters: useSliderRoot.Parameters): useSliderRo
233
234
} ,
234
235
) ;
235
236
237
+ // for pointer drag only
238
+ const commitValue = useEventCallback ( ( value : number | readonly number [ ] , event : Event ) => {
239
+ if ( Array . isArray ( value ) ) {
240
+ const newPercentageValues = [ ] ;
241
+ for ( let i = 0 ; i < value . length ; i += 1 ) {
242
+ newPercentageValues . push ( valueToPercent ( value [ i ] , min , max ) ) ;
243
+ }
244
+ } else if ( typeof value === 'number' ) {
245
+ setPercentageValues ( [ valueToPercent ( value , min , max ) ] ) ;
246
+ }
247
+ onValueCommitted ( value , event ) ;
248
+ } ) ;
249
+
236
250
const handleRootRef = useForkRef ( rootRef , sliderRef ) ;
237
251
252
+ // for keypresses only
238
253
const handleInputChange = useEventCallback (
239
254
( valueInput : number , index : number , event : React . KeyboardEvent | React . ChangeEvent ) => {
240
255
const newValue = getSliderValue ( valueInput , index , min , max , range , values ) ;
@@ -375,12 +390,13 @@ export function useSliderRoot(parameters: useSliderRoot.Parameters): useSliderRo
375
390
376
391
return React . useMemo (
377
392
( ) => ( {
378
- getRootProps,
379
393
'aria-labelledby' : ariaLabelledby ,
380
394
active,
395
+ commitValue,
381
396
disabled,
382
397
dragging,
383
398
getFingerState,
399
+ getRootProps,
384
400
handleInputChange,
385
401
largeStep,
386
402
lastChangedValueRef,
@@ -404,12 +420,13 @@ export function useSliderRoot(parameters: useSliderRoot.Parameters): useSliderRo
404
420
values,
405
421
} ) ,
406
422
[
407
- getRootProps ,
408
423
active ,
409
424
ariaLabelledby ,
425
+ commitValue ,
410
426
disabled ,
411
427
dragging ,
412
428
getFingerState ,
429
+ getRootProps ,
413
430
handleInputChange ,
414
431
largeStep ,
415
432
lastChangedValueRef ,
@@ -535,34 +552,27 @@ export namespace useSliderRoot {
535
552
}
536
553
537
554
export interface ReturnValue {
538
- getRootProps : (
539
- externalProps ?: React . ComponentPropsWithRef < 'div' > ,
540
- ) => React . ComponentPropsWithRef < 'div' > ;
541
555
/**
542
556
* The index of the active thumb.
543
557
*/
544
558
active : number ;
545
559
'aria-labelledby' ?: string ;
546
- handleInputChange : (
547
- valueInput : number ,
548
- index : number ,
549
- event : React . KeyboardEvent | React . ChangeEvent ,
550
- ) => void ;
560
+ /**
561
+ * Callback fired when drag ends and invokes onValueCommitted.
562
+ */
563
+ commitValue : ( newValue : number | readonly number [ ] , event : Event ) => void ;
551
564
dragging : boolean ;
552
565
disabled : boolean ;
553
566
getFingerState : (
554
567
fingerPosition : FingerPosition | null ,
555
568
shouldCaptureThumbIndex ?: boolean ,
556
569
offset ?: number ,
557
570
) => FingerState | null ;
558
- /**
559
- * Callback to invoke change handlers after internal value state is updated.
560
- */
561
- setValue : (
562
- newValue : number | number [ ] ,
563
- newPercentageValues : readonly number [ ] ,
564
- activeThumb : number ,
565
- event : Event ,
571
+ getRootProps : ( externalProps ?: GenericHTMLProps ) => GenericHTMLProps ;
572
+ handleInputChange : (
573
+ valueInput : number ,
574
+ index : number ,
575
+ event : React . KeyboardEvent | React . ChangeEvent ,
566
576
) => void ;
567
577
/**
568
578
* The large step value of the slider when incrementing or decrementing while the shift key is held,
@@ -584,23 +594,31 @@ export namespace useSliderRoot {
584
594
*/
585
595
minStepsBetweenValues : number ;
586
596
name : string ;
587
- onValueCommitted : ( value : number | readonly number [ ] , event : Event ) => void ;
588
597
/**
589
598
* The component orientation.
590
599
* @default 'horizontal'
591
600
*/
592
601
orientation : Orientation ;
593
- registerSliderControl : ( element : HTMLElement | null ) => void ;
594
602
/**
595
603
* The value(s) of the slider as percentages
596
604
*/
597
605
percentageValues : readonly number [ ] ;
606
+ registerSliderControl : ( element : HTMLElement | null ) => void ;
598
607
setActive : React . Dispatch < React . SetStateAction < number > > ;
599
608
setDragging : React . Dispatch < React . SetStateAction < boolean > > ;
600
609
setPercentageValues : React . Dispatch < React . SetStateAction < readonly number [ ] > > ;
601
610
setThumbMap : React . Dispatch <
602
611
React . SetStateAction < Map < Node , CompositeMetadata < ThumbMetadata > | null > >
603
612
> ;
613
+ /**
614
+ * Callback fired when dragging and invokes onValueChange.
615
+ */
616
+ setValue : (
617
+ newValue : number | number [ ] ,
618
+ newPercentageValues : readonly number [ ] ,
619
+ activeThumb : number ,
620
+ event : Event ,
621
+ ) => void ;
604
622
/**
605
623
* The step increment of the slider when incrementing or decrementing. It will snap
606
624
* to multiples of this value. Decimal values are supported.
0 commit comments