@zag-js/splitter 0.49.0 → 0.51.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.
package/dist/index.mjs CHANGED
@@ -1,656 +1,2 @@
1
- // src/splitter.anatomy.ts
2
- import { createAnatomy } from "@zag-js/anatomy";
3
- var anatomy = createAnatomy("splitter").parts("root", "panel", "resizeTrigger");
4
- var parts = anatomy.build();
5
-
6
- // src/splitter.connect.ts
7
- import { getEventKey, getEventStep } from "@zag-js/dom-event";
8
- import { dataAttr } from "@zag-js/dom-query";
9
-
10
- // src/splitter.dom.ts
11
- import { createScope, queryAll } from "@zag-js/dom-query";
12
- var dom = createScope({
13
- getRootId: (ctx) => ctx.ids?.root ?? `splitter:${ctx.id}`,
14
- getResizeTriggerId: (ctx, id) => ctx.ids?.resizeTrigger?.(id) ?? `splitter:${ctx.id}:splitter:${id}`,
15
- getLabelId: (ctx) => ctx.ids?.label ?? `splitter:${ctx.id}:label`,
16
- getPanelId: (ctx, id) => ctx.ids?.panel?.(id) ?? `splitter:${ctx.id}:panel:${id}`,
17
- globalCursorId: (ctx) => `splitter:${ctx.id}:global-cursor`,
18
- getRootEl: (ctx) => dom.getById(ctx, dom.getRootId(ctx)),
19
- getResizeTriggerEl: (ctx, id) => dom.getById(ctx, dom.getResizeTriggerId(ctx, id)),
20
- getPanelEl: (ctx, id) => dom.getById(ctx, dom.getPanelId(ctx, id)),
21
- getCursor(ctx) {
22
- const x = ctx.isHorizontal;
23
- let cursor = x ? "col-resize" : "row-resize";
24
- if (ctx.activeResizeState.isAtMin)
25
- cursor = x ? "e-resize" : "s-resize";
26
- if (ctx.activeResizeState.isAtMax)
27
- cursor = x ? "w-resize" : "n-resize";
28
- return cursor;
29
- },
30
- getPanelStyle(ctx, id) {
31
- const flexGrow = ctx.panels.find((panel) => panel.id === id)?.size ?? "0";
32
- return {
33
- flexBasis: 0,
34
- flexGrow,
35
- flexShrink: 1,
36
- overflow: "hidden"
37
- };
38
- },
39
- getActiveHandleEl(ctx) {
40
- const activeId = ctx.activeResizeId;
41
- if (activeId == null)
42
- return;
43
- return dom.getById(ctx, dom.getResizeTriggerId(ctx, activeId));
44
- },
45
- getResizeTriggerEls(ctx) {
46
- const ownerId = CSS.escape(dom.getRootId(ctx));
47
- return queryAll(dom.getRootEl(ctx), `[role=separator][data-ownedby='${ownerId}']`);
48
- },
49
- setupGlobalCursor(ctx) {
50
- const styleEl = dom.getById(ctx, dom.globalCursorId(ctx));
51
- const textContent = `* { cursor: ${dom.getCursor(ctx)} !important; }`;
52
- if (styleEl) {
53
- styleEl.textContent = textContent;
54
- } else {
55
- const style = dom.getDoc(ctx).createElement("style");
56
- style.id = dom.globalCursorId(ctx);
57
- style.textContent = textContent;
58
- dom.getDoc(ctx).head.appendChild(style);
59
- }
60
- },
61
- removeGlobalCursor(ctx) {
62
- dom.getById(ctx, dom.globalCursorId(ctx))?.remove();
63
- }
64
- });
65
-
66
- // src/splitter.utils.ts
67
- function validateSize(key, size) {
68
- if (Math.floor(size) > 100) {
69
- throw new Error(`Total ${key} of panels cannot be greater than 100`);
70
- }
71
- }
72
- function getNormalizedPanels(ctx) {
73
- let numOfPanelsWithoutSize = 0;
74
- let totalSize = 0;
75
- let totalMinSize = 0;
76
- const panels = ctx.size.map((panel) => {
77
- const minSize = panel.minSize ?? 0;
78
- const maxSize = panel.maxSize ?? 100;
79
- totalMinSize += minSize;
80
- if (panel.size == null) {
81
- numOfPanelsWithoutSize++;
82
- } else {
83
- totalSize += panel.size;
84
- }
85
- return {
86
- ...panel,
87
- minSize,
88
- maxSize
89
- };
90
- });
91
- validateSize("minSize", totalMinSize);
92
- validateSize("size", totalSize);
93
- let end = 0;
94
- let remainingSize = 0;
95
- const result = panels.map((panel) => {
96
- let start = end;
97
- if (panel.size != null) {
98
- end += panel.size;
99
- remainingSize = panel.size - panel.minSize;
100
- return {
101
- ...panel,
102
- start,
103
- end,
104
- remainingSize
105
- };
106
- }
107
- const size = (100 - totalSize) / numOfPanelsWithoutSize;
108
- end += size;
109
- remainingSize = size - panel.minSize;
110
- return { ...panel, size, start, end, remainingSize };
111
- });
112
- return result;
113
- }
114
- function getHandlePanels(ctx, id = ctx.activeResizeId) {
115
- const [beforeId, afterId] = id?.split(":") ?? [];
116
- if (!beforeId || !afterId)
117
- return;
118
- const beforeIndex = ctx.previousPanels.findIndex((panel) => panel.id === beforeId);
119
- const afterIndex = ctx.previousPanels.findIndex((panel) => panel.id === afterId);
120
- if (beforeIndex === -1 || afterIndex === -1)
121
- return;
122
- const before = ctx.previousPanels[beforeIndex];
123
- const after = ctx.previousPanels[afterIndex];
124
- return {
125
- before: {
126
- ...before,
127
- index: beforeIndex
128
- },
129
- after: {
130
- ...after,
131
- index: afterIndex
132
- }
133
- };
134
- }
135
- function getHandleBounds(ctx, id = ctx.activeResizeId) {
136
- const panels = getHandlePanels(ctx, id);
137
- if (!panels)
138
- return;
139
- const { before, after } = panels;
140
- return {
141
- min: Math.max(before.start + before.minSize, after.end - after.maxSize),
142
- max: Math.min(after.end - after.minSize, before.maxSize + before.start)
143
- };
144
- }
145
- function getPanelBounds(ctx, id) {
146
- const bounds = getHandleBounds(ctx, id);
147
- const panels = getHandlePanels(ctx, id);
148
- if (!bounds || !panels)
149
- return;
150
- const { before, after } = panels;
151
- const beforeMin = Math.abs(before.start - bounds.min);
152
- const afterMin = after.size + (before.size - beforeMin);
153
- const beforeMax = Math.abs(before.start - bounds.max);
154
- const afterMax = after.size - (beforeMax - before.size);
155
- return {
156
- before: {
157
- index: before.index,
158
- min: beforeMin,
159
- max: beforeMax,
160
- isAtMin: beforeMin === before.size,
161
- isAtMax: beforeMax === before.size,
162
- up(step) {
163
- return Math.min(before.size + step, beforeMax);
164
- },
165
- down(step) {
166
- return Math.max(before.size - step, beforeMin);
167
- }
168
- },
169
- after: {
170
- index: after.index,
171
- min: afterMin,
172
- max: afterMax,
173
- isAtMin: afterMin === after.size,
174
- isAtMax: afterMax === after.size,
175
- up(step) {
176
- return Math.min(after.size + step, afterMin);
177
- },
178
- down(step) {
179
- return Math.max(after.size - step, afterMax);
180
- }
181
- }
182
- };
183
- }
184
- function clamp(value, min, max) {
185
- return Math.min(Math.max(value, min), max);
186
- }
187
-
188
- // src/splitter.connect.ts
189
- function connect(state, send, normalize) {
190
- const horizontal = state.context.isHorizontal;
191
- const focused = state.hasTag("focus");
192
- const dragging = state.matches("dragging");
193
- const panels = state.context.panels;
194
- function getResizeTriggerState(props2) {
195
- const { id, disabled } = props2;
196
- const ids = id.split(":");
197
- const panelIds = ids.map((id2) => dom.getPanelId(state.context, id2));
198
- const panels2 = getHandleBounds(state.context, id);
199
- return {
200
- disabled: !!disabled,
201
- focused: state.context.activeResizeId === id && focused,
202
- panelIds,
203
- min: panels2?.min,
204
- max: panels2?.max,
205
- value: 0
206
- };
207
- }
208
- return {
209
- focused,
210
- dragging,
211
- getResizeTriggerState,
212
- bounds: getHandleBounds(state.context),
213
- setToMinSize(id) {
214
- const panel = panels.find((panel2) => panel2.id === id);
215
- send({ type: "SET_PANEL_SIZE", id, size: panel?.minSize, src: "setToMinSize" });
216
- },
217
- setToMaxSize(id) {
218
- const panel = panels.find((panel2) => panel2.id === id);
219
- send({ type: "SET_PANEL_SIZE", id, size: panel?.maxSize, src: "setToMaxSize" });
220
- },
221
- setSize(id, size) {
222
- send({ type: "SET_PANEL_SIZE", id, size });
223
- },
224
- rootProps: normalize.element({
225
- ...parts.root.attrs,
226
- "data-orientation": state.context.orientation,
227
- id: dom.getRootId(state.context),
228
- dir: state.context.dir,
229
- style: {
230
- display: "flex",
231
- flexDirection: horizontal ? "row" : "column",
232
- height: "100%",
233
- width: "100%",
234
- overflow: "hidden"
235
- }
236
- }),
237
- getPanelProps(props2) {
238
- const { id } = props2;
239
- return normalize.element({
240
- ...parts.panel.attrs,
241
- "data-orientation": state.context.orientation,
242
- dir: state.context.dir,
243
- id: dom.getPanelId(state.context, id),
244
- "data-ownedby": dom.getRootId(state.context),
245
- style: dom.getPanelStyle(state.context, id)
246
- });
247
- },
248
- getResizeTriggerProps(props2) {
249
- const { id, disabled, step = 1 } = props2;
250
- const triggerState = getResizeTriggerState(props2);
251
- return normalize.element({
252
- ...parts.resizeTrigger.attrs,
253
- dir: state.context.dir,
254
- id: dom.getResizeTriggerId(state.context, id),
255
- role: "separator",
256
- "data-ownedby": dom.getRootId(state.context),
257
- tabIndex: disabled ? void 0 : 0,
258
- "aria-valuenow": triggerState.value,
259
- "aria-valuemin": triggerState.min,
260
- "aria-valuemax": triggerState.max,
261
- "data-orientation": state.context.orientation,
262
- "aria-orientation": state.context.orientation,
263
- "aria-controls": triggerState.panelIds.join(" "),
264
- "data-focus": dataAttr(triggerState.focused),
265
- "data-disabled": dataAttr(disabled),
266
- style: {
267
- touchAction: "none",
268
- userSelect: "none",
269
- flex: "0 0 auto",
270
- pointerEvents: dragging && !triggerState.focused ? "none" : void 0,
271
- cursor: horizontal ? "col-resize" : "row-resize",
272
- [horizontal ? "minHeight" : "minWidth"]: "0"
273
- },
274
- onPointerDown(event) {
275
- if (disabled) {
276
- event.preventDefault();
277
- return;
278
- }
279
- send({ type: "POINTER_DOWN", id });
280
- event.currentTarget.setPointerCapture(event.pointerId);
281
- event.preventDefault();
282
- event.stopPropagation();
283
- },
284
- onPointerUp(event) {
285
- if (disabled)
286
- return;
287
- if (event.currentTarget.hasPointerCapture(event.pointerId)) {
288
- event.currentTarget.releasePointerCapture(event.pointerId);
289
- }
290
- },
291
- onPointerOver() {
292
- if (disabled)
293
- return;
294
- send({ type: "POINTER_OVER", id });
295
- },
296
- onPointerLeave() {
297
- if (disabled)
298
- return;
299
- send({ type: "POINTER_LEAVE", id });
300
- },
301
- onBlur() {
302
- send("BLUR");
303
- },
304
- onFocus() {
305
- send({ type: "FOCUS", id });
306
- },
307
- onDoubleClick() {
308
- if (disabled)
309
- return;
310
- send({ type: "DOUBLE_CLICK", id });
311
- },
312
- onKeyDown(event) {
313
- if (event.defaultPrevented)
314
- return;
315
- if (disabled)
316
- return;
317
- const moveStep = getEventStep(event) * step;
318
- const keyMap = {
319
- Enter() {
320
- send("ENTER");
321
- },
322
- ArrowUp() {
323
- send({ type: "ARROW_UP", step: moveStep });
324
- },
325
- ArrowDown() {
326
- send({ type: "ARROW_DOWN", step: moveStep });
327
- },
328
- ArrowLeft() {
329
- send({ type: "ARROW_LEFT", step: moveStep });
330
- },
331
- ArrowRight() {
332
- send({ type: "ARROW_RIGHT", step: moveStep });
333
- },
334
- Home() {
335
- send("HOME");
336
- },
337
- End() {
338
- send("END");
339
- }
340
- };
341
- const key = getEventKey(event, state.context);
342
- const exec = keyMap[key];
343
- if (exec) {
344
- exec(event);
345
- event.preventDefault();
346
- }
347
- }
348
- });
349
- }
350
- };
351
- }
352
-
353
- // src/splitter.machine.ts
354
- import { createMachine } from "@zag-js/core";
355
- import { getRelativePoint, trackPointerMove } from "@zag-js/dom-event";
356
- import { raf } from "@zag-js/dom-query";
357
- import { compact } from "@zag-js/utils";
358
- function machine(userContext) {
359
- const ctx = compact(userContext);
360
- return createMachine(
361
- {
362
- id: "splitter",
363
- initial: "idle",
364
- context: {
365
- orientation: "horizontal",
366
- activeResizeId: null,
367
- previousPanels: [],
368
- size: [],
369
- initialSize: [],
370
- activeResizeState: {
371
- isAtMin: false,
372
- isAtMax: false
373
- },
374
- ...ctx
375
- },
376
- created: ["setPreviousPanels", "setInitialSize"],
377
- watch: {
378
- size: ["setActiveResizeState"]
379
- },
380
- computed: {
381
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
382
- panels: (ctx2) => getNormalizedPanels(ctx2)
383
- },
384
- on: {
385
- SET_PANEL_SIZE: {
386
- actions: "setPanelSize"
387
- }
388
- },
389
- states: {
390
- idle: {
391
- entry: ["clearActiveHandleId"],
392
- on: {
393
- POINTER_OVER: {
394
- target: "hover:temp",
395
- actions: ["setActiveHandleId"]
396
- },
397
- FOCUS: {
398
- target: "focused",
399
- actions: ["setActiveHandleId"]
400
- },
401
- DOUBLE_CLICK: {
402
- actions: ["resetStartPanel", "setPreviousPanels"]
403
- }
404
- }
405
- },
406
- "hover:temp": {
407
- after: {
408
- HOVER_DELAY: "hover"
409
- },
410
- on: {
411
- POINTER_DOWN: {
412
- target: "dragging",
413
- actions: ["setActiveHandleId"]
414
- },
415
- POINTER_LEAVE: "idle"
416
- }
417
- },
418
- hover: {
419
- tags: ["focus"],
420
- on: {
421
- POINTER_DOWN: "dragging",
422
- POINTER_LEAVE: "idle"
423
- }
424
- },
425
- focused: {
426
- tags: ["focus"],
427
- on: {
428
- BLUR: "idle",
429
- POINTER_DOWN: {
430
- target: "dragging",
431
- actions: ["setActiveHandleId"]
432
- },
433
- ARROW_LEFT: {
434
- guard: "isHorizontal",
435
- actions: ["shrinkStartPanel", "setPreviousPanels"]
436
- },
437
- ARROW_RIGHT: {
438
- guard: "isHorizontal",
439
- actions: ["expandStartPanel", "setPreviousPanels"]
440
- },
441
- ARROW_UP: {
442
- guard: "isVertical",
443
- actions: ["shrinkStartPanel", "setPreviousPanels"]
444
- },
445
- ARROW_DOWN: {
446
- guard: "isVertical",
447
- actions: ["expandStartPanel", "setPreviousPanels"]
448
- },
449
- ENTER: [
450
- {
451
- guard: "isStartPanelAtMax",
452
- actions: ["setStartPanelToMin", "setPreviousPanels"]
453
- },
454
- { actions: ["setStartPanelToMax", "setPreviousPanels"] }
455
- ],
456
- HOME: {
457
- actions: ["setStartPanelToMin", "setPreviousPanels"]
458
- },
459
- END: {
460
- actions: ["setStartPanelToMax", "setPreviousPanels"]
461
- }
462
- }
463
- },
464
- dragging: {
465
- tags: ["focus"],
466
- entry: "focusResizeHandle",
467
- activities: ["trackPointerMove"],
468
- on: {
469
- POINTER_MOVE: {
470
- actions: ["setPointerValue", "setGlobalCursor", "invokeOnResize"]
471
- },
472
- POINTER_UP: {
473
- target: "focused",
474
- actions: ["setPreviousPanels", "clearGlobalCursor", "blurResizeHandle", "invokeOnResizeEnd"]
475
- }
476
- }
477
- }
478
- }
479
- },
480
- {
481
- activities: {
482
- trackPointerMove: (ctx2, _evt, { send }) => {
483
- const doc = dom.getDoc(ctx2);
484
- return trackPointerMove(doc, {
485
- onPointerMove(info) {
486
- send({ type: "POINTER_MOVE", point: info.point });
487
- },
488
- onPointerUp() {
489
- send("POINTER_UP");
490
- }
491
- });
492
- }
493
- },
494
- guards: {
495
- isStartPanelAtMin: (ctx2) => ctx2.activeResizeState.isAtMin,
496
- isStartPanelAtMax: (ctx2) => ctx2.activeResizeState.isAtMax,
497
- isHorizontal: (ctx2) => ctx2.isHorizontal,
498
- isVertical: (ctx2) => !ctx2.isHorizontal
499
- },
500
- delays: {
501
- HOVER_DELAY: 250
502
- },
503
- actions: {
504
- setGlobalCursor(ctx2) {
505
- dom.setupGlobalCursor(ctx2);
506
- },
507
- clearGlobalCursor(ctx2) {
508
- dom.removeGlobalCursor(ctx2);
509
- },
510
- invokeOnResize(ctx2) {
511
- ctx2.onSizeChange?.({ size: Array.from(ctx2.size), activeHandleId: ctx2.activeResizeId });
512
- },
513
- invokeOnResizeEnd(ctx2) {
514
- ctx2.onSizeChangeEnd?.({ size: Array.from(ctx2.size), activeHandleId: ctx2.activeResizeId });
515
- },
516
- setActiveHandleId(ctx2, evt) {
517
- ctx2.activeResizeId = evt.id;
518
- },
519
- clearActiveHandleId(ctx2) {
520
- ctx2.activeResizeId = null;
521
- },
522
- setInitialSize(ctx2) {
523
- ctx2.initialSize = ctx2.panels.slice().map((panel) => ({
524
- id: panel.id,
525
- size: panel.size
526
- }));
527
- },
528
- setPanelSize(ctx2, evt) {
529
- const { id, size } = evt;
530
- ctx2.size = ctx2.size.map((panel) => {
531
- const panelSize = clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
532
- return panel.id === id ? { ...panel, size: panelSize } : panel;
533
- });
534
- },
535
- setStartPanelToMin(ctx2) {
536
- const bounds = getPanelBounds(ctx2);
537
- if (!bounds)
538
- return;
539
- const { before, after } = bounds;
540
- ctx2.size[before.index].size = before.min;
541
- ctx2.size[after.index].size = after.min;
542
- },
543
- setStartPanelToMax(ctx2) {
544
- const bounds = getPanelBounds(ctx2);
545
- if (!bounds)
546
- return;
547
- const { before, after } = bounds;
548
- ctx2.size[before.index].size = before.max;
549
- ctx2.size[after.index].size = after.max;
550
- },
551
- expandStartPanel(ctx2, evt) {
552
- const bounds = getPanelBounds(ctx2);
553
- if (!bounds)
554
- return;
555
- const { before, after } = bounds;
556
- ctx2.size[before.index].size = before.up(evt.step);
557
- ctx2.size[after.index].size = after.down(evt.step);
558
- },
559
- shrinkStartPanel(ctx2, evt) {
560
- const bounds = getPanelBounds(ctx2);
561
- if (!bounds)
562
- return;
563
- const { before, after } = bounds;
564
- ctx2.size[before.index].size = before.down(evt.step);
565
- ctx2.size[after.index].size = after.up(evt.step);
566
- },
567
- resetStartPanel(ctx2, evt) {
568
- const bounds = getPanelBounds(ctx2, evt.id);
569
- if (!bounds)
570
- return;
571
- const { before, after } = bounds;
572
- ctx2.size[before.index].size = ctx2.initialSize[before.index].size;
573
- ctx2.size[after.index].size = ctx2.initialSize[after.index].size;
574
- },
575
- focusResizeHandle(ctx2) {
576
- raf(() => {
577
- dom.getActiveHandleEl(ctx2)?.focus({ preventScroll: true });
578
- });
579
- },
580
- blurResizeHandle(ctx2) {
581
- raf(() => {
582
- dom.getActiveHandleEl(ctx2)?.blur();
583
- });
584
- },
585
- setPreviousPanels(ctx2) {
586
- ctx2.previousPanels = ctx2.panels.slice();
587
- },
588
- setActiveResizeState(ctx2) {
589
- const panels = getPanelBounds(ctx2);
590
- if (!panels)
591
- return;
592
- const { before } = panels;
593
- ctx2.activeResizeState = {
594
- isAtMin: before.isAtMin,
595
- isAtMax: before.isAtMax
596
- };
597
- },
598
- setPointerValue(ctx2, evt) {
599
- const panels = getHandlePanels(ctx2);
600
- const bounds = getHandleBounds(ctx2);
601
- if (!panels || !bounds)
602
- return;
603
- const rootEl = dom.getRootEl(ctx2);
604
- if (!rootEl)
605
- return;
606
- const relativePoint = getRelativePoint(evt.point, rootEl);
607
- const percentValue = relativePoint.getPercentValue({
608
- dir: ctx2.dir,
609
- orientation: ctx2.orientation
610
- });
611
- let pointValue = percentValue * 100;
612
- ctx2.activeResizeState = {
613
- isAtMin: pointValue < bounds.min,
614
- isAtMax: pointValue > bounds.max
615
- };
616
- pointValue = clamp(pointValue, bounds.min, bounds.max);
617
- const { before, after } = panels;
618
- const offset = pointValue - before.end;
619
- ctx2.size[before.index].size = before.size + offset;
620
- ctx2.size[after.index].size = after.size - offset;
621
- }
622
- }
623
- }
624
- );
625
- }
626
-
627
- // src/splitter.props.ts
628
- import { createProps } from "@zag-js/types";
629
- import { createSplitProps } from "@zag-js/utils";
630
- var props = createProps()([
631
- "dir",
632
- "getRootNode",
633
- "id",
634
- "ids",
635
- "onSizeChange",
636
- "onSizeChangeEnd",
637
- "orientation",
638
- "size"
639
- ]);
640
- var splitProps = createSplitProps(props);
641
- var panelProps = createProps()(["id", "snapSize"]);
642
- var splitPanelProps = createSplitProps(panelProps);
643
- var resizeTriggerProps = createProps()(["disabled", "id", "step"]);
644
- var splitResizeTriggerProps = createSplitProps(resizeTriggerProps);
645
- export {
646
- anatomy,
647
- connect,
648
- machine,
649
- panelProps,
650
- props,
651
- resizeTriggerProps,
652
- splitPanelProps,
653
- splitProps,
654
- splitResizeTriggerProps
655
- };
1
+ import{createAnatomy}from"@zag-js/anatomy";var anatomy=createAnatomy("splitter").parts("root","panel","resizeTrigger");var parts=anatomy.build();import{getEventKey,getEventStep}from"@zag-js/dom-event";import{dataAttr}from"@zag-js/dom-query";import{createScope,queryAll}from"@zag-js/dom-query";var dom=createScope({getRootId:ctx=>ctx.ids?.root??`splitter:${ctx.id}`,getResizeTriggerId:(ctx,id)=>ctx.ids?.resizeTrigger?.(id)??`splitter:${ctx.id}:splitter:${id}`,getLabelId:ctx=>ctx.ids?.label??`splitter:${ctx.id}:label`,getPanelId:(ctx,id)=>ctx.ids?.panel?.(id)??`splitter:${ctx.id}:panel:${id}`,globalCursorId:ctx=>`splitter:${ctx.id}:global-cursor`,getRootEl:ctx=>dom.getById(ctx,dom.getRootId(ctx)),getResizeTriggerEl:(ctx,id)=>dom.getById(ctx,dom.getResizeTriggerId(ctx,id)),getPanelEl:(ctx,id)=>dom.getById(ctx,dom.getPanelId(ctx,id)),getCursor(ctx){const x=ctx.isHorizontal;let cursor=x?"col-resize":"row-resize";if(ctx.activeResizeState.isAtMin)cursor=x?"e-resize":"s-resize";if(ctx.activeResizeState.isAtMax)cursor=x?"w-resize":"n-resize";return cursor},getPanelStyle(ctx,id){const flexGrow=ctx.panels.find(panel=>panel.id===id)?.size??"0";return{flexBasis:0,flexGrow,flexShrink:1,overflow:"hidden"}},getActiveHandleEl(ctx){const activeId=ctx.activeResizeId;if(activeId==null)return;return dom.getById(ctx,dom.getResizeTriggerId(ctx,activeId))},getResizeTriggerEls(ctx){const ownerId=CSS.escape(dom.getRootId(ctx));return queryAll(dom.getRootEl(ctx),`[role=separator][data-ownedby='${ownerId}']`)},setupGlobalCursor(ctx){const styleEl=dom.getById(ctx,dom.globalCursorId(ctx));const textContent=`* { cursor: ${dom.getCursor(ctx)} !important; }`;if(styleEl){styleEl.textContent=textContent}else{const style=dom.getDoc(ctx).createElement("style");style.id=dom.globalCursorId(ctx);style.textContent=textContent;dom.getDoc(ctx).head.appendChild(style)}},removeGlobalCursor(ctx){dom.getById(ctx,dom.globalCursorId(ctx))?.remove()}});function validateSize(key,size){if(Math.floor(size)>100){throw new Error(`Total ${key} of panels cannot be greater than 100`)}}function getNormalizedPanels(ctx){let numOfPanelsWithoutSize=0;let totalSize=0;let totalMinSize=0;const panels=ctx.size.map(panel=>{const minSize=panel.minSize??0;const maxSize=panel.maxSize??100;totalMinSize+=minSize;if(panel.size==null){numOfPanelsWithoutSize++}else{totalSize+=panel.size}return{...panel,minSize,maxSize}});validateSize("minSize",totalMinSize);validateSize("size",totalSize);let end=0;let remainingSize=0;const result=panels.map(panel=>{let start=end;if(panel.size!=null){end+=panel.size;remainingSize=panel.size-panel.minSize;return{...panel,start,end,remainingSize}}const size=(100-totalSize)/numOfPanelsWithoutSize;end+=size;remainingSize=size-panel.minSize;return{...panel,size,start,end,remainingSize}});return result}function getHandlePanels(ctx,id=ctx.activeResizeId){const[beforeId,afterId]=id?.split(":")??[];if(!beforeId||!afterId)return;const beforeIndex=ctx.previousPanels.findIndex(panel=>panel.id===beforeId);const afterIndex=ctx.previousPanels.findIndex(panel=>panel.id===afterId);if(beforeIndex===-1||afterIndex===-1)return;const before=ctx.previousPanels[beforeIndex];const after=ctx.previousPanels[afterIndex];return{before:{...before,index:beforeIndex},after:{...after,index:afterIndex}}}function getHandleBounds(ctx,id=ctx.activeResizeId){const panels=getHandlePanels(ctx,id);if(!panels)return;const{before,after}=panels;return{min:Math.max(before.start+before.minSize,after.end-after.maxSize),max:Math.min(after.end-after.minSize,before.maxSize+before.start)}}function getPanelBounds(ctx,id){const bounds=getHandleBounds(ctx,id);const panels=getHandlePanels(ctx,id);if(!bounds||!panels)return;const{before,after}=panels;const beforeMin=Math.abs(before.start-bounds.min);const afterMin=after.size+(before.size-beforeMin);const beforeMax=Math.abs(before.start-bounds.max);const afterMax=after.size-(beforeMax-before.size);return{before:{index:before.index,min:beforeMin,max:beforeMax,isAtMin:beforeMin===before.size,isAtMax:beforeMax===before.size,up(step){return Math.min(before.size+step,beforeMax)},down(step){return Math.max(before.size-step,beforeMin)}},after:{index:after.index,min:afterMin,max:afterMax,isAtMin:afterMin===after.size,isAtMax:afterMax===after.size,up(step){return Math.min(after.size+step,afterMin)},down(step){return Math.max(after.size-step,afterMax)}}}}function clamp(value,min,max){return Math.min(Math.max(value,min),max)}function connect(state,send,normalize){const horizontal=state.context.isHorizontal;const focused=state.hasTag("focus");const dragging=state.matches("dragging");const panels=state.context.panels;function getResizeTriggerState(props2){const{id,disabled}=props2;const ids=id.split(":");const panelIds=ids.map(id2=>dom.getPanelId(state.context,id2));const panels2=getHandleBounds(state.context,id);return{disabled:!!disabled,focused:state.context.activeResizeId===id&&focused,panelIds,min:panels2?.min,max:panels2?.max,value:0}}return{focused,dragging,getResizeTriggerState,bounds:getHandleBounds(state.context),setToMinSize(id){const panel=panels.find(panel2=>panel2.id===id);send({type:"SET_PANEL_SIZE",id,size:panel?.minSize,src:"setToMinSize"})},setToMaxSize(id){const panel=panels.find(panel2=>panel2.id===id);send({type:"SET_PANEL_SIZE",id,size:panel?.maxSize,src:"setToMaxSize"})},setSize(id,size){send({type:"SET_PANEL_SIZE",id,size})},rootProps:normalize.element({...parts.root.attrs,"data-orientation":state.context.orientation,id:dom.getRootId(state.context),dir:state.context.dir,style:{display:"flex",flexDirection:horizontal?"row":"column",height:"100%",width:"100%",overflow:"hidden"}}),getPanelProps(props2){const{id}=props2;return normalize.element({...parts.panel.attrs,"data-orientation":state.context.orientation,dir:state.context.dir,id:dom.getPanelId(state.context,id),"data-ownedby":dom.getRootId(state.context),style:dom.getPanelStyle(state.context,id)})},getResizeTriggerProps(props2){const{id,disabled,step=1}=props2;const triggerState=getResizeTriggerState(props2);return normalize.element({...parts.resizeTrigger.attrs,dir:state.context.dir,id:dom.getResizeTriggerId(state.context,id),role:"separator","data-ownedby":dom.getRootId(state.context),tabIndex:disabled?void 0:0,"aria-valuenow":triggerState.value,"aria-valuemin":triggerState.min,"aria-valuemax":triggerState.max,"data-orientation":state.context.orientation,"aria-orientation":state.context.orientation,"aria-controls":triggerState.panelIds.join(" "),"data-focus":dataAttr(triggerState.focused),"data-disabled":dataAttr(disabled),style:{touchAction:"none",userSelect:"none",flex:"0 0 auto",pointerEvents:dragging&&!triggerState.focused?"none":void 0,cursor:horizontal?"col-resize":"row-resize",[horizontal?"minHeight":"minWidth"]:"0"},onPointerDown(event){if(disabled){event.preventDefault();return}send({type:"POINTER_DOWN",id});event.currentTarget.setPointerCapture(event.pointerId);event.preventDefault();event.stopPropagation()},onPointerUp(event){if(disabled)return;if(event.currentTarget.hasPointerCapture(event.pointerId)){event.currentTarget.releasePointerCapture(event.pointerId)}},onPointerOver(){if(disabled)return;send({type:"POINTER_OVER",id})},onPointerLeave(){if(disabled)return;send({type:"POINTER_LEAVE",id})},onBlur(){send("BLUR")},onFocus(){send({type:"FOCUS",id})},onDoubleClick(){if(disabled)return;send({type:"DOUBLE_CLICK",id})},onKeyDown(event){if(event.defaultPrevented)return;if(disabled)return;const moveStep=getEventStep(event)*step;const keyMap={Enter(){send("ENTER")},ArrowUp(){send({type:"ARROW_UP",step:moveStep})},ArrowDown(){send({type:"ARROW_DOWN",step:moveStep})},ArrowLeft(){send({type:"ARROW_LEFT",step:moveStep})},ArrowRight(){send({type:"ARROW_RIGHT",step:moveStep})},Home(){send("HOME")},End(){send("END")}};const key=getEventKey(event,state.context);const exec=keyMap[key];if(exec){exec(event);event.preventDefault()}}})}}}import{createMachine}from"@zag-js/core";import{getRelativePoint,trackPointerMove}from"@zag-js/dom-event";import{raf}from"@zag-js/dom-query";import{compact}from"@zag-js/utils";function machine(userContext){const ctx=compact(userContext);return createMachine({id:"splitter",initial:"idle",context:{orientation:"horizontal",activeResizeId:null,previousPanels:[],size:[],initialSize:[],activeResizeState:{isAtMin:false,isAtMax:false},...ctx},created:["setPreviousPanels","setInitialSize"],watch:{size:["setActiveResizeState"]},computed:{isHorizontal:ctx2=>ctx2.orientation==="horizontal",panels:ctx2=>getNormalizedPanels(ctx2)},on:{SET_PANEL_SIZE:{actions:"setPanelSize"}},states:{idle:{entry:["clearActiveHandleId"],on:{POINTER_OVER:{target:"hover:temp",actions:["setActiveHandleId"]},FOCUS:{target:"focused",actions:["setActiveHandleId"]},DOUBLE_CLICK:{actions:["resetStartPanel","setPreviousPanels"]}}},"hover:temp":{after:{HOVER_DELAY:"hover"},on:{POINTER_DOWN:{target:"dragging",actions:["setActiveHandleId"]},POINTER_LEAVE:"idle"}},hover:{tags:["focus"],on:{POINTER_DOWN:"dragging",POINTER_LEAVE:"idle"}},focused:{tags:["focus"],on:{BLUR:"idle",POINTER_DOWN:{target:"dragging",actions:["setActiveHandleId"]},ARROW_LEFT:{guard:"isHorizontal",actions:["shrinkStartPanel","setPreviousPanels"]},ARROW_RIGHT:{guard:"isHorizontal",actions:["expandStartPanel","setPreviousPanels"]},ARROW_UP:{guard:"isVertical",actions:["shrinkStartPanel","setPreviousPanels"]},ARROW_DOWN:{guard:"isVertical",actions:["expandStartPanel","setPreviousPanels"]},ENTER:[{guard:"isStartPanelAtMax",actions:["setStartPanelToMin","setPreviousPanels"]},{actions:["setStartPanelToMax","setPreviousPanels"]}],HOME:{actions:["setStartPanelToMin","setPreviousPanels"]},END:{actions:["setStartPanelToMax","setPreviousPanels"]}}},dragging:{tags:["focus"],entry:"focusResizeHandle",activities:["trackPointerMove"],on:{POINTER_MOVE:{actions:["setPointerValue","setGlobalCursor","invokeOnResize"]},POINTER_UP:{target:"focused",actions:["setPreviousPanels","clearGlobalCursor","blurResizeHandle","invokeOnResizeEnd"]}}}}},{activities:{trackPointerMove:(ctx2,_evt,{send})=>{const doc=dom.getDoc(ctx2);return trackPointerMove(doc,{onPointerMove(info){send({type:"POINTER_MOVE",point:info.point})},onPointerUp(){send("POINTER_UP")}})}},guards:{isStartPanelAtMin:ctx2=>ctx2.activeResizeState.isAtMin,isStartPanelAtMax:ctx2=>ctx2.activeResizeState.isAtMax,isHorizontal:ctx2=>ctx2.isHorizontal,isVertical:ctx2=>!ctx2.isHorizontal},delays:{HOVER_DELAY:250},actions:{setGlobalCursor(ctx2){dom.setupGlobalCursor(ctx2)},clearGlobalCursor(ctx2){dom.removeGlobalCursor(ctx2)},invokeOnResize(ctx2){ctx2.onSizeChange?.({size:Array.from(ctx2.size),activeHandleId:ctx2.activeResizeId})},invokeOnResizeEnd(ctx2){ctx2.onSizeChangeEnd?.({size:Array.from(ctx2.size),activeHandleId:ctx2.activeResizeId})},setActiveHandleId(ctx2,evt){ctx2.activeResizeId=evt.id},clearActiveHandleId(ctx2){ctx2.activeResizeId=null},setInitialSize(ctx2){ctx2.initialSize=ctx2.panels.slice().map(panel=>({id:panel.id,size:panel.size}))},setPanelSize(ctx2,evt){const{id,size}=evt;ctx2.size=ctx2.size.map(panel=>{const panelSize=clamp(size,panel.minSize??0,panel.maxSize??100);return panel.id===id?{...panel,size:panelSize}:panel})},setStartPanelToMin(ctx2){const bounds=getPanelBounds(ctx2);if(!bounds)return;const{before,after}=bounds;ctx2.size[before.index].size=before.min;ctx2.size[after.index].size=after.min},setStartPanelToMax(ctx2){const bounds=getPanelBounds(ctx2);if(!bounds)return;const{before,after}=bounds;ctx2.size[before.index].size=before.max;ctx2.size[after.index].size=after.max},expandStartPanel(ctx2,evt){const bounds=getPanelBounds(ctx2);if(!bounds)return;const{before,after}=bounds;ctx2.size[before.index].size=before.up(evt.step);ctx2.size[after.index].size=after.down(evt.step)},shrinkStartPanel(ctx2,evt){const bounds=getPanelBounds(ctx2);if(!bounds)return;const{before,after}=bounds;ctx2.size[before.index].size=before.down(evt.step);ctx2.size[after.index].size=after.up(evt.step)},resetStartPanel(ctx2,evt){const bounds=getPanelBounds(ctx2,evt.id);if(!bounds)return;const{before,after}=bounds;ctx2.size[before.index].size=ctx2.initialSize[before.index].size;ctx2.size[after.index].size=ctx2.initialSize[after.index].size},focusResizeHandle(ctx2){raf(()=>{dom.getActiveHandleEl(ctx2)?.focus({preventScroll:true})})},blurResizeHandle(ctx2){raf(()=>{dom.getActiveHandleEl(ctx2)?.blur()})},setPreviousPanels(ctx2){ctx2.previousPanels=ctx2.panels.slice()},setActiveResizeState(ctx2){const panels=getPanelBounds(ctx2);if(!panels)return;const{before}=panels;ctx2.activeResizeState={isAtMin:before.isAtMin,isAtMax:before.isAtMax}},setPointerValue(ctx2,evt){const panels=getHandlePanels(ctx2);const bounds=getHandleBounds(ctx2);if(!panels||!bounds)return;const rootEl=dom.getRootEl(ctx2);if(!rootEl)return;const relativePoint=getRelativePoint(evt.point,rootEl);const percentValue=relativePoint.getPercentValue({dir:ctx2.dir,orientation:ctx2.orientation});let pointValue=percentValue*100;ctx2.activeResizeState={isAtMin:pointValue<bounds.min,isAtMax:pointValue>bounds.max};pointValue=clamp(pointValue,bounds.min,bounds.max);const{before,after}=panels;const offset=pointValue-before.end;ctx2.size[before.index].size=before.size+offset;ctx2.size[after.index].size=after.size-offset}}})}import{createProps}from"@zag-js/types";import{createSplitProps}from"@zag-js/utils";var props=createProps()(["dir","getRootNode","id","ids","onSizeChange","onSizeChangeEnd","orientation","size"]);var splitProps=createSplitProps(props);var panelProps=createProps()(["id","snapSize"]);var splitPanelProps=createSplitProps(panelProps);var resizeTriggerProps=createProps()(["disabled","id","step"]);var splitResizeTriggerProps=createSplitProps(resizeTriggerProps);export{anatomy,connect,machine,panelProps,props,resizeTriggerProps,splitPanelProps,splitProps,splitResizeTriggerProps};
656
2
  //# sourceMappingURL=index.mjs.map