@zag-js/slider 0.1.8 → 0.1.9

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.
package/dist/index.js CHANGED
@@ -48,10 +48,19 @@ var __pow = Math.pow;
48
48
  var dataAttr = (guard) => {
49
49
  return guard ? "" : void 0;
50
50
  };
51
- var isDom = () => typeof window !== "undefined";
51
+ var runIfFn = (v, ...a) => {
52
+ const res = typeof v === "function" ? v(...a) : v;
53
+ return res != null ? res : void 0;
54
+ };
55
+ var callAll = (...fns) => (...a) => {
56
+ fns.forEach(function(fn) {
57
+ fn == null ? void 0 : fn(...a);
58
+ });
59
+ };
52
60
  var isArray = (v) => Array.isArray(v);
53
61
  var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
54
62
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
63
+ var isDom = () => typeof window !== "undefined";
55
64
  function getPlatform() {
56
65
  var _a;
57
66
  const agent = navigator.userAgentData;
@@ -62,103 +71,7 @@ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
62
71
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
63
72
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
64
73
  var isIos = () => isApple() && !isMac();
65
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
66
- var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
67
- var supportsMouseEvent = () => isDom() && window.onmousedown === null;
68
- var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
69
- var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
70
- var isLeftClick = (v) => v.button === 0;
71
- var runIfFn = (v, ...a) => {
72
- const res = typeof v === "function" ? v(...a) : v;
73
- return res != null ? res : void 0;
74
- };
75
- var noop = () => {
76
- };
77
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
78
- var isRef = (v) => hasProp(v, "current");
79
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
80
- function extractInfo(event, type = "page") {
81
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
82
- return {
83
- point: {
84
- x: point[`${type}X`],
85
- y: point[`${type}Y`]
86
- }
87
- };
88
- }
89
- function addDomEvent(target, eventName, handler, options) {
90
- const node = isRef(target) ? target.current : runIfFn(target);
91
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
92
- return () => {
93
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
94
- };
95
- }
96
- function addPointerEvent(target, event, listener, options) {
97
- var _a;
98
- const type = (_a = getEventName(event)) != null ? _a : event;
99
- return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
100
- }
101
- function wrapHandler(fn, filter = false) {
102
- const listener = (event) => {
103
- fn(event, extractInfo(event));
104
- };
105
- return filter ? filterPrimaryPointer(listener) : listener;
106
- }
107
- function filterPrimaryPointer(fn) {
108
- return (event) => {
109
- var _a;
110
- const win = (_a = event.view) != null ? _a : window;
111
- const isMouseEvent2 = event instanceof win.MouseEvent;
112
- const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
113
- if (isPrimary)
114
- fn(event);
115
- };
116
- }
117
- var mouseEventNames = {
118
- pointerdown: "mousedown",
119
- pointermove: "mousemove",
120
- pointerup: "mouseup",
121
- pointercancel: "mousecancel",
122
- pointerover: "mouseover",
123
- pointerout: "mouseout",
124
- pointerenter: "mouseenter",
125
- pointerleave: "mouseleave"
126
- };
127
- var touchEventNames = {
128
- pointerdown: "touchstart",
129
- pointermove: "touchmove",
130
- pointerup: "touchend",
131
- pointercancel: "touchcancel"
132
- };
133
- function getEventName(evt) {
134
- if (supportsPointerEvent())
135
- return evt;
136
- if (supportsTouchEvent())
137
- return touchEventNames[evt];
138
- if (supportsMouseEvent())
139
- return mouseEventNames[evt];
140
- return evt;
141
- }
142
- function nextTick(fn) {
143
- const set = /* @__PURE__ */ new Set();
144
- function raf2(fn2) {
145
- const id = globalThis.requestAnimationFrame(fn2);
146
- set.add(() => globalThis.cancelAnimationFrame(id));
147
- }
148
- raf2(() => raf2(fn));
149
- return function cleanup() {
150
- set.forEach(function(fn2) {
151
- fn2();
152
- });
153
- };
154
- }
155
- function raf(fn) {
156
- const id = globalThis.requestAnimationFrame(fn);
157
- return function cleanup() {
158
- globalThis.cancelAnimationFrame(id);
159
- };
160
- }
161
- function getOwnerWindow(el) {
74
+ function getWindow(el) {
162
75
  var _a;
163
76
  return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
164
77
  }
@@ -166,9 +79,16 @@ function getNativeEvent(e) {
166
79
  var _a;
167
80
  return (_a = e.nativeEvent) != null ? _a : e;
168
81
  }
82
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
83
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
84
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
85
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
86
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
87
+ var isLeftClick = (v) => v.button === 0;
88
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
169
89
  function observeAttributes(node, attributes, fn) {
170
90
  if (!node)
171
- return noop;
91
+ return;
172
92
  const attrs = Array.isArray(attributes) ? attributes : [attributes];
173
93
  const win = node.ownerDocument.defaultView || window;
174
94
  const obs = new win.MutationObserver((changes) => {
@@ -206,20 +126,57 @@ function trackFieldsetDisabled(el, callback) {
206
126
  callback(fieldset.disabled);
207
127
  return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
208
128
  }
129
+ function getElementOffset(element) {
130
+ let left = 0;
131
+ let top = 0;
132
+ let el = element;
133
+ if (el.parentNode) {
134
+ do {
135
+ left += el.offsetLeft;
136
+ top += el.offsetTop;
137
+ } while ((el = el.offsetParent) && el.nodeType < 9);
138
+ el = element;
139
+ do {
140
+ left -= el.scrollLeft;
141
+ top -= el.scrollTop;
142
+ } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
143
+ }
144
+ return {
145
+ top,
146
+ right: innerWidth - left - element.offsetWidth,
147
+ bottom: innerHeight - top - element.offsetHeight,
148
+ left
149
+ };
150
+ }
151
+ var fallback = {
152
+ pageX: 0,
153
+ pageY: 0,
154
+ clientX: 0,
155
+ clientY: 0
156
+ };
157
+ function getEventPoint(event, type = "page") {
158
+ var _a, _b;
159
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
160
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
161
+ }
162
+ function getPointRelativeToNode(point, element) {
163
+ const offset = getElementOffset(element);
164
+ const x = point.x - offset.left;
165
+ const y = point.y - offset.top;
166
+ return { x, y };
167
+ }
209
168
  function getDescriptor(el, options) {
210
169
  var _a;
211
170
  const { type, property } = options;
212
- const win = getOwnerWindow(el);
213
- const _type = type === "input" ? "HTMLInputElement" : "HTMLTextAreaElement";
214
- const proto = win[_type].prototype;
171
+ const proto = getWindow(el)[type].prototype;
215
172
  return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
216
173
  }
217
174
  function dispatchInputValueEvent(el, value) {
218
175
  var _a;
219
- const win = getOwnerWindow(el);
176
+ const win = getWindow(el);
220
177
  if (!(el instanceof win.HTMLInputElement))
221
178
  return;
222
- const desc = getDescriptor(el, { type: "input", property: "value" });
179
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
223
180
  (_a = desc.set) == null ? void 0 : _a.call(el, value);
224
181
  const event = new win.Event("input", { bubbles: true });
225
182
  el.dispatchEvent(event);
@@ -261,6 +218,89 @@ function getEventStep(event) {
261
218
  return isSkipKey ? 10 : 1;
262
219
  }
263
220
  }
221
+ var isRef = (v) => hasProp(v, "current");
222
+ var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
223
+ function extractInfo(event, type = "page") {
224
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
225
+ return {
226
+ point: {
227
+ x: point[`${type}X`],
228
+ y: point[`${type}Y`]
229
+ }
230
+ };
231
+ }
232
+ function addDomEvent(target, eventName, handler, options) {
233
+ const node = isRef(target) ? target.current : runIfFn(target);
234
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
235
+ return () => {
236
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
237
+ };
238
+ }
239
+ function addPointerEvent(target, event, listener, options) {
240
+ var _a;
241
+ const type = (_a = getEventName(event)) != null ? _a : event;
242
+ return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
243
+ }
244
+ function wrapHandler(fn, filter = false) {
245
+ const listener = (event) => {
246
+ fn(event, extractInfo(event));
247
+ };
248
+ return filter ? filterPrimaryPointer(listener) : listener;
249
+ }
250
+ function filterPrimaryPointer(fn) {
251
+ return (event) => {
252
+ var _a;
253
+ const win = (_a = event.view) != null ? _a : window;
254
+ const isMouseEvent2 = event instanceof win.MouseEvent;
255
+ const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
256
+ if (isPrimary)
257
+ fn(event);
258
+ };
259
+ }
260
+ var mouseEventNames = {
261
+ pointerdown: "mousedown",
262
+ pointermove: "mousemove",
263
+ pointerup: "mouseup",
264
+ pointercancel: "mousecancel",
265
+ pointerover: "mouseover",
266
+ pointerout: "mouseout",
267
+ pointerenter: "mouseenter",
268
+ pointerleave: "mouseleave"
269
+ };
270
+ var touchEventNames = {
271
+ pointerdown: "touchstart",
272
+ pointermove: "touchmove",
273
+ pointerup: "touchend",
274
+ pointercancel: "touchcancel"
275
+ };
276
+ function getEventName(evt) {
277
+ if (supportsPointerEvent())
278
+ return evt;
279
+ if (supportsTouchEvent())
280
+ return touchEventNames[evt];
281
+ if (supportsMouseEvent())
282
+ return mouseEventNames[evt];
283
+ return evt;
284
+ }
285
+ function nextTick(fn) {
286
+ const set = /* @__PURE__ */ new Set();
287
+ function raf2(fn2) {
288
+ const id = globalThis.requestAnimationFrame(fn2);
289
+ set.add(() => globalThis.cancelAnimationFrame(id));
290
+ }
291
+ raf2(() => raf2(fn));
292
+ return function cleanup() {
293
+ set.forEach(function(fn2) {
294
+ fn2();
295
+ });
296
+ };
297
+ }
298
+ function raf(fn) {
299
+ const id = globalThis.requestAnimationFrame(fn);
300
+ return function cleanup() {
301
+ globalThis.cancelAnimationFrame(id);
302
+ };
303
+ }
264
304
  var state = "default";
265
305
  var savedUserSelect = "";
266
306
  var modifiedElementMap = /* @__PURE__ */ new WeakMap();
@@ -322,26 +362,7 @@ function trackPointerMove(opts) {
322
362
  }
323
363
  onPointerMove(info, event);
324
364
  };
325
- return pipe(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
326
- }
327
-
328
- // ../../utilities/rect/dist/index.mjs
329
- var isArray2 = (v) => Array.isArray(v);
330
- var isObject2 = (v) => !(v == null || typeof v !== "object" || isArray2(v));
331
- var hasProp2 = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
332
- var isTouchEvent2 = (v) => isObject2(v) && hasProp2(v, "touches");
333
- var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
334
- function getEventPoint(e, t = "page") {
335
- const p = isTouchEvent2(e) ? e.touches[0] || e.changedTouches[0] || fallback2 : e;
336
- return { x: p[`${t}X`], y: p[`${t}Y`] };
337
- }
338
- function relativeToNode(p, el) {
339
- const dx = p.x - el.offsetLeft - el.clientLeft + el.scrollLeft;
340
- const dy = p.y - el.offsetTop - el.clientTop + el.scrollTop;
341
- return {
342
- point: { x: dx, y: dy },
343
- progress: { x: dx / el.offsetWidth, y: dy / el.offsetHeight }
344
- };
365
+ return callAll(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
345
366
  }
346
367
 
347
368
  // ../../utilities/number/dist/index.mjs
@@ -577,15 +598,17 @@ var dom = __spreadProps(__spreadValues({}, styles), {
577
598
  getControlEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getControlId(ctx)),
578
599
  getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
579
600
  getValueFromPoint(ctx, point) {
580
- const control = dom.getControlEl(ctx);
581
- if (!control)
601
+ const el = dom.getControlEl(ctx);
602
+ if (!el)
582
603
  return;
583
- const { progress } = relativeToNode(point, control);
604
+ const relativePoint = getPointRelativeToNode(point, el);
605
+ const percentX = relativePoint.x / el.offsetWidth;
606
+ const percentY = relativePoint.y / el.offsetHeight;
584
607
  let percent;
585
608
  if (ctx.isHorizontal) {
586
- percent = ctx.isRtl ? 1 - progress.x : progress.x;
609
+ percent = ctx.isRtl ? 1 - percentX : percentX;
587
610
  } else {
588
- percent = 1 - progress.y;
611
+ percent = 1 - percentY;
589
612
  }
590
613
  return utils.fromPercent(ctx, percent);
591
614
  },
@@ -597,22 +620,8 @@ var dom = __spreadProps(__spreadValues({}, styles), {
597
620
  }
598
621
  });
599
622
 
600
- // ../../types/dist/index.mjs
601
- function createNormalizer(fn) {
602
- return new Proxy({}, {
603
- get() {
604
- return fn;
605
- }
606
- });
607
- }
608
- var normalizeProp = createNormalizer((v) => v);
609
-
610
- // ../../utilities/core/dist/index.mjs
611
- var isLeftClick2 = (v) => v.button === 0;
612
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
613
-
614
623
  // src/slider.connect.ts
615
- function connect(state2, send, normalize = normalizeProp) {
624
+ function connect(state2, send, normalize) {
616
625
  var _a, _b;
617
626
  const ariaLabel = state2.context["aria-label"];
618
627
  const ariaLabelledBy = state2.context["aria-labelledby"];
@@ -789,7 +798,7 @@ function connect(state2, send, normalize = normalizeProp) {
789
798
  if (!isInteractive)
790
799
  return;
791
800
  const evt = getNativeEvent(event);
792
- if (!isLeftClick2(evt) || isModifiedEvent(evt))
801
+ if (!isLeftClick(evt) || isModifiedEvent(evt))
793
802
  return;
794
803
  const point = getEventPoint(evt);
795
804
  send({ type: "POINTER_DOWN", point });
package/dist/index.mjs CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  var __defProp = Object.defineProperty;
3
2
  var __defProps = Object.defineProperties;
4
3
  var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
@@ -24,10 +23,19 @@ var __pow = Math.pow;
24
23
  var dataAttr = (guard) => {
25
24
  return guard ? "" : void 0;
26
25
  };
27
- var isDom = () => typeof window !== "undefined";
26
+ var runIfFn = (v, ...a) => {
27
+ const res = typeof v === "function" ? v(...a) : v;
28
+ return res != null ? res : void 0;
29
+ };
30
+ var callAll = (...fns) => (...a) => {
31
+ fns.forEach(function(fn) {
32
+ fn == null ? void 0 : fn(...a);
33
+ });
34
+ };
28
35
  var isArray = (v) => Array.isArray(v);
29
36
  var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
30
37
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
38
+ var isDom = () => typeof window !== "undefined";
31
39
  function getPlatform() {
32
40
  var _a;
33
41
  const agent = navigator.userAgentData;
@@ -38,103 +46,7 @@ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
38
46
  var isMac = () => pt(/^Mac/) && !isTouchDevice;
39
47
  var isApple = () => pt(/mac|iphone|ipad|ipod/i);
40
48
  var isIos = () => isApple() && !isMac();
41
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
42
- var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
43
- var supportsMouseEvent = () => isDom() && window.onmousedown === null;
44
- var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
45
- var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
46
- var isLeftClick = (v) => v.button === 0;
47
- var runIfFn = (v, ...a) => {
48
- const res = typeof v === "function" ? v(...a) : v;
49
- return res != null ? res : void 0;
50
- };
51
- var noop = () => {
52
- };
53
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
54
- var isRef = (v) => hasProp(v, "current");
55
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
56
- function extractInfo(event, type = "page") {
57
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
58
- return {
59
- point: {
60
- x: point[`${type}X`],
61
- y: point[`${type}Y`]
62
- }
63
- };
64
- }
65
- function addDomEvent(target, eventName, handler, options) {
66
- const node = isRef(target) ? target.current : runIfFn(target);
67
- node == null ? void 0 : node.addEventListener(eventName, handler, options);
68
- return () => {
69
- node == null ? void 0 : node.removeEventListener(eventName, handler, options);
70
- };
71
- }
72
- function addPointerEvent(target, event, listener, options) {
73
- var _a;
74
- const type = (_a = getEventName(event)) != null ? _a : event;
75
- return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
76
- }
77
- function wrapHandler(fn, filter = false) {
78
- const listener = (event) => {
79
- fn(event, extractInfo(event));
80
- };
81
- return filter ? filterPrimaryPointer(listener) : listener;
82
- }
83
- function filterPrimaryPointer(fn) {
84
- return (event) => {
85
- var _a;
86
- const win = (_a = event.view) != null ? _a : window;
87
- const isMouseEvent2 = event instanceof win.MouseEvent;
88
- const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
89
- if (isPrimary)
90
- fn(event);
91
- };
92
- }
93
- var mouseEventNames = {
94
- pointerdown: "mousedown",
95
- pointermove: "mousemove",
96
- pointerup: "mouseup",
97
- pointercancel: "mousecancel",
98
- pointerover: "mouseover",
99
- pointerout: "mouseout",
100
- pointerenter: "mouseenter",
101
- pointerleave: "mouseleave"
102
- };
103
- var touchEventNames = {
104
- pointerdown: "touchstart",
105
- pointermove: "touchmove",
106
- pointerup: "touchend",
107
- pointercancel: "touchcancel"
108
- };
109
- function getEventName(evt) {
110
- if (supportsPointerEvent())
111
- return evt;
112
- if (supportsTouchEvent())
113
- return touchEventNames[evt];
114
- if (supportsMouseEvent())
115
- return mouseEventNames[evt];
116
- return evt;
117
- }
118
- function nextTick(fn) {
119
- const set = /* @__PURE__ */ new Set();
120
- function raf2(fn2) {
121
- const id = globalThis.requestAnimationFrame(fn2);
122
- set.add(() => globalThis.cancelAnimationFrame(id));
123
- }
124
- raf2(() => raf2(fn));
125
- return function cleanup() {
126
- set.forEach(function(fn2) {
127
- fn2();
128
- });
129
- };
130
- }
131
- function raf(fn) {
132
- const id = globalThis.requestAnimationFrame(fn);
133
- return function cleanup() {
134
- globalThis.cancelAnimationFrame(id);
135
- };
136
- }
137
- function getOwnerWindow(el) {
49
+ function getWindow(el) {
138
50
  var _a;
139
51
  return (_a = el == null ? void 0 : el.ownerDocument.defaultView) != null ? _a : window;
140
52
  }
@@ -142,9 +54,16 @@ function getNativeEvent(e) {
142
54
  var _a;
143
55
  return (_a = e.nativeEvent) != null ? _a : e;
144
56
  }
57
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
58
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
59
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
60
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
61
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
62
+ var isLeftClick = (v) => v.button === 0;
63
+ var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
145
64
  function observeAttributes(node, attributes, fn) {
146
65
  if (!node)
147
- return noop;
66
+ return;
148
67
  const attrs = Array.isArray(attributes) ? attributes : [attributes];
149
68
  const win = node.ownerDocument.defaultView || window;
150
69
  const obs = new win.MutationObserver((changes) => {
@@ -182,20 +101,57 @@ function trackFieldsetDisabled(el, callback) {
182
101
  callback(fieldset.disabled);
183
102
  return observeAttributes(fieldset, ["disabled"], () => callback(fieldset.disabled));
184
103
  }
104
+ function getElementOffset(element) {
105
+ let left = 0;
106
+ let top = 0;
107
+ let el = element;
108
+ if (el.parentNode) {
109
+ do {
110
+ left += el.offsetLeft;
111
+ top += el.offsetTop;
112
+ } while ((el = el.offsetParent) && el.nodeType < 9);
113
+ el = element;
114
+ do {
115
+ left -= el.scrollLeft;
116
+ top -= el.scrollTop;
117
+ } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
118
+ }
119
+ return {
120
+ top,
121
+ right: innerWidth - left - element.offsetWidth,
122
+ bottom: innerHeight - top - element.offsetHeight,
123
+ left
124
+ };
125
+ }
126
+ var fallback = {
127
+ pageX: 0,
128
+ pageY: 0,
129
+ clientX: 0,
130
+ clientY: 0
131
+ };
132
+ function getEventPoint(event, type = "page") {
133
+ var _a, _b;
134
+ const point = isTouchEvent(event) ? (_b = (_a = event.touches[0]) != null ? _a : event.changedTouches[0]) != null ? _b : fallback : event;
135
+ return { x: point[`${type}X`], y: point[`${type}Y`] };
136
+ }
137
+ function getPointRelativeToNode(point, element) {
138
+ const offset = getElementOffset(element);
139
+ const x = point.x - offset.left;
140
+ const y = point.y - offset.top;
141
+ return { x, y };
142
+ }
185
143
  function getDescriptor(el, options) {
186
144
  var _a;
187
145
  const { type, property } = options;
188
- const win = getOwnerWindow(el);
189
- const _type = type === "input" ? "HTMLInputElement" : "HTMLTextAreaElement";
190
- const proto = win[_type].prototype;
146
+ const proto = getWindow(el)[type].prototype;
191
147
  return (_a = Object.getOwnPropertyDescriptor(proto, property)) != null ? _a : {};
192
148
  }
193
149
  function dispatchInputValueEvent(el, value) {
194
150
  var _a;
195
- const win = getOwnerWindow(el);
151
+ const win = getWindow(el);
196
152
  if (!(el instanceof win.HTMLInputElement))
197
153
  return;
198
- const desc = getDescriptor(el, { type: "input", property: "value" });
154
+ const desc = getDescriptor(el, { type: "HTMLInputElement", property: "value" });
199
155
  (_a = desc.set) == null ? void 0 : _a.call(el, value);
200
156
  const event = new win.Event("input", { bubbles: true });
201
157
  el.dispatchEvent(event);
@@ -237,6 +193,89 @@ function getEventStep(event) {
237
193
  return isSkipKey ? 10 : 1;
238
194
  }
239
195
  }
196
+ var isRef = (v) => hasProp(v, "current");
197
+ var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
198
+ function extractInfo(event, type = "page") {
199
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
200
+ return {
201
+ point: {
202
+ x: point[`${type}X`],
203
+ y: point[`${type}Y`]
204
+ }
205
+ };
206
+ }
207
+ function addDomEvent(target, eventName, handler, options) {
208
+ const node = isRef(target) ? target.current : runIfFn(target);
209
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
210
+ return () => {
211
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
212
+ };
213
+ }
214
+ function addPointerEvent(target, event, listener, options) {
215
+ var _a;
216
+ const type = (_a = getEventName(event)) != null ? _a : event;
217
+ return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
218
+ }
219
+ function wrapHandler(fn, filter = false) {
220
+ const listener = (event) => {
221
+ fn(event, extractInfo(event));
222
+ };
223
+ return filter ? filterPrimaryPointer(listener) : listener;
224
+ }
225
+ function filterPrimaryPointer(fn) {
226
+ return (event) => {
227
+ var _a;
228
+ const win = (_a = event.view) != null ? _a : window;
229
+ const isMouseEvent2 = event instanceof win.MouseEvent;
230
+ const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
231
+ if (isPrimary)
232
+ fn(event);
233
+ };
234
+ }
235
+ var mouseEventNames = {
236
+ pointerdown: "mousedown",
237
+ pointermove: "mousemove",
238
+ pointerup: "mouseup",
239
+ pointercancel: "mousecancel",
240
+ pointerover: "mouseover",
241
+ pointerout: "mouseout",
242
+ pointerenter: "mouseenter",
243
+ pointerleave: "mouseleave"
244
+ };
245
+ var touchEventNames = {
246
+ pointerdown: "touchstart",
247
+ pointermove: "touchmove",
248
+ pointerup: "touchend",
249
+ pointercancel: "touchcancel"
250
+ };
251
+ function getEventName(evt) {
252
+ if (supportsPointerEvent())
253
+ return evt;
254
+ if (supportsTouchEvent())
255
+ return touchEventNames[evt];
256
+ if (supportsMouseEvent())
257
+ return mouseEventNames[evt];
258
+ return evt;
259
+ }
260
+ function nextTick(fn) {
261
+ const set = /* @__PURE__ */ new Set();
262
+ function raf2(fn2) {
263
+ const id = globalThis.requestAnimationFrame(fn2);
264
+ set.add(() => globalThis.cancelAnimationFrame(id));
265
+ }
266
+ raf2(() => raf2(fn));
267
+ return function cleanup() {
268
+ set.forEach(function(fn2) {
269
+ fn2();
270
+ });
271
+ };
272
+ }
273
+ function raf(fn) {
274
+ const id = globalThis.requestAnimationFrame(fn);
275
+ return function cleanup() {
276
+ globalThis.cancelAnimationFrame(id);
277
+ };
278
+ }
240
279
  var state = "default";
241
280
  var savedUserSelect = "";
242
281
  var modifiedElementMap = /* @__PURE__ */ new WeakMap();
@@ -298,26 +337,7 @@ function trackPointerMove(opts) {
298
337
  }
299
338
  onPointerMove(info, event);
300
339
  };
301
- return pipe(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
302
- }
303
-
304
- // ../../utilities/rect/dist/index.mjs
305
- var isArray2 = (v) => Array.isArray(v);
306
- var isObject2 = (v) => !(v == null || typeof v !== "object" || isArray2(v));
307
- var hasProp2 = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
308
- var isTouchEvent2 = (v) => isObject2(v) && hasProp2(v, "touches");
309
- var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
310
- function getEventPoint(e, t = "page") {
311
- const p = isTouchEvent2(e) ? e.touches[0] || e.changedTouches[0] || fallback2 : e;
312
- return { x: p[`${t}X`], y: p[`${t}Y`] };
313
- }
314
- function relativeToNode(p, el) {
315
- const dx = p.x - el.offsetLeft - el.clientLeft + el.scrollLeft;
316
- const dy = p.y - el.offsetTop - el.clientTop + el.scrollTop;
317
- return {
318
- point: { x: dx, y: dy },
319
- progress: { x: dx / el.offsetWidth, y: dy / el.offsetHeight }
320
- };
340
+ return callAll(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
321
341
  }
322
342
 
323
343
  // ../../utilities/number/dist/index.mjs
@@ -553,15 +573,17 @@ var dom = __spreadProps(__spreadValues({}, styles), {
553
573
  getControlEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getControlId(ctx)),
554
574
  getInputEl: (ctx) => dom.getRootNode(ctx).getElementById(dom.getInputId(ctx)),
555
575
  getValueFromPoint(ctx, point) {
556
- const control = dom.getControlEl(ctx);
557
- if (!control)
576
+ const el = dom.getControlEl(ctx);
577
+ if (!el)
558
578
  return;
559
- const { progress } = relativeToNode(point, control);
579
+ const relativePoint = getPointRelativeToNode(point, el);
580
+ const percentX = relativePoint.x / el.offsetWidth;
581
+ const percentY = relativePoint.y / el.offsetHeight;
560
582
  let percent;
561
583
  if (ctx.isHorizontal) {
562
- percent = ctx.isRtl ? 1 - progress.x : progress.x;
584
+ percent = ctx.isRtl ? 1 - percentX : percentX;
563
585
  } else {
564
- percent = 1 - progress.y;
586
+ percent = 1 - percentY;
565
587
  }
566
588
  return utils.fromPercent(ctx, percent);
567
589
  },
@@ -573,22 +595,8 @@ var dom = __spreadProps(__spreadValues({}, styles), {
573
595
  }
574
596
  });
575
597
 
576
- // ../../types/dist/index.mjs
577
- function createNormalizer(fn) {
578
- return new Proxy({}, {
579
- get() {
580
- return fn;
581
- }
582
- });
583
- }
584
- var normalizeProp = createNormalizer((v) => v);
585
-
586
- // ../../utilities/core/dist/index.mjs
587
- var isLeftClick2 = (v) => v.button === 0;
588
- var isModifiedEvent = (v) => v.ctrlKey || v.altKey || v.metaKey;
589
-
590
598
  // src/slider.connect.ts
591
- function connect(state2, send, normalize = normalizeProp) {
599
+ function connect(state2, send, normalize) {
592
600
  var _a, _b;
593
601
  const ariaLabel = state2.context["aria-label"];
594
602
  const ariaLabelledBy = state2.context["aria-labelledby"];
@@ -765,7 +773,7 @@ function connect(state2, send, normalize = normalizeProp) {
765
773
  if (!isInteractive)
766
774
  return;
767
775
  const evt = getNativeEvent(event);
768
- if (!isLeftClick2(evt) || isModifiedEvent(evt))
776
+ if (!isLeftClick(evt) || isModifiedEvent(evt))
769
777
  return;
770
778
  const point = getEventPoint(evt);
771
779
  send({ type: "POINTER_DOWN", point });
@@ -1,6 +1,6 @@
1
- import { PropTypes, ReactPropTypes } from "@zag-js/types";
1
+ import type { NormalizeProps, PropTypes } from "@zag-js/types";
2
2
  import type { Send, State } from "./slider.types";
3
- export declare function connect<T extends PropTypes = ReactPropTypes>(state: State, send: Send, normalize?: import("@zag-js/types").NormalizeProps): {
3
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
4
4
  isFocused: boolean;
5
5
  isDragging: boolean;
6
6
  value: number;
@@ -1,5 +1,4 @@
1
- import type { Point } from "@zag-js/rect-utils";
2
- import type { MachineContext as Ctx } from "./slider.types";
1
+ import type { MachineContext as Ctx, Point } from "./slider.types";
3
2
  export declare const dom: {
4
3
  getDoc: (ctx: Ctx) => Document;
5
4
  getRootNode: (ctx: Ctx) => Document | ShadowRoot;
@@ -19,12 +18,12 @@ export declare const dom: {
19
18
  getValueFromPoint(ctx: Ctx, point: Point): number | undefined;
20
19
  dispatchChangeEvent(ctx: Ctx): void;
21
20
  getThumbOffset: (ctx: import("./slider.types").SharedContext) => string;
22
- getControlStyle: () => import("@zag-js/types").Style;
23
- getThumbStyle: (ctx: import("./slider.types").SharedContext) => import("@zag-js/types").Style;
24
- getRangeStyle: (ctx: Pick<import("./slider.types").SharedContext, "isVertical" | "isRtl">) => import("@zag-js/types").Style;
25
- getRootStyle: (ctx: Ctx) => import("@zag-js/types").Style;
26
- getMarkerStyle: (ctx: Pick<import("./slider.types").SharedContext, "isHorizontal" | "isRtl">, percent: number) => import("@zag-js/types").Style;
27
- getLabelStyle: () => import("@zag-js/types").Style;
28
- getTrackStyle: () => import("@zag-js/types").Style;
29
- getMarkerGroupStyle: () => import("@zag-js/types").Style;
21
+ getControlStyle: () => JSX.CSSProperties;
22
+ getThumbStyle: (ctx: import("./slider.types").SharedContext) => JSX.CSSProperties;
23
+ getRangeStyle: (ctx: Pick<import("./slider.types").SharedContext, "isVertical" | "isRtl">) => JSX.CSSProperties;
24
+ getRootStyle: (ctx: Ctx) => JSX.CSSProperties;
25
+ getMarkerStyle: (ctx: Pick<import("./slider.types").SharedContext, "isHorizontal" | "isRtl">, percent: number) => JSX.CSSProperties;
26
+ getLabelStyle: () => JSX.CSSProperties;
27
+ getTrackStyle: () => JSX.CSSProperties;
28
+ getMarkerGroupStyle: () => JSX.CSSProperties;
30
29
  };
@@ -1,2 +1,2 @@
1
- import { MachineContext, MachineState, UserDefinedContext } from "./slider.types";
1
+ import type { MachineContext, MachineState, UserDefinedContext } from "./slider.types";
2
2
  export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
@@ -166,4 +166,8 @@ export declare type SharedContext = {
166
166
  orientation?: "horizontal" | "vertical";
167
167
  readonly hasMeasuredThumbSize: boolean;
168
168
  };
169
+ export declare type Point = {
170
+ x: number;
171
+ y: number;
172
+ };
169
173
  export {};
@@ -1,4 +1,4 @@
1
- import { MachineContext as Ctx } from "./slider.types";
1
+ import type { MachineContext as Ctx } from "./slider.types";
2
2
  export declare const utils: {
3
3
  fromPercent(ctx: Ctx, percent: number): number;
4
4
  clamp(ctx: Ctx, value: number): number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/slider",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "description": "Core logic for the slider widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -29,11 +29,10 @@
29
29
  "url": "https://github.com/chakra-ui/zag/issues"
30
30
  },
31
31
  "dependencies": {
32
- "@zag-js/core": "0.1.6",
33
- "@zag-js/dom-utils": "0.1.5",
32
+ "@zag-js/core": "0.1.7",
33
+ "@zag-js/dom-utils": "0.1.6",
34
34
  "@zag-js/number-utils": "0.1.2",
35
- "@zag-js/rect-utils": "0.1.2",
36
- "@zag-js/types": "0.2.0",
35
+ "@zag-js/types": "0.2.1",
37
36
  "@zag-js/utils": "0.1.2"
38
37
  },
39
38
  "scripts": {