@zag-js/slider 0.1.14 → 0.1.15

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.
Files changed (3) hide show
  1. package/dist/index.js +10 -39
  2. package/package.json +10 -10
  3. package/dist/index.mjs +0 -1019
package/dist/index.js CHANGED
@@ -1,32 +1,4 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var src_exports = {};
22
- __export(src_exports, {
23
- connect: () => connect,
24
- machine: () => machine,
25
- unstable__dom: () => dom
26
- });
27
- module.exports = __toCommonJS(src_exports);
28
-
29
- // ../../utilities/dom/dist/index.mjs
1
+ // ../../utilities/dom/dist/index.js
30
2
  var dataAttr = (guard) => {
31
3
  return guard ? "" : void 0;
32
4
  };
@@ -314,7 +286,7 @@ function trackPointerMove(doc, opts) {
314
286
  );
315
287
  }
316
288
 
317
- // ../../utilities/form-utils/dist/index.mjs
289
+ // ../../utilities/form-utils/dist/index.js
318
290
  function getWindow(el) {
319
291
  return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
320
292
  }
@@ -376,7 +348,7 @@ function trackFieldsetDisabled(el, callback) {
376
348
  return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
377
349
  }
378
350
 
379
- // ../../utilities/number/dist/index.mjs
351
+ // ../../utilities/number/dist/index.js
380
352
  function round(v, t) {
381
353
  let num = valueOf(v);
382
354
  const p = 10 ** (t ?? 10);
@@ -834,10 +806,10 @@ function connect(state2, send, normalize) {
834
806
  }
835
807
 
836
808
  // src/slider.machine.ts
837
- var import_core = require("@zag-js/core");
838
- var import_element_size = require("@zag-js/element-size");
809
+ import { createMachine } from "@zag-js/core";
810
+ import { trackElementSize } from "@zag-js/element-size";
839
811
  function machine(ctx) {
840
- return (0, import_core.createMachine)(
812
+ return createMachine(
841
813
  {
842
814
  id: "slider",
843
815
  initial: "unknown",
@@ -982,7 +954,7 @@ function machine(ctx) {
982
954
  trackThumbSize(ctx2, _evt) {
983
955
  if (ctx2.thumbAlignment !== "contain")
984
956
  return;
985
- return (0, import_element_size.trackElementSize)(dom.getThumbEl(ctx2), (size) => {
957
+ return trackElementSize(dom.getThumbEl(ctx2), (size) => {
986
958
  if (size)
987
959
  ctx2.thumbSize = size;
988
960
  });
@@ -1040,9 +1012,8 @@ function machine(ctx) {
1040
1012
  }
1041
1013
  );
1042
1014
  }
1043
- // Annotate the CommonJS export names for ESM import in node:
1044
- 0 && (module.exports = {
1015
+ export {
1045
1016
  connect,
1046
1017
  machine,
1047
- unstable__dom
1048
- });
1018
+ dom as unstable__dom
1019
+ };
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
+ "type": "module",
2
3
  "name": "@zag-js/slider",
3
- "version": "0.1.14",
4
+ "version": "0.1.15",
4
5
  "description": "Core logic for the slider widget implemented as a state machine",
5
6
  "keywords": [
6
7
  "js",
@@ -16,7 +17,6 @@
16
17
  "license": "MIT",
17
18
  "main": "dist/index.js",
18
19
  "types": "dist/index.d.ts",
19
- "module": "dist/index.mjs",
20
20
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/slider",
21
21
  "sideEffects": false,
22
22
  "files": [
@@ -29,19 +29,19 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.10",
33
- "@zag-js/element-size": "0.2.1",
34
- "@zag-js/types": "0.2.5"
32
+ "@zag-js/core": "0.1.11",
33
+ "@zag-js/element-size": "0.2.2",
34
+ "@zag-js/types": "0.2.6"
35
35
  },
36
36
  "devDependencies": {
37
- "@zag-js/dom-utils": "0.1.11",
38
- "@zag-js/form-utils": "0.1.1",
39
- "@zag-js/number-utils": "0.1.4"
37
+ "@zag-js/dom-utils": "0.1.12",
38
+ "@zag-js/form-utils": "0.1.2",
39
+ "@zag-js/number-utils": "0.1.5"
40
40
  },
41
41
  "scripts": {
42
- "build-fast": "tsup src/index.ts --format=esm,cjs",
42
+ "build-fast": "tsup src/index.ts --format=esm",
43
43
  "start": "pnpm build --watch",
44
- "build": "tsup src/index.ts --format=esm,cjs --dts",
44
+ "build": "tsup src/index.ts --format=esm --dts",
45
45
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
46
46
  "lint": "eslint src --ext .ts,.tsx",
47
47
  "test-ci": "pnpm test --ci --runInBand",
package/dist/index.mjs DELETED
@@ -1,1019 +0,0 @@
1
- // ../../utilities/dom/dist/index.mjs
2
- var dataAttr = (guard) => {
3
- return guard ? "" : void 0;
4
- };
5
- var runIfFn = (v, ...a) => {
6
- const res = typeof v === "function" ? v(...a) : v;
7
- return res ?? void 0;
8
- };
9
- var callAll = (...fns) => (...a) => {
10
- fns.forEach(function(fn) {
11
- fn == null ? void 0 : fn(...a);
12
- });
13
- };
14
- var isArray = (v) => Array.isArray(v);
15
- var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
16
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
17
- var isDom = () => typeof window !== "undefined";
18
- function getPlatform() {
19
- const agent = navigator.userAgentData;
20
- return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
21
- }
22
- var pt = (v) => isDom() && v.test(getPlatform());
23
- var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
24
- var isMac = () => pt(/^Mac/) && !isTouchDevice;
25
- var isApple = () => pt(/mac|iphone|ipad|ipod/i);
26
- var isIos = () => isApple() && !isMac();
27
- function isDocument(el) {
28
- return el.nodeType === Node.DOCUMENT_NODE;
29
- }
30
- function isWindow(value) {
31
- return (value == null ? void 0 : value.toString()) === "[object Window]";
32
- }
33
- function getDocument(el) {
34
- if (isWindow(el))
35
- return el.document;
36
- if (isDocument(el))
37
- return el;
38
- return (el == null ? void 0 : el.ownerDocument) ?? document;
39
- }
40
- function defineDomHelpers(helpers) {
41
- const dom2 = {
42
- getRootNode: (ctx) => {
43
- var _a;
44
- return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
45
- },
46
- getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
47
- getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
48
- getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
49
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
50
- };
51
- return {
52
- ...dom2,
53
- ...helpers
54
- };
55
- }
56
- function getNativeEvent(e) {
57
- return e.nativeEvent ?? e;
58
- }
59
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
60
- var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
61
- var supportsMouseEvent = () => isDom() && window.onmousedown === null;
62
- var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
63
- var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
64
- var isLeftClick = (v) => v.button === 0;
65
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
66
- function getElementOffset(element) {
67
- let left = 0;
68
- let top = 0;
69
- let el = element;
70
- if (el.parentNode) {
71
- do {
72
- left += el.offsetLeft;
73
- top += el.offsetTop;
74
- } while ((el = el.offsetParent) && el.nodeType < 9);
75
- el = element;
76
- do {
77
- left -= el.scrollLeft;
78
- top -= el.scrollTop;
79
- } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
80
- }
81
- return {
82
- top,
83
- right: innerWidth - left - element.offsetWidth,
84
- bottom: innerHeight - top - element.offsetHeight,
85
- left
86
- };
87
- }
88
- var fallback = {
89
- pageX: 0,
90
- pageY: 0,
91
- clientX: 0,
92
- clientY: 0
93
- };
94
- function getEventPoint(event, type = "page") {
95
- const point = isTouchEvent(event) ? event.touches[0] ?? event.changedTouches[0] ?? fallback : event;
96
- return { x: point[`${type}X`], y: point[`${type}Y`] };
97
- }
98
- function getPointRelativeToNode(point, element) {
99
- const offset = getElementOffset(element);
100
- const x = point.x - offset.left;
101
- const y = point.y - offset.top;
102
- return { x, y };
103
- }
104
- var rtlKeyMap = {
105
- ArrowLeft: "ArrowRight",
106
- ArrowRight: "ArrowLeft"
107
- };
108
- var sameKeyMap = {
109
- Up: "ArrowUp",
110
- Down: "ArrowDown",
111
- Esc: "Escape",
112
- " ": "Space",
113
- ",": "Comma",
114
- Left: "ArrowLeft",
115
- Right: "ArrowRight"
116
- };
117
- function getEventKey(event, options = {}) {
118
- const { dir = "ltr", orientation = "horizontal" } = options;
119
- let { key } = event;
120
- key = sameKeyMap[key] ?? key;
121
- const isRtl = dir === "rtl" && orientation === "horizontal";
122
- if (isRtl && key in rtlKeyMap) {
123
- key = rtlKeyMap[key];
124
- }
125
- return key;
126
- }
127
- var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
128
- var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
129
- function getEventStep(event) {
130
- if (event.ctrlKey || event.metaKey) {
131
- return 0.1;
132
- } else {
133
- const isPageKey = PAGE_KEYS.has(event.key);
134
- const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
135
- return isSkipKey ? 10 : 1;
136
- }
137
- }
138
- var isRef = (v) => hasProp(v, "current");
139
- var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
140
- function extractInfo(event, type = "page") {
141
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
142
- return {
143
- point: {
144
- x: point[`${type}X`],
145
- y: point[`${type}Y`]
146
- }
147
- };
148
- }
149
- function addDomEvent(target, eventName, handler, options) {
150
- const node = isRef(target) ? target.current : runIfFn(target);
151
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
152
- return () => {
153
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
154
- };
155
- }
156
- function addPointerEvent(target, event, listener, options) {
157
- const type = getEventName(event) ?? event;
158
- return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
159
- }
160
- function wrapHandler(fn, filter = false) {
161
- const listener = (event) => {
162
- fn(event, extractInfo(event));
163
- };
164
- return filter ? filterPrimaryPointer(listener) : listener;
165
- }
166
- function filterPrimaryPointer(fn) {
167
- return (event) => {
168
- const win = event.view ?? window;
169
- const isMouseEvent2 = event instanceof win.MouseEvent;
170
- const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
171
- if (isPrimary)
172
- fn(event);
173
- };
174
- }
175
- var mouseEventNames = {
176
- pointerdown: "mousedown",
177
- pointermove: "mousemove",
178
- pointerup: "mouseup",
179
- pointercancel: "mousecancel",
180
- pointerover: "mouseover",
181
- pointerout: "mouseout",
182
- pointerenter: "mouseenter",
183
- pointerleave: "mouseleave"
184
- };
185
- var touchEventNames = {
186
- pointerdown: "touchstart",
187
- pointermove: "touchmove",
188
- pointerup: "touchend",
189
- pointercancel: "touchcancel"
190
- };
191
- function getEventName(evt) {
192
- if (supportsPointerEvent())
193
- return evt;
194
- if (supportsTouchEvent())
195
- return touchEventNames[evt];
196
- if (supportsMouseEvent())
197
- return mouseEventNames[evt];
198
- return evt;
199
- }
200
- function nextTick(fn) {
201
- const set = /* @__PURE__ */ new Set();
202
- function raf2(fn2) {
203
- const id = globalThis.requestAnimationFrame(fn2);
204
- set.add(() => globalThis.cancelAnimationFrame(id));
205
- }
206
- raf2(() => raf2(fn));
207
- return function cleanup() {
208
- set.forEach(function(fn2) {
209
- fn2();
210
- });
211
- };
212
- }
213
- function raf(fn) {
214
- const id = globalThis.requestAnimationFrame(fn);
215
- return function cleanup() {
216
- globalThis.cancelAnimationFrame(id);
217
- };
218
- }
219
- var state = "default";
220
- var savedUserSelect = "";
221
- var modifiedElementMap = /* @__PURE__ */ new WeakMap();
222
- function disableTextSelection({ target, doc } = {}) {
223
- const _document = doc ?? document;
224
- if (isIos()) {
225
- if (state === "default") {
226
- savedUserSelect = _document.documentElement.style.webkitUserSelect;
227
- _document.documentElement.style.webkitUserSelect = "none";
228
- }
229
- state = "disabled";
230
- } else if (target) {
231
- modifiedElementMap.set(target, target.style.userSelect);
232
- target.style.userSelect = "none";
233
- }
234
- return () => restoreTextSelection({ target, doc: _document });
235
- }
236
- function restoreTextSelection({ target, doc } = {}) {
237
- const _document = doc ?? document;
238
- if (isIos()) {
239
- if (state !== "disabled")
240
- return;
241
- state = "restoring";
242
- setTimeout(() => {
243
- nextTick(() => {
244
- if (state === "restoring") {
245
- if (_document.documentElement.style.webkitUserSelect === "none") {
246
- _document.documentElement.style.webkitUserSelect = savedUserSelect || "";
247
- }
248
- savedUserSelect = "";
249
- state = "default";
250
- }
251
- });
252
- }, 300);
253
- } else {
254
- if (target && modifiedElementMap.has(target)) {
255
- let targetOldUserSelect = modifiedElementMap.get(target);
256
- if (target.style.userSelect === "none") {
257
- target.style.userSelect = targetOldUserSelect ?? "";
258
- }
259
- if (target.getAttribute("style") === "") {
260
- target.removeAttribute("style");
261
- }
262
- modifiedElementMap.delete(target);
263
- }
264
- }
265
- }
266
- var THRESHOLD = 5;
267
- function trackPointerMove(doc, opts) {
268
- const { onPointerMove, onPointerUp } = opts;
269
- const handlePointerMove = (event, info) => {
270
- const { point: p } = info;
271
- const distance = Math.sqrt(p.x ** 2 + p.y ** 2);
272
- if (distance < THRESHOLD)
273
- return;
274
- if (isMouseEvent(event) && isLeftClick(event)) {
275
- onPointerUp();
276
- return;
277
- }
278
- onPointerMove(info, event);
279
- };
280
- return callAll(
281
- addPointerEvent(doc, "pointermove", handlePointerMove, false),
282
- addPointerEvent(doc, "pointerup", onPointerUp, false),
283
- addPointerEvent(doc, "pointercancel", onPointerUp, false),
284
- addPointerEvent(doc, "contextmenu", onPointerUp, false),
285
- disableTextSelection({ doc })
286
- );
287
- }
288
-
289
- // ../../utilities/form-utils/dist/index.mjs
290
- function getWindow(el) {
291
- return (el == null ? void 0 : el.ownerDocument.defaultView) ?? window;
292
- }
293
- function observeAttributes(node, attributes, fn) {
294
- if (!node)
295
- return;
296
- const attrs = Array.isArray(attributes) ? attributes : [attributes];
297
- const win = node.ownerDocument.defaultView || window;
298
- const obs = new win.MutationObserver((changes) => {
299
- for (const change of changes) {
300
- if (change.type === "attributes" && change.attributeName && attrs.includes(change.attributeName)) {
301
- fn(change);
302
- }
303
- }
304
- });
305
- obs.observe(node, { attributes: true, attributeFilter: attrs });
306
- return () => obs.disconnect();
307
- }
308
- function getDescriptor(el, options) {
309
- const { type, property } = options;
310
- const proto = getWindow(el)[type].prototype;
311
- return Object.getOwnPropertyDescriptor(proto, property) ?? {};
312
- }
313
- function dispatchInputValueEvent(el, value) {
314
- var _a;
315
- if (!el)
316
- return;
317
- const win = getWindow(el);
318
- if (!(el instanceof win.HTMLInputElement))
319
- return;
320
- const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
321
- (_a = desc.set) == null ? void 0 : _a.call(el, value);
322
- const event = new win.Event("input", { bubbles: true });
323
- el.dispatchEvent(event);
324
- }
325
- function getClosestForm(el) {
326
- if (isFormElement(el))
327
- return el.form;
328
- else
329
- return el.closest("form");
330
- }
331
- function isFormElement(el) {
332
- return el.matches("textarea, input, select, button");
333
- }
334
- function trackFormReset(el, callback) {
335
- if (!el)
336
- return;
337
- const form = getClosestForm(el);
338
- form == null ? void 0 : form.addEventListener("reset", callback, { passive: true });
339
- return () => {
340
- form == null ? void 0 : form.removeEventListener("reset", callback);
341
- };
342
- }
343
- function trackFieldsetDisabled(el, callback) {
344
- const fieldset = el == null ? void 0 : el.closest("fieldset");
345
- if (!fieldset)
346
- return;
347
- callback(fieldset.disabled);
348
- return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
349
- }
350
-
351
- // ../../utilities/number/dist/index.mjs
352
- function round(v, t) {
353
- let num = valueOf(v);
354
- const p = 10 ** (t ?? 10);
355
- num = Math.round(num * p) / p;
356
- return t ? num.toFixed(t) : v.toString();
357
- }
358
- var valueToPercent = (v, r) => (valueOf(v) - r.min) * 100 / (r.max - r.min);
359
- var percentToValue = (v, r) => r.min + (r.max - r.min) * valueOf(v);
360
- function clamp(v, o) {
361
- return Math.min(Math.max(valueOf(v), o.min), o.max);
362
- }
363
- function countDecimals(value) {
364
- if (!Number.isFinite(value))
365
- return 0;
366
- let e = 1, p = 0;
367
- while (Math.round(value * e) / e !== value) {
368
- e *= 10;
369
- p += 1;
370
- }
371
- return p;
372
- }
373
- var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
374
- var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
375
- function snapToStep(value, step) {
376
- const num = valueOf(value);
377
- const p = countDecimals(step);
378
- const v = Math.round(num / step) * step;
379
- return round(v, p);
380
- }
381
- function valueOf(v) {
382
- if (typeof v === "number")
383
- return v;
384
- const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
385
- return !Number.isNaN(num) ? num : 0;
386
- }
387
- function decimalOperation(a, op, b) {
388
- let result = op === "+" ? a + b : a - b;
389
- if (a % 1 !== 0 || b % 1 !== 0) {
390
- const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
391
- a = Math.round(a * multiplier);
392
- b = Math.round(b * multiplier);
393
- result = op === "+" ? a + b : a - b;
394
- result /= multiplier;
395
- }
396
- return result;
397
- }
398
- var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
399
- var transform = (a, b) => {
400
- const i = { min: a[0], max: a[1] };
401
- const o = { min: b[0], max: b[1] };
402
- return (v) => {
403
- if (i.min === i.max || o.min === o.max)
404
- return o.min;
405
- const ratio = (o.max - o.min) / (i.max - i.min);
406
- return o.min + ratio * (v - i.min);
407
- };
408
- };
409
-
410
- // src/slider.style.ts
411
- function getVerticalThumbOffset(ctx) {
412
- const { height = 0 } = ctx.thumbSize ?? {};
413
- const getValue = transform([ctx.min, ctx.max], [-height / 2, height / 2]);
414
- return parseFloat(getValue(ctx.value).toFixed(2));
415
- }
416
- function getHorizontalThumbOffset(ctx) {
417
- const { width = 0 } = ctx.thumbSize ?? {};
418
- if (ctx.isRtl) {
419
- const getValue2 = transform([ctx.max, ctx.min], [-width * 1.5, -width / 2]);
420
- return -1 * parseFloat(getValue2(ctx.value).toFixed(2));
421
- }
422
- const getValue = transform([ctx.min, ctx.max], [-width / 2, width / 2]);
423
- return parseFloat(getValue(ctx.value).toFixed(2));
424
- }
425
- function getThumbOffset(ctx) {
426
- const percent = valueToPercent(ctx.value, ctx);
427
- if (ctx.thumbAlignment === "center")
428
- return `${percent}%`;
429
- const offset = ctx.isVertical ? getVerticalThumbOffset(ctx) : getHorizontalThumbOffset(ctx);
430
- return `calc(${percent}% - ${offset}px)`;
431
- }
432
- function getThumbStyle(ctx) {
433
- const placementProp = ctx.isVertical ? "bottom" : ctx.isRtl ? "right" : "left";
434
- return {
435
- visibility: ctx.hasMeasuredThumbSize ? "visible" : "hidden",
436
- position: "absolute",
437
- transform: "var(--slider-thumb-transform)",
438
- [placementProp]: "var(--slider-thumb-offset)"
439
- };
440
- }
441
- function getRangeOffsets(ctx) {
442
- const percent = valueToPercent(ctx.value, ctx);
443
- let start = "0%";
444
- let end = `${100 - percent}%`;
445
- if (ctx.origin === "center") {
446
- const isNegative = percent < 50;
447
- start = isNegative ? `${percent}%` : "50%";
448
- end = isNegative ? "50%" : end;
449
- }
450
- return { start, end };
451
- }
452
- function getRangeStyle(ctx) {
453
- if (ctx.isVertical) {
454
- return {
455
- position: "absolute",
456
- bottom: "var(--slider-range-start)",
457
- top: "var(--slider-range-end)"
458
- };
459
- }
460
- return {
461
- position: "absolute",
462
- [ctx.isRtl ? "right" : "left"]: "var(--slider-range-start)",
463
- [ctx.isRtl ? "left" : "right"]: "var(--slider-range-end)"
464
- };
465
- }
466
- function getControlStyle() {
467
- return {
468
- touchAction: "none",
469
- userSelect: "none",
470
- position: "relative"
471
- };
472
- }
473
- function getRootStyle(ctx) {
474
- const range = getRangeOffsets(ctx);
475
- return {
476
- "--slider-thumb-transform": ctx.isVertical ? "translateY(50%)" : "translateX(-50%)",
477
- "--slider-thumb-offset": getThumbOffset(ctx),
478
- "--slider-range-start": range.start,
479
- "--slider-range-end": range.end
480
- };
481
- }
482
- function getMarkerStyle(ctx, percent) {
483
- return {
484
- position: "absolute",
485
- pointerEvents: "none",
486
- [ctx.isHorizontal ? "left" : "bottom"]: `${ctx.isRtl ? 100 - percent : percent}%`
487
- };
488
- }
489
- function getLabelStyle() {
490
- return { userSelect: "none" };
491
- }
492
- function getTrackStyle() {
493
- return { position: "relative" };
494
- }
495
- function getMarkerGroupStyle() {
496
- return {
497
- userSelect: "none",
498
- pointerEvents: "none",
499
- position: "relative"
500
- };
501
- }
502
- var styles = {
503
- getThumbOffset,
504
- getControlStyle,
505
- getThumbStyle,
506
- getRangeStyle,
507
- getRootStyle,
508
- getMarkerStyle,
509
- getLabelStyle,
510
- getTrackStyle,
511
- getMarkerGroupStyle
512
- };
513
-
514
- // src/slider.utils.ts
515
- var utils = {
516
- fromPercent(ctx, percent) {
517
- percent = clamp(percent, { min: 0, max: 1 });
518
- return parseFloat(snapToStep(percentToValue(percent, ctx), ctx.step));
519
- },
520
- clamp(ctx, value) {
521
- return clamp(value, ctx);
522
- },
523
- convert(ctx, value) {
524
- return clamp(parseFloat(snapToStep(value, ctx.step)), ctx);
525
- },
526
- decrement(ctx, step) {
527
- let value = decrement(ctx.value, step ?? ctx.step);
528
- return utils.convert(ctx, value);
529
- },
530
- increment(ctx, step) {
531
- let value = increment(ctx.value, step ?? ctx.step);
532
- return utils.convert(ctx, value);
533
- }
534
- };
535
-
536
- // src/slider.dom.ts
537
- var dom = defineDomHelpers({
538
- ...styles,
539
- getRootId: (ctx) => {
540
- var _a;
541
- return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `slider:${ctx.id}`;
542
- },
543
- getThumbId: (ctx) => {
544
- var _a;
545
- return ((_a = ctx.ids) == null ? void 0 : _a.thumb) ?? `slider:${ctx.id}:thumb`;
546
- },
547
- getControlId: (ctx) => {
548
- var _a;
549
- return ((_a = ctx.ids) == null ? void 0 : _a.control) ?? `slider:${ctx.id}:control`;
550
- },
551
- getInputId: (ctx) => `slider:${ctx.id}:input`,
552
- getOutputId: (ctx) => {
553
- var _a;
554
- return ((_a = ctx.ids) == null ? void 0 : _a.output) ?? `slider:${ctx.id}:output`;
555
- },
556
- getTrackId: (ctx) => {
557
- var _a;
558
- return ((_a = ctx.ids) == null ? void 0 : _a.track) ?? `slider:${ctx.id}track`;
559
- },
560
- getRangeId: (ctx) => {
561
- var _a;
562
- return ((_a = ctx.ids) == null ? void 0 : _a.track) ?? `slider:${ctx.id}:range`;
563
- },
564
- getLabelId: (ctx) => {
565
- var _a;
566
- return ((_a = ctx.ids) == null ? void 0 : _a.label) ?? `slider:${ctx.id}:label`;
567
- },
568
- getMarkerId: (ctx, value) => `slider:${ctx.id}:marker:${value}`,
569
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
570
- getThumbEl: (ctx) => dom.getById(ctx, dom.getThumbId(ctx)),
571
- getControlEl: (ctx) => dom.getById(ctx, dom.getControlId(ctx)),
572
- getInputEl: (ctx) => dom.getById(ctx, dom.getInputId(ctx)),
573
- getValueFromPoint(ctx, point) {
574
- const el = dom.getControlEl(ctx);
575
- if (!el)
576
- return;
577
- const relativePoint = getPointRelativeToNode(point, el);
578
- const percentX = relativePoint.x / el.offsetWidth;
579
- const percentY = relativePoint.y / el.offsetHeight;
580
- let percent;
581
- if (ctx.isHorizontal) {
582
- percent = ctx.isRtl ? 1 - percentX : percentX;
583
- } else {
584
- percent = 1 - percentY;
585
- }
586
- return utils.fromPercent(ctx, percent);
587
- },
588
- dispatchChangeEvent(ctx) {
589
- const input = dom.getInputEl(ctx);
590
- if (!input)
591
- return;
592
- dispatchInputValueEvent(input, ctx.value);
593
- }
594
- });
595
-
596
- // src/slider.connect.ts
597
- function connect(state2, send, normalize) {
598
- var _a, _b;
599
- const ariaLabel = state2.context["aria-label"];
600
- const ariaLabelledBy = state2.context["aria-labelledby"];
601
- const ariaValueText = (_b = (_a = state2.context).getAriaValueText) == null ? void 0 : _b.call(_a, state2.context.value);
602
- const isFocused = state2.matches("focus");
603
- const isDragging = state2.matches("dragging");
604
- const isDisabled = state2.context.disabled;
605
- const isInteractive = state2.context.isInteractive;
606
- const isInvalid = state2.context.invalid;
607
- return {
608
- isFocused,
609
- isDragging,
610
- value: state2.context.value,
611
- percent: valueToPercent(state2.context.value, state2.context),
612
- setValue(value) {
613
- send({ type: "SET_VALUE", value });
614
- },
615
- getPercentValue(percent) {
616
- return percentToValue(percent, state2.context);
617
- },
618
- focus() {
619
- var _a2;
620
- (_a2 = dom.getThumbEl(state2.context)) == null ? void 0 : _a2.focus();
621
- },
622
- increment() {
623
- send("INCREMENT");
624
- },
625
- decrement() {
626
- send("DECREMENT");
627
- },
628
- rootProps: normalize.element({
629
- "data-part": "root",
630
- "data-disabled": dataAttr(isDisabled),
631
- "data-focus": dataAttr(isFocused),
632
- "data-orientation": state2.context.orientation,
633
- "data-invalid": dataAttr(isInvalid),
634
- id: dom.getRootId(state2.context),
635
- dir: state2.context.dir,
636
- style: dom.getRootStyle(state2.context)
637
- }),
638
- labelProps: normalize.label({
639
- "data-part": "label",
640
- "data-disabled": dataAttr(isDisabled),
641
- "data-invalid": dataAttr(isInvalid),
642
- "data-focus": dataAttr(isFocused),
643
- id: dom.getLabelId(state2.context),
644
- htmlFor: dom.getInputId(state2.context),
645
- onClick(event) {
646
- var _a2;
647
- if (!isInteractive)
648
- return;
649
- event.preventDefault();
650
- (_a2 = dom.getThumbEl(state2.context)) == null ? void 0 : _a2.focus();
651
- },
652
- style: dom.getLabelStyle()
653
- }),
654
- thumbProps: normalize.element({
655
- "data-part": "thumb",
656
- id: dom.getThumbId(state2.context),
657
- "data-disabled": dataAttr(isDisabled),
658
- "data-orientation": state2.context.orientation,
659
- "data-focus": dataAttr(isFocused),
660
- draggable: false,
661
- "aria-invalid": isInvalid || void 0,
662
- "data-invalid": dataAttr(isInvalid),
663
- "aria-disabled": isDisabled || void 0,
664
- "aria-label": ariaLabel,
665
- "aria-labelledby": ariaLabel ? void 0 : ariaLabelledBy ?? dom.getLabelId(state2.context),
666
- "aria-orientation": state2.context.orientation,
667
- "aria-valuemax": state2.context.max,
668
- "aria-valuemin": state2.context.min,
669
- "aria-valuenow": state2.context.value,
670
- "aria-valuetext": ariaValueText,
671
- role: "slider",
672
- tabIndex: isDisabled ? void 0 : 0,
673
- onBlur() {
674
- if (!isInteractive)
675
- return;
676
- send("BLUR");
677
- },
678
- onFocus() {
679
- if (!isInteractive)
680
- return;
681
- send("FOCUS");
682
- },
683
- onKeyDown(event) {
684
- if (!isInteractive)
685
- return;
686
- const step = getEventStep(event) * state2.context.step;
687
- let prevent = true;
688
- const keyMap = {
689
- ArrowUp() {
690
- send({ type: "ARROW_UP", step });
691
- prevent = state2.context.isVertical;
692
- },
693
- ArrowDown() {
694
- send({ type: "ARROW_DOWN", step });
695
- prevent = state2.context.isVertical;
696
- },
697
- ArrowLeft() {
698
- send({ type: "ARROW_LEFT", step });
699
- prevent = state2.context.isHorizontal;
700
- },
701
- ArrowRight() {
702
- send({ type: "ARROW_RIGHT", step });
703
- prevent = state2.context.isHorizontal;
704
- },
705
- PageUp() {
706
- send({ type: "PAGE_UP", step });
707
- },
708
- PageDown() {
709
- send({ type: "PAGE_DOWN", step });
710
- },
711
- Home() {
712
- send("HOME");
713
- },
714
- End() {
715
- send("END");
716
- }
717
- };
718
- const key = getEventKey(event, state2.context);
719
- const exec = keyMap[key];
720
- if (!exec)
721
- return;
722
- exec(event);
723
- if (prevent) {
724
- event.preventDefault();
725
- }
726
- },
727
- style: dom.getThumbStyle(state2.context)
728
- }),
729
- inputProps: normalize.input({
730
- "data-part": "input",
731
- type: "text",
732
- defaultValue: state2.context.value,
733
- name: state2.context.name,
734
- id: dom.getInputId(state2.context),
735
- hidden: true
736
- }),
737
- outputProps: normalize.output({
738
- "data-part": "output",
739
- "data-disabled": dataAttr(isDisabled),
740
- "data-invalid": dataAttr(isInvalid),
741
- id: dom.getOutputId(state2.context),
742
- htmlFor: dom.getInputId(state2.context),
743
- "aria-live": "off"
744
- }),
745
- trackProps: normalize.element({
746
- "data-part": "track",
747
- id: dom.getTrackId(state2.context),
748
- "data-disabled": dataAttr(isDisabled),
749
- "data-focus": dataAttr(isFocused),
750
- "data-invalid": dataAttr(isInvalid),
751
- "data-orientation": state2.context.orientation,
752
- style: dom.getTrackStyle()
753
- }),
754
- rangeProps: normalize.element({
755
- "data-part": "range",
756
- id: dom.getRangeId(state2.context),
757
- "data-focus": dataAttr(isFocused),
758
- "data-invalid": dataAttr(isInvalid),
759
- "data-disabled": dataAttr(isDisabled),
760
- "data-orientation": state2.context.orientation,
761
- style: dom.getRangeStyle(state2.context)
762
- }),
763
- controlProps: normalize.element({
764
- "data-part": "control",
765
- id: dom.getControlId(state2.context),
766
- "data-disabled": dataAttr(isDisabled),
767
- "data-invalid": dataAttr(isInvalid),
768
- "data-orientation": state2.context.orientation,
769
- "data-focus": dataAttr(isFocused),
770
- onPointerDown(event) {
771
- if (!isInteractive)
772
- return;
773
- const evt = getNativeEvent(event);
774
- if (!isLeftClick(evt) || isModifiedEvent(evt))
775
- return;
776
- const point = getEventPoint(evt);
777
- send({ type: "POINTER_DOWN", point });
778
- event.preventDefault();
779
- event.stopPropagation();
780
- },
781
- style: dom.getControlStyle()
782
- }),
783
- markerGroupProps: normalize.element({
784
- "data-part": "marker-group",
785
- role: "presentation",
786
- "aria-hidden": true,
787
- "data-orientation": state2.context.orientation,
788
- style: dom.getMarkerGroupStyle()
789
- }),
790
- getMarkerProps({ value }) {
791
- const percent = valueToPercent(value, state2.context);
792
- const style = dom.getMarkerStyle(state2.context, percent);
793
- const markerState = value > state2.context.value ? "over-value" : value < state2.context.value ? "under-value" : "at-value";
794
- return normalize.element({
795
- "data-part": "marker",
796
- role: "presentation",
797
- "data-orientation": state2.context.orientation,
798
- id: dom.getMarkerId(state2.context, value),
799
- "data-value": value,
800
- "data-disabled": dataAttr(isDisabled),
801
- "data-state": markerState,
802
- style
803
- });
804
- }
805
- };
806
- }
807
-
808
- // src/slider.machine.ts
809
- import { createMachine } from "@zag-js/core";
810
- import { trackElementSize } from "@zag-js/element-size";
811
- function machine(ctx) {
812
- return createMachine(
813
- {
814
- id: "slider",
815
- initial: "unknown",
816
- context: {
817
- thumbSize: null,
818
- thumbAlignment: "contain",
819
- disabled: false,
820
- threshold: 5,
821
- dir: "ltr",
822
- origin: "start",
823
- orientation: "horizontal",
824
- initialValue: null,
825
- value: 0,
826
- step: 1,
827
- min: 0,
828
- max: 100,
829
- ...ctx
830
- },
831
- computed: {
832
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
833
- isVertical: (ctx2) => ctx2.orientation === "vertical",
834
- isRtl: (ctx2) => ctx2.orientation === "horizontal" && ctx2.dir === "rtl",
835
- isInteractive: (ctx2) => !(ctx2.disabled || ctx2.readonly),
836
- hasMeasuredThumbSize: (ctx2) => ctx2.thumbSize !== null
837
- },
838
- watch: {
839
- value: ["invokeOnChange", "dispatchChangeEvent"]
840
- },
841
- activities: ["trackFormReset", "trackFieldsetDisabled", "trackThumbSize"],
842
- on: {
843
- SET_VALUE: {
844
- actions: "setValue"
845
- },
846
- INCREMENT: {
847
- actions: "increment"
848
- },
849
- DECREMENT: {
850
- actions: "decrement"
851
- }
852
- },
853
- states: {
854
- unknown: {
855
- on: {
856
- SETUP: {
857
- target: "idle",
858
- actions: ["checkValue"]
859
- }
860
- }
861
- },
862
- idle: {
863
- on: {
864
- POINTER_DOWN: {
865
- target: "dragging",
866
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
867
- },
868
- FOCUS: "focus"
869
- }
870
- },
871
- focus: {
872
- entry: "focusThumb",
873
- on: {
874
- POINTER_DOWN: {
875
- target: "dragging",
876
- actions: ["setPointerValue", "invokeOnChangeStart", "focusThumb"]
877
- },
878
- ARROW_LEFT: {
879
- guard: "isHorizontal",
880
- actions: "decrement"
881
- },
882
- ARROW_RIGHT: {
883
- guard: "isHorizontal",
884
- actions: "increment"
885
- },
886
- ARROW_UP: {
887
- guard: "isVertical",
888
- actions: "increment"
889
- },
890
- ARROW_DOWN: {
891
- guard: "isVertical",
892
- actions: "decrement"
893
- },
894
- PAGE_UP: {
895
- actions: "increment"
896
- },
897
- PAGE_DOWN: {
898
- actions: "decrement"
899
- },
900
- HOME: {
901
- actions: "setToMin"
902
- },
903
- END: {
904
- actions: "setToMax"
905
- },
906
- BLUR: "idle"
907
- }
908
- },
909
- dragging: {
910
- entry: "focusThumb",
911
- activities: "trackPointerMove",
912
- on: {
913
- POINTER_UP: {
914
- target: "focus",
915
- actions: "invokeOnChangeEnd"
916
- },
917
- POINTER_MOVE: {
918
- actions: "setPointerValue"
919
- }
920
- }
921
- }
922
- }
923
- },
924
- {
925
- guards: {
926
- isHorizontal: (ctx2) => ctx2.isHorizontal,
927
- isVertical: (ctx2) => ctx2.isVertical
928
- },
929
- activities: {
930
- trackFieldsetDisabled(ctx2) {
931
- return trackFieldsetDisabled(dom.getRootEl(ctx2), (disabled) => {
932
- if (disabled) {
933
- ctx2.disabled = disabled;
934
- }
935
- });
936
- },
937
- trackFormReset(ctx2) {
938
- return trackFormReset(dom.getInputEl(ctx2), () => {
939
- if (ctx2.initialValue != null) {
940
- ctx2.value = ctx2.initialValue;
941
- }
942
- });
943
- },
944
- trackPointerMove(ctx2, _evt, { send }) {
945
- return trackPointerMove(dom.getDoc(ctx2), {
946
- onPointerMove(info) {
947
- send({ type: "POINTER_MOVE", point: info.point });
948
- },
949
- onPointerUp() {
950
- send("POINTER_UP");
951
- }
952
- });
953
- },
954
- trackThumbSize(ctx2, _evt) {
955
- if (ctx2.thumbAlignment !== "contain")
956
- return;
957
- return trackElementSize(dom.getThumbEl(ctx2), (size) => {
958
- if (size)
959
- ctx2.thumbSize = size;
960
- });
961
- }
962
- },
963
- actions: {
964
- checkValue(ctx2) {
965
- const value = utils.convert(ctx2, ctx2.value);
966
- ctx2.value = value;
967
- ctx2.initialValue = value;
968
- },
969
- invokeOnChangeStart(ctx2) {
970
- var _a;
971
- (_a = ctx2.onChangeStart) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
972
- },
973
- invokeOnChangeEnd(ctx2) {
974
- var _a;
975
- (_a = ctx2.onChangeEnd) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
976
- },
977
- invokeOnChange(ctx2) {
978
- var _a;
979
- (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
980
- },
981
- dispatchChangeEvent(ctx2) {
982
- dom.dispatchChangeEvent(ctx2);
983
- },
984
- setPointerValue(ctx2, evt) {
985
- const value = dom.getValueFromPoint(ctx2, evt.point);
986
- if (value == null)
987
- return;
988
- ctx2.value = utils.clamp(ctx2, value);
989
- },
990
- focusThumb(ctx2) {
991
- raf(() => {
992
- var _a;
993
- return (_a = dom.getThumbEl(ctx2)) == null ? void 0 : _a.focus();
994
- });
995
- },
996
- decrement(ctx2, evt) {
997
- ctx2.value = utils.decrement(ctx2, evt.step);
998
- },
999
- increment(ctx2, evt) {
1000
- ctx2.value = utils.increment(ctx2, evt.step);
1001
- },
1002
- setToMin(ctx2) {
1003
- ctx2.value = ctx2.min;
1004
- },
1005
- setToMax(ctx2) {
1006
- ctx2.value = ctx2.max;
1007
- },
1008
- setValue(ctx2, evt) {
1009
- ctx2.value = utils.convert(ctx2, evt.value);
1010
- }
1011
- }
1012
- }
1013
- );
1014
- }
1015
- export {
1016
- connect,
1017
- machine,
1018
- dom as unstable__dom
1019
- };