@zag-js/splitter 0.10.2 → 0.10.3

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,58 @@
1
- import {
2
- dom
3
- } from "./chunk-3GDOPT5W.mjs";
4
- export {
5
- dom
6
- };
1
+ import { createScope, queryAll } from '@zag-js/dom-query';
2
+
3
+ const dom = createScope({
4
+ getRootId: (ctx) => ctx.ids?.root ?? `splitter:${ctx.id}`,
5
+ getResizeTriggerId: (ctx, id) => ctx.ids?.resizeTrigger?.(id) ?? `splitter:${ctx.id}:splitter:${id}`,
6
+ getToggleTriggerId: (ctx) => ctx.ids?.toggleTrigger?.(ctx.id) ?? `splitter:${ctx.id}:toggle-btn`,
7
+ getLabelId: (ctx) => ctx.ids?.label ?? `splitter:${ctx.id}:label`,
8
+ getPanelId: (ctx, id) => ctx.ids?.panel?.(id) ?? `splitter:${ctx.id}:panel:${id}`,
9
+ globalCursorId: (ctx) => `splitter:${ctx.id}:global-cursor`,
10
+ getRootEl: (ctx) => dom.queryById(ctx, dom.getRootId(ctx)),
11
+ getResizeTriggerEl: (ctx, id) => dom.getById(ctx, dom.getResizeTriggerId(ctx, id)),
12
+ getPanelEl: (ctx, id) => dom.getById(ctx, dom.getPanelId(ctx, id)),
13
+ getCursor(ctx) {
14
+ const x = ctx.isHorizontal;
15
+ let cursor = x ? "col-resize" : "row-resize";
16
+ if (ctx.activeResizeState.isAtMin)
17
+ cursor = x ? "e-resize" : "s-resize";
18
+ if (ctx.activeResizeState.isAtMax)
19
+ cursor = x ? "w-resize" : "n-resize";
20
+ return cursor;
21
+ },
22
+ getPanelStyle(ctx, id) {
23
+ const flexGrow = ctx.panels.find((panel) => panel.id === id)?.size ?? "0";
24
+ return {
25
+ flexBasis: 0,
26
+ flexGrow,
27
+ flexShrink: 1,
28
+ overflow: "hidden"
29
+ };
30
+ },
31
+ getActiveHandleEl(ctx) {
32
+ const activeId = ctx.activeResizeId;
33
+ if (activeId == null)
34
+ return;
35
+ return dom.getById(ctx, dom.getResizeTriggerId(ctx, activeId));
36
+ },
37
+ getResizeTriggerEls(ctx) {
38
+ const ownerId = CSS.escape(dom.getRootId(ctx));
39
+ return queryAll(dom.getRootEl(ctx), `[role=separator][data-ownedby='${ownerId}']`);
40
+ },
41
+ setupGlobalCursor(ctx) {
42
+ const styleEl = dom.getById(ctx, dom.globalCursorId(ctx));
43
+ const textContent = `* { cursor: ${dom.getCursor(ctx)} !important; }`;
44
+ if (styleEl) {
45
+ styleEl.textContent = textContent;
46
+ } else {
47
+ const style = dom.getDoc(ctx).createElement("style");
48
+ style.id = dom.globalCursorId(ctx);
49
+ style.textContent = textContent;
50
+ dom.getDoc(ctx).head.appendChild(style);
51
+ }
52
+ },
53
+ removeGlobalCursor(ctx) {
54
+ dom.getById(ctx, dom.globalCursorId(ctx))?.remove();
55
+ }
56
+ });
57
+
58
+ export { dom };
@@ -1,7 +1,3 @@
1
- import * as _zag_js_core from '@zag-js/core';
2
- import { UserDefinedContext, MachineContext, MachineState } from './splitter.types.js';
3
- import '@zag-js/types';
4
-
5
- declare function machine(userContext: UserDefinedContext): _zag_js_core.Machine<MachineContext, MachineState, _zag_js_core.StateMachine.AnyEventObject>;
6
-
7
- export { machine };
1
+ import type { Machine, StateMachine } from '@zag-js/core';
2
+ import type { MachineContext, MachineState, UserDefinedContext } from "./splitter.types";
3
+ export declare function machine(userContext: UserDefinedContext): Machine<MachineContext, MachineState, StateMachine.AnyEventObject>;
@@ -1,216 +1,17 @@
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);
1
+ 'use strict';
19
2
 
