@zag-js/splitter 0.1.14 → 0.2.0

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 +87 -33
  2. package/dist/index.mjs +774 -0
  3. package/package.json +10 -10
package/dist/index.js CHANGED
@@ -1,10 +1,37 @@
1
- // ../../utilities/dom/dist/index.js
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
+ });
26
+ module.exports = __toCommonJS(src_exports);
27
+
28
+ // ../../utilities/dom/dist/index.mjs
2
29
  var dataAttr = (guard) => {
3
30
  return guard ? "" : void 0;
4
31
  };
5
32
  var runIfFn = (v, ...a) => {
6
33
  const res = typeof v === "function" ? v(...a) : v;
7
- return res ?? void 0;
34
+ return res != null ? res : void 0;
8
35
  };
9
36
  var callAll = (...fns) => (...a) => {
10
37
  fns.forEach(function(fn) {
@@ -16,8 +43,9 @@ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
16
43
  var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
17
44
  var isDom = () => typeof window !== "undefined";
18
45
  function getPlatform() {
46
+ var _a;
19
47
  const agent = navigator.userAgentData;
20
- return (agent == null ? void 0 : agent.platform) ?? navigator.platform;
48
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
21
49
  }
22
50
  var pt = (v) => isDom() && v.test(getPlatform());
23
51
  var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
@@ -31,22 +59,44 @@ function isWindow(value) {
31
59
  return (value == null ? void 0 : value.toString()) === "[object Window]";
32
60
  }
33
61
  function getDocument(el) {
62
+ var _a;
34
63
  if (isWindow(el))
35
64
  return el.document;
36
65
  if (isDocument(el))
37
66
  return el;
38
- return (el == null ? void 0 : el.ownerDocument) ?? document;
67
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
39
68
  }
40
69
  function defineDomHelpers(helpers) {
41
70
  const dom2 = {
42
71
  getRootNode: (ctx) => {
43
- var _a;
44
- return ((_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) ?? document;
72
+ var _a, _b;
73
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
45
74
  },
46
75
  getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
47
- getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
76
+ getWin: (ctx) => {
77
+ var _a;
78
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
79
+ },
48
80
  getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
49
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
81
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
82
+ createEmitter: (ctx, target) => {
83
+ const win = dom2.getWin(ctx);
84
+ return function emit(evt, detail, options) {
85
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
86
+ const eventName = `zag:${evt}`;
87
+ const init = { bubbles, cancelable, composed, detail };
88
+ const event = new win.CustomEvent(eventName, init);
89
+ target.dispatchEvent(event);
90
+ };
91
+ },
92
+ createListener: (target) => {
93
+ return function listen(evt, handler) {
94
+ const eventName = `zag:${evt}`;
95
+ const listener = (e) => handler(e);
96
+ target.addEventListener(eventName, listener);
97
+ return () => target.removeEventListener(eventName, listener);
98
+ };
99
+ }
50
100
  };
51
101
  return {
52
102
  ...dom2,
@@ -101,9 +151,10 @@ var sameKeyMap = {
101
151
  Right: "ArrowRight"
102
152
  };
103
153
  function getEventKey(event, options = {}) {
154
+ var _a;
104
155
  const { dir = "ltr", orientation = "horizontal" } = options;
105
156
  let { key } = event;
106
- key = sameKeyMap[key] ?? key;
157
+ key = (_a = sameKeyMap[key]) != null ? _a : key;
107
158
  const isRtl = dir === "rtl" && orientation === "horizontal";
108
159
  if (isRtl && key in rtlKeyMap) {
109
160
  key = rtlKeyMap[key];
@@ -140,7 +191,8 @@ function addDomEvent(target, eventName, handler, options) {
140
191
  };
141
192
  }
142
193
  function addPointerEvent(target, event, listener, options) {
143
- const type = getEventName(event) ?? event;
194
+ var _a;
195
+ const type = (_a = getEventName(event)) != null ? _a : event;
144
196
  return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
145
197
  }
146
198
  function wrapHandler(fn, filter = false) {
@@ -151,7 +203,8 @@ function wrapHandler(fn, filter = false) {
151
203
  }
152
204
  function filterPrimaryPointer(fn) {
153
205
  return (event) => {
154
- const win = event.view ?? window;
206
+ var _a;
207
+ const win = (_a = event.view) != null ? _a : window;
155
208
  const isMouseEvent2 = event instanceof win.MouseEvent;
156
209
  const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
157
210
  if (isPrimary)
@@ -206,7 +259,7 @@ var state = "default";
206
259
  var savedUserSelect = "";
207
260
  var modifiedElementMap = /* @__PURE__ */ new WeakMap();
208
261
  function disableTextSelection({ target, doc } = {}) {
209
- const _document = doc ?? document;
262
+ const _document = doc != null ? doc : document;
210
263
  if (isIos()) {
211
264
  if (state === "default") {
212
265
  savedUserSelect = _document.documentElement.style.webkitUserSelect;
@@ -220,7 +273,7 @@ function disableTextSelection({ target, doc } = {}) {
220
273
  return () => restoreTextSelection({ target, doc: _document });
221
274
  }
222
275
  function restoreTextSelection({ target, doc } = {}) {
223
- const _document = doc ?? document;
276
+ const _document = doc != null ? doc : document;
224
277
  if (isIos()) {
225
278
  if (state !== "disabled")
226
279
  return;
@@ -240,7 +293,7 @@ function restoreTextSelection({ target, doc } = {}) {
240
293
  if (target && modifiedElementMap.has(target)) {
241
294
  let targetOldUserSelect = modifiedElementMap.get(target);
242
295
  if (target.style.userSelect === "none") {
243
- target.style.userSelect = targetOldUserSelect ?? "";
296
+ target.style.userSelect = targetOldUserSelect != null ? targetOldUserSelect : "";
244
297
  }
245
298
  if (target.getAttribute("style") === "") {
246
299
  target.removeAttribute("style");
@@ -275,28 +328,28 @@ function trackPointerMove(doc, opts) {
275
328
  // src/splitter.dom.ts
276
329
  var dom = defineDomHelpers({
277
330
  getRootId: (ctx) => {
278
- var _a;
279
- return ((_a = ctx.ids) == null ? void 0 : _a.root) ?? `splitter:${ctx.id}`;
331
+ var _a, _b;
332
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `splitter:${ctx.id}`;
280
333
  },
281
334
  getSplitterId: (ctx) => {
282
- var _a;
283
- return ((_a = ctx.ids) == null ? void 0 : _a.splitter) ?? `splitter:${ctx.id}:splitter`;
335
+ var _a, _b;
336
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.splitter) != null ? _b : `splitter:${ctx.id}:splitter`;
284
337
  },
285
338
  getToggleButtonId: (ctx) => {
286
- var _a;
287
- return ((_a = ctx.ids) == null ? void 0 : _a.toggleBtn) ?? `splitter:${ctx.id}:toggle-btn`;
339
+ var _a, _b;
340
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.toggleBtn) != null ? _b : `splitter:${ctx.id}:toggle-btn`;
288
341
  },
289
342
  getLabelId: (ctx) => {
290
- var _a;
291
- return ((_a = ctx.ids) == null ? void 0 : _a.label) ?? `splitter:${ctx.id}:label`;
343
+ var _a, _b;
344
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `splitter:${ctx.id}:label`;
292
345
  },
293
346
  getPrimaryPaneId: (ctx) => {
294
- var _a;
295
- return ((_a = ctx.ids) == null ? void 0 : _a.primaryPane) ?? `splitter:${ctx.id}:primary`;
347
+ var _a, _b;
348
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.primaryPane) != null ? _b : `splitter:${ctx.id}:primary`;
296
349
  },
297
350
  getSecondaryPaneId: (ctx) => {
298
- var _a;
299
- return ((_a = ctx.ids) == null ? void 0 : _a.secondaryPane) ?? `splitter:${ctx.id}:secondary`;
351
+ var _a, _b;
352
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.secondaryPane) != null ? _b : `splitter:${ctx.id}:secondary`;
300
353
  },
301
354
  getSplitterEl: (ctx) => dom.getById(ctx, dom.getSplitterId(ctx)),
302
355
  getPrimaryPaneEl: (ctx) => dom.getById(ctx, dom.getPrimaryPaneId(ctx)),
@@ -479,12 +532,12 @@ function connect(state2, send, normalize) {
479
532
  }
480
533
 
481
534
  // src/splitter.machine.ts
482
- import { createMachine, guards } from "@zag-js/core";
535
+ var import_core = require("@zag-js/core");
483
536
 
484
- // ../../utilities/number/dist/index.js
537
+ // ../../utilities/number/dist/index.mjs
485
538
  function round(v, t) {
486
539
  let num = valueOf(v);
487
- const p = 10 ** (t ?? 10);
540
+ const p = 10 ** (t != null ? t : 10);
488
541
  num = Math.round(num * p) / p;
489
542
  return t ? num.toFixed(t) : v.toString();
490
543
  }
@@ -529,9 +582,9 @@ function decimalOperation(a, op, b) {
529
582
  var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
530
583
 
531
584
  // src/splitter.machine.ts
532
- var { not } = guards;
585
+ var { not } = import_core.guards;
533
586
  function machine(ctx) {
534
- return createMachine(
587
+ return (0, import_core.createMachine)(
535
588
  {
536
589
  id: "splitter",
537
590
  initial: "unknown",
@@ -742,7 +795,8 @@ function machine(ctx) {
742
795
  }
743
796
  );
744
797
  }
745
- export {
798
+ // Annotate the CommonJS export names for ESM import in node:
799
+ 0 && (module.exports = {
746
800
  connect,
747
801
  machine
748
- };
802
+ });
package/dist/index.mjs ADDED
@@ -0,0 +1,774 @@
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 != null ? 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
+ var _a;
20
+ const agent = navigator.userAgentData;
21
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
22
+ }
23
+ var pt = (v) => isDom() && v.test(getPlatform());
24
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
25
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
26
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
27
+ var isIos = () => isApple() && !isMac();
28
+ function isDocument(el) {
29
+ return el.nodeType === Node.DOCUMENT_NODE;
30
+ }
31
+ function isWindow(value) {
32
+ return (value == null ? void 0 : value.toString()) === "[object Window]";
33
+ }
34
+ function getDocument(el) {
35
+ var _a;
36
+ if (isWindow(el))
37
+ return el.document;
38
+ if (isDocument(el))
39
+ return el;
40
+ return (_a = el == null ? void 0 : el.ownerDocument) != null ? _a : document;
41
+ }
42
+ function defineDomHelpers(helpers) {
43
+ const dom2 = {
44
+ getRootNode: (ctx) => {
45
+ var _a, _b;
46
+ return (_b = (_a = ctx.getRootNode) == null ? void 0 : _a.call(ctx)) != null ? _b : document;
47
+ },
48
+ getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
49
+ getWin: (ctx) => {
50
+ var _a;
51
+ return (_a = dom2.getDoc(ctx).defaultView) != null ? _a : window;
52
+ },
53
+ getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
54
+ getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id),
55
+ createEmitter: (ctx, target) => {
56
+ const win = dom2.getWin(ctx);
57
+ return function emit(evt, detail, options) {
58
+ const { bubbles = true, cancelable, composed = true } = options != null ? options : {};
59
+ const eventName = `zag:${evt}`;
60
+ const init = { bubbles, cancelable, composed, detail };
61
+ const event = new win.CustomEvent(eventName, init);
62
+ target.dispatchEvent(event);
63
+ };
64
+ },
65
+ createListener: (target) => {
66
+ return function listen(evt, handler) {
67
+ const eventName = `zag:${evt}`;
68
+ const listener = (e) => handler(e);
69
+ target.addEventListener(eventName, listener);
70
+ return () => target.removeEventListener(eventName, listener);
71
+ };
72
+ }
73
+ };
74
+ return {
75
+ ...dom2,
76
+ ...helpers
77
+ };
78
+ }
79
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
80
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
81
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
82
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
83
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
84
+ var isLeftClick = (v) => v.button === 0;
85
+ function getElementOffset(element) {
86
+ let left = 0;
87
+ let top = 0;
88
+ let el = element;
89
+ if (el.parentNode) {
90
+ do {
91
+ left += el.offsetLeft;
92
+ top += el.offsetTop;
93
+ } while ((el = el.offsetParent) && el.nodeType < 9);
94
+ el = element;
95
+ do {
96
+ left -= el.scrollLeft;
97
+ top -= el.scrollTop;
98
+ } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
99
+ }
100
+ return {
101
+ top,
102
+ right: innerWidth - left - element.offsetWidth,
103
+ bottom: innerHeight - top - element.offsetHeight,
104
+ left
105
+ };
106
+ }
107
+ function getPointRelativeToNode(point, element) {
108
+ const offset = getElementOffset(element);
109
+ const x = point.x - offset.left;
110
+ const y = point.y - offset.top;
111
+ return { x, y };
112
+ }
113
+ var rtlKeyMap = {
114
+ ArrowLeft: "ArrowRight",
115
+ ArrowRight: "ArrowLeft"
116
+ };
117
+ var sameKeyMap = {
118
+ Up: "ArrowUp",
119
+ Down: "ArrowDown",
120
+ Esc: "Escape",
121
+ " ": "Space",
122
+ ",": "Comma",
123
+ Left: "ArrowLeft",
124
+ Right: "ArrowRight"
125
+ };
126
+ function getEventKey(event, options = {}) {
127
+ var _a;
128
+ const { dir = "ltr", orientation = "horizontal" } = options;
129
+ let { key } = event;
130
+ key = (_a = sameKeyMap[key]) != null ? _a : key;
131
+ const isRtl = dir === "rtl" && orientation === "horizontal";
132
+ if (isRtl && key in rtlKeyMap) {
133
+ key = rtlKeyMap[key];
134
+ }
135
+ return key;
136
+ }
137
+ var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
138
+ var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
139
+ function getEventStep(event) {
140
+ if (event.ctrlKey || event.metaKey) {
141
+ return 0.1;
142
+ } else {
143
+ const isPageKey = PAGE_KEYS.has(event.key);
144
+ const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
145
+ return isSkipKey ? 10 : 1;
146
+ }
147
+ }
148
+ var isRef = (v) => hasProp(v, "current");
149
+ var fallback2 = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
150
+ function extractInfo(event, type = "page") {
151
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback2 : event;
152
+ return {
153
+ point: {
154
+ x: point[`${type}X`],
155
+ y: point[`${type}Y`]
156
+ }
157
+ };
158
+ }
159
+ function addDomEvent(target, eventName, handler, options) {
160
+ const node = isRef(target) ? target.current : runIfFn(target);
161
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
162
+ return () => {
163
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
164
+ };
165
+ }
166
+ function addPointerEvent(target, event, listener, options) {
167
+ var _a;
168
+ const type = (_a = getEventName(event)) != null ? _a : event;
169
+ return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
170
+ }
171
+ function wrapHandler(fn, filter = false) {
172
+ const listener = (event) => {
173
+ fn(event, extractInfo(event));
174
+ };
175
+ return filter ? filterPrimaryPointer(listener) : listener;
176
+ }
177
+ function filterPrimaryPointer(fn) {
178
+ return (event) => {
179
+ var _a;
180
+ const win = (_a = event.view) != null ? _a : window;
181
+ const isMouseEvent2 = event instanceof win.MouseEvent;
182
+ const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
183
+ if (isPrimary)
184
+ fn(event);
185
+ };
186
+ }
187
+ var mouseEventNames = {
188
+ pointerdown: "mousedown",
189
+ pointermove: "mousemove",
190
+ pointerup: "mouseup",
191
+ pointercancel: "mousecancel",
192
+ pointerover: "mouseover",
193
+ pointerout: "mouseout",
194
+ pointerenter: "mouseenter",
195
+ pointerleave: "mouseleave"
196
+ };
197
+ var touchEventNames = {
198
+ pointerdown: "touchstart",
199
+ pointermove: "touchmove",
200
+ pointerup: "touchend",
201
+ pointercancel: "touchcancel"
202
+ };
203
+ function getEventName(evt) {
204
+ if (supportsPointerEvent())
205
+ return evt;
206
+ if (supportsTouchEvent())
207
+ return touchEventNames[evt];
208
+ if (supportsMouseEvent())
209
+ return mouseEventNames[evt];
210
+ return evt;
211
+ }
212
+ function nextTick(fn) {
213
+ const set = /* @__PURE__ */ new Set();
214
+ function raf2(fn2) {
215
+ const id = globalThis.requestAnimationFrame(fn2);
216
+ set.add(() => globalThis.cancelAnimationFrame(id));
217
+ }
218
+ raf2(() => raf2(fn));
219
+ return function cleanup() {
220
+ set.forEach(function(fn2) {
221
+ fn2();
222
+ });
223
+ };
224
+ }
225
+ function raf(fn) {
226
+ const id = globalThis.requestAnimationFrame(fn);
227
+ return function cleanup() {
228
+ globalThis.cancelAnimationFrame(id);
229
+ };
230
+ }
231
+ var state = "default";
232
+ var savedUserSelect = "";
233
+ var modifiedElementMap = /* @__PURE__ */ new WeakMap();
234
+ function disableTextSelection({ target, doc } = {}) {
235
+ const _document = doc != null ? doc : document;
236
+ if (isIos()) {
237
+ if (state === "default") {
238
+ savedUserSelect = _document.documentElement.style.webkitUserSelect;
239
+ _document.documentElement.style.webkitUserSelect = "none";
240
+ }
241
+ state = "disabled";
242
+ } else if (target) {
243
+ modifiedElementMap.set(target, target.style.userSelect);
244
+ target.style.userSelect = "none";
245
+ }
246
+ return () => restoreTextSelection({ target, doc: _document });
247
+ }
248
+ function restoreTextSelection({ target, doc } = {}) {
249
+ const _document = doc != null ? doc : document;
250
+ if (isIos()) {
251
+ if (state !== "disabled")
252
+ return;
253
+ state = "restoring";
254
+ setTimeout(() => {
255
+ nextTick(() => {
256
+ if (state === "restoring") {
257
+ if (_document.documentElement.style.webkitUserSelect === "none") {
258
+ _document.documentElement.style.webkitUserSelect = savedUserSelect || "";
259
+ }
260
+ savedUserSelect = "";
261
+ state = "default";
262
+ }
263
+ });
264
+ }, 300);
265
+ } else {
266
+ if (target && modifiedElementMap.has(target)) {
267
+ let targetOldUserSelect = modifiedElementMap.get(target);
268
+ if (target.style.userSelect === "none") {
269
+ target.style.userSelect = targetOldUserSelect != null ? targetOldUserSelect : "";
270
+ }
271
+ if (target.getAttribute("style") === "") {
272
+ target.removeAttribute("style");
273
+ }
274
+ modifiedElementMap.delete(target);
275
+ }
276
+ }
277
+ }
278
+ var THRESHOLD = 5;
279
+ function trackPointerMove(doc, opts) {
280
+ const { onPointerMove, onPointerUp } = opts;
281
+ const handlePointerMove = (event, info) => {
282
+ const { point: p } = info;
283
+ const distance = Math.sqrt(p.x ** 2 + p.y ** 2);
284
+ if (distance < THRESHOLD)
285
+ return;
286
+ if (isMouseEvent(event) && isLeftClick(event)) {
287
+ onPointerUp();
288
+ return;
289
+ }
290
+ onPointerMove(info, event);
291
+ };
292
+ return callAll(
293
+ addPointerEvent(doc, "pointermove", handlePointerMove, false),
294
+ addPointerEvent(doc, "pointerup", onPointerUp, false),
295
+ addPointerEvent(doc, "pointercancel", onPointerUp, false),
296
+ addPointerEvent(doc, "contextmenu", onPointerUp, false),
297
+ disableTextSelection({ doc })
298
+ );
299
+ }
300
+
301
+ // src/splitter.dom.ts
302
+ var dom = defineDomHelpers({
303
+ getRootId: (ctx) => {
304
+ var _a, _b;
305
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.root) != null ? _b : `splitter:${ctx.id}`;
306
+ },
307
+ getSplitterId: (ctx) => {
308
+ var _a, _b;
309
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.splitter) != null ? _b : `splitter:${ctx.id}:splitter`;
310
+ },
311
+ getToggleButtonId: (ctx) => {
312
+ var _a, _b;
313
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.toggleBtn) != null ? _b : `splitter:${ctx.id}:toggle-btn`;
314
+ },
315
+ getLabelId: (ctx) => {
316
+ var _a, _b;
317
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.label) != null ? _b : `splitter:${ctx.id}:label`;
318
+ },
319
+ getPrimaryPaneId: (ctx) => {
320
+ var _a, _b;
321
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.primaryPane) != null ? _b : `splitter:${ctx.id}:primary`;
322
+ },
323
+ getSecondaryPaneId: (ctx) => {
324
+ var _a, _b;
325
+ return (_b = (_a = ctx.ids) == null ? void 0 : _a.secondaryPane) != null ? _b : `splitter:${ctx.id}:secondary`;
326
+ },
327
+ getSplitterEl: (ctx) => dom.getById(ctx, dom.getSplitterId(ctx)),
328
+ getPrimaryPaneEl: (ctx) => dom.getById(ctx, dom.getPrimaryPaneId(ctx)),
329
+ getCursor(ctx) {
330
+ if (ctx.disabled || ctx.fixed)
331
+ return "default";
332
+ const x = ctx.isHorizontal;
333
+ let cursor = x ? "col-resize" : "row-resize";
334
+ if (ctx.isAtMin)
335
+ cursor = x ? "e-resize" : "s-resize";
336
+ if (ctx.isAtMax)
337
+ cursor = x ? "w-resize" : "n-resize";
338
+ return cursor;
339
+ }
340
+ });
341
+
342
+ // src/splitter.connect.ts
343
+ function connect(state2, send, normalize) {
344
+ const isHorizontal = state2.context.isHorizontal;
345
+ const isDisabled = state2.context.disabled;
346
+ const isFocused = state2.hasTag("focus");
347
+ const isDragging = state2.matches("dragging");
348
+ const isAtMin = state2.context.isAtMin;
349
+ const isAtMax = state2.context.isAtMax;
350
+ const min = state2.context.min;
351
+ const max = state2.context.max;
352
+ const value = state2.context.value;
353
+ return {
354
+ isCollapsed: isAtMin,
355
+ isExpanded: isAtMax,
356
+ isFocused,
357
+ isDragging,
358
+ value,
359
+ collapse() {
360
+ send("COLLAPSE");
361
+ },
362
+ expand() {
363
+ send("EXPAND");
364
+ },
365
+ toggle() {
366
+ send("TOGGLE");
367
+ },
368
+ setSize(size) {
369
+ send({ type: "SET_SIZE", size });
370
+ },
371
+ rootProps: normalize.element({
372
+ "data-part": "root",
373
+ "data-orientation": state2.context.orientation,
374
+ "data-disabled": dataAttr(isDisabled),
375
+ id: dom.getRootId(state2.context),
376
+ style: {
377
+ display: "flex",
378
+ flex: "1 1 0%",
379
+ flexDirection: isHorizontal ? "row" : "column"
380
+ }
381
+ }),
382
+ secondaryPaneProps: normalize.element({
383
+ "data-part": "secondary-pane",
384
+ "data-disabled": dataAttr(isDisabled),
385
+ id: dom.getSecondaryPaneId(state2.context),
386
+ style: {
387
+ height: isHorizontal ? "100%" : "auto",
388
+ width: isHorizontal ? "auto" : "100%",
389
+ flex: "1 1 auto",
390
+ position: "relative"
391
+ }
392
+ }),
393
+ primaryPaneProps: normalize.element({
394
+ "data-part": "primary-pane",
395
+ id: dom.getPrimaryPaneId(state2.context),
396
+ "data-disabled": dataAttr(isDisabled),
397
+ "data-state": isAtMax ? "at-max" : isAtMin ? "at-min" : "between",
398
+ style: {
399
+ visibility: "visible",
400
+ flex: `0 0 ${value}px`,
401
+ position: "relative",
402
+ userSelect: isDragging ? "none" : "auto",
403
+ ...isHorizontal ? { minWidth: `${min}px`, maxWidth: `${max}px` } : { minHeight: `${min}px`, maxHeight: `${max}px` }
404
+ }
405
+ }),
406
+ toggleButtonProps: normalize.element({
407
+ "data-part": "toggle-button",
408
+ id: dom.getToggleButtonId(state2.context),
409
+ "aria-label": state2.context.isAtMin ? "Expand Primary Pane" : "Collapse Primary Pane",
410
+ onClick() {
411
+ send("TOGGLE");
412
+ }
413
+ }),
414
+ labelProps: normalize.element({
415
+ "data-part": "label",
416
+ id: dom.getLabelId(state2.context)
417
+ }),
418
+ splitterProps: normalize.element({
419
+ "data-part": "splitter",
420
+ id: dom.getSplitterId(state2.context),
421
+ role: "separator",
422
+ tabIndex: isDisabled ? void 0 : 0,
423
+ "aria-valuenow": value,
424
+ "aria-valuemin": min,
425
+ "aria-valuemax": max,
426
+ "aria-orientation": state2.context.orientation,
427
+ "aria-labelledby": dom.getLabelId(state2.context),
428
+ "aria-controls": dom.getPrimaryPaneId(state2.context),
429
+ "data-orientation": state2.context.orientation,
430
+ "data-focus": dataAttr(isFocused),
431
+ "data-disabled": dataAttr(isDisabled),
432
+ style: {
433
+ touchAction: "none",
434
+ userSelect: "none",
435
+ WebkitUserSelect: "none",
436
+ msUserSelect: "none",
437
+ flex: "0 0 auto",
438
+ cursor: dom.getCursor(state2.context),
439
+ minHeight: isHorizontal ? "0px" : void 0,
440
+ minWidth: isHorizontal ? void 0 : "0px"
441
+ },
442
+ onPointerDown(event) {
443
+ if (isDisabled) {
444
+ event.preventDefault();
445
+ return;
446
+ }
447
+ send("POINTER_DOWN");
448
+ event.preventDefault();
449
+ event.stopPropagation();
450
+ },
451
+ onPointerOver() {
452
+ if (isDisabled)
453
+ return;
454
+ send("POINTER_OVER");
455
+ },
456
+ onPointerLeave() {
457
+ if (isDisabled)
458
+ return;
459
+ send("POINTER_LEAVE");
460
+ },
461
+ onBlur() {
462
+ send("BLUR");
463
+ },
464
+ onFocus() {
465
+ send("FOCUS");
466
+ },
467
+ onDoubleClick() {
468
+ if (isDisabled)
469
+ return;
470
+ send("DOUBLE_CLICK");
471
+ },
472
+ onKeyDown(event) {
473
+ if (isDisabled)
474
+ return;
475
+ const step = getEventStep(event) * state2.context.step;
476
+ const keyMap = {
477
+ ArrowUp() {
478
+ send({ type: "ARROW_UP", step });
479
+ },
480
+ ArrowDown() {
481
+ send({ type: "ARROW_DOWN", step });
482
+ },
483
+ ArrowLeft() {
484
+ send({ type: "ARROW_LEFT", step });
485
+ },
486
+ ArrowRight() {
487
+ send({ type: "ARROW_RIGHT", step });
488
+ },
489
+ Home() {
490
+ send("HOME");
491
+ },
492
+ End() {
493
+ send("END");
494
+ }
495
+ };
496
+ const key = getEventKey(event, state2.context);
497
+ const exec = keyMap[key];
498
+ if (exec) {
499
+ exec(event);
500
+ event.preventDefault();
501
+ }
502
+ }
503
+ })
504
+ };
505
+ }
506
+
507
+ // src/splitter.machine.ts
508
+ import { createMachine, guards } from "@zag-js/core";
509
+
510
+ // ../../utilities/number/dist/index.mjs
511
+ function round(v, t) {
512
+ let num = valueOf(v);
513
+ const p = 10 ** (t != null ? t : 10);
514
+ num = Math.round(num * p) / p;
515
+ return t ? num.toFixed(t) : v.toString();
516
+ }
517
+ function clamp(v, o) {
518
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
519
+ }
520
+ function countDecimals(value) {
521
+ if (!Number.isFinite(value))
522
+ return 0;
523
+ let e = 1, p = 0;
524
+ while (Math.round(value * e) / e !== value) {
525
+ e *= 10;
526
+ p += 1;
527
+ }
528
+ return p;
529
+ }
530
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
531
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
532
+ function snapToStep(value, step) {
533
+ const num = valueOf(value);
534
+ const p = countDecimals(step);
535
+ const v = Math.round(num / step) * step;
536
+ return round(v, p);
537
+ }
538
+ function valueOf(v) {
539
+ if (typeof v === "number")
540
+ return v;
541
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
542
+ return !Number.isNaN(num) ? num : 0;
543
+ }
544
+ function decimalOperation(a, op, b) {
545
+ let result = op === "+" ? a + b : a - b;
546
+ if (a % 1 !== 0 || b % 1 !== 0) {
547
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
548
+ a = Math.round(a * multiplier);
549
+ b = Math.round(b * multiplier);
550
+ result = op === "+" ? a + b : a - b;
551
+ result /= multiplier;
552
+ }
553
+ return result;
554
+ }
555
+ var nf = new Intl.NumberFormat("en-US", { style: "decimal", maximumFractionDigits: 20 });
556
+
557
+ // src/splitter.machine.ts
558
+ var { not } = guards;
559
+ function machine(ctx) {
560
+ return createMachine(
561
+ {
562
+ id: "splitter",
563
+ initial: "unknown",
564
+ context: {
565
+ orientation: "horizontal",
566
+ min: 224,
567
+ max: 340,
568
+ step: 1,
569
+ value: 256,
570
+ snapOffset: 0,
571
+ ...ctx
572
+ },
573
+ computed: {
574
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
575
+ isAtMin: (ctx2) => ctx2.value === ctx2.min,
576
+ isAtMax: (ctx2) => ctx2.value === ctx2.max
577
+ },
578
+ on: {
579
+ COLLAPSE: {
580
+ actions: "setToMin"
581
+ },
582
+ EXPAND: {
583
+ actions: "setToMax"
584
+ },
585
+ TOGGLE: [
586
+ {
587
+ guard: "isCollapsed",
588
+ actions: "setToMax"
589
+ },
590
+ {
591
+ actions: "setToMin"
592
+ }
593
+ ]
594
+ },
595
+ states: {
596
+ unknown: {
597
+ on: {
598
+ SETUP: "idle"
599
+ }
600
+ },
601
+ idle: {
602
+ on: {
603
+ POINTER_OVER: {
604
+ guard: not("isFixed"),
605
+ target: "hover:temp"
606
+ },
607
+ POINTER_LEAVE: "idle",
608
+ FOCUS: "focused"
609
+ }
610
+ },
611
+ "hover:temp": {
612
+ after: {
613
+ HOVER_DELAY: "hover"
614
+ },
615
+ on: {
616
+ POINTER_DOWN: {
617
+ target: "dragging",
618
+ actions: ["invokeOnChangeStart"]
619
+ },
620
+ POINTER_LEAVE: "idle"
621
+ }
622
+ },
623
+ hover: {
624
+ tags: ["focus"],
625
+ on: {
626
+ POINTER_DOWN: {
627
+ target: "dragging",
628
+ actions: ["invokeOnChangeStart"]
629
+ },
630
+ POINTER_LEAVE: "idle"
631
+ }
632
+ },
633
+ focused: {
634
+ tags: ["focus"],
635
+ on: {
636
+ BLUR: "idle",
637
+ POINTER_DOWN: {
638
+ target: "dragging",
639
+ actions: ["invokeOnChangeStart"]
640
+ },
641
+ ARROW_LEFT: {
642
+ guard: "isHorizontal",
643
+ actions: "decrement"
644
+ },
645
+ ARROW_RIGHT: {
646
+ guard: "isHorizontal",
647
+ actions: "increment"
648
+ },
649
+ ARROW_UP: {
650
+ guard: "isVertical",
651
+ actions: "increment"
652
+ },
653
+ ARROW_DOWN: {
654
+ guard: "isVertical",
655
+ actions: "decrement"
656
+ },
657
+ ENTER: [
658
+ {
659
+ guard: "isCollapsed",
660
+ actions: "setToMin"
661
+ },
662
+ { actions: "setToMin" }
663
+ ],
664
+ HOME: {
665
+ actions: "setToMin"
666
+ },
667
+ END: {
668
+ actions: "setToMax"
669
+ },
670
+ DOUBLE_CLICK: [
671
+ {
672
+ guard: "isCollapsed",
673
+ actions: "setToMax"
674
+ },
675
+ { actions: "setToMin" }
676
+ ]
677
+ }
678
+ },
679
+ dragging: {
680
+ tags: ["focus"],
681
+ entry: "focusSplitter",
682
+ activities: "trackPointerMove",
683
+ on: {
684
+ POINTER_UP: {
685
+ target: "focused",
686
+ actions: ["invokeOnChangeEnd"]
687
+ },
688
+ POINTER_MOVE: {
689
+ actions: "setPointerValue"
690
+ }
691
+ }
692
+ }
693
+ }
694
+ },
695
+ {
696
+ activities: {
697
+ trackPointerMove: (ctx2, _evt, { send }) => {
698
+ const doc = dom.getDoc(ctx2);
699
+ return trackPointerMove(doc, {
700
+ onPointerMove(info) {
701
+ send({ type: "POINTER_MOVE", point: info.point });
702
+ doc.documentElement.style.cursor = dom.getCursor(ctx2);
703
+ },
704
+ onPointerUp() {
705
+ send("POINTER_UP");
706
+ doc.documentElement.style.cursor = "";
707
+ }
708
+ });
709
+ }
710
+ },
711
+ guards: {
712
+ isCollapsed: (ctx2) => ctx2.isAtMin,
713
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
714
+ isVertical: (ctx2) => !ctx2.isHorizontal,
715
+ isFixed: (ctx2) => !!ctx2.fixed
716
+ },
717
+ delays: {
718
+ HOVER_DELAY: 250
719
+ },
720
+ actions: {
721
+ invokeOnChange(ctx2, evt) {
722
+ var _a;
723
+ if (evt.type !== "SETUP") {
724
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
725
+ }
726
+ },
727
+ invokeOnChangeStart(ctx2) {
728
+ var _a;
729
+ (_a = ctx2.onChangeStart) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
730
+ },
731
+ invokeOnChangeEnd(ctx2) {
732
+ var _a;
733
+ (_a = ctx2.onChangeEnd) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
734
+ },
735
+ setToMin(ctx2) {
736
+ ctx2.value = ctx2.min;
737
+ },
738
+ setToMax(ctx2) {
739
+ ctx2.value = ctx2.max;
740
+ },
741
+ increment(ctx2, evt) {
742
+ ctx2.value = clamp(increment(ctx2.value, evt.step), ctx2);
743
+ },
744
+ decrement(ctx2, evt) {
745
+ ctx2.value = clamp(decrement(ctx2.value, evt.step), ctx2);
746
+ },
747
+ focusSplitter(ctx2) {
748
+ raf(() => {
749
+ var _a;
750
+ return (_a = dom.getSplitterEl(ctx2)) == null ? void 0 : _a.focus();
751
+ });
752
+ },
753
+ setPointerValue(ctx2, evt) {
754
+ const el = dom.getPrimaryPaneEl(ctx2);
755
+ if (!el)
756
+ return;
757
+ const relativePoint = getPointRelativeToNode(evt.point, el);
758
+ let currentPoint = ctx2.isHorizontal ? relativePoint.x : relativePoint.y;
759
+ let value = parseFloat(snapToStep(clamp(currentPoint, ctx2), ctx2.step));
760
+ if (Math.abs(value - ctx2.min) <= ctx2.snapOffset) {
761
+ value = ctx2.min;
762
+ } else if (Math.abs(value - ctx2.max) <= ctx2.snapOffset) {
763
+ value = ctx2.max;
764
+ }
765
+ ctx2.value = value;
766
+ }
767
+ }
768
+ }
769
+ );
770
+ }
771
+ export {
772
+ connect,
773
+ machine
774
+ };
package/package.json CHANGED
@@ -1,8 +1,10 @@
1
1
  {
2
- "type": "module",
3
2
  "name": "@zag-js/splitter",
4
- "version": "0.1.14",
3
+ "version": "0.2.0",
5
4
  "description": "Core logic for the splitter widget implemented as a state machine",
5
+ "main": "dist/index.js",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.ts",
6
8
  "keywords": [
7
9
  "js",
8
10
  "machine",
@@ -16,8 +18,6 @@
16
18
  "author": "Segun Adebayo <sage@adebayosegun.com>",
17
19
  "homepage": "https://github.com/chakra-ui/zag#readme",
18
20
  "license": "MIT",
19
- "main": "dist/index.js",
20
- "types": "dist/index.d.ts",
21
21
  "repository": "https://github.com/chakra-ui/zag/tree/main/packages/splitter",
22
22
  "sideEffects": false,
23
23
  "files": [
@@ -30,17 +30,17 @@
30
30
  "url": "https://github.com/chakra-ui/zag/issues"
31
31
  },
32
32
  "dependencies": {
33
- "@zag-js/core": "0.1.11",
34
- "@zag-js/types": "0.2.6"
33
+ "@zag-js/core": "0.2.0",
34
+ "@zag-js/types": "0.3.0"
35
35
  },
36
36
  "devDependencies": {
37
- "@zag-js/dom-utils": "0.1.12",
38
- "@zag-js/number-utils": "0.1.5"
37
+ "@zag-js/dom-utils": "0.2.0",
38
+ "@zag-js/number-utils": "0.2.0"
39
39
  },
40
40
  "scripts": {
41
- "build-fast": "tsup src/index.ts --format=esm",
41
+ "build-fast": "tsup src/index.ts --format=esm,cjs",
42
42
  "start": "pnpm build --watch",
43
- "build": "tsup src/index.ts --format=esm --dts",
43
+ "build": "tsup src/index.ts --format=esm,cjs --dts",
44
44
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
45
45
  "lint": "eslint src --ext .ts,.tsx",
46
46
  "test-ci": "pnpm test --ci --runInBand",