@zag-js/splitter 0.1.6 → 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.d.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export { connect } from "./splitter.connect";
2
2
  export { machine } from "./splitter.machine";
3
3
  export type { UserDefinedContext as Context, MachineState } from "./splitter.types";
4
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
+ "use strict";
1
2
  var __defProp = Object.defineProperty;
2
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
@@ -43,107 +44,104 @@ var __pow = Math.pow;
43
44
  var dataAttr = (guard) => {
44
45
  return guard ? "" : void 0;
45
46
  };
46
- function nextTick(fn) {
47
- const set = /* @__PURE__ */ new Set();
48
- function raf2(fn2) {
49
- const id = globalThis.requestAnimationFrame(fn2);
50
- set.add(() => globalThis.cancelAnimationFrame(id));
51
- }
52
- raf2(() => raf2(fn));
53
- return function cleanup() {
54
- set.forEach(function(fn2) {
55
- fn2();
56
- });
57
- };
58
- }
59
- function raf(fn) {
60
- const id = globalThis.requestAnimationFrame(fn);
61
- return function cleanup() {
62
- globalThis.cancelAnimationFrame(id);
63
- };
64
- }
65
- var noop = () => {
47
+ var runIfFn = (v, ...a) => {
48
+ const res = typeof v === "function" ? v(...a) : v;
49
+ return res != null ? res : void 0;
50
+ };
51
+ var callAll = (...fns) => (...a) => {
52
+ fns.forEach(function(fn) {
53
+ fn == null ? void 0 : fn(...a);
54
+ });
66
55
  };
67
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
68
- var platform = (v) => isDom() && v.test(navigator.platform);
69
- var isDom = () => !!(typeof window !== "undefined");
70
- var isMac = () => platform(/^Mac/);
71
- var isIPhone = () => platform(/^iPhone/);
72
- var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
73
- var isIos = () => isIPhone() || isIPad();
74
56
  var isArray = (v) => Array.isArray(v);
75
57
  var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
76
- var isMouseEvent = (v) => isObject(v) && "button" in v;
58
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
59
+ var isDom = () => typeof window !== "undefined";
60
+ function getPlatform() {
61
+ var _a;
62
+ const agent = navigator.userAgentData;
63
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
64
+ }
65
+ var pt = (v) => isDom() && v.test(getPlatform());
66
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
67
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
68
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
69
+ var isIos = () => isApple() && !isMac();
70
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
71
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
72
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
73
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
74
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
77
75
  var isLeftClick = (v) => v.button === 0;
78
- function getListenerElements() {
79
- ;
80
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
81
- return globalThis.__listenerElements__;
76
+ function getElementOffset(element) {
77
+ let left = 0;
78
+ let top = 0;
79
+ let el = element;
80
+ if (el.parentNode) {
81
+ do {
82
+ left += el.offsetLeft;
83
+ top += el.offsetTop;
84
+ } while ((el = el.offsetParent) && el.nodeType < 9);
85
+ el = element;
86
+ do {
87
+ left -= el.scrollLeft;
88
+ top -= el.scrollTop;
89
+ } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
90
+ }
91
+ return {
92
+ top,
93
+ right: innerWidth - left - element.offsetWidth,
94
+ bottom: innerHeight - top - element.offsetHeight,
95
+ left
96
+ };
82
97
  }
83
- function getListenerCache() {
84
- ;
85
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
86
- return globalThis.__listenerCache__;
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 };
87
103
  }
88
- function globalEventBus(node, type, handler, options) {
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 = {}) {
89
120
  var _a;
90
- if (!node)
91
- return noop;
92
- const hash = JSON.stringify({ type, options });
93
- const listenerElements = getListenerElements();
94
- const listenerCache = getListenerCache();
95
- const group = listenerElements.get(node);
96
- if (!listenerElements.has(node)) {
97
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
98
- listenerElements.set(node, group2);
99
- } else if (group == null ? void 0 : group.has(hash)) {
100
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
101
- } else {
102
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
121
+ const { dir = "ltr", orientation = "horizontal" } = options;
122
+ let { key } = event;
123
+ key = (_a = sameKeyMap[key]) != null ? _a : key;
124
+ const isRtl = dir === "rtl" && orientation === "horizontal";
125
+ if (isRtl && key in rtlKeyMap) {
126
+ key = rtlKeyMap[key];
103
127
  }
104
- function attach(node2) {
105
- var _a2, _b;
106
- function listener(event) {
107
- var _a3;
108
- const group2 = listenerElements.get(node2);
109
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
110
- }
111
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
112
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
113
- node2.addEventListener(type, listener, options);
114
- return;
115
- }
116
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
117
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
118
- node2.addEventListener(type, listener, options);
119
- }
128
+ return key;
129
+ }
130
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
131
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
132
+ function getEventStep(event) {
133
+ if (event.ctrlKey || event.metaKey) {
134
+ return 0.1;
135
+ } else {
136
+ const isPageKey = PAGE_KEYS.has(event.key);
137
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
138
+ return isSkipKey ? 10 : 1;
120
139
  }
121
- attach(node);
122
- return function remove() {
123
- var _a2, _b, _c, _d;
124
- if (!listenerElements.has(node))
125
- return;
126
- const group2 = listenerElements.get(node);
127
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
128
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
129
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
130
- node.removeEventListener(type, listener, options);
131
- group2 == null ? void 0 : group2.delete(hash);
132
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
133
- if ((group2 == null ? void 0 : group2.size) === 0) {
134
- listenerElements.delete(node);
135
- listenerCache.delete(node);
136
- }
137
- }
138
- };
139
140
  }
