@zag-js/slider 2.0.0-next.2 → 2.0.0-next.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,355 +1,371 @@
1
- // src/slider.machine.ts
1
+ import { dispatchChangeEvent, getDisabledThumbIndices, getHiddenInputEl, getPointValue, getRootEl, getThumbEl, getThumbEls } from "./slider.dom.mjs";
2
+ import { constrainValue, getClosestIndex, getRangeAtIndex, normalizeValues, resolveStackedThumb, resolveThumbCollision, selectMovableThumb, stepThumb, toThumbSize } from "./slider.utils.mjs";
3
+ import { AnimationFrame, resizeObserverBorderBox, setElementValue, trackFormControl, trackPointerMove } from "@zag-js/dom-query";
4
+ import { callAll, clampValue, getValuePercent, getValueRanges, isEqual, isValueWithinRange, setValueAtIndex, snapValueToStep } from "@zag-js/utils";
2
5
  import { createMachine, memo } from "@zag-js/core";
3
- import { raf, resizeObserverBorderBox, setElementValue, trackFormControl, trackPointerMove } from "@zag-js/dom-query";
4
- import {
5
- callAll,
6
- clampValue,
7
- getValuePercent,
8
- getValueRanges,
9
- isEqual,
10
- isValueWithinRange,
11
- pick,
12
- setValueAtIndex,
13
- snapValueToStep
14
- } from "@zag-js/utils";
15
- import * as dom from "./slider.dom.mjs";
16
- import {
17
- constrainValue,
18
- decrement,
19
- getClosestIndex,
20
- getRangeAtIndex,
21
- increment,
22
- normalizeValues,
23
- resolveThumbCollision,
24
- selectMovableThumb
25
- } from "./slider.utils.mjs";
26
- var isEqualSize = (a, b) => {
27
- return a?.width === b?.width && a?.height === b?.height;
28
- };
29
- var normalize = (value, min, max, step, minStepsBetweenThumbs) => {
30
- const ranges = getValueRanges(value, min, max, minStepsBetweenThumbs * step);
31
- return ranges.map((range) => {
32
- const snapValue = snapValueToStep(range.value, range.min, range.max, step);
33
- const rangeValue = clampValue(snapValue, range.min, range.max);
34
- if (!isValueWithinRange(rangeValue, min, max)) {
35
- throw new Error(
36
- "[zag-js/slider] The configured `min`, `max`, `step` or `minStepsBetweenThumbs` values are invalid"
37
- );
38
- }
39
- return rangeValue;
40
- });
6
+ // src/slider.machine.ts
7
+ const normalize = (value, min, max, step, minStepsBetweenThumbs) => {
8
+ return getValueRanges(value, min, max, minStepsBetweenThumbs * step).map((range) => {
9
+ const snapValue = snapValueToStep(range.value, range.min, range.max, step);
10
+ const rangeValue = clampValue(snapValue, range.min, range.max);
11
+ if (!isValueWithinRange(rangeValue, min, max)) throw new Error("[zag-js/slider] The configured `min`, `max`, `step` or `minStepsBetweenThumbs` values are invalid");
12
+ return rangeValue;
13
+ });
41
14
  };
42
- var machine = createMachine({
43
- props({ props }) {
44
- const min = props.min ?? 0;
45
- const max = props.max ?? 100;
46
- const step = props.step ?? 1;
47
- const defaultValue = props.defaultValue ?? [min];
48
- const minStepsBetweenThumbs = props.minStepsBetweenThumbs ?? 0;
49
- return {
50
- dir: "ltr",
51
- thumbAlignment: "contain",
52
- origin: "start",
53
- orientation: "horizontal",
54
- thumbCollisionBehavior: "none",
55
- minStepsBetweenThumbs,
56
- ...props,
57
- largeStep: props.largeStep ?? 10 * step,
58
- defaultValue: normalize(defaultValue, min, max, step, minStepsBetweenThumbs),
59
- value: props.value ? normalize(props.value, min, max, step, minStepsBetweenThumbs) : void 0,
60
- max,
61
- step,
62
- min
63
- };
64
- },
65
- initialState() {
66
- return "idle";
67
- },
68
- context({ prop, bindable, getContext }) {
69
- return {
70
- thumbSize: bindable(() => ({
71
- defaultValue: prop("thumbSize") || null
72
- })),
73
- value: bindable(() => ({
74
- defaultValue: prop("defaultValue"),
75
- value: prop("value"),
76
- isEqual,
77
- hash(a) {
78
- return a.join(",");
79
- },
80
- onChange(value) {
81
- prop("onValueChange")?.({ value });
82
- }
83
- })),
84
- focusedIndex: bindable(() => ({
85
- defaultValue: -1,
86
- onChange(value) {
87
- const ctx = getContext();
88
- prop("onFocusChange")?.({ focusedIndex: value, value: ctx.get("value") });
89
- }
90
- })),
91
- fieldsetDisabled: bindable(() => ({
92
- defaultValue: false
93
- }))
94
- };
95
- },
96
- refs() {
97
- return {
98
- thumbDragOffset: null,
99
- thumbDragStartValue: null
100
- };
101
- },
102
- computed: {
103
- isHorizontal: ({ prop }) => prop("orientation") === "horizontal",
104
- isVertical: ({ prop }) => prop("orientation") === "vertical",
105
- isRtl: ({ prop }) => prop("orientation") === "horizontal" && prop("dir") === "rtl",
106
- isDisabled: ({ context, prop }) => !!prop("disabled") || context.get("fieldsetDisabled"),
107
- isInteractive: ({ prop, computed }) => !(prop("readOnly") || computed("isDisabled")),
108
- hasMeasuredThumbSize: ({ context }) => context.get("thumbSize") != null,
109
- valuePercent: memo(
110
- ({ context, prop }) => [context.get("value"), prop("min"), prop("max")],
111
- ([value, min, max]) => value.map((value2) => 100 * getValuePercent(value2, min, max))
112
- )
113
- },
114
- watch({ track, action, context, computed, send }) {
115
- track([() => context.hash("value")], () => {
116
- action(["syncInputElements", "dispatchChangeEvent"]);
117
- });
118
- track([() => computed("isDisabled")], () => {
119
- if (computed("isDisabled")) {
120
- send({ type: "POINTER_CANCEL" });
121
- }
122
- });
123
- },
124
- effects: ["trackFormControlState", "trackThumbSize"],
125
- on: {
126
- SET_VALUE: [
127
- {
128
- guard: "hasIndex",
129
- actions: ["setValueAtIndex", "invokeOnChangeEnd"]
130
- },
131
- {
132
- actions: ["setValue", "invokeOnChangeEnd"]
133
- }
134
- ],
135
- INCREMENT: {
136
- actions: ["incrementThumbAtIndex", "invokeOnChangeEnd"]
137
- },
138
- DECREMENT: {
139
- actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
140
- }
141
- },
142
- states: {
143
- idle: {
144
- on: {
145
- POINTER_DOWN: {
146
- target: "dragging",
147
- actions: ["setClosestThumbIndex", "setThumbDragStartValue", "setPointerValue", "focusActiveThumb"]
148
- },
149
- FOCUS: {
150
- target: "focus",
151
- actions: ["setFocusedIndex"]
152
- },
153
- THUMB_POINTER_DOWN: {
154
- target: "dragging",
155
- actions: ["setFocusedIndex", "setThumbDragOffset", "setThumbDragStartValue", "focusActiveThumb"]
156
- }
157
- }
158
- },
159
- focus: {
160
- entry: ["focusActiveThumb"],
161
- on: {
162
- POINTER_DOWN: {
163
- target: "dragging",
164
- actions: ["setClosestThumbIndex", "setThumbDragStartValue", "setPointerValue", "focusActiveThumb"]
165
- },
166
- THUMB_POINTER_DOWN: {
167
- target: "dragging",
168
- actions: ["setFocusedIndex", "setThumbDragOffset", "setThumbDragStartValue", "focusActiveThumb"]
169
- },
170
- ARROW_DEC: {
171
- actions: ["decrementThumbAtIndex", "invokeOnChangeEnd"]
172
- },
173
- ARROW_INC: {
174
- actions: ["incrementThumbAtIndex", "invokeOnChangeEnd"]
175
- },
176
- HOME: {
177
- actions: ["setFocusedThumbToMin", "invokeOnChangeEnd"]
178
- },
179
- END: {
180
- actions: ["setFocusedThumbToMax", "invokeOnChangeEnd"]
181
- },
182
- BLUR: {
183
- target: "idle",
184
- actions: ["clearFocusedIndex"]
185
- }
186
- }
187
- },
188
- dragging: {
189
- entry: ["focusActiveThumb"],
190
- effects: ["trackPointerMove"],
191
- on: {
192
- POINTER_UP: {
193
- target: "focus",
194
- actions: ["invokeOnChangeEnd", "clearThumbDragOffset", "clearThumbDragStartValue"]
195
- },
196
- POINTER_MOVE: {
197
- actions: ["setPointerValue"]
198
- },
199
- POINTER_CANCEL: {
200
- target: "idle",
201
- actions: ["clearFocusedIndex", "clearThumbDragOffset", "clearThumbDragStartValue"]
202
- }
203
- }
204
- }
205
- },
206
- implementations: {
207
- guards: {
208
- hasIndex: ({ event }) => event.index != null
209
- },
210
- effects: {
211
- trackFormControlState({ context, scope }) {
212
- return trackFormControl(dom.getRootEl(scope), {
213
- onFieldsetDisabledChange(disabled) {
214
- context.set("fieldsetDisabled", disabled);
215
- },
216
- onFormReset() {
217
- context.set("value", context.initial("value"));
218
- }
219
- });
220
- },
221
- trackPointerMove({ scope, send }) {
222
- return trackPointerMove(scope.getDoc(), {
223
- onPointerMove(info) {
224
- send({ type: "POINTER_MOVE", point: info.point });
225
- },
226
- onPointerUp() {
227
- send({ type: "POINTER_UP" });
228
- }
229
- });
230
- },
231
- trackThumbSize({ context, scope, prop }) {
232
- if (prop("thumbAlignment") !== "contain" || prop("thumbSize")) return;
233
- const exec = (el) => {
234
- const rect = dom.getOffsetRect(el);
235
- const size = pick(rect, ["width", "height"]);
236
- if (isEqualSize(context.get("thumbSize"), size)) return;
237
- context.set("thumbSize", size);
238
- };
239
- const thumbEls = dom.getThumbEls(scope);
240
- thumbEls.forEach(exec);
241
- const cleanups = thumbEls.map((el) => resizeObserverBorderBox.observe(el, () => exec(el)));
242
- return callAll(...cleanups);
243
- }
244
- },
245
- actions: {
246
- dispatchChangeEvent({ context, scope }) {
247
- dom.dispatchChangeEvent(scope, context.get("value"));
248
- },
249
- syncInputElements({ context, scope }) {
250
- context.get("value").forEach((value, index) => {
251
- const inputEl = dom.getHiddenInputEl(scope, index);
252
- setElementValue(inputEl, value.toString());
253
- });
254
- },
255
- invokeOnChangeEnd({ prop, context }) {
256
- queueMicrotask(() => {
257
- prop("onValueChangeEnd")?.({ value: context.get("value") });
258
- });
259
- },
260
- setClosestThumbIndex(params) {
261
- const { context, event } = params;
262
- const pointValue = dom.getPointValue(params, event.point);
263
- if (pointValue == null) return;
264
- const focusedIndex = getClosestIndex(params, pointValue);
265
- context.set("focusedIndex", focusedIndex);
266
- },
267
- setFocusedIndex(params) {
268
- const { context, event } = params;
269
- const movableIndex = selectMovableThumb(params, event.index);
270
- context.set("focusedIndex", movableIndex);
271
- },
272
- clearFocusedIndex({ context }) {
273
- context.set("focusedIndex", -1);
274
- },
275
- setThumbDragOffset(params) {
276
- const { refs, event } = params;
277
- refs.set("thumbDragOffset", event.offset ?? null);
278
- },
279
- clearThumbDragOffset({ refs }) {
280
- refs.set("thumbDragOffset", null);
281
- },
282
- setThumbDragStartValue({ refs, context }) {
283
- refs.set("thumbDragStartValue", context.get("value").slice());
284
- },
285
- clearThumbDragStartValue({ refs }) {
286
- refs.set("thumbDragStartValue", null);
287
- },
288
- setPointerValue(params) {
289
- queueMicrotask(() => {
290
- const { context, event, prop, refs } = params;
291
- const pointValue = dom.getPointValue(params, event.point);
292
- if (pointValue == null) return;
293
- const focusedIndex = context.get("focusedIndex");
294
- const startValues = refs.get("thumbDragStartValue");
295
- const result = resolveThumbCollision(
296
- prop("thumbCollisionBehavior"),
297
- focusedIndex,
298
- pointValue,
299
- context.get("value"),
300
- prop("min"),
301
- prop("max"),
302
- prop("step"),
303
- prop("minStepsBetweenThumbs"),
304
- startValues?.[focusedIndex]
305
- );
306
- if (result.swapped) {
307
- context.set("focusedIndex", result.index);
308
- }
309
- context.set("value", result.values);
310
- });
311
- },
312
- focusActiveThumb({ scope, context }) {
313
- raf(() => {
314
- const thumbEl = dom.getThumbEl(scope, context.get("focusedIndex"));
315
- thumbEl?.focus({ preventScroll: true });
316
- });
317
- },
318
- decrementThumbAtIndex(params) {
319
- const { context, event } = params;
320
- const value = decrement(params, event.index, event.step);
321
- context.set("value", value);
322
- },
323
- incrementThumbAtIndex(params) {
324
- const { context, event } = params;
325
- const value = increment(params, event.index, event.step);
326
- context.set("value", value);
327
- },
328
- setFocusedThumbToMin(params) {
329
- const { context } = params;
330
- const index = context.get("focusedIndex");
331
- const { min } = getRangeAtIndex(params, index);
332
- context.set("value", (prev) => setValueAtIndex(prev, index, min));
333
- },
334
- setFocusedThumbToMax(params) {
335
- const { context } = params;
336
- const index = context.get("focusedIndex");
337
- const { max } = getRangeAtIndex(params, index);
338
- context.set("value", (prev) => setValueAtIndex(prev, index, max));
339
- },
340
- setValueAtIndex(params) {
341
- const { context, event } = params;
342
- const value = constrainValue(params, event.value, event.index);
343
- context.set("value", (prev) => setValueAtIndex(prev, event.index, value));
344
- },
345
- setValue(params) {
346
- const { context, event } = params;
347
- const value = normalizeValues(params, event.value);
348
- context.set("value", value);
349
- }
350
- }
351
- }
15
+ const machine = createMachine({
16
+ props({ props }) {
17
+ const min = props.min ?? 0;
18
+ const max = props.max ?? 100;
19
+ const step = props.step ?? 1;
20
+ const defaultValue = props.defaultValue ?? [min];
21
+ const minStepsBetweenThumbs = props.minStepsBetweenThumbs ?? 0;
22
+ return {
23
+ dir: "ltr",
24
+ thumbAlignment: "contain",
25
+ origin: "start",
26
+ orientation: "horizontal",
27
+ thumbCollisionBehavior: "none",
28
+ minStepsBetweenThumbs,
29
+ ...props,
30
+ largeStep: props.largeStep ?? 10 * step,
31
+ defaultValue: normalize(defaultValue, min, max, step, minStepsBetweenThumbs),
32
+ value: props.value ? normalize(props.value, min, max, step, minStepsBetweenThumbs) : void 0,
33
+ max,
34
+ step,
35
+ min
36
+ };
37
+ },
38
+ initialState() {
39
+ return "idle";
40
+ },
41
+ context({ prop, bindable, getContext, getEvent }) {
42
+ return {
43
+ thumbSize: bindable(() => ({ defaultValue: toThumbSize(prop("thumbSize")) })),
44
+ value: bindable(() => ({
45
+ defaultValue: prop("defaultValue"),
46
+ value: prop("value"),
47
+ isEqual,
48
+ hash(a) {
49
+ return a.join(",");
50
+ },
51
+ onChange(value) {
52
+ prop("onValueChange")?.({
53
+ value,
54
+ reason: getEvent().src
55
+ });
56
+ }
57
+ })),
58
+ focusedIndex: bindable(() => ({
59
+ defaultValue: -1,
60
+ onChange(value) {
61
+ const ctx = getContext();
62
+ prop("onFocusChange")?.({
63
+ focusedIndex: value,
64
+ value: ctx.get("value")
65
+ });
66
+ }
67
+ })),
68
+ fieldsetDisabled: bindable(() => ({ defaultValue: false }))
69
+ };
70
+ },
71
+ refs() {
72
+ return {
73
+ thumbDragOffset: null,
74
+ startValue: null,
75
+ pointerReason: void 0,
76
+ focusFrame: AnimationFrame.create()
77
+ };
78
+ },
79
+ computed: {
80
+ isHorizontal: ({ prop }) => prop("orientation") === "horizontal",
81
+ isVertical: ({ prop }) => prop("orientation") === "vertical",
82
+ isRtl: ({ prop }) => prop("orientation") === "horizontal" && prop("dir") === "rtl",
83
+ isDisabled: ({ context, prop }) => !!prop("disabled") || context.get("fieldsetDisabled"),
84
+ isInteractive: ({ prop, computed }) => !(prop("readOnly") || computed("isDisabled")),
85
+ hasMeasuredThumbSize: ({ context }) => context.get("thumbSize") != null,
86
+ valuePercent: memo(({ context, prop }) => [
87
+ context.get("value"),
88
+ prop("min"),
89
+ prop("max")
90
+ ], ([value, min, max]) => value.map((value) => 100 * getValuePercent(value, min, max)))
91
+ },
92
+ watch({ track, action, context, computed, send }) {
93
+ track([() => context.hash("value")], () => {
94
+ action(["syncInputElements", "dispatchChangeEvent"]);
95
+ });
96
+ track([() => computed("isDisabled")], () => {
97
+ if (computed("isDisabled")) send({ type: "POINTER_CANCEL" });
98
+ });
99
+ },
100
+ effects: ["trackFormControlState", "trackThumbSize"],
101
+ exit: ["cancelFocusFrame"],
102
+ on: {
103
+ SET_VALUE: [{
104
+ guard: "hasIndex",
105
+ actions: [
106
+ "setStartValue",
107
+ "setValueAtIndex",
108
+ "invokeOnChangeEnd"
109
+ ]
110
+ }, { actions: [
111
+ "setStartValue",
112
+ "setValue",
113
+ "invokeOnChangeEnd"
114
+ ] }],
115
+ INCREMENT: { actions: [
116
+ "setStartValue",
117
+ "incrementThumbAtIndex",
118
+ "invokeOnChangeEnd"
119
+ ] },
120
+ DECREMENT: { actions: [
121
+ "setStartValue",
122
+ "decrementThumbAtIndex",
123
+ "invokeOnChangeEnd"
124
+ ] }
125
+ },
126
+ states: {
127
+ idle: { on: {
128
+ POINTER_DOWN: {
129
+ target: "dragging",
130
+ actions: [
131
+ "setClosestThumbIndex",
132
+ "setStartValue",
133
+ "setPointerValue",
134
+ "focusActiveThumb"
135
+ ]
136
+ },
137
+ FOCUS: {
138
+ target: "focus",
139
+ actions: ["setFocusedIndex"]
140
+ },
141
+ THUMB_POINTER_DOWN: {
142
+ target: "dragging",
143
+ actions: [
144
+ "setFocusedIndex",
145
+ "setThumbDragOffset",
146
+ "setStartValue",
147
+ "focusActiveThumb"
148
+ ]
149
+ }
150
+ } },
151
+ focus: {
152
+ entry: ["focusActiveThumb"],
153
+ on: {
154
+ POINTER_DOWN: {
155
+ target: "dragging",
156
+ actions: [
157
+ "setClosestThumbIndex",
158
+ "setStartValue",
159
+ "setPointerValue",
160
+ "focusActiveThumb"
161
+ ]
162
+ },
163
+ THUMB_POINTER_DOWN: {
164
+ target: "dragging",
165
+ actions: [
166
+ "setFocusedIndex",
167
+ "setThumbDragOffset",
168
+ "setStartValue",
169
+ "focusActiveThumb"
170
+ ]
171
+ },
172
+ ARROW_DEC: { actions: [
173
+ "setStartValue",
174
+ "decrementThumbAtIndex",
175
+ "invokeOnChangeEnd"
176
+ ] },
177
+ ARROW_INC: { actions: [
178
+ "setStartValue",
179
+ "incrementThumbAtIndex",
180
+ "invokeOnChangeEnd"
181
+ ] },
182
+ HOME: { actions: [
183
+ "setStartValue",
184
+ "setFocusedThumbToMin",
185
+ "invokeOnChangeEnd"
186
+ ] },
187
+ END: { actions: [
188
+ "setStartValue",
189
+ "setFocusedThumbToMax",
190
+ "invokeOnChangeEnd"
191
+ ] },
192
+ BLUR: {
193
+ target: "idle",
194
+ actions: ["clearFocusedIndex"]
195
+ }
196
+ }
197
+ },
198
+ dragging: {
199
+ entry: ["focusActiveThumb"],
200
+ effects: ["trackPointerMove"],
201
+ on: {
202
+ POINTER_UP: {
203
+ target: "focus",
204
+ actions: ["invokeOnChangeEnd", "clearDragState"]
205
+ },
206
+ POINTER_MOVE: { actions: ["setPointerValue"] },
207
+ POINTER_CANCEL: {
208
+ target: "idle",
209
+ actions: ["clearFocusedIndex", "clearDragState"]
210
+ }
211
+ }
212
+ }
213
+ },
214
+ implementations: {
215
+ guards: { hasIndex: ({ event }) => event.index != null },
216
+ effects: {
217
+ trackFormControlState({ context, scope }) {
218
+ return trackFormControl(getRootEl(scope), {
219
+ onFieldsetDisabledChange(disabled) {
220
+ context.set("fieldsetDisabled", disabled);
221
+ },
222
+ onFormReset() {
223
+ context.set("value", context.initial("value"));
224
+ }
225
+ });
226
+ },
227
+ trackPointerMove({ scope, send }) {
228
+ return trackPointerMove(scope.getDoc(), {
229
+ onPointerMove(info) {
230
+ send({
231
+ type: "POINTER_MOVE",
232
+ point: info.point,
233
+ src: "drag"
234
+ });
235
+ },
236
+ onPointerUp() {
237
+ send({ type: "POINTER_UP" });
238
+ }
239
+ });
240
+ },
241
+ trackThumbSize({ context, scope, prop }) {
242
+ if (prop("thumbAlignment") !== "contain" || prop("thumbSize")) return;
243
+ const exec = (el) => {
244
+ const size = {
245
+ width: el.offsetWidth,
246
+ height: el.offsetHeight
247
+ };
248
+ if (isEqual(context.get("thumbSize"), size)) return;
249
+ context.set("thumbSize", size);
250
+ };
251
+ const thumbEls = getThumbEls(scope);
252
+ thumbEls.forEach(exec);
253
+ const cleanups = thumbEls.map((el) => resizeObserverBorderBox.observe(el, () => exec(el)));
254
+ return callAll(...cleanups);
255
+ }
256
+ },
257
+ actions: {
258
+ dispatchChangeEvent({ context, scope }) {
259
+ dispatchChangeEvent(scope, context.get("value"));
260
+ },
261
+ syncInputElements({ context, scope }) {
262
+ context.get("value").forEach((value, index) => {
263
+ const inputEl = getHiddenInputEl(scope, index);
264
+ setElementValue(inputEl, value.toString());
265
+ });
266
+ },
267
+ invokeOnChangeEnd({ prop, context, refs, event }) {
268
+ const startValue = refs.get("startValue");
269
+ const reason = event.type === "POINTER_UP" ? refs.get("pointerReason") : event.src;
270
+ queueMicrotask(() => {
271
+ const value = context.get("value");
272
+ if (startValue && isEqual(startValue, value)) return;
273
+ prop("onValueChangeEnd")?.({
274
+ value,
275
+ reason
276
+ });
277
+ });
278
+ },
279
+ setStartValue({ refs, context }) {
280
+ refs.set("startValue", context.get("value").slice());
281
+ },
282
+ clearDragState({ refs }) {
283
+ refs.set("thumbDragOffset", null);
284
+ refs.set("pointerReason", void 0);
285
+ },
286
+ setClosestThumbIndex(params) {
287
+ const { context, event } = params;
288
+ const pointValue = getPointValue(params, event.point);
289
+ if (pointValue == null) return;
290
+ const focusedIndex = getClosestIndex(params, pointValue, getDisabledThumbIndices(params.scope));
291
+ context.set("focusedIndex", focusedIndex);
292
+ },
293
+ setFocusedIndex(params) {
294
+ const { context, event } = params;
295
+ const movableIndex = selectMovableThumb(params, event.index, getDisabledThumbIndices(params.scope));
296
+ context.set("focusedIndex", movableIndex);
297
+ },
298
+ clearFocusedIndex({ context }) {
299
+ context.set("focusedIndex", -1);
300
+ },
301
+ setThumbDragOffset(params) {
302
+ const { refs, event } = params;
303
+ refs.set("thumbDragOffset", event.offset ?? null);
304
+ },
305
+ setPointerValue(params) {
306
+ queueMicrotask(() => {
307
+ const { context, event, prop, refs, scope } = params;
308
+ const pointValue = getPointValue(params, event.point);
309
+ if (pointValue == null) return;
310
+ let focusedIndex = context.get("focusedIndex");
311
+ if (focusedIndex < 0) return;
312
+ const startValues = refs.get("startValue");
313
+ const disabledIndices = getDisabledThumbIndices(scope);
314
+ refs.set("pointerReason", event.src);
315
+ let stackedIndex;
316
+ if (prop("thumbCollisionBehavior") === "none") {
317
+ const index = resolveStackedThumb(context.get("value"), focusedIndex, pointValue, disabledIndices);
318
+ if (index !== focusedIndex) {
319
+ focusedIndex = index;
320
+ stackedIndex = index;
321
+ context.set("focusedIndex", index);
322
+ }
323
+ }
324
+ const result = resolveThumbCollision(prop("thumbCollisionBehavior"), focusedIndex, pointValue, context.get("value"), prop("min"), prop("max"), prop("step"), prop("minStepsBetweenThumbs"), startValues?.[focusedIndex], disabledIndices);
325
+ if (result.swapped) context.set("focusedIndex", result.index);
326
+ context.set("value", result.values);
327
+ if (stackedIndex != null) getThumbEl(scope, stackedIndex)?.focus({ preventScroll: true });
328
+ });
329
+ },
330
+ focusActiveThumb({ scope, context, refs }) {
331
+ refs.get("focusFrame").request(() => {
332
+ getThumbEl(scope, context.get("focusedIndex"))?.focus({ preventScroll: true });
333
+ });
334
+ },
335
+ cancelFocusFrame({ refs }) {
336
+ refs.get("focusFrame").cancel();
337
+ },
338
+ decrementThumbAtIndex(params) {
339
+ const { context, event, prop } = params;
340
+ context.set("value", stepThumb(params, -(event.step ?? prop("step")), event.index));
341
+ },
342
+ incrementThumbAtIndex(params) {
343
+ const { context, event, prop } = params;
344
+ context.set("value", stepThumb(params, event.step ?? prop("step"), event.index));
345
+ },
346
+ setFocusedThumbToMin(params) {
347
+ const { context } = params;
348
+ const index = context.get("focusedIndex");
349
+ const { min } = getRangeAtIndex(params, index);
350
+ context.set("value", (prev) => setValueAtIndex(prev, index, min));
351
+ },
352
+ setFocusedThumbToMax(params) {
353
+ const { context } = params;
354
+ const index = context.get("focusedIndex");
355
+ const { max } = getRangeAtIndex(params, index);
356
+ context.set("value", (prev) => setValueAtIndex(prev, index, max));
357
+ },
358
+ setValueAtIndex(params) {
359
+ const { context, event } = params;
360
+ const value = constrainValue(params, event.value, event.index);
361
+ context.set("value", (prev) => setValueAtIndex(prev, event.index, value));
362
+ },
363
+ setValue(params) {
364
+ const { context, event } = params;
365
+ const value = normalizeValues(params, event.value);
366
+ context.set("value", value);
367
+ }
368
+ }
369
+ }
352
370
  });
353
- export {
354
- machine
355
- };
371
+ export { machine };