@@ -19,7 +19,7 @@ import {useLabel} from '@react-aria/label';
19
19
import { useLocale } from '@react-aria/i18n' ;
20
20
import { useMove } from '@react-aria/interactions' ;
21
21
22
- interface SliderAria {
22
+ export interface SliderAria {
23
23
/** Props for the label element. */
24
24
labelProps : HTMLAttributes < HTMLElement > ,
25
25
@@ -45,7 +45,9 @@ export function useSlider(
45
45
state : SliderState ,
46
46
trackRef : React . RefObject < HTMLElement >
47
47
) : SliderAria {
48
- const { labelProps, fieldProps} = useLabel ( props ) ;
48
+ let { labelProps, fieldProps} = useLabel ( props ) ;
49
+
50
+ let isVertical = props . orientation === 'vertical' ;
49
51
50
52
// Attach id of the label to the state so it can be accessed by useSliderThumb.
51
53
sliderIds . set ( state , labelProps . id ?? fieldProps . id ) ;
@@ -68,14 +70,22 @@ export function useSlider(
68
70
onMoveStart ( ) {
69
71
currentPosition . current = null ;
70
72
} ,
71
- onMove ( { deltaX} ) {
73
+ onMove ( { deltaX, deltaY} ) {
74
+ let size = isVertical ? trackRef . current . offsetHeight : trackRef . current . offsetWidth ;
75
+
72
76
if ( currentPosition . current == null ) {
73
- currentPosition . current = stateRef . current . getThumbPercent ( realTimeTrackDraggingIndex . current ) * trackRef . current . offsetWidth ;
77
+ currentPosition . current = stateRef . current . getThumbPercent ( realTimeTrackDraggingIndex . current ) * size ;
74
78
}
75
- currentPosition . current += reverseX ? - deltaX : deltaX ;
79
+
80
+ let delta = isVertical ? deltaY : deltaX ;
81
+ if ( isVertical || reverseX ) {
82
+ delta = - delta ;
83
+ }
84
+
85
+ currentPosition . current += delta ;
76
86
77
87
if ( realTimeTrackDraggingIndex . current != null && trackRef . current ) {
78
- const percent = clamp ( currentPosition . current / trackRef . current . offsetWidth , 0 , 1 ) ;
88
+ const percent = clamp ( currentPosition . current / size , 0 , 1 ) ;
79
89
stateRef . current . setThumbPercent ( realTimeTrackDraggingIndex . current , percent ) ;
80
90
}
81
91
} ,
@@ -88,15 +98,16 @@ export function useSlider(
88
98
} ) ;
89
99
90
100
let currentPointer = useRef < number | null | undefined > ( undefined ) ;
91
- let onDownTrack = ( e : React . UIEvent , id : number , clientX : number ) => {
101
+ let onDownTrack = ( e : React . UIEvent , id : number , clientX : number , clientY : number ) => {
92
102
// We only trigger track-dragging if the user clicks on the track itself and nothing is currently being dragged.
93
103
if ( trackRef . current && ! props . isDisabled && state . values . every ( ( _ , i ) => ! state . isThumbDragging ( i ) ) ) {
104
+ let size = isVertical ? trackRef . current . offsetHeight : trackRef . current . offsetWidth ;
94
105
// Find the closest thumb
95
- const trackPosition = trackRef . current . getBoundingClientRect ( ) . left ;
96
- const clickPosition = clientX ;
106
+ const trackPosition = trackRef . current . getBoundingClientRect ( ) [ isVertical ? 'top' : ' left' ] ;
107
+ const clickPosition = isVertical ? clientY : clientX ;
97
108
const offset = clickPosition - trackPosition ;
98
- let percent = offset / trackRef . current . offsetWidth ;
99
- if ( direction === 'rtl' ) {
109
+ let percent = offset / size ;
110
+ if ( direction === 'rtl' || isVertical ) {
100
111
percent = 1 - percent ;
101
112
}
102
113
let value = state . getPercentValue ( percent ) ;
@@ -148,9 +159,9 @@ export function useSlider(
148
159
...fieldProps
149
160
} ,
150
161
trackProps : mergeProps ( {
151
- onMouseDown ( e : React . MouseEvent < HTMLElement > ) { onDownTrack ( e , undefined , e . clientX ) ; } ,
152
- onPointerDown ( e : React . PointerEvent < HTMLElement > ) { onDownTrack ( e , e . pointerId , e . clientX ) ; } ,
153
- onTouchStart ( e : React . TouchEvent < HTMLElement > ) { onDownTrack ( e , e . changedTouches [ 0 ] . identifier , e . changedTouches [ 0 ] . clientX ) ; }
162
+ onMouseDown ( e : React . MouseEvent < HTMLElement > ) { onDownTrack ( e , undefined , e . clientX , e . clientY ) ; } ,
163
+ onPointerDown ( e : React . PointerEvent < HTMLElement > ) { onDownTrack ( e , e . pointerId , e . clientX , e . clientY ) ; } ,
164
+ onTouchStart ( e : React . TouchEvent < HTMLElement > ) { onDownTrack ( e , e . changedTouches [ 0 ] . identifier , e . changedTouches [ 0 ] . clientX , e . changedTouches [ 0 ] . clientY ) ; }
154
165
} , moveProps )
155
166
} ;
156
167
}
0 commit comments