@zag-js/splitter 0.10.5 → 0.11.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.
@@ -1,58 +0,0 @@
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,3 +0,0 @@
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,285 +0,0 @@
1
- 'use strict';
2
-
3
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
-
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');
11
-
12
- function machine(userContext) {
13
- const ctx = utils.compact(userContext);
14
- return core.createMachine(
15
- {
16
- id: "splitter",
17
- initial: "idle",
18
- context: {
19
- orientation: "horizontal",
20
- activeResizeId: null,
21
- previousPanels: [],
22
- size: [],
23
- initialSize: [],
24
- activeResizeState: {
25
- isAtMin: false,
26
- isAtMax: false
27
- },
28
- ...ctx
29
- },
30
- created: ["setPreviousPanels", "setInitialSize"],
31
- watch: {
32
- size: ["setActiveResizeState"]
33
- },
34
- computed: {
35
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
36
- panels: (ctx2) => splitter_utils.getNormalizedPanels(ctx2)
37
- },
38
- on: {
39
- SET_PANEL_SIZE: {
40
- actions: "setPanelSize"
41
- }
42
- },
43
- states: {
44
- idle: {
45
- entry: ["clearActiveHandleId"],
46
- on: {
47
- POINTER_OVER: {
48
- target: "hover:temp",
49
- actions: ["setActiveHandleId"]
50
- },
51
- FOCUS: {
52
- target: "focused",
53
- actions: ["setActiveHandleId"]
54
- },
55
- DOUBLE_CLICK: {
56
- actions: ["resetStartPanel", "setPreviousPanels"]
57
- }
58
- }
59
- },
60
- "hover:temp": {
61
- after: {
62
- HOVER_DELAY: "hover"
63
- },
64
- on: {
65
- POINTER_DOWN: {
66
- target: "dragging",
67
- actions: ["setActiveHandleId", "invokeOnResizeStart"]
68
- },
69
- POINTER_LEAVE: "idle"
70
- }
71
- },
72
- hover: {
73
- tags: ["focus"],
74
- on: {
75
- POINTER_DOWN: {
76
- target: "dragging",
77
- actions: ["invokeOnResizeStart"]
78
- },
79
- POINTER_LEAVE: "idle"
80
- }
81
- },
82
- focused: {
83
- tags: ["focus"],
84
- on: {
85
- BLUR: "idle",
86
- POINTER_DOWN: {
87
- target: "dragging",
88
- actions: ["setActiveHandleId", "invokeOnResizeStart"]
89
- },
90
- ARROW_LEFT: {
91
- guard: "isHorizontal",
92
- actions: ["shrinkStartPanel", "setPreviousPanels"]
93
- },
94
- ARROW_RIGHT: {
95
- guard: "isHorizontal",
96
- actions: ["expandStartPanel", "setPreviousPanels"]
97
- },
98
- ARROW_UP: {
99
- guard: "isVertical",
100
- actions: ["shrinkStartPanel", "setPreviousPanels"]
101
- },
102
- ARROW_DOWN: {
103
- guard: "isVertical",
104
- actions: ["expandStartPanel", "setPreviousPanels"]
105
- },
106
- ENTER: [
107
- {
108
- guard: "isStartPanelAtMax",
109
- actions: ["setStartPanelToMin", "setPreviousPanels"]
110
- },
111
- { actions: ["setStartPanelToMax", "setPreviousPanels"] }
112
- ],
113
- HOME: {
114
- actions: ["setStartPanelToMin", "setPreviousPanels"]
115
- },
116
- END: {
117
- actions: ["setStartPanelToMax", "setPreviousPanels"]
118
- }
119
- }
120
- },
121
- dragging: {
122
- tags: ["focus"],
123
- entry: "focusResizeHandle",
124
- activities: ["trackPointerMove"],
125
- on: {
126
- POINTER_MOVE: {
127
- actions: ["setPointerValue", "setGlobalCursor"]
128
- },
129
- POINTER_UP: {
130
- target: "focused",
131
- actions: ["invokeOnResizeEnd", "setPreviousPanels", "clearGlobalCursor", "blurResizeHandle"]
132
- }
133
- }
134
- }
135
- }
136
- },
137
- {
138
- activities: {
139
- trackPointerMove: (ctx2, _evt, { send }) => {
140
- const doc = splitter_dom.dom.getDoc(ctx2);
141
- return domEvent.trackPointerMove(doc, {
142
- onPointerMove(info) {
143
- send({ type: "POINTER_MOVE", point: info.point });
144
- },
145
- onPointerUp() {
146
- send("POINTER_UP");
147
- }
148
- });
149
- }
150
- },
151
- guards: {
152
- isStartPanelAtMin: (ctx2) => ctx2.activeResizeState.isAtMin,
153
- isStartPanelAtMax: (ctx2) => ctx2.activeResizeState.isAtMax,
154
- isHorizontal: (ctx2) => ctx2.isHorizontal,
155
- isVertical: (ctx2) => !ctx2.isHorizontal
156
- },
157
- delays: {
158
- HOVER_DELAY: 250
159
- },
160
- actions: {
161
- setGlobalCursor(ctx2) {
162
- splitter_dom.dom.setupGlobalCursor(ctx2);
163
- },
164
- clearGlobalCursor(ctx2) {
165
- splitter_dom.dom.removeGlobalCursor(ctx2);
166
- },
167
- invokeOnResize(ctx2) {
168
- ctx2.onResize?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
169
- },
170
- invokeOnResizeStart(ctx2) {
171
- ctx2.onResizeStart?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
172
- },
173
- invokeOnResizeEnd(ctx2) {
174
- ctx2.onResizeEnd?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
175
- },
176
- setActiveHandleId(ctx2, evt) {
177
- ctx2.activeResizeId = evt.id;
178
- },
179
- clearActiveHandleId(ctx2) {
180
- ctx2.activeResizeId = null;
181
- },
182
- setInitialSize(ctx2) {
183
- ctx2.initialSize = ctx2.panels.slice().map((panel) => ({
184
- id: panel.id,
185
- size: panel.size
186
- }));
187
- },
188
- setPanelSize(ctx2, evt) {
189
- const { id, size } = evt;
190
- ctx2.size = ctx2.size.map((panel) => {
191
- const panelSize = splitter_utils.clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
192
- return panel.id === id ? { ...panel, size: panelSize } : panel;
193
- });
194
- },
195
- setStartPanelToMin(ctx2) {
196
- const bounds = splitter_utils.getPanelBounds(ctx2);
197
- if (!bounds)
198
- return;
199
- const { before, after } = bounds;
200
- ctx2.size[before.index].size = before.min;
201
- ctx2.size[after.index].size = after.min;
202
- },
203
- setStartPanelToMax(ctx2) {
204
- const bounds = splitter_utils.getPanelBounds(ctx2);
205
- if (!bounds)
206
- return;
207
- const { before, after } = bounds;
208
- ctx2.size[before.index].size = before.max;
209
- ctx2.size[after.index].size = after.max;
210
- },
211
- expandStartPanel(ctx2, evt) {
212
- const bounds = splitter_utils.getPanelBounds(ctx2);
213
- if (!bounds)
214
- return;
215
- const { before, after } = bounds;
216
- ctx2.size[before.index].size = before.up(evt.step);
217
- ctx2.size[after.index].size = after.down(evt.step);
218
- },
219
- shrinkStartPanel(ctx2, evt) {
220
- const bounds = splitter_utils.getPanelBounds(ctx2);
221
- if (!bounds)
222
- return;
223
- const { before, after } = bounds;
224
- ctx2.size[before.index].size = before.down(evt.step);
225
- ctx2.size[after.index].size = after.up(evt.step);
226
- },
227
- resetStartPanel(ctx2, evt) {
228
- const bounds = splitter_utils.getPanelBounds(ctx2, evt.id);
229
- if (!bounds)
230
- return;
231
- const { before, after } = bounds;
232
- ctx2.size[before.index].size = ctx2.initialSize[before.index].size;
233
- ctx2.size[after.index].size = ctx2.initialSize[after.index].size;
234
- },
235
- focusResizeHandle(ctx2) {
236
- domQuery.raf(() => {
237
- splitter_dom.dom.getActiveHandleEl(ctx2)?.focus({ preventScroll: true });
238
- });
239
- },
240
- blurResizeHandle(ctx2) {
241
- domQuery.raf(() => {
242
- splitter_dom.dom.getActiveHandleEl(ctx2)?.blur();
243
- });
244
- },
245
- setPreviousPanels(ctx2) {
246
- ctx2.previousPanels = ctx2.panels.slice();
247
- },
248
- setActiveResizeState(ctx2) {
249
- const panels = splitter_utils.getPanelBounds(ctx2);
250
- if (!panels)
251
- return;
252
- const { before } = panels;
253
- ctx2.activeResizeState = {
254
- isAtMin: before.isAtMin,
255
- isAtMax: before.isAtMax
256
- };
257
- },
258
- setPointerValue(ctx2, evt) {
259
- const panels = splitter_utils.getHandlePanels(ctx2);
260
- const bounds = splitter_utils.getHandleBounds(ctx2);
261
- if (!panels || !bounds)
262
- return;
263
- const rootEl = splitter_dom.dom.getRootEl(ctx2);
264
- const relativePoint = domEvent.getRelativePoint(evt.point, rootEl);
265
- const percentValue = relativePoint.getPercentValue({
266
- dir: ctx2.dir,
267
- orientation: ctx2.orientation
268
- });
269
- let pointValue = percentValue * 100;
270
- ctx2.activeResizeState = {
271
- isAtMin: pointValue < bounds.min,
272
- isAtMax: pointValue > bounds.max
273
- };
274
- pointValue = splitter_utils.clamp(pointValue, bounds.min, bounds.max);
275
- const { before, after } = panels;
276
- const offset = pointValue - before.end;
277
- ctx2.size[before.index].size = before.size + offset;
278
- ctx2.size[after.index].size = after.size - offset;
279
- }
280
- }
281
- }
282
- );
283
- }
284
-
285
- exports.machine = machine;
@@ -1,281 +0,0 @@
1
- import { createMachine } from '@zag-js/core';
2
- import { trackPointerMove, getRelativePoint } from '@zag-js/dom-event';
3
- import { raf } from '@zag-js/dom-query';
4
- import { compact } from '@zag-js/utils';
5
- import { dom } from './splitter.dom.mjs';
6
- import { getNormalizedPanels, clamp, getPanelBounds, getHandlePanels, getHandleBounds } from './splitter.utils.mjs';
7
-
8
- function machine(userContext) {
9
- const ctx = compact(userContext);
10
- return createMachine(
11
- {
12
- id: "splitter",
13
- initial: "idle",
14
- context: {
15
- orientation: "horizontal",
16
- activeResizeId: null,
17
- previousPanels: [],
18
- size: [],
19
- initialSize: [],
20
- activeResizeState: {
21
- isAtMin: false,
22
- isAtMax: false
23
- },
24
- ...ctx
25
- },
26
- created: ["setPreviousPanels", "setInitialSize"],
27
- watch: {
28
- size: ["setActiveResizeState"]
29
- },
30
- computed: {
31
- isHorizontal: (ctx2) => ctx2.orientation === "horizontal",
32
- panels: (ctx2) => getNormalizedPanels(ctx2)
33
- },
34
- on: {
35
- SET_PANEL_SIZE: {
36
- actions: "setPanelSize"
37
- }
38
- },
39
- states: {
40
- idle: {
41
- entry: ["clearActiveHandleId"],
42
- on: {
43
- POINTER_OVER: {
44
- target: "hover:temp",
45
- actions: ["setActiveHandleId"]
46
- },
47
- FOCUS: {
48
- target: "focused",
49
- actions: ["setActiveHandleId"]
50
- },
51
- DOUBLE_CLICK: {
52
- actions: ["resetStartPanel", "setPreviousPanels"]
53
- }
54
- }
55
- },
56
- "hover:temp": {
57
- after: {
58
- HOVER_DELAY: "hover"
59
- },
60
- on: {
61
- POINTER_DOWN: {
62
- target: "dragging",
63
- actions: ["setActiveHandleId", "invokeOnResizeStart"]
64
- },
65
- POINTER_LEAVE: "idle"
66
- }
67
- },
68
- hover: {
69
- tags: ["focus"],
70
- on: {
71
- POINTER_DOWN: {
72
- target: "dragging",
73
- actions: ["invokeOnResizeStart"]
74
- },
75
- POINTER_LEAVE: "idle"
76
- }
77
- },
78
- focused: {
79
- tags: ["focus"],
80
- on: {
81
- BLUR: "idle",
82
- POINTER_DOWN: {
83
- target: "dragging",
84
- actions: ["setActiveHandleId", "invokeOnResizeStart"]
85
- },
86
- ARROW_LEFT: {
87
- guard: "isHorizontal",
88
- actions: ["shrinkStartPanel", "setPreviousPanels"]
89
- },
90
- ARROW_RIGHT: {
91
- guard: "isHorizontal",
92
- actions: ["expandStartPanel", "setPreviousPanels"]
93
- },
94
- ARROW_UP: {
95
- guard: "isVertical",
96
- actions: ["shrinkStartPanel", "setPreviousPanels"]
97
- },
98
- ARROW_DOWN: {
99
- guard: "isVertical",
100
- actions: ["expandStartPanel", "setPreviousPanels"]
101
- },
102
- ENTER: [
103
- {
104
- guard: "isStartPanelAtMax",
105
- actions: ["setStartPanelToMin", "setPreviousPanels"]
106
- },
107
- { actions: ["setStartPanelToMax", "setPreviousPanels"] }
108
- ],
109
- HOME: {
110
- actions: ["setStartPanelToMin", "setPreviousPanels"]
111
- },
112
- END: {
113
- actions: ["setStartPanelToMax", "setPreviousPanels"]
114
- }
115
- }
116
- },
117
- dragging: {
118
- tags: ["focus"],
119
- entry: "focusResizeHandle",
120
- activities: ["trackPointerMove"],
121
- on: {
122
- POINTER_MOVE: {
123
- actions: ["setPointerValue", "setGlobalCursor"]
124
- },
125
- POINTER_UP: {
126
- target: "focused",
127
- actions: ["invokeOnResizeEnd", "setPreviousPanels", "clearGlobalCursor", "blurResizeHandle"]
128
- }
129
- }
130
- }
131
- }
132
- },
133
- {
134
- activities: {
135
- trackPointerMove: (ctx2, _evt, { send }) => {
136
- const doc = dom.getDoc(ctx2);
137
- return trackPointerMove(doc, {
138
- onPointerMove(info) {
139
- send({ type: "POINTER_MOVE", point: info.point });
140
- },
141
- onPointerUp() {
142
- send("POINTER_UP");
143
- }
144
- });
145
- }
146
- },
147
- guards: {
148
- isStartPanelAtMin: (ctx2) => ctx2.activeResizeState.isAtMin,
149
- isStartPanelAtMax: (ctx2) => ctx2.activeResizeState.isAtMax,
150
- isHorizontal: (ctx2) => ctx2.isHorizontal,
151
- isVertical: (ctx2) => !ctx2.isHorizontal
152
- },
153
- delays: {
154
- HOVER_DELAY: 250
155
- },
156
- actions: {
157
- setGlobalCursor(ctx2) {
158
- dom.setupGlobalCursor(ctx2);
159
- },
160
- clearGlobalCursor(ctx2) {
161
- dom.removeGlobalCursor(ctx2);
162
- },
163
- invokeOnResize(ctx2) {
164
- ctx2.onResize?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
165
- },
166
- invokeOnResizeStart(ctx2) {
167
- ctx2.onResizeStart?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
168
- },
169
- invokeOnResizeEnd(ctx2) {
170
- ctx2.onResizeEnd?.({ size: ctx2.size, activeHandleId: ctx2.activeResizeId });
171
- },
172
- setActiveHandleId(ctx2, evt) {
173
- ctx2.activeResizeId = evt.id;
174
- },
175
- clearActiveHandleId(ctx2) {
176
- ctx2.activeResizeId = null;
177
- },
178
- setInitialSize(ctx2) {
179
- ctx2.initialSize = ctx2.panels.slice().map((panel) => ({
180
- id: panel.id,
181
- size: panel.size
182
- }));
183
- },
184
- setPanelSize(ctx2, evt) {
185
- const { id, size } = evt;
186
- ctx2.size = ctx2.size.map((panel) => {
187
- const panelSize = clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100);
188
- return panel.id === id ? { ...panel, size: panelSize } : panel;
189
- });
190
- },
191
- setStartPanelToMin(ctx2) {
192
- const bounds = getPanelBounds(ctx2);
193
- if (!bounds)
194
- return;
195
- const { before, after } = bounds;
196
- ctx2.size[before.index].size = before.min;
197
- ctx2.size[after.index].size = after.min;
198
- },
199
- setStartPanelToMax(ctx2) {
200
- const bounds = getPanelBounds(ctx2);
201
- if (!bounds)
202
- return;
203
- const { before, after } = bounds;
204
- ctx2.size[before.index].size = before.max;
205
- ctx2.size[after.index].size = after.max;
206
- },
207
- expandStartPanel(ctx2, evt) {
208
- const bounds = getPanelBounds(ctx2);
209
- if (!bounds)
210
- return;
211
- const { before, after } = bounds;
212
- ctx2.size[before.index].size = before.up(evt.step);
213
- ctx2.size[after.index].size = after.down(evt.step);
214
- },
215
- shrinkStartPanel(ctx2, evt) {
216
- const bounds = getPanelBounds(ctx2);
217
- if (!bounds)
218
- return;
219
- const { before, after } = bounds;
220
- ctx2.size[before.index].size = before.down(evt.step);
221
- ctx2.size[after.index].size = after.up(evt.step);
222
- },
223
- resetStartPanel(ctx2, evt) {
224
- const bounds = getPanelBounds(ctx2, evt.id);
225
- if (!bounds)
226
- return;
227
- const { before, after } = bounds;
228
- ctx2.size[before.index].size = ctx2.initialSize[before.index].size;
229
- ctx2.size[after.index].size = ctx2.initialSize[after.index].size;
230
- },
231
- focusResizeHandle(ctx2) {
232
- raf(() => {
233
- dom.getActiveHandleEl(ctx2)?.focus({ preventScroll: true });
234
- });
235
- },
236
- blurResizeHandle(ctx2) {
237
- raf(() => {
238
- dom.getActiveHandleEl(ctx2)?.blur();
239
- });
240
- },
241
- setPreviousPanels(ctx2) {
242
- ctx2.previousPanels = ctx2.panels.slice();
243
- },
244
- setActiveResizeState(ctx2) {
245
- const panels = getPanelBounds(ctx2);
246
- if (!panels)
247
- return;
248
- const { before } = panels;
249
- ctx2.activeResizeState = {
250
- isAtMin: before.isAtMin,
251
- isAtMax: before.isAtMax
252
- };
253
- },
254
- setPointerValue(ctx2, evt) {
255
- const panels = getHandlePanels(ctx2);
256
- const bounds = getHandleBounds(ctx2);
257
- if (!panels || !bounds)
258
- return;
259
- const rootEl = dom.getRootEl(ctx2);
260
- const relativePoint = getRelativePoint(evt.point, rootEl);
261
- const percentValue = relativePoint.getPercentValue({
262
- dir: ctx2.dir,
263
- orientation: ctx2.orientation
264
- });
265
- let pointValue = percentValue * 100;
266
- ctx2.activeResizeState = {
267
- isAtMin: pointValue < bounds.min,
268
- isAtMax: pointValue > bounds.max
269
- };
270
- pointValue = clamp(pointValue, bounds.min, bounds.max);
271
- const { before, after } = panels;
272
- const offset = pointValue - before.end;
273
- ctx2.size[before.index].size = before.size + offset;
274
- ctx2.size[after.index].size = after.size - offset;
275
- }
276
- }
277
- }
278
- );
279
- }
280
-
281
- export { machine };