@zag-js/splitter 0.2.5 → 0.2.6

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.
@@ -0,0 +1,510 @@
1
+ import {
2
+ dom
3
+ } from "./chunk-A3GF7W2N.mjs";
4
+
5
+ // src/splitter.machine.ts
6
+ import { createMachine, guards } from "@zag-js/core";
7
+
8
+ // ../../utilities/core/src/functions.ts
9
+ var runIfFn = (v, ...a) => {
10
+ const res = typeof v === "function" ? v(...a) : v;
11
+ return res != null ? res : void 0;
12
+ };
13
+ var callAll = (...fns) => (...a) => {
14
+ fns.forEach(function(fn) {
15
+ fn == null ? void 0 : fn(...a);
16
+ });
17
+ };
18
+
19
+ // ../../utilities/core/src/guard.ts
20
+ var isArray = (v) => Array.isArray(v);
21
+ var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
22
+ var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
23
+
24
+ // ../../utilities/core/src/object.ts
25
+ function compact(obj) {
26
+ if (obj === void 0)
27
+ return obj;
28
+ return Object.fromEntries(
29
+ Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
30
+ );
31
+ }
32
+
33
+ // ../../utilities/dom/src/platform.ts
34
+ var isDom = () => typeof window !== "undefined";
35
+ function getPlatform() {
36
+ var _a;
37
+ const agent = navigator.userAgentData;
38
+ return (_a = agent == null ? void 0 : agent.platform) != null ? _a : navigator.platform;
39
+ }
40
+ var pt = (v) => isDom() && v.test(getPlatform());
41
+ var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
42
+ var isMac = () => pt(/^Mac/) && !isTouchDevice;
43
+ var isApple = () => pt(/mac|iphone|ipad|ipod/i);
44
+ var isIos = () => isApple() && !isMac();
45
+
46
+ // ../../utilities/dom/src/event.ts
47
+ var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
48
+ var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
49
+ var supportsMouseEvent = () => isDom() && window.onmousedown === null;
50
+ var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
51
+ var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
52
+ var isLeftClick = (v) => v.button === 0;
53
+
54
+ // ../../utilities/dom/src/get-element-offset.ts
55
+ function getElementOffset(element) {
56
+ let left = 0;
57
+ let top = 0;
58
+ let el = element;
59
+ if (el.parentNode) {
60
+ do {
61
+ left += el.offsetLeft;
62
+ top += el.offsetTop;
63
+ } while ((el = el.offsetParent) && el.nodeType < 9);
64
+ el = element;
65
+ do {
66
+ left -= el.scrollLeft;
67
+ top -= el.scrollTop;
68
+ } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
69
+ }
70
+ return {
71
+ top,
72
+ right: innerWidth - left - element.offsetWidth,
73
+ bottom: innerHeight - top - element.offsetHeight,
74
+ left
75
+ };
76
+ }
77
+
78
+ // ../../utilities/dom/src/get-point-relative-to-element.ts
79
+ function getPointRelativeToNode(point, element) {
80
+ const offset = getElementOffset(element);
81
+ const x = point.x - offset.left;
82
+ const y = point.y - offset.top;
83
+ return { x, y };
84
+ }
85
+
86
+ // ../../utilities/dom/src/listener.ts
87
+ var isRef = (v) => hasProp(v, "current");
88
+ var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
89
+ function extractInfo(event, type = "page") {
90
+ const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
91
+ return {
92
+ point: {
93
+ x: point[`${type}X`],
94
+ y: point[`${type}Y`]
95
+ }
96
+ };
97
+ }
98
+ function addDomEvent(target, eventName, handler, options) {
99
+ const node = isRef(target) ? target.current : runIfFn(target);
100
+ node == null ? void 0 : node.addEventListener(eventName, handler, options);
101
+ return () => {
102
+ node == null ? void 0 : node.removeEventListener(eventName, handler, options);
103
+ };
104
+ }
105
+ function addPointerEvent(target, event, listener, options) {
106
+ var _a;
107
+ const type = (_a = getEventName(event)) != null ? _a : event;
108
+ return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
109
+ }
110
+ function wrapHandler(fn, filter = false) {
111
+ const listener = (event) => {
112
+ fn(event, extractInfo(event));
113
+ };
114
+ return filter ? filterPrimaryPointer(listener) : listener;
115
+ }
116
+ function filterPrimaryPointer(fn) {
117
+ return (event) => {
118
+ var _a;
119
+ const win = (_a = event.view) != null ? _a : window;
120
+ const isMouseEvent2 = event instanceof win.MouseEvent;
121
+ const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
122
+ if (isPrimary)
123
+ fn(event);
124
+ };
125
+ }
126
+ var mouseEventNames = {
127
+ pointerdown: "mousedown",
128
+ pointermove: "mousemove",
129
+ pointerup: "mouseup",
130
+ pointercancel: "mousecancel",
131
+ pointerover: "mouseover",
132
+ pointerout: "mouseout",
133
+ pointerenter: "mouseenter",
134
+ pointerleave: "mouseleave"
135
+ };
136
+ var touchEventNames = {
137
+ pointerdown: "touchstart",
138
+ pointermove: "touchmove",
139
+ pointerup: "touchend",
140
+ pointercancel: "touchcancel"
141
+ };
142
+ function getEventName(evt) {
143
+ if (supportsPointerEvent())
144
+ return evt;
145
+ if (supportsTouchEvent())
146
+ return touchEventNames[evt];
147
+ if (supportsMouseEvent())
148
+ return mouseEventNames[evt];
149
+ return evt;
150
+ }
151
+
152
+ // ../../utilities/dom/src/next-tick.ts
153
+ function nextTick(fn) {
154
+ const set = /* @__PURE__ */ new Set();
155
+ function raf2(fn2) {
156
+ const id = globalThis.requestAnimationFrame(fn2);
157
+ set.add(() => globalThis.cancelAnimationFrame(id));
158
+ }
159
+ raf2(() => raf2(fn));
160
+ return function cleanup() {
161
+ set.forEach(function(fn2) {
162
+ fn2();
163
+ });
164
+ };
165
+ }
166
+ function raf(fn) {
167
+ const id = globalThis.requestAnimationFrame(fn);
168
+ return function cleanup() {
169
+ globalThis.cancelAnimationFrame(id);
170
+ };
171
+ }
172
+
173
+ // ../../utilities/dom/src/text-selection.ts
174
+ var state = "default";
175
+ var savedUserSelect = "";
176
+ var modifiedElementMap = /* @__PURE__ */ new WeakMap();
177
+ function disableTextSelection({ target, doc } = {}) {
178
+ const _document = doc != null ? doc : document;
179
+ if (isIos()) {
180
+ if (state === "default") {
181
+ savedUserSelect = _document.documentElement.style.webkitUserSelect;
182
+ _document.documentElement.style.webkitUserSelect = "none";
183
+ }
184
+ state = "disabled";
185
+ } else if (target) {
186
+ modifiedElementMap.set(target, target.style.userSelect);
187
+ target.style.userSelect = "none";
188
+ }
189
+ return () => restoreTextSelection({ target, doc: _document });
190
+ }
191
+ function restoreTextSelection({ target, doc } = {}) {
192
+ const _document = doc != null ? doc : document;
193
+ if (isIos()) {
194
+ if (state !== "disabled")
195
+ return;
196
+ state = "restoring";
197
+ setTimeout(() => {
198
+ nextTick(() => {
199
+ if (state === "restoring") {
200
+ if (_document.documentElement.style.webkitUserSelect === "none") {
201
+ _document.documentElement.style.webkitUserSelect = savedUserSelect || "";
202
+ }
203
+ savedUserSelect = "";
204
+ state = "default";
205
+ }
206
+ });
207
+ }, 300);
208
+ } else {
209
+ if (target && modifiedElementMap.has(target)) {
210
+ let targetOldUserSelect = modifiedElementMap.get(target);
211
+ if (target.style.userSelect === "none") {
212
+ target.style.userSelect = targetOldUserSelect != null ? targetOldUserSelect : "";
213
+ }
214
+ if (target.getAttribute("style") === "") {
215
+ target.removeAttribute("style");
216
+ }
217
+ modifiedElementMap.delete(target);
218
+ }
219
+ }
220
+ }
221
+
222
+ // ../../utilities/dom/src/pointer-event.ts
223
+ var THRESHOLD = 5;
224
+ function trackPointerMove(doc, opts) {
225
+ const { onPointerMove, onPointerUp } = opts;
226
+ const handlePointerMove = (event, info) => {
227
+ const { point: p } = info;
228
+ const distance = Math.sqrt(p.x ** 2 + p.y ** 2);
229
+ if (distance < THRESHOLD)
230
+ return;
231
+ if (isMouseEvent(event) && isLeftClick(event)) {
232
+ onPointerUp();
233
+ return;
234
+ }
235
+ onPointerMove(info, event);
236
+ };
237
+ return callAll(
238
+ addPointerEvent(doc, "pointermove", handlePointerMove, false),
239
+ addPointerEvent(doc, "pointerup", onPointerUp, false),
240
+ addPointerEvent(doc, "pointercancel", onPointerUp, false),
241
+ addPointerEvent(doc, "contextmenu", onPointerUp, false),
242
+ disableTextSelection({ doc })
243
+ );
244
+ }
245
+
246
+ // ../../utilities/number/src/number.ts
247
+ function round(v, t) {
248
+ let num = valueOf(v);
249
+ const p = 10 ** (t != null ? t : 10);
250
+ num = Math.round(num * p) / p;
251
+ return t ? num.toFixed(t) : v.toString();
252
+ }
253
+ function clamp(v, o) {
254
+ return Math.min(Math.max(valueOf(v), o.min), o.max);
255
+ }
256
+ function countDecimals(value) {
257
+ if (!Number.isFinite(value))
258
+ return 0;
259
+ let e = 1, p = 0;
260
+ while (Math.round(value * e) / e !== value) {
261
+ e *= 10;
262
+ p += 1;
263
+ }
264
+ return p;
265
+ }
266
+ var increment = (v, s) => decimalOperation(valueOf(v), "+", s);
267
+ var decrement = (v, s) => decimalOperation(valueOf(v), "-", s);
268
+ function snapToStep(value, step) {
269
+ const num = valueOf(value);
270
+ const p = countDecimals(step);
271
+ const v = Math.round(num / step) * step;
272
+ return round(v, p);
273
+ }
274
+ function valueOf(v) {
275
+ if (typeof v === "number")
276
+ return v;
277
+ const num = parseFloat(v.toString().replace(/[^\w.-]+/g, ""));
278
+ return !Number.isNaN(num) ? num : 0;
279
+ }
280
+ function decimalOperation(a, op, b) {
281
+ let result = op === "+" ? a + b : a - b;
282
+ if (a % 1 !== 0 || b % 1 !== 0) {
283
+ const multiplier = 10 ** Math.max(countDecimals(a), countDecimals(b));
284
+ a = Math.round(a * multiplier);
285
+ b = Math.round(b * multiplier);
286
+ result = op === "+" ? a + b : a - b;
287
+ result /= multiplier;
288
+ }
289
+ return result;
290
+ }
291
+
292
+ // src/splitter.machine.ts
293
+ var { not } = guards;
294
+ function machine(userContext) {
295
+ const ctx = compact(userContext);
296
+ return createMachine(
297
+ {
298
+ id: "splitter",
299
+ initial: "unknown",
300
+ context: {
301
+ orientation: "horizontal",
302
+ min: 224,
303
+ max: 340,
304
+ step: 1,
305
+ value: 256,
306
+ snapOffset: 0,
307
+ ...ctx
308
+ },
309
+ computed: {
310
+ isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
311
+ isAtMin: (ctx2) => ctx2.value === ctx2.min,
312
+ isAtMax: (ctx2) => ctx2.value === ctx2.max
313
+ },
314
+ on: {
315
+ COLLAPSE: {
316
+ actions: "setToMin"
317
+ },
318
+ EXPAND: {
319
+ actions: "setToMax"
320
+ },
321
+ TOGGLE: [
322
+ {
323
+ guard: "isCollapsed",
324
+ actions: "setToMax"
325
+ },
326
+ {
327
+ actions: "setToMin"
328
+ }
329
+ ]
330
+ },
331
+ states: {
332
+ unknown: {
333
+ on: {
334
+ SETUP: "idle"
335
+ }
336
+ },
337
+ idle: {
338
+ on: {
339
+ POINTER_OVER: {
340
+ guard: not("isFixed"),
341
+ target: "hover:temp"
342
+ },
343
+ POINTER_LEAVE: "idle",
344
+ FOCUS: "focused"
345
+ }
346
+ },
347
+ "hover:temp": {
348
+ after: {
349
+ HOVER_DELAY: "hover"
350
+ },
351
+ on: {
352
+ POINTER_DOWN: {
353
+ target: "dragging",
354
+ actions: ["invokeOnChangeStart"]
355
+ },
356
+ POINTER_LEAVE: "idle"
357
+ }
358
+ },
359
+ hover: {
360
+ tags: ["focus"],
361
+ on: {
362
+ POINTER_DOWN: {
363
+ target: "dragging",
364
+ actions: ["invokeOnChangeStart"]
365
+ },
366
+ POINTER_LEAVE: "idle"
367
+ }
368
+ },
369
+ focused: {
370
+ tags: ["focus"],
371
+ on: {
372
+ BLUR: "idle",
373
+ POINTER_DOWN: {
374
+ target: "dragging",
375
+ actions: ["invokeOnChangeStart"]
376
+ },
377
+ ARROW_LEFT: {
378
+ guard: "isHorizontal",
379
+ actions: "decrement"
380
+ },
381
+ ARROW_RIGHT: {
382
+ guard: "isHorizontal",
383
+ actions: "increment"
384
+ },
385
+ ARROW_UP: {
386
+ guard: "isVertical",
387
+ actions: "increment"
388
+ },
389
+ ARROW_DOWN: {
390
+ guard: "isVertical",
391
+ actions: "decrement"
392
+ },
393
+ ENTER: [
394
+ {
395
+ guard: "isCollapsed",
396
+ actions: "setToMin"
397
+ },
398
+ { actions: "setToMin" }
399
+ ],
400
+ HOME: {
401
+ actions: "setToMin"
402
+ },
403
+ END: {
404
+ actions: "setToMax"
405
+ },
406
+ DOUBLE_CLICK: [
407
+ {
408
+ guard: "isCollapsed",
409
+ actions: "setToMax"
410
+ },
411
+ { actions: "setToMin" }
412
+ ]
413
+ }
414
+ },
415
+ dragging: {
416
+ tags: ["focus"],
417
+ entry: "focusSplitter",
418
+ activities: "trackPointerMove",
419
+ on: {
420
+ POINTER_UP: {
421
+ target: "focused",
422
+ actions: ["invokeOnChangeEnd"]
423
+ },
424
+ POINTER_MOVE: {
425
+ actions: "setPointerValue"
426
+ }
427
+ }
428
+ }
429
+ }
430
+ },
431
+ {
432
+ activities: {
433
+ trackPointerMove: (ctx2, _evt, { send }) => {
434
+ const doc = dom.getDoc(ctx2);
435
+ return trackPointerMove(doc, {
436
+ onPointerMove(info) {
437
+ send({ type: "POINTER_MOVE", point: info.point });
438
+ doc.documentElement.style.cursor = dom.getCursor(ctx2);
439
+ },
440
+ onPointerUp() {
441
+ send("POINTER_UP");
442
+ doc.documentElement.style.cursor = "";
443
+ }
444
+ });
445
+ }
446
+ },
447
+ guards: {
448
+ isCollapsed: (ctx2) => ctx2.isAtMin,
449
+ isHorizontal: (ctx2) => ctx2.isHorizontal,
450
+ isVertical: (ctx2) => !ctx2.isHorizontal,
451
+ isFixed: (ctx2) => !!ctx2.fixed
452
+ },
453
+ delays: {
454
+ HOVER_DELAY: 250
455
+ },
456
+ actions: {
457
+ invokeOnChange(ctx2, evt) {
458
+ var _a;
459
+ if (evt.type !== "SETUP") {
460
+ (_a = ctx2.onChange) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
461
+ }
462
+ },
463
+ invokeOnChangeStart(ctx2) {
464
+ var _a;
465
+ (_a = ctx2.onChangeStart) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
466
+ },
467
+ invokeOnChangeEnd(ctx2) {
468
+ var _a;
469
+ (_a = ctx2.onChangeEnd) == null ? void 0 : _a.call(ctx2, { value: ctx2.value });
470
+ },
471
+ setToMin(ctx2) {
472
+ ctx2.value = ctx2.min;
473
+ },
474
+ setToMax(ctx2) {
475
+ ctx2.value = ctx2.max;
476
+ },
477
+ increment(ctx2, evt) {
478
+ ctx2.value = clamp(increment(ctx2.value, evt.step), ctx2);
479
+ },
480
+ decrement(ctx2, evt) {
481
+ ctx2.value = clamp(decrement(ctx2.value, evt.step), ctx2);
482
+ },
483
+ focusSplitter(ctx2) {
484
+ raf(() => {
485
+ var _a;
486
+ return (_a = dom.getSplitterEl(ctx2)) == null ? void 0 : _a.focus();
487
+ });
488
+ },
489
+ setPointerValue(ctx2, evt) {
490
+ const el = dom.getPrimaryPaneEl(ctx2);
491
+ if (!el)
492
+ return;
493
+ const relativePoint = getPointRelativeToNode(evt.point, el);
494
+ let currentPoint = ctx2.isHorizontal ? relativePoint.x : relativePoint.y;
495
+ let value = parseFloat(snapToStep(clamp(currentPoint, ctx2), ctx2.step));
496
+ if (Math.abs(value - ctx2.min) <= ctx2.snapOffset) {
497
+ value = ctx2.min;
498
+ } else if (Math.abs(value - ctx2.max) <= ctx2.snapOffset) {
499
+ value = ctx2.max;
500
+ }
501
+ ctx2.value = value;
502
+ }
503
+ }
504
+ }
505
+ );
506
+ }
507
+
508
+ export {
509
+ machine
510
+ };