@zag-js/slider 1.34.0 → 1.35.0

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