20
- // src/splitter.machine.ts
21
- var splitter_machine_exports = {};
22
- __export(splitter_machine_exports, {
23
- machine: () => machine
24
- });
25
- module.exports = __toCommonJS(splitter_machine_exports);
26
- var import_core = require("@zag-js/core");
27
- var import_dom_event = require("@zag-js/dom-event");
28
- var import_dom_query2 = require("@zag-js/dom-query");
29
- var import_utils = require("@zag-js/utils");
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
30
4
 
31
- // src/splitter.dom.ts
32
- var import_dom_query = require("@zag-js/dom-query");
33
- var dom = (0, import_dom_query.createScope)({
34
- getRootId: (ctx) => ctx.ids?.root ?? `splitter:${ctx.id}`,
35
- getResizeTriggerId: (ctx, id) => ctx.ids?.resizeTrigger?.(id) ?? `splitter:${ctx.id}:splitter:${id}`,
36
- getToggleTriggerId: (ctx) => ctx.ids?.toggleTrigger?.(ctx.id) ?? `splitter:${ctx.id}:toggle-btn`,
37
- getLabelId: (ctx) => ctx.ids?.label ?? `splitter:${ctx.id}:label`,
38
- getPanelId: (ctx, id) => ctx.ids?.panel?.(id) ?? `splitter:${ctx.id}:panel:${id}`,
39
- globalCursorId: (ctx) => `splitter:${ctx.id}:global-cursor`,
40
- getRootEl: (ctx) => dom.queryById(ctx, dom.getRootId(ctx)),
41
- getResizeTriggerEl: (ctx, id) => dom.getById(ctx, dom.getResizeTriggerId(ctx, id)),
42
- getPanelEl: (ctx, id) => dom.getById(ctx, dom.getPanelId(ctx, id)),
43
- getCursor(ctx) {
44
- const x = ctx.isHorizontal;
45
- let cursor = x ? "col-resize" : "row-resize";
46
- if (ctx.activeResizeState.isAtMin)
47
- cursor = x ? "e-resize" : "s-resize";
48
- if (ctx.activeResizeState.isAtMax)
49
- cursor = x ? "w-resize" : "n-resize";
50
- return cursor;
51
- },
52
- getPanelStyle(ctx, id) {
53
- const flexGrow = ctx.panels.find((panel) => panel.id === id)?.size ?? "0";
54
- return {
55
- flexBasis: 0,
56
- flexGrow,
57
- flexShrink: 1,
58
- overflow: "hidden"
59
- };
60
- },
61
- getActiveHandleEl(ctx) {
62
- const activeId = ctx.activeResizeId;
63
- if (activeId == null)
64
- return;
65
- return dom.getById(ctx, dom.getResizeTriggerId(ctx, activeId));
66
- },
67
- getResizeTriggerEls(ctx) {
68
- const ownerId = CSS.escape(dom.getRootId(ctx));
69
- return (0, import_dom_query.queryAll)(dom.getRootEl(ctx), `[role=separator][data-ownedby='${ownerId}']`);
70
- },
71
- setupGlobalCursor(ctx) {
72
- const styleEl = dom.getById(ctx, dom.globalCursorId(ctx));
73
- const textContent = `* { cursor: ${dom.getCursor(ctx)} !important; }`;
74
- if (styleEl) {
75
- styleEl.textContent = textContent;
76
- } else {
77
- const style = dom.getDoc(ctx).createElement("style");
78
- style.id = dom.globalCursorId(ctx);
79
- style.textContent = textContent;
80
- dom.getDoc(ctx).head.appendChild(style);
81
- }
82
- },
83
- removeGlobalCursor(ctx) {
84
- dom.getById(ctx, dom.globalCursorId(ctx))?.remove();
85
- }
86
- });
5
+ const core = require('@zag-js/core');
6
+ const domEvent = require('@zag-js/dom-event');
7
+ const domQuery = require('@zag-js/dom-query');
8
+ const utils = require('@zag-js/utils');
9
+ const splitter_dom = require('./splitter.dom.js');
10
+ const splitter_utils = require('./splitter.utils.js');
87
11
 
