@zag-js/slider 0.2.6 → 0.2.7

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,469 @@
1
+ import {
2
+ hasProp,
3
+ isIos,
4
+ isLeftClick,
5
+ isMouseEvent,
6
+ isObject,
7
+ isTouchEvent,
8
+ supportsMouseEvent,
9
+ supportsPointerEvent,
10
+ supportsTouchEvent
11
+ } from "./chunk-SGCWELVB.mjs";
12
+ import {
13
+ dom
14
+ } from "./chunk-MR2MUD77.mjs";
15
+ import {
16
+ utils
17
+ } from "./chunk-FUTBDWTA.mjs";
18
+
19
+ // src/slider.machine.ts
20
+ import { createMachine } from "@zag-js/core";
21
+
22
+ // ../../utilities/core/src/functions.ts
23
+ var runIfFn = (v, ...a) => {
24
+ const res = typeof v === "function" ? v(...a) : v;
25
+ return res != null ? res : void 0;
26
+ };
27
+ var callAll = (...fns) => (...a) => {
28
+ fns.forEach(function(fn) {
29
+ fn == null ? void 0 : fn(...a);
30
+ });
31
+ };
32
+
33
+ // ../../utilities/core/src/object.ts
34
+ function compact(obj) {
35
+ if (obj === void 0)
36
+ return obj;
37
+ return Object.fromEntries(
38
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
39
+ );
40
+ }
41
+
42
+ // ../../utilities/dom/src/listener.ts
43
+ var isRef = (v) => hasProp(v, "current");
44
+ var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
45
+ function extractInfo(event, type = "page") {
46
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
47
+ return {
48
+ point: {
49
+ x: point[`${type}X`],
50
+ y: point[`${type}Y`]
51
+ }
52
+ };
53
+ }
54
+ function addDomEvent(target, eventName, handler, options) {
55
+ const node = isRef(target) ? target.current : runIfFn(target);
56
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
57
+ return () => {
58
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
59
+ };
60
+ }
61
+ function addPointerEvent(target, event, listener, options) {
62
+ var _a;
63
+ const type = (_a = getEventName(event)) != null ? _a : event;
64
+ return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
65
+ }
66
+ function wrapHandler(fn, filter = false) {
67
+ const listener = (event) => {
68
+ fn(event, extractInfo(event));
69
+ };
70
+ return filter ? filterPrimaryPointer(listener) : listener;
71
+ }
72
+ function filterPrimaryPointer(fn) {
73
+ return (event) => {
74
+ var _a;
75
+ const win = (_a = event.view) != null ? _a : 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 != null ? 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 != null ? 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 != null ? 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
+ // ../../utilities/form-utils/src/form.ts
220
+ function getClosestForm(el) {
221
+ if (isFormElement(el))
222
+ return el.form;
223
+ else
224
+ return el.closest("form");
225
+ }
226
+ function isFormElement(el) {
227
+ return el.matches("textarea, input, select, button");
228
+ }
229
+ function trackFormReset(el, callback) {
230
+ if (!el)
231
+ return;
232
+ const form = getClosestForm(el);
233
+ form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
234
+ return () => {
235
+ form == null ? void 0 : form.removeEventListener("reset", callback);
236
+ };
237
+ }
238
+ function trackFieldsetDisabled(el, callback) {
239
+ const fieldset = el == null ? void 0 : el.closest("fieldset");
240
+ if (!fieldset)
241
+ return;
242
+ callback(fieldset.disabled);
243
+ return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
244
+ }
245
+ function trackFormControl(el, options) {
246
+ if (!el)
247
+ return;
248
+ const { onFieldsetDisabled, onFormReset } = options;
249
+ const cleanups = [
250
+ trackFormReset(el, onFormReset),
251
+ trackFieldsetDisabled(el, (disabled) => {
252
+ if (disabled)
253
+ onFieldsetDisabled();
254
+ })
255
+ ];
256
+ return () => {
257
+ cleanups.forEach((cleanup) => cleanup == null ? void 0 : cleanup());
258
+ };
259
+ }
260
+
261
+ // src/slider.machine.ts
262
+ import { trackElementSize } from "@zag-js/element-size";
263
+ function machine(userContext) {
264
+ const ctx = compact(userContext);
265
+ return createMachine(
266
+ {
267
+ id: "slider",
268
+ initial: "unknown",
269
+ context: {
270
+ thumbSize: null,
271
+ thumbAlignment: "contain",
272
+ disabled: false,
273
+ threshold: 5,
274
+ dir: "ltr",
275
+ origin: "start",
276
+ orientation: "horizontal",
277
+ initialValue: null,
278
+ value: 0,
279
+ step: 1,
280
+ min: 0,
281
+ max: 100,
282
+ ...ctx
283
+ },
284
+ computed: {
285
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
286
+ isVertical: (ctx2) => ctx2.orientation === "vertical",
287
+ isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
288
+ isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readOnly),
289
+ hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null
290
+ },
291
+ watch: {
292
+ value: ["invokeOnChange", "dispatchChangeEvent"]
293
+ },
294
+ activities: ["trackFormControlState", "trackThumbSize"],
295
+ on: {
296
+ SET_VALUE: {
297
+ actions: "setValue"
298
+ },
299
+ INCREMENT: {
300
+ actions: "increment"
301
+ },
302
+ DECREMENT: {
303
+ actions: "decrement"
304
+ }
305
+ },
306
+ states: {
307
+ unknown: {
308
+ on: {
309
+ SETUP: {
310
+ target: "idle",
311
+ actions: ["checkValue"]
312
+ }
313
+ }
314
+ },
315
+ idle: {
316
+ on: {
317
+ POINTER_DOWN: {
318
+ target: "dragging",
319
+ actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
320
+ },
321
+ FOCUS: "focus"
322
+ }
323
+ },
324
+ focus: {
325
+ entry: "focusThumb",
326
+ on: {
327
+ POINTER_DOWN: {
328
+ target: "dragging",
329
+ actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
330
+ },
331
+ ARROW_LEFT: {
332
+ guard: "isHorizontal",
333
+ actions: "decrement"
334
+ },
335
+ ARROW_RIGHT: {
336
+ guard: "isHorizontal",
337
+ actions: "increment"
338
+ },
339
+ ARROW_UP: {
340
+ guard: "isVertical",
341
+ actions: "increment"
342
+ },
343
+ ARROW_DOWN: {
344
+ guard: "isVertical",
345
+ actions: "decrement"
346
+ },
347
+ PAGE_UP: {
348
+ actions: "increment"
349
+ },
350
+ PAGE_DOWN: {
351
+ actions: "decrement"
352
+ },
353
+ HOME: {
354
+ actions: "setToMin"
355
+ },
356
+ END: {
357
+ actions: "setToMax"
358
+ },
359
+ BLUR: "idle"
360
+ }
361
+ },
362
+ dragging: {
363
+ entry: "focusThumb",
364
+ activities: "trackPointerMove",
365
+ on: {
366
+ POINTER_UP: {
367
+ target: "focus",
368
+ actions: "invokeOnChangeEnd"
369
+ },
370
+ POINTER_MOVE: {
371
+ actions: "setPointerValue"
372
+ }
373
+ }
374
+ }
375
+ }
376
+ },
377
+ {
378
+ guards: {
379
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
380
+ isVertical: (ctx2) => ctx2.isVertical
381
+ },
382
+ activities: {
383
+ trackFormControlState(ctx2) {
384
+ return trackFormControl(dom.getHiddenInputEl(ctx2), {
385
+ onFieldsetDisabled() {
386
+ ctx2.disabled = true;
387
+ },
388
+ onFormReset() {
389
+ if (ctx2.initialValue != null) {
390
+ ctx2.value = ctx2.initialValue;
391
+ }
392
+ }
393
+ });
394
+ },
395
+ trackPointerMove(ctx2, _evt, { send }) {
396
+ return trackPointerMove(dom.getDoc(ctx2), {
397
+ onPointerMove(info) {
398
+ send({ type: "POINTER_MOVE", point: info.point });
399
+ },
400
+ onPointerUp() {
401
+ send("POINTER_UP");
402
+ }
403
+ });
404
+ },
405
+ trackThumbSize(ctx2, _evt) {
406
+ if (ctx2.thumbAlignment !== "contain")
407
+ return;
408
+ return trackElementSize(dom.getThumbEl(ctx2), (size) => {
409
+ if (size)
410
+ ctx2.thumbSize = size;
411
+ });
412
+ }
413
+ },
414
+ actions: {
415
+ checkValue(ctx2) {
416
+ const value = utils.convert(ctx2, ctx2.value);
417
+ ctx2.value = value;
418
+ ctx2.initialValue = value;
419
+ },
420
+ invokeOnChangeStart(ctx2) {
421
+ var _a;
422
+ (_a = ctx2.onChangeStart) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
423
+ },
424
+ invokeOnChangeEnd(ctx2) {
425
+ var _a;
426
+ (_a = ctx2.onChangeEnd) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
427
+ },
428
+ invokeOnChange(ctx2) {
429
+ var _a;
430
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
431
+ },
432
+ dispatchChangeEvent(ctx2) {
433
+ dom.dispatchChangeEvent(ctx2);
434
+ },
435
+ setPointerValue(ctx2, evt) {
436
+ const value = dom.getValueFromPoint(ctx2, evt.point);
437
+ if (value == null)
438
+ return;
439
+ ctx2.value = utils.clamp(ctx2, value);
440
+ },
441
+ focusThumb(ctx2) {
442
+ raf(() => {
443
+ var _a;
444
+ return (_a = dom.getThumbEl(ctx2)) == null ? void 0 : _a.focus();
445
+ });
446
+ },
447
+ decrement(ctx2, evt) {
448
+ ctx2.value = utils.decrement(ctx2, evt.step);
449
+ },
450
+ increment(ctx2, evt) {
451
+ ctx2.value = utils.increment(ctx2, evt.step);
452
+ },
453
+ setToMin(ctx2) {
454
+ ctx2.value = ctx2.min;
455
+ },
456
+ setToMax(ctx2) {
457
+ ctx2.value = ctx2.max;
458
+ },
459
+ setValue(ctx2, evt) {
460
+ ctx2.value = utils.convert(ctx2, evt.value);
461
+ }
462
+ }
463
+ }
464
+ );
465
+ }
466
+
467
+ export {
468
+ machine
469
+ };
@@ -0,0 +1,20 @@
1
+ // src/slider.anatomy.ts
2
+ import { createAnatomy } from "@zag-js/anatomy";
3
+ var anatomy = createAnatomy("slider").parts(
4
+ "root",
5
+ "label",
6
+ "thumb",
7
+ "hiddenInput",
8
+ "output",
9
+ "track",
10
+ "range",
11
+ "control",
12
+ "markerGroup",
13
+ "marker"
14
+ );
15
+ var parts = anatomy.build();
16
+
17
+ export {
18
+ anatomy,
19
+ parts
20
+ };
@@ -0,0 +1,33 @@
1
+ import {
2
+ clamp,
3
+ decrement,
4
+ increment,
5
+ percentToValue,
6
+ snapToStep
7
+ } from "./chunk-GBYBRQZL.mjs";
8
+
9
+ // src/slider.utils.ts
10
+ var utils = {
11
+ fromPercent(ctx, percent) {
12
+ percent = clamp(percent, { min: 0, max: 1 });
13
+ return parseFloat(snapToStep(percentToValue(percent, ctx), ctx.step));
14
+ },
15
+ clamp(ctx, value) {
16
+ return clamp(value, ctx);
17
+ },
18
+ convert(ctx, value) {
19
+ return clamp(parseFloat(snapToStep(value, ctx.step)), ctx);
20
+ },
21
+ decrement(ctx, step) {
22
+ let value = decrement(ctx.value, step != null ? step : ctx.step);
23
+ return utils.convert(ctx, value);
24
+ },
25
+ increment(ctx, step) {
26
+ let value = increment(ctx.value, step != null ? step : ctx.step);
27
+ return utils.convert(ctx, value);
28
+ }
29
+ };
30
+
31
+ export {
32
+ utils
33
+ };
@@ -0,0 +1,56 @@
1
+ // ../../utilities/number/src/number.ts
2
+ function round(v, t) {
3
+ let num = valueOf(v);
4
+ const p = 10 ** (t != null ? t : 10);
5
+ num = Math.round(num * p) / p;
6
+ return t ? num.toFixed(t) : v.toString();
7
+ }
8
+ var valueToPercent = (v, r) => (valueOf(v) - r.min) * 100 / (r.max - r.min);
9
+ var percentToValue = (v, r) => r.min + (r.max - r.min) * valueOf(v);
10
+ function clamp(v, o) {
11
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
12
+ }
13
+ function countDecimals(value) {
14
+ if (!Number.isFinite(value))
15
+ return 0;
16
+ let e = 1, p = 0;
17
+ while (Math.round(value * e) / e !== value) {
18
+ e *= 10;
19
+ p += 1;
20
+ }
21
+ return p;
22
+ }
23
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
24
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
25
+ function snapToStep(value, step) {
26
+ const num = valueOf(value);
27
+ const p = countDecimals(step);
28
+ const v = Math.round(num / step) * step;
29
+ return round(v, p);
30
+ }
31
+ function valueOf(v) {
32
+ if (typeof v === "number")
33
+ return v;
34
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
35
+ return !Number.isNaN(num) ? num : 0;
36
+ }
37
+ function decimalOperation(a, op, b) {
38
+ let result = op === "+" ? a + b : a - b;
39
+ if (a % 1 !== 0 || b % 1 !== 0) {
40
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
41
+ a = Math.round(a * multiplier);
42
+ b = Math.round(b * multiplier);
43
+ result = op === "+" ? a + b : a - b;
44
+ result /= multiplier;
45
+ }
46
+ return result;
47
+ }
48
+
49
+ export {
50
+ valueToPercent,
51
+ percentToValue,
52
+ clamp,
53
+ increment,
54
+ decrement,
55
+ snapToStep
56
+ };