@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.mjs CHANGED
@@ -20,107 +20,104 @@ var __pow = Math.pow;
20
20
  var dataAttr = (guard) => {
21
21
  return guard ? "" : void 0;
22
22
  };
23
- function nextTick(fn) {
24
- const set = /* @__PURE__ */ new Set();
25
- function raf2(fn2) {
26
- const id = globalThis.requestAnimationFrame(fn2);
27
- set.add(() => globalThis.cancelAnimationFrame(id));
28
- }
29
- raf2(() => raf2(fn));
30
- return function cleanup() {
31
- set.forEach(function(fn2) {
32
- fn2();
33
- });
34
- };
35
- }
36
- function raf(fn) {
37
- const id = globalThis.requestAnimationFrame(fn);
38
- return function cleanup() {
39
- globalThis.cancelAnimationFrame(id);
40
- };
41
- }
42
- var noop = () => {
23
+ var runIfFn = (v, ...a) => {
24
+ const res = typeof v === "function" ? v(...a) : v;
25
+ return res != null ? res : void 0;
26
+ };
27
+ var callAll = (...fns) => (...a) => {
28
+ fns.forEach(function(fn) {
29
+ fn == null ? void 0 : fn(...a);
30
+ });
43
31
  };
44
- var pipe = (...fns) => (v) => fns.reduce((a, b) => b(a), v);
45
- var platform = (v) => isDom() && v.test(navigator.platform);
46
- var isDom = () => !!(typeof window !== "undefined");
47
- var isMac = () => platform(/^Mac/);
48
- var isIPhone = () => platform(/^iPhone/);
49
- var isIPad = () => platform(/^iPad/) || isMac() && navigator.maxTouchPoints > 1;
50
- var isIos = () => isIPhone() || isIPad();
51
32
  var isArray = (v) => Array.isArray(v);
52
33
  var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
53
- var isMouseEvent = (v) => isObject(v) && "button" in v;
34
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
35
+ var isDom = () => typeof window !== "undefined";
36
+ function getPlatform() {
37
+ var _a;
38
+ const agent = navigator.userAgentData;
39
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
40
+ }
41
+ var pt = (v) => isDom() && v.test(getPlatform());
42
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
43
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
44
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
45
+ var isIos = () => isApple() && !isMac();
46
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
47
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
48
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
49
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
50
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
54
51
  var isLeftClick = (v) => v.button === 0;
55
- function getListenerElements() {
56
- ;
57
- globalThis.__listenerElements__ = globalThis.__listenerElements__ || /* @__PURE__ */ new Map();
58
- return globalThis.__listenerElements__;
52
+ function getElementOffset(element) {
53
+ let left = 0;
54
+ let top = 0;
55
+ let el = element;
56
+ if (el.parentNode) {
57
+ do {
58
+ left += el.offsetLeft;
59
+ top += el.offsetTop;
60
+ } while ((el = el.offsetParent) && el.nodeType < 9);
61
+ el = element;
62
+ do {
63
+ left -= el.scrollLeft;
64
+ top -= el.scrollTop;
65
+ } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
66
+ }
67
+ return {
68
+ top,
69
+ right: innerWidth - left - element.offsetWidth,
70
+ bottom: innerHeight - top - element.offsetHeight,
71
+ left
72
+ };
59
73
  }
60
- function getListenerCache() {
61
- ;
62
- globalThis.__listenerCache__ = globalThis.__listenerCache__ || /* @__PURE__ */ new Map();
63
- return globalThis.__listenerCache__;
74
+ function getPointRelativeToNode(point, element) {
75
+ const offset = getElementOffset(element);
76
+ const x = point.x - offset.left;
77
+ const y = point.y - offset.top;
78
+ return { x, y };
64
79
  }
65
- function globalEventBus(node, type, handler, options) {
80
+ var rtlKeyMap = {
81
+ ArrowLeft: "ArrowRight",
82
+ ArrowRight: "ArrowLeft",
83
+ Home: "End",
84
+ End: "Home"
85
+ };
86
+ var sameKeyMap = {
87
+ Up: "ArrowUp",
88
+ Down: "ArrowDown",
89
+ Esc: "Escape",
90
+ " ": "Space",
91
+ ",": "Comma",
92
+ Left: "ArrowLeft",
93
+ Right: "ArrowRight"
94
+ };
95
+ function getEventKey(event, options = {}) {
66
96
  var _a;
67
- if (!node)
68
- return noop;
69
- const hash = JSON.stringify({ type, options });
70
- const listenerElements = getListenerElements();
71
- const listenerCache = getListenerCache();
72
- const group = listenerElements.get(node);
73
- if (!listenerElements.has(node)) {
74
- const group2 = /* @__PURE__ */ new Map([[hash, /* @__PURE__ */ new Set([handler])]]);
75
- listenerElements.set(node, group2);
76
- } else if (group == null ? void 0 : group.has(hash)) {
77
- (_a = group == null ? void 0 : group.get(hash)) == null ? void 0 : _a.add(handler);
78
- } else {
79
- group == null ? void 0 : group.set(hash, /* @__PURE__ */ new Set([handler]));
97
+ const { dir = "ltr", orientation = "horizontal" } = options;
98
+ let { key } = event;
99
+ key = (_a = sameKeyMap[key]) != null ? _a : key;
100
+ const isRtl = dir === "rtl" && orientation === "horizontal";
101
+ if (isRtl && key in rtlKeyMap) {
102
+ key = rtlKeyMap[key];
80
103
  }
81
- function attach(node2) {
82
- var _a2, _b;
83
- function listener(event) {
84
- var _a3;
85
- const group2 = listenerElements.get(node2);
86
- (_a3 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a3.forEach((fn) => fn(event));
87
- }
88
- if (!(listenerCache == null ? void 0 : listenerCache.has(node2))) {
89
- listenerCache.set(node2, /* @__PURE__ */ new Map([[hash, listener]]));
90
- node2.addEventListener(type, listener, options);
91
- return;
92
- }
93
- if (!((_a2 = listenerCache == null ? void 0 : listenerCache.get(node2)) == null ? void 0 : _a2.has(hash))) {
94
- (_b = listenerCache.get(node2)) == null ? void 0 : _b.set(hash, listener);
95
- node2.addEventListener(type, listener, options);
96
- }
104
+ return key;
105
+ }
106
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
107
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
108
+ function getEventStep(event) {
109
+ if (event.ctrlKey || event.metaKey) {
110
+ return 0.1;
111
+ } else {
112
+ const isPageKey = PAGE_KEYS.has(event.key);
113
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
114
+ return isSkipKey ? 10 : 1;
97
115
  }
98
- attach(node);
99
- return function remove() {
100
- var _a2, _b, _c, _d;
101
- if (!listenerElements.has(node))
102
- return;
103
- const group2 = listenerElements.get(node);
104
- (_a2 = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _a2.delete(handler);
105
- if (((_b = group2 == null ? void 0 : group2.get(hash)) == null ? void 0 : _b.size) === 0) {
106
- const listener = (_c = listenerCache.get(node)) == null ? void 0 : _c.get(hash);
107
- node.removeEventListener(type, listener, options);
108
- group2 == null ? void 0 : group2.delete(hash);
109
- (_d = listenerCache.get(node)) == null ? void 0 : _d.delete(hash);
110
- if ((group2 == null ? void 0 : group2.size) === 0) {
111
- listenerElements.delete(node);
112
- listenerCache.delete(node);
113
- }
114
- }
115
- };
116
116
  }
117
- var t = (v) => Object.prototype.toString.call(v).slice(8, -1);
118
- var isRef = (v) => t(v) === "Object" && "current" in v;
119
- var runIfFn = (fn) => t(fn) === "Function" ? fn() : fn;
120
- var isTouchEvent = (v) => t(v) === "Object" && !!v.touches;
121
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
117
+ var isRef = (v) => hasProp(v, "current");
118
+ var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
122
119
  function extractInfo(event, type = "page") {
123
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
120
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
124
121
  return {
125
122
  point: {
126
123
  x: point[`${type}X`],
@@ -128,9 +125,12 @@ function extractInfo(event, type = "page") {
128
125
  }
129
126
  };
130
127
  }
131
- function addDomEvent(target, event, listener, options) {
128
+ function addDomEvent(target, eventName, handler, options) {
132
129
  const node = isRef(target) ? target.current : runIfFn(target);
133
- return globalEventBus(node, event, listener, options);
130
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
131
+ return () => {
132
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
133
+ };
134
134
  }
135
135
  function addPointerEvent(target, event, listener, options) {
136
136
  var _a;
@@ -153,9 +153,6 @@ function filterPrimaryPointer(fn) {
153
153
  fn(event);
154
154
  };
155
155
  }
156
- var supportsPointerEvent = () => typeof window !== "undefined" && window.onpointerdown === null;
157
- var supportsTouchEvent = () => typeof window !== "undefined" && window.ontouchstart === null;
158
- var supportsMouseEvent = () => typeof window !== "undefined" && window.onmousedown === null;
159
156
  var mouseEventNames = {
160
157
  pointerdown: "mousedown",
161
158
  pointermove: "mousemove",
@@ -181,66 +178,24 @@ function getEventName(evt) {
181
178
  return mouseEventNames[evt];
182
179
  return evt;
183
180
  }
184
- var rtlKeyMap = {
185
- ArrowLeft: "ArrowRight",
186
- ArrowRight: "ArrowLeft",
187
- Home: "End",
188
- End: "Home"
189
- };
190
- var sameKeyMap = {
191
- Up: "ArrowUp",
192
- Down: "ArrowDown",
193
- Esc: "Escape",
194
- " ": "Space",
195
- ",": "Comma",
196
- Left: "ArrowLeft",
197
- Right: "ArrowRight"
198
- };
199
- function getEventKey(event, options = {}) {
200
- var _a;
201
- const { dir = "ltr", orientation = "horizontal" } = options;
202
- let { key } = event;
203
- key = (_a = sameKeyMap[key]) != null ? _a : key;
204
- const isRtl = dir === "rtl" && orientation === "horizontal";
205
- if (isRtl && key in rtlKeyMap) {
206
- key = rtlKeyMap[key];
207
- }
208
- return key;
209
- }
210
- var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
211
- var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
212
- function getEventStep(event) {
213
- if (event.ctrlKey || event.metaKey) {
214
- return 0.1;
215
- } else {
216
- const isPageKey = PAGE_KEYS.has(event.key);
217
- const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
218
- return isSkipKey ? 10 : 1;
181
+ function nextTick(fn) {
182
+ const set = /* @__PURE__ */ new Set();
183
+ function raf2(fn2) {
184
+ const id = globalThis.requestAnimationFrame(fn2);
185
+ set.add(() => globalThis.cancelAnimationFrame(id));
219
186
  }
187
+ raf2(() => raf2(fn));
188
+ return function cleanup() {
189
+ set.forEach(function(fn2) {
190
+ fn2();
191
+ });
192
+ };
220
193
  }
221
- function itemById(v, id) {
222
- return v.find((node) => node.id === id);
223
- }
224
- function indexOfId(v, id) {
225
- const item = itemById(v, id);
226
- return item ? v.indexOf(item) : -1;
227
- }
228
- var getValueText = (item) => {
229
- var _a, _b;
230
- return (_b = (_a = item.dataset.valuetext) != null ? _a : item.textContent) != null ? _b : "";
231
- };
232
- var match = (valueText, query2) => valueText.toLowerCase().startsWith(query2.toLowerCase());
233
- var wrap = (v, idx) => {
234
- return v.map((_, index) => v[(Math.max(idx, 0) + index) % v.length]);
235
- };
236
- function findByText(v, text, currentId) {
237
- const index = currentId ? indexOfId(v, currentId) : -1;
238
- let items = currentId ? wrap(v, index) : v;
239
- const isSingleKey = text.length === 1;
240
- if (isSingleKey) {
241
- items = items.filter((item) => item.id !== currentId);
242
- }
243
- return items.find((item) => match(getValueText(item), text));
194
+ function raf(fn) {
195
+ const id = globalThis.requestAnimationFrame(fn);
196
+ return function cleanup() {
197
+ globalThis.cancelAnimationFrame(id);
198
+ };
244
199
  }
245
200
  var state = "default";
246
201
  var savedUserSelect = "";
@@ -303,46 +258,8 @@ function trackPointerMove(opts) {
303
258
  }
304
259
  onPointerMove(info, event);
305
260
  };
306
- return pipe(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
261
+ return callAll(addPointerEvent(doc, "pointermove", handlePointerMove, false), addPointerEvent(doc, "pointerup", onPointerUp, false), addPointerEvent(doc, "pointercancel", onPointerUp, false), addPointerEvent(doc, "contextmenu", onPointerUp, false), disableTextSelection({ doc }));
307
262
  }
308
- function findByTypeahead(_items, options) {
309
- const { state: state2, activeId, key, timeout = 350 } = options;
310
- const search = state2.keysSoFar + key;
311
- const isRepeated = search.length > 1 && Array.from(search).every((char) => char === search[0]);
312
- const query2 = isRepeated ? search[0] : search;
313
- let items = _items.slice();
314
- const next = findByText(items, query2, activeId);
315
- function cleanup() {
316
- clearTimeout(state2.timer);
317
- state2.timer = -1;
318
- }
319
- function update(value) {
320
- state2.keysSoFar = value;
321
- cleanup();
322
- if (value !== "") {
323
- state2.timer = +setTimeout(() => {
324
- update("");
325
- cleanup();
326
- }, timeout);
327
- }
328
- }
329
- update(search);
330
- return next;
331
- }
332
- findByTypeahead.defaultOptions = {
333
- keysSoFar: "",
334
- timer: -1
335
- };
336
-
337
- // ../../types/dist/index.mjs
338
- function createNormalizer(fn) {
339
- return new Proxy({}, {
340
- get() {
341
- return fn;
342
- }
343
- });
344
- }
345
- var normalizeProp = createNormalizer((v) => v);
346
263
 
347
264
  // src/splitter.dom.ts
348
265
  var dom = {
@@ -394,7 +311,7 @@ var dom = {
394
311
  };
395
312
 
396
313
  // src/splitter.connect.ts
397
- function connect(state2, send, normalize = normalizeProp) {
314
+ function connect(state2, send, normalize) {
398
315
  const isHorizontal = state2.context.isHorizontal;
399
316
  const isDisabled = state2.context.disabled;
400
317
  const isFocused = state2.hasTag("focus");
@@ -562,11 +479,11 @@ import { createMachine, guards, ref } from "@zag-js/core";
562
479
 
563
480
  // ../../utilities/number/dist/index.mjs
564
481
  var __pow2 = Math.pow;
565
- function round(v, t2) {
482
+ function round(v, t) {
566
483
  let num = valueOf(v);
567
- const p = __pow2(10, t2 != null ? t2 : 10);
484
+ const p = __pow2(10, t != null ? t : 10);
568
485
  num = Math.round(num * p) / p;
569
- return t2 ? num.toFixed(t2) : v.toString();
486
+ return t ? num.toFixed(t) : v.toString();
570
487
  }
571
488
  function clamp(v, o) {
572
489
  return Math.min(Math.max(valueOf(v), o.min), o.max);
@@ -608,16 +525,6 @@ function decimalOperation(a, op, b) {
608
525
  }
609
526
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
610
527
 
611
- // ../../utilities/rect/dist/index.mjs
612
- function relativeToNode(p, el) {
613
- const dx = p.x - el.offsetLeft - el.clientLeft + el.scrollLeft;
614
- const dy = p.y - el.offsetTop - el.clientTop + el.scrollTop;
615
- return {
616
- point: { x: dx, y: dy },
617
- progress: { x: dx / el.offsetWidth, y: dy / el.offsetHeight }
618
- };
619
- }
620
-
621
528
  // src/splitter.machine.ts
622
529
  var { not } = guards;
623
530
  function machine(ctx = {}) {
@@ -824,11 +731,11 @@ function machine(ctx = {}) {
824
731
  });
825
732
  },
826
733
  setPointerValue(ctx2, evt) {
827
- const primaryPane = dom.getPrimaryPaneEl(ctx2);
828
- if (!primaryPane)
734
+ const el = dom.getPrimaryPaneEl(ctx2);
735
+ if (!el)
829
736
  return;
830
- const { point } = relativeToNode(evt.point, primaryPane);
831
- let currentPoint = ctx2.isHorizontal ? point.x : point.y;
737
+ const relativePoint = getPointRelativeToNode(evt.point, el);
738
+ let currentPoint = ctx2.isHorizontal ? relativePoint.x : relativePoint.y;
832
739
  let value = parseFloat(snapToStep(clamp(currentPoint, ctx2), ctx2.step));
833
740
  if (Math.abs(value - ctx2.min) <= ctx2.snapOffset) {
834
741
  value = ctx2.min;
@@ -844,4 +751,3 @@ export {
844
751
  connect,
845
752
  machine
846
753
  };
847
- //# sourceMappingURL=index.mjs.map
@@ -1,6 +1,6 @@
1
- import { PropTypes, ReactPropTypes } from "@zag-js/types";
2
- import { Send, State } from "./splitter.types";
3
- export declare function connect<T extends PropTypes = ReactPropTypes>(state: State, send: Send, normalize?: import("@zag-js/types").NormalizeProps): {
1
+ import type { NormalizeProps, PropTypes } from "@zag-js/types";
2
+ import type { Send, State } from "./splitter.types";
3
+ export declare function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): {
4
4
  isCollapsed: boolean;
5
5
  isExpanded: boolean;
6
6
  isFocused: boolean;
@@ -17,4 +17,3 @@ export declare function connect<T extends PropTypes = ReactPropTypes>(state: Sta
17
17
  labelProps: T["element"];
18
18
  splitterProps: T["element"];
19
19
  };
20
- //# sourceMappingURL=splitter.connect.d.ts.map
@@ -1,4 +1,4 @@
1
- import { MachineContext as Ctx } from "./splitter.types";
1
+ import type { MachineContext as Ctx } from "./splitter.types";
2
2
  export declare const dom: {
3
3
  getDoc: (ctx: Ctx) => Document;
4
4
  getRootNode: (ctx: Ctx) => Document | ShadowRoot;
@@ -10,6 +10,5 @@ export declare const dom: {
10
10
  getSecondaryPaneId: (ctx: Ctx) => string;
11
11
  getSplitterEl: (ctx: Ctx) => HTMLElement;
12
12
  getPrimaryPaneEl: (ctx: Ctx) => HTMLElement;
13
- getCursor(ctx: Ctx): "default" | (string & {}) | "col-resize" | "e-resize" | "n-resize" | "row-resize" | "s-resize" | "w-resize";
13
+ getCursor(ctx: Ctx): any;
14
14
  };
15
- //# sourceMappingURL=splitter.dom.d.ts.map
@@ -1,3 +1,2 @@
1
- import { MachineContext, MachineState, UserDefinedContext } from "./splitter.types";
1
+ import type { MachineContext, MachineState, UserDefinedContext } from "./splitter.types";
2
2
  export declare function machine(ctx?: UserDefinedContext): import("@zag-js/core").Machine<MachineContext, MachineState, import("@zag-js/core").StateMachine.AnyEventObject>;
3
- //# sourceMappingURL=splitter.machine.d.ts.map
@@ -1,5 +1,5 @@
1
- import { StateMachine as S } from "@zag-js/core";
2
- import { Context, DirectionProperty } from "@zag-js/types";
1
+ import type { StateMachine as S } from "@zag-js/core";
2
+ import type { Context, DirectionProperty } from "@zag-js/types";
3
3
  declare type ElementIds = Partial<{
4
4
  root: string;
5
5
  splitter: string;
@@ -92,4 +92,3 @@ export declare type MachineState = {
92
92
  export declare type State = S.State<MachineContext, MachineState>;
93
93
  export declare type Send = S.Send<S.AnyEventObject>;
94
94
  export {};
95
- //# sourceMappingURL=splitter.types.d.ts.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/splitter",
3
- "version": "0.1.6",
3
+ "version": "0.1.9",
4
4
  "description": "Core logic for the splitter widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -30,11 +30,10 @@
30
30
  "url": "https://github.com/chakra-ui/zag/issues"
31
31
  },
32
32
  "dependencies": {
33
- "@zag-js/core": "0.1.5",
34
- "@zag-js/dom-utils": "0.1.3",
33
+ "@zag-js/core": "0.1.7",
34
+ "@zag-js/dom-utils": "0.1.6",
35
35
  "@zag-js/number-utils": "0.1.2",
36
- "@zag-js/rect-utils": "0.1.1",
37
- "@zag-js/types": "0.1.2"
36
+ "@zag-js/types": "0.2.1"
38
37
  },
39
38
  "scripts": {
40
39
  "build:fast": "zag build",
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,OAAO,EAAE,OAAO,EAAE,MAAM,oBAAoB,CAAA;AAC5C,YAAY,EAAE,kBAAkB,IAAI,OAAO,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAA"}