@zag-js/splitter 0.2.12 → 0.2.14

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.
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  dom
3
- } from "./chunk-QY3R66FZ.mjs";
3
+ } from "./chunk-BSW3DTW4.mjs";
4
4
  import {
5
5
  clamp,
6
6
  getHandleBounds,
@@ -11,259 +11,9 @@ import {
11
11
 
12
12
  // src/splitter.machine.ts
13
13
  import { createMachine } from "@zag-js/core";
14
-
15
- // ../../utilities/core/src/functions.ts
16
- var runIfFn = (v, ...a) => {
17
- const res = typeof v === "function" ? v(...a) : v;
18
- return res ?? void 0;
19
- };
20
- var callAll = (...fns) => (...a) => {
21
- fns.forEach(function(fn) {
22
- fn?.(...a);
23
- });
24
- };
25
-
26
- // ../../utilities/core/src/guard.ts
27
- var isArray = (v) => Array.isArray(v);
28
- var isObject = (v) => !(v == null || typeof v !== "object" || isArray(v));
29
- var hasProp = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop);
30
-
31
- // ../../utilities/core/src/object.ts
32
- function compact(obj) {
33
- if (obj === void 0)
34
- return obj;
35
- return Object.fromEntries(
36
- Object.entries(obj).filter(([, value]) => value !== void 0).map(([key, value]) => [key, isObject(value) ? compact(value) : value])
37
- );
38
- }
39
-
40
- // ../../utilities/dom/src/platform.ts
41
- var isDom = () => typeof window !== "undefined";
42
- function getPlatform() {
43
- const agent = navigator.userAgentData;
44
- return agent?.platform ?? navigator.platform;
45
- }
46
- var pt = (v) => isDom() && v.test(getPlatform());
47
- var isTouchDevice = () => isDom() && !!navigator.maxTouchPoints;
48
- var isMac = () => pt(/^Mac/) && !isTouchDevice;
49
- var isApple = () => pt(/mac|iphone|ipad|ipod/i);
50
- var isIos = () => isApple() && !isMac();
51
-
52
- // ../../utilities/dom/src/event.ts
53
- var supportsPointerEvent = () => isDom() && window.onpointerdown === null;
54
- var supportsTouchEvent = () => isDom() && window.ontouchstart === null;
55
- var supportsMouseEvent = () => isDom() && window.onmousedown === null;
56
- var isMouseEvent = (v) => isObject(v) && hasProp(v, "button");
57
- var isTouchEvent = (v) => isObject(v) && hasProp(v, "touches");
58
- var isLeftClick = (v) => v.button === 0;
59
-
60
- // ../../utilities/dom/src/get-element-offset.ts
61
- function getElementOffset(element) {
62
- let left = 0;
63
- let top = 0;
64
- let el = element;
65
- if (el.parentNode) {
66
- do {
67
- left += el.offsetLeft;
68
- top += el.offsetTop;
69
- } while ((el = el.offsetParent) && el.nodeType < 9);
70
- el = element;
71
- do {
72
- left -= el.scrollLeft;
73
- top -= el.scrollTop;
74
- } while ((el = el.parentNode) && !/body/i.test(el.nodeName));
75
- }
76
- return {
77
- top,
78
- right: innerWidth - left - element.offsetWidth,
79
- bottom: innerHeight - top - element.offsetHeight,
80
- left
81
- };
82
- }
83
-
84
- // ../../utilities/dom/src/get-point-relative-to-element.ts
85
- function getPointRelativeToNode(point, element) {
86
- const offset = getElementOffset(element);
87
- const x = point.x - offset.left;
88
- const y = point.y - offset.top;
89
- return { x, y };
90
- }
91
- var clampPercent = (value) => Math.max(0, Math.min(100, value));
92
- function getPointPercentRelativeToNode(point, element) {
93
- const relativePoint = getPointRelativeToNode(point, element);
94
- const x = relativePoint.x / element.offsetWidth * 100;
95
- const y = relativePoint.y / element.offsetHeight * 100;
96
- return { x: clampPercent(x), y: clampPercent(y) };
97
- }
98
- function normalizePointValue(point, options) {
99
- const { dir = "ltr", orientation = "horizontal" } = options;
100
- const { x, y } = point;
101
- let result = { x, y };
102
- if (orientation === "horizontal" && dir === "rtl") {
103
- result = { x: 100 - x, y };
104
- }
105
- return orientation === "horizontal" ? result.x : result.y;
106
- }
107
-
108
- // ../../utilities/dom/src/listener.ts
109
- var isRef = (v) => hasProp(v, "current");
110
- var fallback = { pageX: 0, pageY: 0, clientX: 0, clientY: 0 };
111
- function extractInfo(event, type = "page") {
112
- const point = isTouchEvent(event) ? event.touches[0] || event.changedTouches[0] || fallback : event;
113
- return {
114
- point: {
115
- x: point[`${type}X`],
116
- y: point[`${type}Y`]
117
- }
118
- };
119
- }
120
- function addDomEvent(target, eventName, handler, options) {
121
- const node = isRef(target) ? target.current : runIfFn(target);
122
- node?.addEventListener(eventName, handler, options);
123
- return () => {
124
- node?.removeEventListener(eventName, handler, options);
125
- };
126
- }
127
- function addPointerEvent(target, event, listener, options) {
128
- const type = getEventName(event) ?? event;
129
- return addDomEvent(target, type, wrapHandler(listener, event === "pointerdown"), options);
130
- }
131
- function wrapHandler(fn, filter = false) {
132
- const listener = (event) => {
133
- fn(event, extractInfo(event));
134
- };
135
- return filter ? filterPrimaryPointer(listener) : listener;
136
- }
137
- function filterPrimaryPointer(fn) {
138
- return (event) => {
139
- const win = event.view ?? window;
140
- const isMouseEvent2 = event instanceof win.MouseEvent;
141
- const isPrimary = !isMouseEvent2 || isMouseEvent2 && event.button === 0;
142
- if (isPrimary)
143
- fn(event);
144
- };
145
- }
146
- var mouseEventNames = {
147
- pointerdown: "mousedown",
148
- pointermove: "mousemove",
149
- pointerup: "mouseup",
150
- pointercancel: "mousecancel",
151
- pointerover: "mouseover",
152
- pointerout: "mouseout",
153
- pointerenter: "mouseenter",
154
- pointerleave: "mouseleave"
155
- };
156
- var touchEventNames = {
157
- pointerdown: "touchstart",
158
- pointermove: "touchmove",
159
- pointerup: "touchend",
160
- pointercancel: "touchcancel"
161
- };
162
- function getEventName(evt) {
163
- if (supportsPointerEvent())
164
- return evt;
165
- if (supportsTouchEvent())
166
- return touchEventNames[evt];
167
- if (supportsMouseEvent())
168
- return mouseEventNames[evt];
169
- return evt;
170
- }
171
-
172
- // ../../utilities/dom/src/next-tick.ts
173
- function nextTick(fn) {
174
- const set = /* @__PURE__ */ new Set();
175
- function raf2(fn2) {
176
- const id = globalThis.requestAnimationFrame(fn2);
177
- set.add(() => globalThis.cancelAnimationFrame(id));
178
- }
179
- raf2(() => raf2(fn));
180
- return function cleanup() {
181
- set.forEach(function(fn2) {
182
- fn2();
183
- });
184
- };
185
- }
186
- function raf(fn) {
187
- const id = globalThis.requestAnimationFrame(fn);
188
- return function cleanup() {
189
- globalThis.cancelAnimationFrame(id);
190
- };
191
- }
192
-
193
- // ../../utilities/dom/src/text-selection.ts
194
- var state = "default";
195
- var savedUserSelect = "";
196
- var modifiedElementMap = /* @__PURE__ */ new WeakMap();
197
- function disableTextSelection({ target, doc } = {}) {
198
- const _document = doc ?? document;
199
- if (isIos()) {
200
- if (state === "default") {
201
- savedUserSelect = _document.documentElement.style.webkitUserSelect;
202
- _document.documentElement.style.webkitUserSelect = "none";
203
- }
204
- state = "disabled";
205
- } else if (target) {
206
- modifiedElementMap.set(target, target.style.userSelect);
207
- target.style.userSelect = "none";
208
- }
209
- return () => restoreTextSelection({ target, doc: _document });
210
- }
211
- function restoreTextSelection({ target, doc } = {}) {
212
- const _document = doc ?? document;
213
- if (isIos()) {
214
- if (state !== "disabled")
215
- return;
216
- state = "restoring";
217
- setTimeout(() => {
218
- nextTick(() => {
219
- if (state === "restoring") {
220
- if (_document.documentElement.style.webkitUserSelect === "none") {
221
- _document.documentElement.style.webkitUserSelect = savedUserSelect || "";
222
- }
223
- savedUserSelect = "";
224
- state = "default";
225
- }
226
- });
227
- }, 300);
228
- } else {
229
- if (target && modifiedElementMap.has(target)) {
230
- let targetOldUserSelect = modifiedElementMap.get(target);
231
- if (target.style.userSelect === "none") {
232
- target.style.userSelect = targetOldUserSelect ?? "";
233
- }
234
- if (target.getAttribute("style") === "") {
235
- target.removeAttribute("style");
236
- }
237
- modifiedElementMap.delete(target);
238
- }
239
- }
240
- }
241
-
242
- // ../../utilities/dom/src/pointer-event.ts
243
- var THRESHOLD = 5;
244
- function trackPointerMove(doc, opts) {
245
- const { onPointerMove, onPointerUp } = opts;
246
- const handlePointerMove = (event, info) => {
247
- const { point: p } = info;
248
- const distance = Math.sqrt(p.x ** 2 + p.y ** 2);
249
- if (distance < THRESHOLD)
250
- return;
251
- if (isMouseEvent(event) && isLeftClick(event)) {
252
- onPointerUp();
253
- return;
254
- }
255
- onPointerMove(info, event);
256
- };
257
- return callAll(
258
- addPointerEvent(doc, "pointermove", handlePointerMove, false),
259
- addPointerEvent(doc, "pointerup", onPointerUp, false),
260
- addPointerEvent(doc, "pointercancel", onPointerUp, false),
261
- addPointerEvent(doc, "contextmenu", onPointerUp, false),
262
- disableTextSelection({ doc })
263
- );
264
- }
265
-
266
- // src/splitter.machine.ts
14
+ import { getRelativePointPercent, trackPointerMove } from "@zag-js/dom-event";
15
+ import { raf } from "@zag-js/dom-query";
16
+ import { compact } from "@zag-js/utils";
267
17
  function machine(userContext) {
268
18
  const ctx = compact(userContext);
269
19
  return createMachine(
@@ -521,8 +271,7 @@ function machine(userContext) {
521
271
  const rootEl = dom.getRootEl(ctx2);
522
272
  if (!panels || !rootEl || !bounds)
523
273
  return;
524
- const percent = getPointPercentRelativeToNode(evt.point, rootEl);
525
- let pointValue = normalizePointValue(percent, ctx2);
274
+ let pointValue = getRelativePointPercent(evt.point, rootEl).normalize(ctx2);
526
275
  ctx2.activeResizeState = {
527
276
  isAtMin: pointValue < bounds.min,
528
277
  isAtMax: pointValue > bounds.max
@@ -1,38 +1,6 @@
1
- // ../../utilities/dom/src/query.ts
2
- function isDocument(el) {
3
- return el.nodeType === Node.DOCUMENT_NODE;
4
- }
5
- function isWindow(value) {
6
- return value?.toString() === "[object Window]";
7
- }
8
- function getDocument(el) {
9
- if (isWindow(el))
10
- return el.document;
11
- if (isDocument(el))
12
- return el;
13
- return el?.ownerDocument ?? document;
14
- }
15
- function defineDomHelpers(helpers) {
16
- const dom2 = {
17
- getRootNode: (ctx) => ctx.getRootNode?.() ?? document,
18
- getDoc: (ctx) => getDocument(dom2.getRootNode(ctx)),
19
- getWin: (ctx) => dom2.getDoc(ctx).defaultView ?? window,
20
- getActiveElement: (ctx) => dom2.getDoc(ctx).activeElement,
21
- getById: (ctx, id) => dom2.getRootNode(ctx).getElementById(id)
22
- };
23
- return {
24
- ...dom2,
25
- ...helpers
26
- };
27
- }
28
-
29
- // ../../utilities/dom/src/nodelist.ts
30
- function queryAll(root, selector) {
31
- return Array.from(root?.querySelectorAll(selector) ?? []);
32
- }
33
-
34
1
  // src/splitter.dom.ts
35
- var dom = defineDomHelpers({
2
+ import { createScope, queryAll } from "@zag-js/dom-query";
3
+ var dom = createScope({
36
4
  getRootId: (ctx) => `splitter:${ctx.id}`,
37
5
  getResizeTriggerId: (ctx, id) => `splitter:${ctx.id}:splitter:${id}`,
38
6
  getToggleTriggerId: (ctx) => `splitter:${ctx.id}:toggle-btn`,
@@ -3,73 +3,72 @@ import {
3
3
  } from "./chunk-HPRMFGOY.mjs";
4
4
  import {
5
5
  dom
6
- } from "./chunk-QY3R66FZ.mjs";
6
+ } from "./chunk-BSW3DTW4.mjs";
7
7
  import {
8
8
  getHandleBounds
9
9
  } from "./chunk-MV44GBQY.mjs";
10
10
 
11
- // ../../utilities/dom/src/attrs.ts
12
- var dataAttr = (guard) => {
13
- return guard ? "" : void 0;
14
- };
15
-
16
- // ../../utilities/dom/src/keyboard-event.ts
17
- var rtlKeyMap = {
18
- ArrowLeft: "ArrowRight",
19
- ArrowRight: "ArrowLeft"
20
- };
21
- var sameKeyMap = {
22
- Up: "ArrowUp",
23
- Down: "ArrowDown",
24
- Esc: "Escape",
25
- " ": "Space",
26
- ",": "Comma",
27
- Left: "ArrowLeft",
28
- Right: "ArrowRight"
29
- };
30
- function getEventKey(event, options = {}) {
31
- const { dir = "ltr", orientation = "horizontal" } = options;
32
- let { key } = event;
33
- key = sameKeyMap[key] ?? key;
34
- const isRtl = dir === "rtl" && orientation === "horizontal";
35
- if (isRtl && key in rtlKeyMap) {
36
- key = rtlKeyMap[key];
37
- }
38
- return key;
39
- }
40
- var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
41
- var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
42
- function getEventStep(event) {
43
- if (event.ctrlKey || event.metaKey) {
44
- return 0.1;
45
- } else {
46
- const isPageKey = PAGE_KEYS.has(event.key);
47
- const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
48
- return isSkipKey ? 10 : 1;
49
- }
50
- }
51
-
52
11
  // src/splitter.connect.ts
12
+ import { getEventKey, getEventStep } from "@zag-js/dom-event";
13
+ import { dataAttr } from "@zag-js/dom-query";
53
14
  function connect(state, send, normalize) {
54
15
  const isHorizontal = state.context.isHorizontal;
55
16
  const isFocused = state.hasTag("focus");
56
17
  const isDragging = state.matches("dragging");
57
18
  const api = {
19
+ /**
20
+ * Whether the splitter is focused.
21
+ */
58
22
  isFocused,
23
+ /**
24
+ * Whether the splitter is being dragged.
25
+ */
59
26
  isDragging,
27
+ /**
28
+ * The bounds of the currently dragged splitter handle.
29
+ */
60
30
  bounds: getHandleBounds(state.context),
31
+ /**
32
+ * Function to collapse a panel.
33
+ */
61
34
  collapse(id) {
62
35
  send({ type: "COLLAPSE", id });
63
36
  },
37
+ /**
38
+ * Function to expand a panel.
39
+ */
64
40
  expand(id) {
65
41
  send({ type: "EXPAND", id });
66
42
  },
43
+ /**
44
+ * Function to toggle a panel between collapsed and expanded.
45
+ */
67
46
  toggle(id) {
68
47
  send({ type: "TOGGLE", id });
69
48
  },
49
+ /**
50
+ * Function to set the size of a panel.
51
+ */
70
52
  setSize(id, size) {
71
53
  send({ type: "SET_SIZE", id, size });
72
54
  },
55
+ /**
56
+ * Returns the state details for a resize trigger.
57
+ */
58
+ getResizeTriggerState(props) {
59
+ const { id, disabled } = props;
60
+ const ids = id.split(":");
61
+ const panelIds = ids.map((id2) => dom.getPanelId(state.context, id2));
62
+ const panels = getHandleBounds(state.context, id);
63
+ return {
64
+ isDisabled: !!disabled,
65
+ isFocused: state.context.activeResizeId === id && isFocused,
66
+ panelIds,
67
+ min: panels?.min,
68
+ max: panels?.max,
69
+ value: 0
70
+ };
71
+ },
73
72
  rootProps: normalize.element({
74
73
  ...parts.root.attrs,
75
74
  "data-orientation": state.context.orientation,
@@ -93,20 +92,14 @@ function connect(state, send, normalize) {
93
92
  style: dom.getPanelStyle(state.context, id)
94
93
  });
95
94
  },
96
- getResizeTriggerState(props) {
97
- const { id, disabled } = props;
98
- const ids = id.split(":");
99
- const panelIds = ids.map((id2) => dom.getPanelId(state.context, id2));
100
- const panels = getHandleBounds(state.context, id);
101
- return {
102
- isDisabled: !!disabled,
103
- isFocused: state.context.activeResizeId === id && isFocused,
104
- panelIds,
105
- min: panels?.min,
106
- max: panels?.max,
107
- value: 0
108
- };
109
- },
95
+ // toggleTriggerProps: normalize.element({
96
+ // ...parts.toggleButton.attrs,
97
+ // id: dom.getToggleButtonId(state.context),
98
+ // "aria-label": state.context.isAtMin ? "Expand Primary Pane" : "Collapse Primary Pane",
99
+ // onClick() {
100
+ // send("TOGGLE")
101
+ // },
102
+ // }),
110
103
  getResizeTriggerProps(props) {
111
104
  const { id, disabled, step = 1 } = props;
112
105
  const triggerState = api.getResizeTriggerState(props);