88
- // src/splitter.utils.ts
89
- function validateSize(key, size) {
90
- if (Math.floor(size) > 100) {
91
- throw new Error(`Total ${key} of panels cannot be greater than 100`);
92
- }
93
- }
94
- function getNormalizedPanels(ctx) {
95
- let numOfPanelsWithoutSize = 0;
96
- let totalSize = 0;
97
- let totalMinSize = 0;
98
- const panels = ctx.size.map((panel) => {
99
- const minSize = panel.minSize ?? 0;
100
- const maxSize = panel.maxSize ?? 100;
101
- totalMinSize += minSize;
102
- if (panel.size == null) {
103
- numOfPanelsWithoutSize++;
104
- } else {
105
- totalSize += panel.size;
106
- }
107
- return {
108
- ...panel,
109
- minSize,
110
- maxSize
111
- };
112
- });
113
- validateSize("minSize", totalMinSize);
114
- validateSize("size", totalSize);
115
- let end = 0;
116
- let remainingSize = 0;
117
- const result = panels.map((panel) => {
118
- let start = end;
119
- if (panel.size != null) {
120
- end += panel.size;
121
- remainingSize = panel.size - panel.minSize;
122
- return {
123
- ...panel,
124
- start,
125
- end,
126
- remainingSize
127
- };
128
- }
129
- const size = (100 - totalSize) / numOfPanelsWithoutSize;
130
- end += size;
131
- remainingSize = size - panel.minSize;
132
- return { ...panel, size, start, end, remainingSize };
133
- });
134
- return result;
135
- }
136
- function getHandlePanels(ctx, id = ctx.activeResizeId) {
137
- const [beforeId, afterId] = id?.split(":") ?? [];
138
- if (!beforeId || !afterId)
139
- return;
140
- const beforeIndex = ctx.previousPanels.findIndex((panel) => panel.id === beforeId);
141
- const afterIndex = ctx.previousPanels.findIndex((panel) => panel.id === afterId);
142
- if (beforeIndex === -1 || afterIndex === -1)
143
- return;
144
- const before = ctx.previousPanels[beforeIndex];
145
- const after = ctx.previousPanels[afterIndex];
146
- return {
147
- before: {
148
- ...before,
149
- index: beforeIndex
150
- },
151
- after: {
152
- ...after,
153
- index: afterIndex
154
- }
155
- };
156
- }
157
- function getHandleBounds(ctx, id = ctx.activeResizeId) {
158
- const panels = getHandlePanels(ctx, id);
159
- if (!panels)
160
- return;
161
- const { before, after } = panels;
162
- return {
163
- min: Math.max(before.start + before.minSize, after.end - after.maxSize),
164
- max: Math.min(after.end - after.minSize, before.maxSize + before.start)
165
- };
166
- }
167
- function getPanelBounds(ctx, id) {
168
- const bounds = getHandleBounds(ctx, id);
169
- const panels = getHandlePanels(ctx, id);
170
- if (!bounds || !panels)
171
- return;
172
- const { before, after } = panels;
173
- const beforeMin = Math.abs(before.start - bounds.min);
174
- const afterMin = after.size + (before.size - beforeMin);
175
- const beforeMax = Math.abs(before.start - bounds.max);
176
- const afterMax = after.size - (beforeMax - before.size);
177
- return {
178
- before: {
179
- index: before.index,
180
- min: beforeMin,
181
- max: beforeMax,
182
- isAtMin: beforeMin === before.size,
183
- isAtMax: beforeMax === before.size,
184
- up(step) {
185
- return Math.min(before.size + step, beforeMax);
186
- },
187
- down(step) {
188
- return Math.max(before.size - step, beforeMin);
189
- }
190
- },
191
- after: {
192
- index: after.index,
193
- min: afterMin,
194
- max: afterMax,
195
- isAtMin: afterMin === after.size,
196
- isAtMax: afterMax === after.size,
197
- up(step) {
198
- return Math.min(after.size + step, afterMin);
199
- },
200
- down(step) {
201
- return Math.max(after.size - step, afterMax);
202
- }
203
- }
204
- };
205
- }
206
- function clamp(value, min, max) {
207
- return Math.min(Math.max(value, min), max);
208
- }
209
-
210
- // src/splitter.machine.ts
211
12
  function machine(userContext) {
212
- const ctx = (0, import_utils.compact)(userContext);
213
- return (0, import_core.createMachine)(
13
+ const ctx = utils.compact(userContext);
14
+ return core.createMachine(
214
15
  {
215
16
  id: "splitter",
216
17
  initial: "idle",
@@ -232,7 +33,7 @@ function machine(userContext) {
232
33
  },
233
34
  computed: {
234
35
  isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
235
- panels: (ctx2) => getNormalizedPanels(ctx2)
36
+ panels: (ctx2) => splitter_utils.getNormalizedPanels(ctx2)
236
37
  },
237
38
  on: {
238
39
  SET_PANEL_SIZE: {
@@ -336,8 +137,8 @@ function machine(userContext) {
336
137
  {
337
138
  activities: {
338
139
  trackPointerMove: (ctx2, _evt, { send }) => {
339
- const doc = dom.getDoc(ctx2);
340
- return (0, import_dom_event.trackPointerMove)(doc, {
140
+ const doc = splitter_dom.dom.getDoc(ctx2);
141
+ return domEvent.trackPointerMove(doc, {
341
142
  onPointerMove(info) {
342
143
  send({ type: "POINTER_MOVE", point: info.point });
343
144
  },
@@ -358,10 +159,10 @@ function machine(userContext) {
358
159
  },
359
160
  actions: {
360
161
  setGlobalCursor(ctx2) {
361
- dom.setupGlobalCursor(ctx2);
162
+ splitter_dom.dom.setupGlobalCursor(ctx2);
362
163
  },
363
164
  clearGlobalCursor(ctx2) {
364
- dom.removeGlobalCursor(ctx2);
165
+ splitter_dom.dom.removeGlobalCursor(ctx2);
365
166
  },
366
167
  invokeOnResize(ctx2) {
367
168
  ctx2.onResize?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
@@ -387,12 +188,12 @@ function machine(userContext) {
387
188
  setPanelSize(ctx2, evt) {
388
189
  const { id, size } = evt;
389
190
  ctx2.size = ctx2.size.map((panel) => {
390
- const panelSize = clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
191
+ const panelSize = splitter_utils.clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
391
192
  return panel.id === id ? { ...panel, size: panelSize } : panel;
392
193
  });
393
194
  },
394
195
  setStartPanelToMin(ctx2) {
395
- const bounds = getPanelBounds(ctx2);
196
+ const bounds = splitter_utils.getPanelBounds(ctx2);
396
197
  if (!bounds)
397
198
  return;
398
199
  const { before, after } = bounds;
@@ -400,7 +201,7 @@ function machine(userContext) {
400
201
  ctx2.size[after.index].size = after.min;
401
202
  },
402
203
  setStartPanelToMax(ctx2) {
403
- const bounds = getPanelBounds(ctx2);
204
+ const bounds = splitter_utils.getPanelBounds(ctx2);
404
205
  if (!bounds)
405
206
  return;
406
207
  const { before, after } = bounds;
@@ -408,7 +209,7 @@ function machine(userContext) {
408
209
  ctx2.size[after.index].size = after.max;
409
210
  },
410
211
  expandStartPanel(ctx2, evt) {
411
- const bounds = getPanelBounds(ctx2);
212
+ const bounds = splitter_utils.getPanelBounds(ctx2);
412
213
  if (!bounds)
413
214
  return;
414
215
  const { before, after } = bounds;
@@ -416,7 +217,7 @@ function machine(userContext) {
416
217
  ctx2.size[after.index].size = after.down(evt.step);
417
218
  },
418
219
  shrinkStartPanel(ctx2, evt) {
419
- const bounds = getPanelBounds(ctx2);
220
+ const bounds = splitter_utils.getPanelBounds(ctx2);
420
221
  if (!bounds)
421
222
  return;
422
223
  const { before, after } = bounds;
@@ -424,7 +225,7 @@ function machine(userContext) {
424
225
  ctx2.size[after.index].size = after.up(evt.step);
425
226
  },
426
227
  resetStartPanel(ctx2, evt) {
427
- const bounds = getPanelBounds(ctx2, evt.id);
228
+ const bounds = splitter_utils.getPanelBounds(ctx2, evt.id);
428
229
  if (!bounds)
429
230
  return;
430
231
  const { before, after } = bounds;
@@ -432,20 +233,20 @@ function machine(userContext) {
432
233
  ctx2.size[after.index].size = ctx2.initialSize[after.index].size;
433
234
  },
434
235
  focusResizeHandle(ctx2) {
435
- (0, import_dom_query2.raf)(() => {
436
- dom.getActiveHandleEl(ctx2)?.focus({ preventScroll: true });
236
+ domQuery.raf(() => {
237
+ splitter_dom.dom.getActiveHandleEl(ctx2)?.focus({ preventScroll: true });
437
238
  });
438
239
  },
439
240
  blurResizeHandle(ctx2) {
440
- (0, import_dom_query2.raf)(() => {
441
- dom.getActiveHandleEl(ctx2)?.blur();
241
+ domQuery.raf(() => {
242
+ splitter_dom.dom.getActiveHandleEl(ctx2)?.blur();
442
243
  });
443
244
  },
444
245
  setPreviousPanels(ctx2) {
445
246
  ctx2.previousPanels = ctx2.panels.slice();
446
247
  },
447
248
  setActiveResizeState(ctx2) {
448
- const panels = getPanelBounds(ctx2);
249
+ const panels = splitter_utils.getPanelBounds(ctx2);
449
250
  if (!panels)
450
251
  return;
451
252
  const { before } = panels;
@@ -455,12 +256,12 @@ function machine(userContext) {
455
256
  };
456
257
  },
457
258
  setPointerValue(ctx2, evt) {
458
- const panels = getHandlePanels(ctx2);
459
- const bounds = getHandleBounds(ctx2);
259
+ const panels = splitter_utils.getHandlePanels(ctx2);
260
+ const bounds = splitter_utils.getHandleBounds(ctx2);
460
261
  if (!panels || !bounds)
461
262
  return;
462
- const rootEl = dom.getRootEl(ctx2);
463
- const relativePoint = (0, import_dom_event.getRelativePoint)(evt.point, rootEl);
263
+ const rootEl = splitter_dom.dom.getRootEl(ctx2);
264
+ const relativePoint = domEvent.getRelativePoint(evt.point, rootEl);
464
265
  const percentValue = relativePoint.getPercentValue({
465
266
  dir: ctx2.dir,
466
267
  orientation: ctx2.orientation
@@ -470,7 +271,7 @@ function machine(userContext) {
470
271
  isAtMin: pointValue < bounds.min,
471
272
  isAtMax: pointValue > bounds.max
472
273
  };
473
- pointValue = clamp(pointValue, bounds.min, bounds.max);
274
+ pointValue = splitter_utils.clamp(pointValue, bounds.min, bounds.max);
474
275
  const { before, after } = panels;
475
276
  const offset = pointValue - before.end;
476
277
  ctx2.size[before.index].size = before.size + offset;
@@ -480,7 +281,5 @@ function machine(userContext) {
480
281
  }
481
282
  );
482
283
  }
483
- // Annotate the CommonJS export names for ESM import in node:
484
- 0 && (module.exports = {
485
- machine
486
- });
284
+
285
+ exports.machine = machine;