1
1
import { createMachine } from "@zag-js/core"
2
- import { raf , setElementValue , trackFormControl , trackPointerMove } from "@zag-js/dom-query"
3
- import { trackElementsSize , type ElementSize } from "@zag-js/element-size"
4
- import { clampValue , getValuePercent , getValueRanges , isEqual , setValueAtIndex , snapValueToStep } from "@zag-js/utils"
2
+ import { raf , setElementValue , trackFormControl , trackPointerMove , trackElementRect } from "@zag-js/dom-query"
3
+ import type { Size } from "@zag-js/types"
4
+ import {
5
+ clampValue ,
6
+ getValuePercent ,
7
+ getValueRanges ,
8
+ isEqual ,
9
+ pick ,
10
+ setValueAtIndex ,
11
+ snapValueToStep ,
12
+ } from "@zag-js/utils"
5
13
import * as dom from "./slider.dom"
6
14
import type { SliderSchema } from "./slider.types"
7
15
import { constrainValue , decrement , getClosestIndex , getRangeAtIndex , increment , normalizeValues } from "./slider.utils"
8
16
9
- const isEqualSize = ( a : ElementSize | null , b : ElementSize | null ) => {
17
+ const isEqualSize = ( a : Size | null , b : Size | null ) => {
10
18
return a ?. width === b ?. width && a ?. height === b ?. height
11
19
}
12
20
@@ -91,7 +99,7 @@ export const machine = createMachine<SliderSchema>({
91
99
} )
92
100
} ,
93
101
94
- effects : [ "trackFormControlState" , "trackThumbsSize " ] ,
102
+ effects : [ "trackFormControlState" , "trackThumbSize " ] ,
95
103
96
104
on : {
97
105
SET_VALUE : [
@@ -201,19 +209,24 @@ export const machine = createMachine<SliderSchema>({
201
209
} ,
202
210
} )
203
211
} ,
204
- trackThumbsSize ( { context, scope, prop } ) {
212
+
213
+ trackThumbSize ( { context, scope, prop } ) {
205
214
if ( prop ( "thumbAlignment" ) !== "contain" || prop ( "thumbSize" ) ) return
206
215
207
- return trackElementsSize ( {
208
- getNodes : ( ) => dom . getElements ( scope ) ,
209
- observeMutation : true ,
210
- callback ( size ) {
211
- if ( ! size || isEqualSize ( context . get ( "thumbSize" ) , size ) ) return
216
+ return trackElementRect ( dom . getThumbEls ( scope ) , {
217
+ box : "border-box" ,
218
+ measure ( el ) {
219
+ return dom . getOffsetRect ( el )
220
+ } ,
221
+ onEntry ( { rects } ) {
222
+ const size = pick ( rects [ 0 ] , [ "width" , "height" ] )
223
+ if ( isEqualSize ( context . get ( "thumbSize" ) , size ) ) return
212
224
context . set ( "thumbSize" , size )
213
225
} ,
214
226
} )
215
227
} ,
216
228
} ,
229
+
217
230
actions : {
218
231
dispatchChangeEvent ( { context, scope } ) {
219
232
dom . dispatchChangeEvent ( scope , context . get ( "value" ) )
@@ -230,7 +243,7 @@ export const machine = createMachine<SliderSchema>({
230
243
setClosestThumbIndex ( params ) {
231
244
const { context, event } = params
232
245
233
- const pointValue = dom . getValueFromPoint ( params , event . point )
246
+ const pointValue = dom . getPointValue ( params , event . point )
234
247
if ( pointValue == null ) return
235
248
236
249
const focusedIndex = getClosestIndex ( params , pointValue )
@@ -245,7 +258,7 @@ export const machine = createMachine<SliderSchema>({
245
258
setPointerValue ( params ) {
246
259
queueMicrotask ( ( ) => {
247
260
const { context, event } = params
248
- const pointValue = dom . getValueFromPoint ( params , event . point )
261
+ const pointValue = dom . getPointValue ( params , event . point )
249
262
if ( pointValue == null ) return
250
263
251
264
const focusedIndex = context . get ( "focusedIndex" )
0 commit comments