@zag-js/slider 0.2.12 → 0.2.14

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,460 +0,0 @@
1
- import {
2
- hasProp,
3
- isIos,
4
- isLeftClick,
5
- isMouseEvent,
6
- isObject,
7
- isTouchEvent,
8
- supportsMouseEvent,
9
- supportsPointerEvent,
10
- supportsTouchEvent
11
- } from "./chunk-3UP6QL6A.mjs";
12
- import {
13
- dom
14
- } from "./chunk-55KEN77D.mjs";
15
- import {
16
- constrainValue,
17
- decrement,
18
- increment
19
- } from "./chunk-YGFSMEUO.mjs";
20
-
21
- // src/slider.machine.ts
22
- import { createMachine } from "@zag-js/core";
23
-
24
- // ../../utilities/core/src/functions.ts
25
- var runIfFn = (v, ...a) => {
26
- const res = typeof v === "function" ? v(...a) : v;
27
- return res ?? void 0;
28
- };
29
- var callAll = (...fns) => (...a) => {
30
- fns.forEach(function(fn) {
31
- fn?.(...a);
32
- });
33
- };
34
-
35
- // ../../utilities/core/src/object.ts
36
- function compact(obj) {
37
- if (obj === void 0)
38
- return obj;
39
- return Object.fromEntries(
40
- Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
41
- );
42
- }
43
-
44
- // ../../utilities/dom/src/listener.ts
45
- var isRef = (v) => hasProp(v, "current");
46
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
47
- function extractInfo(event, type = "page") {
48
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
49
- return {
50
- point: {
51
- x: point[`${type}X`],
52
- y: point[`${type}Y`]
53
- }
54
- };
55
- }
56
- function addDomEvent(target, eventName, handler, options) {
57
- const node = isRef(target) ? target.current : runIfFn(target);
58
- node?.addEventListener(eventName, handler, options);
59
- return () => {
60
- node?.removeEventListener(eventName, handler, options);
61
- };
62
- }
63
- function addPointerEvent(target, event, listener, options) {
64
- const type = getEventName(event) ?? event;
65
- return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
66
- }
67
- function wrapHandler(fn, filter = false) {
68
- const listener = (event) => {
69
- fn(event, extractInfo(event));
70
- };
71
- return filter ? filterPrimaryPointer(listener) : listener;
72
- }
73
- function filterPrimaryPointer(fn) {
74
- return (event) => {
75
- const win = event.view ?? window;
76
- const isMouseEvent2 = event instanceof win.MouseEvent;
77
- const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
78
- if (isPrimary)
79
- fn(event);
80
- };
81
- }
82
- var mouseEventNames = {
83
- pointerdown: "mousedown",
84
- pointermove: "mousemove",
85
- pointerup: "mouseup",
86
- pointercancel: "mousecancel",
87
- pointerover: "mouseover",
88
- pointerout: "mouseout",
89
- pointerenter: "mouseenter",
90
- pointerleave: "mouseleave"
91
- };
92
- var touchEventNames = {
93
- pointerdown: "touchstart",
94
- pointermove: "touchmove",
95
- pointerup: "touchend",
96
- pointercancel: "touchcancel"
97
- };
98
- function getEventName(evt) {
99
- if (supportsPointerEvent())
100
- return evt;
101
- if (supportsTouchEvent())
102
- return touchEventNames[evt];
103
- if (supportsMouseEvent())
104
- return mouseEventNames[evt];
105
- return evt;
106
- }
107
-
108
- // ../../utilities/dom/src/mutation-observer.ts
109
- function observeAttributes(node, attributes, fn) {
110
- if (!node)
111
- return;
112
- const attrs = Array.isArray(attributes) ? attributes : [attributes];
113
- const win = node.ownerDocument.defaultView || window;
114
- const obs = new win.MutationObserver((changes) => {
115
- for (const change of changes) {
116
- if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
117
- fn(change);
118
- }
119
- }
120
- });
121
- obs.observe(node, { attributes: true, attributeFilter: attrs });
122
- return () => obs.disconnect();
123
- }
124
-
125
- // ../../utilities/dom/src/next-tick.ts
126
- function nextTick(fn) {
127
- const set = /* @__PURE__ */ new Set();
128
- function raf2(fn2) {
129
- const id = globalThis.requestAnimationFrame(fn2);
130
- set.add(() => globalThis.cancelAnimationFrame(id));
131
- }
132
- raf2(() => raf2(fn));
133
- return function cleanup() {
134
- set.forEach(function(fn2) {
135
- fn2();
136
- });
137
- };
138
- }
139
- function raf(fn) {
140
- const id = globalThis.requestAnimationFrame(fn);
141
- return function cleanup() {
142
- globalThis.cancelAnimationFrame(id);
143
- };
144
- }
145
-
146
- // ../../utilities/dom/src/text-selection.ts
147
- var state = "default";
148
- var savedUserSelect = "";
149
- var modifiedElementMap = /* @__PURE__ */ new WeakMap();
150
- function disableTextSelection({ target, doc } = {}) {
151
- const _document = doc ?? document;
152
- if (isIos()) {
153
- if (state === "default") {
154
- savedUserSelect = _document.documentElement.style.webkitUserSelect;
155
- _document.documentElement.style.webkitUserSelect = "none";
156
- }
157
- state = "disabled";
158
- } else if (target) {
159
- modifiedElementMap.set(target, target.style.userSelect);
160
- target.style.userSelect = "none";
161
- }
162
- return () => restoreTextSelection({ target, doc: _document });
163
- }
164
- function restoreTextSelection({ target, doc } = {}) {
165
- const _document = doc ?? document;
166
- if (isIos()) {
167
- if (state !== "disabled")
168
- return;
169
- state = "restoring";
170
- setTimeout(() => {
171
- nextTick(() => {
172
- if (state === "restoring") {
173
- if (_document.documentElement.style.webkitUserSelect === "none") {
174
- _document.documentElement.style.webkitUserSelect = savedUserSelect || "";
175
- }
176
- savedUserSelect = "";
177
- state = "default";
178
- }
179
- });
180
- }, 300);
181
- } else {
182
- if (target && modifiedElementMap.has(target)) {
183
- let targetOldUserSelect = modifiedElementMap.get(target);
184
- if (target.style.userSelect === "none") {
185
- target.style.userSelect = targetOldUserSelect ?? "";
186
- }
187
- if (target.getAttribute("style") === "") {
188
- target.removeAttribute("style");
189
- }
190
- modifiedElementMap.delete(target);
191
- }
192
- }
193
- }
194
-
195
- // ../../utilities/dom/src/pointer-event.ts
196
- var THRESHOLD = 5;
197
- function trackPointerMove(doc, opts) {
198
- const { onPointerMove, onPointerUp } = opts;
199
- const handlePointerMove = (event, info) => {
200
- const { point: p } = info;
201
- const distance = Math.sqrt(p.x ** 2 + p.y ** 2);
202
- if (distance < THRESHOLD)
203
- return;
204
- if (isMouseEvent(event) && isLeftClick(event)) {
205
- onPointerUp();
206
- return;
207
- }
208
- onPointerMove(info, event);
209
- };
210
- return callAll(
211
- addPointerEvent(doc, "pointermove", handlePointerMove, false),
212
- addPointerEvent(doc, "pointerup", onPointerUp, false),
213
- addPointerEvent(doc, "pointercancel", onPointerUp, false),
214
- addPointerEvent(doc, "contextmenu", onPointerUp, false),
215
- disableTextSelection({ doc })
216
- );
217
- }
218
-
219
- // src/slider.machine.ts
220
- import { trackElementSize } from "@zag-js/element-size";
221
-
222
- // ../../utilities/form-utils/src/form.ts
223
- function getClosestForm(el) {
224
- if (isFormElement(el))
225
- return el.form;
226
- else
227
- return el.closest("form");
228
- }
229
- function isFormElement(el) {
230
- return el.matches("textarea, input, select, button");
231
- }
232
- function trackFormReset(el, callback) {
233
- if (!el)
234
- return;
235
- const form = getClosestForm(el);
236
- form?.addEventListener("reset", callback, { passive: true });
237
- return () => {
238
- form?.removeEventListener("reset", callback);
239
- };
240
- }
241
- function trackFieldsetDisabled(el, callback) {
242
- const fieldset = el?.closest("fieldset");
243
- if (!fieldset)
244
- return;
245
- callback(fieldset.disabled);
246
- return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
247
- }
248
- function trackFormControl(el, options) {
249
- if (!el)
250
- return;
251
- const { onFieldsetDisabled, onFormReset } = options;
252
- const cleanups = [
253
- trackFormReset(el, onFormReset),
254
- trackFieldsetDisabled(el, (disabled) => {
255
- if (disabled)
256
- onFieldsetDisabled();
257
- })
258
- ];
259
- return () => {
260
- cleanups.forEach((cleanup) => cleanup?.());
261
- };
262
- }
263
-
264
- // src/slider.machine.ts
265
- import { clampValue, getValuePercent } from "@zag-js/numeric-range";
266
- function machine(userContext) {
267
- const ctx = compact(userContext);
268
- return createMachine(
269
- {
270
- id: "slider",
271
- initial: "idle",
272
- context: {
273
- thumbSize: null,
274
- thumbAlignment: "contain",
275
- disabled: false,
276
- threshold: 5,
277
- dir: "ltr",
278
- origin: "start",
279
- orientation: "horizontal",
280
- initialValue: null,
281
- value: 0,
282
- step: 1,
283
- min: 0,
284
- max: 100,
285
- ...ctx
286
- },
287
- computed: {
288
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
289
- isVertical: (ctx2) => ctx2.orientation === "vertical",
290
- isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
291
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly),
292
- hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null,
293
- valuePercent: (ctx2) => 100 * getValuePercent(ctx2.value, ctx2.min, ctx2.max)
294
- },
295
- watch: {
296
- value: ["invokeOnChange", "dispatchChangeEvent"]
297
- },
298
- activities: ["trackFormControlState", "trackThumbSize"],
299
- on: {
300
- SET_VALUE: {
301
- actions: "setValue"
302
- },
303
- INCREMENT: {
304
- actions: "increment"
305
- },
306
- DECREMENT: {
307
- actions: "decrement"
308
- }
309
- },
310
- entry: ["checkValue"],
311
- states: {
312
- idle: {
313
- on: {
314
- POINTER_DOWN: {
315
- target: "dragging",
316
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
317
- },
318
- FOCUS: "focus"
319
- }
320
- },
321
- focus: {
322
- entry: "focusThumb",
323
- on: {
324
- POINTER_DOWN: {
325
- target: "dragging",
326
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
327
- },
328
- ARROW_LEFT: {
329
- guard: "isHorizontal",
330
- actions: "decrement"
331
- },
332
- ARROW_RIGHT: {
333
- guard: "isHorizontal",
334
- actions: "increment"
335
- },
336
- ARROW_UP: {
337
- guard: "isVertical",
338
- actions: "increment"
339
- },
340
- ARROW_DOWN: {
341
- guard: "isVertical",
342
- actions: "decrement"
343
- },
344
- PAGE_UP: {
345
- actions: "increment"
346
- },
347
- PAGE_DOWN: {
348
- actions: "decrement"
349
- },
350
- HOME: {
351
- actions: "setToMin"
352
- },
353
- END: {
354
- actions: "setToMax"
355
- },
356
- BLUR: "idle"
357
- }
358
- },
359
- dragging: {
360
- entry: "focusThumb",
361
- activities: "trackPointerMove",
362
- on: {
363
- POINTER_UP: {
364
- target: "focus",
365
- actions: "invokeOnChangeEnd"
366
- },
367
- POINTER_MOVE: {
368
- actions: "setPointerValue"
369
- }
370
- }
371
- }
372
- }
373
- },
374
- {
375
- guards: {
376
- isHorizontal: (ctx2) => ctx2.isHorizontal,
377
- isVertical: (ctx2) => ctx2.isVertical
378
- },
379
- activities: {
380
- trackFormControlState(ctx2) {
381
- return trackFormControl(dom.getHiddenInputEl(ctx2), {
382
- onFieldsetDisabled() {
383
- ctx2.disabled = true;
384
- },
385
- onFormReset() {
386
- if (ctx2.initialValue != null) {
387
- ctx2.value = ctx2.initialValue;
388
- }
389
- }
390
- });
391
- },
392
- trackPointerMove(ctx2, _evt, { send }) {
393
- return trackPointerMove(dom.getDoc(ctx2), {
394
- onPointerMove(info) {
395
- send({ type: "POINTER_MOVE", point: info.point });
396
- },
397
- onPointerUp() {
398
- send("POINTER_UP");
399
- }
400
- });
401
- },
402
- trackThumbSize(ctx2, _evt) {
403
- if (ctx2.thumbAlignment !== "contain")
404
- return;
405
- return trackElementSize(dom.getThumbEl(ctx2), (size) => {
406
- if (size)
407
- ctx2.thumbSize = size;
408
- });
409
- }
410
- },
411
- actions: {
412
- checkValue(ctx2) {
413
- const value = constrainValue(ctx2, ctx2.value);
414
- ctx2.value = value;
415
- ctx2.initialValue = value;
416
- },
417
- invokeOnChangeStart(ctx2) {
418
- ctx2.onChangeStart?.({ value: ctx2.value });
419
- },
420
- invokeOnChangeEnd(ctx2) {
421
- ctx2.onChangeEnd?.({ value: ctx2.value });
422
- },
423
- invokeOnChange(ctx2) {
424
- ctx2.onChange?.({ value: ctx2.value });
425
- },
426
- dispatchChangeEvent(ctx2) {
427
- dom.dispatchChangeEvent(ctx2);
428
- },
429
- setPointerValue(ctx2, evt) {
430
- const value = dom.getValueFromPoint(ctx2, evt.point);
431
- if (value == null)
432
- return;
433
- ctx2.value = clampValue(value, ctx2.min, ctx2.max);
434
- },
435
- focusThumb(ctx2) {
436
- raf(() => dom.getThumbEl(ctx2)?.focus());
437
- },
438
- decrement(ctx2, evt) {
439
- ctx2.value = decrement(ctx2, evt.step);
440
- },
441
- increment(ctx2, evt) {
442
- ctx2.value = increment(ctx2, evt.step);
443
- },
444
- setToMin(ctx2) {
445
- ctx2.value = ctx2.min;
446
- },
447
- setToMax(ctx2) {
448
- ctx2.value = ctx2.max;
449
- },
450
- setValue(ctx2, evt) {
451
- ctx2.value = constrainValue(ctx2, evt.value);
452
- }
453
- }
454
- }
455
- );
456
- }
457
-
458
- export {
459
- machine
460
- };