140
- var t = (v) => Object.prototype.toString.call(v).slice(8, -1);
141
- var isRef = (v) => t(v) === "Object" && "current" in v;
142
- var runIfFn = (fn) => t(fn) === "Function" ? fn() : fn;
143
- var isTouchEvent = (v) => t(v) === "Object" && !!v.touches;
144
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
141
+ var isRef = (v) => hasProp(v, "current");
142
+ var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
145
143
  function extractInfo(event, type = "page") {
146
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
144
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
147
145
  return {
148
146
  point: {
149
147
  x: point[`${type}X`],
@@ -151,9 +149,12 @@ function extractInfo(event, type = "page") {
151
149
  }
152
150
  };
153
151
  }
154
- function addDomEvent(target, event, listener, options) {
152
+ function addDomEvent(target, eventName, handler, options) {
155
153
  const node = isRef(target) ? target.current : runIfFn(target);
156
- return globalEventBus(node, event, listener, options);
154
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
155
+ return () => {
156
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
157
+ };
157
158
  }
158
159
  function addPointerEvent(target, event, listener, options) {
159
160
  var _a;
@@ -176,9 +177,6 @@ function filterPrimaryPointer(fn) {
176
177
  fn(event);
177
178
  };
178
179
  }
179
- var supportsPointerEvent = () => typeof window !== "undefined" && window.onpointerdown === null;
180
- var supportsTouchEvent = () => typeof window !== "undefined" && window.ontouchstart === null;
181
- var supportsMouseEvent = () => typeof window !== "undefined" && window.onmousedown === null;
182
180
  var mouseEventNames = {
183
181
  pointerdown: "mousedown",
184
182
  pointermove: "mousemove",
@@ -204,66 +202,24 @@ function getEventName(evt) {
204
202
  return mouseEventNames[evt];
205
203
  return evt;
206
204
  }
207
- var rtlKeyMap = {
208
- ArrowLeft: "ArrowRight",
209
- ArrowRight: "ArrowLeft",
210
- Home: "End",
211
- End: "Home"
212
- };
213
- var sameKeyMap = {
214
- Up: "ArrowUp",
215
- Down: "ArrowDown",
216
- Esc: "Escape",
217
- " ": "Space",
218
- ",": "Comma",
219
- Left: "ArrowLeft",
220
- Right: "ArrowRight"
221
- };
222
- function getEventKey(event, options = {}) {
223
- var _a;
224
- const { dir = "ltr", orientation = "horizontal" } = options;
225
- let { key } = event;
226
- key = (_a = sameKeyMap[key]) != null ? _a : key;
227
- const isRtl = dir === "rtl" && orientation === "horizontal";
228
- if (isRtl && key in rtlKeyMap) {
229
- key = rtlKeyMap[key];
230
- }
231
- return key;
232
- }
233
- var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
234
- var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
235
- function getEventStep(event) {
236
- if (event.ctrlKey || event.metaKey) {
237
- return 0.1;
238
- } else {
239
- const isPageKey = PAGE_KEYS.has(event.key);
240
- const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
241
- return isSkipKey ? 10 : 1;
205
+ function nextTick(fn) {
206
+ const set = /* @__PURE__ */ new Set();
207
+ function raf2(fn2) {
208
+ const id = globalThis.requestAnimationFrame(fn2);
209
+ set.add(() => globalThis.cancelAnimationFrame(id));
242
210
  }
211
+ raf2(() => raf2(fn));
212
+ return function cleanup() {
213
+ set.forEach(function(fn2) {
214
+ fn2();
215
+ });
216
+ };
243
217
  }
244
- function itemById(v, id) {
245
- return v.find((node) => node.id === id);
246
- }
247
- function indexOfId(v, id) {
248
- const item = itemById(v, id);
249
- return item ? v.indexOf(item) : -1;
250
- }
251
- var getValueText = (item) => {
252
- var _a, _b;
253
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
254
- };
255
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
256
- var wrap = (v, idx) => {
257
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
258
- };
259
- function findByText(v, text, currentId) {
260
- const index = currentId ? indexOfId(v, currentId) : -1;
261
- let items = currentId ? wrap(v, index) : v;
262
- const isSingleKey = text.length === 1;
263
- if (isSingleKey) {
264
- items = items.filter((item) => item.id !== currentId);
265
- }
266
- return items.find((item) => match(getValueText(item), text));
218
+ function raf(fn) {
219
+ const id = globalThis.requestAnimationFrame(fn);
220
+ return function cleanup() {
221
+ globalThis.cancelAnimationFrame(id);
222
+ };
267
223
  }
268
224
  var state = "default";
269
225
  var savedUserSelect = "";
@@ -326,46 +282,8 @@ function trackPointerMove(opts) {
326
282
  }
327
283
  onPointerMove(info, event);
328
284
  };
329
- return pipe(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
285
+ return callAll(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
330
286
  }
331
- function findByTypeahead(_items, options) {
332
- const { state: state2, activeId, key, timeout = 350 } = options;
333
- const search = state2.keysSoFar + key;
334
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
335
- const query2 = isRepeated ? search[0] : search;
336
- let items = _items.slice();
337
- const next = findByText(items, query2, activeId);
338
- function cleanup() {
339
- clearTimeout(state2.timer);
340
- state2.timer = -1;
341
- }
342
- function update(value) {
343
- state2.keysSoFar = value;
344
- cleanup();
345
- if (value !== "") {
346
- state2.timer = +setTimeout(() => {
347
- update("");
348
- cleanup();
349
- }, timeout);
350
- }
351
- }
352
- update(search);
353
- return next;
354
- }
355
- findByTypeahead.defaultOptions = {
356
- keysSoFar: "",
357
- timer: -1
358
- };
359
-
360
- // ../../types/dist/index.mjs
361
- function createNormalizer(fn) {
362
- return new Proxy({}, {
363
- get() {
364
- return fn;
365
- }
366
- });
367
- }
368
- var normalizeProp = createNormalizer((v) => v);
369
287
 
370
288
  // src/splitter.dom.ts
371
289
  var dom = {
@@ -417,7 +335,7 @@ var dom = {
417
335
  };
418
336
 
419
337
  // src/splitter.connect.ts
420
- function connect(state2, send, normalize = normalizeProp) {
338
+ function connect(state2, send, normalize) {
421
339
  const isHorizontal = state2.context.isHorizontal;
422
340
  const isDisabled = state2.context.disabled;
423
341
  const isFocused = state2.hasTag("focus");
@@ -585,11 +503,11 @@ var import_core = require("@zag-js/core");
585
503
 
586
504
  // ../../utilities/number/dist/index.mjs
587
505
  var __pow2 = Math.pow;
588
- function round(v, t2) {
506
+ function round(v, t) {
589
507
  let num = valueOf(v);
590
- const p = __pow2(10, t2 != null ? t2 : 10);
508
+ const p = __pow2(10, t != null ? t : 10);
591
509
  num = Math.round(num * p) / p;
592
- return t2 ? num.toFixed(t2) : v.toString();
510
+ return t ? num.toFixed(t) : v.toString();
593
511
  }
594
512
  function clamp(v, o) {
595
513
  return Math.min(Math.max(valueOf(v), o.min), o.max);
@@ -631,16 +549,6 @@ function decimalOperation(a, op, b) {
631
549
  }
632
550
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
633
551
 
634
- // ../../utilities/rect/dist/index.mjs
635
- function relativeToNode(p, el) {
636
- const dx = p.x - el.offsetLeft - el.clientLeft + el.scrollLeft;
637
- const dy = p.y - el.offsetTop - el.clientTop + el.scrollTop;
638
- return {
639
- point: { x: dx, y: dy },
640
- progress: { x: dx / el.offsetWidth, y: dy / el.offsetHeight }
641
- };
642
- }
643
-
644
552
  // src/splitter.machine.ts
645
553
  var { not } = import_core.guards;
646
554
  function machine(ctx = {}) {
@@ -847,11 +755,11 @@ function machine(ctx = {}) {
847
755
  });
848
756
  },
849
757
  setPointerValue(ctx2, evt) {
850
- const primaryPane = dom.getPrimaryPaneEl(ctx2);
851
- if (!primaryPane)
758
+ const el = dom.getPrimaryPaneEl(ctx2);
759
+ if (!el)
852
760
  return;
853
- const { point } = relativeToNode(evt.point, primaryPane);
854
- let currentPoint = ctx2.isHorizontal ? point.x : point.y;
761
+ const relativePoint = getPointRelativeToNode(evt.point, el);
762
+ let currentPoint = ctx2.isHorizontal ? relativePoint.x : relativePoint.y;
855
763
  let value = parseFloat(snapToStep(clamp(currentPoint, ctx2), ctx2.step));
856
764
  if (Math.abs(value - ctx2.min) <= ctx2.snapOffset) {
857
765
  value = ctx2.min;
@@ -863,4 +771,3 @@ function machine(ctx = {}) {
863
771
  }
864
772
  });
865
773
  }
866
- //# sourceMappingURL=index.js.map