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