@zag-js/splitter 0.10.1 → 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.
- package/dist/index.d.ts +4 -7
- package/dist/index.js +8 -684
- package/dist/index.mjs +3 -16
- package/dist/splitter.anatomy.d.ts +3 -6
- package/dist/splitter.anatomy.js +10 -33
- package/dist/splitter.anatomy.mjs +6 -8
- package/dist/splitter.connect.d.ts +3 -7
- package/dist/splitter.connect.js +28 -144
- package/dist/splitter.connect.mjs +181 -9
- package/dist/splitter.dom.d.ts +25 -29
- package/dist/splitter.dom.js +9 -31
- package/dist/splitter.dom.mjs +58 -6
- package/dist/splitter.machine.d.ts +3 -7
- package/dist/splitter.machine.js +34 -250
- package/dist/splitter.machine.mjs +281 -8
- package/dist/splitter.types.d.ts +12 -14
- package/dist/splitter.utils.d.ts +6 -11
- package/dist/splitter.utils.js +10 -37
- package/dist/splitter.utils.mjs +122 -14
- package/package.json +9 -14
- package/src/splitter.connect.ts +3 -3
- package/src/splitter.machine.ts +1 -16
- package/src/splitter.utils.ts +1 -1
- package/dist/chunk-3GDOPT5W.mjs +0 -60
- package/dist/chunk-53T2VZ2R.mjs +0 -190
- package/dist/chunk-HPRMFGOY.mjs +0 -9
- package/dist/chunk-MV44GBQY.mjs +0 -129
- package/dist/chunk-X2E2LAC5.mjs +0 -307
- package/dist/splitter.types.js +0 -18
- package/dist/splitter.types.mjs +0 -0
|
@@ -1,8 +1,281 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
} from
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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 };
|
package/dist/splitter.types.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { StateMachine } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
type PanelId = string | number;
|
|
1
|
+
import type { StateMachine as S } from "@zag-js/core";
|
|
2
|
+
import type { CommonProperties, Context, DirectionProperty, RequiredBy } from "@zag-js/types";
|
|
3
|
+
export type PanelId = string | number;
|
|
5
4
|
type PanelSizeData = {
|
|
6
5
|
id: PanelId;
|
|
7
6
|
size?: number;
|
|
@@ -45,8 +44,8 @@ type PublicContext = DirectionProperty & CommonProperties & {
|
|
|
45
44
|
*/
|
|
46
45
|
ids?: ElementIds;
|
|
47
46
|
};
|
|
48
|
-
type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
49
|
-
type NormalizedPanelData = Array<Required<PanelSizeData> & {
|
|
47
|
+
export type UserDefinedContext = RequiredBy<PublicContext, "id">;
|
|
48
|
+
export type NormalizedPanelData = Array<Required<PanelSizeData> & {
|
|
50
49
|
remainingSize: number;
|
|
51
50
|
minSize: number;
|
|
52
51
|
maxSize: number;
|
|
@@ -74,21 +73,20 @@ type PrivateContext = Context<{
|
|
|
74
73
|
};
|
|
75
74
|
initialSize: Array<Required<Pick<PanelSizeData, "id" | "size">>>;
|
|
76
75
|
}>;
|
|
77
|
-
type MachineContext = PublicContext & ComputedContext & PrivateContext;
|
|
78
|
-
type MachineState = {
|
|
76
|
+
export type MachineContext = PublicContext & ComputedContext & PrivateContext;
|
|
77
|
+
export type MachineState = {
|
|
79
78
|
value: "idle" | "hover:temp" | "hover" | "dragging" | "focused";
|
|
80
79
|
tags: "focus";
|
|
81
80
|
};
|
|
82
|
-
type State =
|
|
83
|
-
type Send =
|
|
84
|
-
type PanelProps = {
|
|
81
|
+
export type State = S.State<MachineContext, MachineState>;
|
|
82
|
+
export type Send = S.Send<S.AnyEventObject>;
|
|
83
|
+
export type PanelProps = {
|
|
85
84
|
id: PanelId;
|
|
86
85
|
snapSize?: number;
|
|
87
86
|
};
|
|
88
|
-
type ResizeTriggerProps = {
|
|
87
|
+
export type ResizeTriggerProps = {
|
|
89
88
|
id: `${PanelId}:${PanelId}`;
|
|
90
89
|
step?: number;
|
|
91
90
|
disabled?: boolean;
|
|
92
91
|
};
|
|
93
|
-
|
|
94
|
-
export { MachineContext, MachineState, NormalizedPanelData, PanelId, PanelProps, ResizeTriggerProps, Send, State, UserDefinedContext };
|
|
92
|
+
export {};
|
package/dist/splitter.utils.d.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
import { MachineContext,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
declare function getNormalizedPanels(ctx: MachineContext): NormalizedPanelData;
|
|
6
|
-
declare function getHandlePanels(ctx: MachineContext, id?: string | null): {
|
|
1
|
+
import type { PanelId, MachineContext as Ctx, NormalizedPanelData } from './splitter.types';
|
|
2
|
+
export declare function getNormalizedPanels(ctx: Ctx): NormalizedPanelData;
|
|
3
|
+
export declare function getHandlePanels(ctx: Ctx, id?: string | null): {
|
|
7
4
|
before: {
|
|
8
5
|
index: number;
|
|
9
6
|
id: PanelId;
|
|
@@ -25,11 +22,11 @@ declare function getHandlePanels(ctx: MachineContext, id?: string | null): {
|
|
|
25
22
|
end: number;
|
|
26
23
|
};
|
|
27
24
|
} | undefined;
|
|
28
|
-
declare function getHandleBounds(ctx:
|
|
25
|
+
export declare function getHandleBounds(ctx: Ctx, id?: string | null): {
|
|
29
26
|
min: number;
|
|
30
27
|
max: number;
|
|
31
28
|
} | undefined;
|
|
32
|
-
declare function getPanelBounds(ctx:
|
|
29
|
+
export declare function getPanelBounds(ctx: Ctx, id?: string | null): {
|
|
33
30
|
before: {
|
|
34
31
|
index: number;
|
|
35
32
|
min: number;
|
|
@@ -49,6 +46,4 @@ declare function getPanelBounds(ctx: MachineContext, id?: string | null): {
|
|
|
49
46
|
down(step: number): number;
|
|
50
47
|
};
|
|
51
48
|
} | undefined;
|
|
52
|
-
declare function clamp(value: number, min: number, max: number): number;
|
|
53
|
-
|
|
54
|
-
export { clamp, getHandleBounds, getHandlePanels, getNormalizedPanels, getPanelBounds };
|
|
49
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
package/dist/splitter.utils.js
CHANGED
|
@@ -1,32 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
19
4
|
|
|
20
|
-
// src/splitter.utils.ts
|
|
21
|
-
var splitter_utils_exports = {};
|
|
22
|
-
__export(splitter_utils_exports, {
|
|
23
|
-
clamp: () => clamp,
|
|
24
|
-
getHandleBounds: () => getHandleBounds,
|
|
25
|
-
getHandlePanels: () => getHandlePanels,
|
|
26
|
-
getNormalizedPanels: () => getNormalizedPanels,
|
|
27
|
-
getPanelBounds: () => getPanelBounds
|
|
28
|
-
});
|
|
29
|
-
module.exports = __toCommonJS(splitter_utils_exports);
|
|
30
5
|
function validateSize(key, size) {
|
|
31
6
|
if (Math.floor(size) > 100) {
|
|
32
7
|
throw new Error(`Total ${key} of panels cannot be greater than 100`);
|
|
@@ -37,7 +12,7 @@ function getNormalizedPanels(ctx) {
|
|
|
37
12
|
let totalSize = 0;
|
|
38
13
|
let totalMinSize = 0;
|
|
39
14
|
const panels = ctx.size.map((panel) => {
|
|
40
|
-
const minSize = panel.minSize ??
|
|
15
|
+
const minSize = panel.minSize ?? 0;
|
|
41
16
|
const maxSize = panel.maxSize ?? 100;
|
|
42
17
|
totalMinSize += minSize;
|
|
43
18
|
if (panel.size == null) {
|
|
@@ -147,11 +122,9 @@ function getPanelBounds(ctx, id) {
|
|
|
147
122
|
function clamp(value, min, max) {
|
|
148
123
|
return Math.min(Math.max(value, min), max);
|
|
149
124
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
getPanelBounds
|
|
157
|
-
});
|
|
125
|
+
|
|
126
|
+
exports.clamp = clamp;
|
|
127
|
+
exports.getHandleBounds = getHandleBounds;
|
|
128
|
+
exports.getHandlePanels = getHandlePanels;
|
|
129
|
+
exports.getNormalizedPanels = getNormalizedPanels;
|
|
130
|
+
exports.getPanelBounds = getPanelBounds;
|
package/dist/splitter.utils.mjs
CHANGED
|
@@ -1,14 +1,122 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
function validateSize(key, size) {
|
|
2
|
+
if (Math.floor(size) > 100) {
|
|
3
|
+
throw new Error(`Total ${key} of panels cannot be greater than 100`);
|
|
4
|
+
}
|
|
5
|
+
}
|
|
6
|
+
function getNormalizedPanels(ctx) {
|
|
7
|
+
let numOfPanelsWithoutSize = 0;
|
|
8
|
+
let totalSize = 0;
|
|
9
|
+
let totalMinSize = 0;
|
|
10
|
+
const panels = ctx.size.map((panel) => {
|
|
11
|
+
const minSize = panel.minSize ?? 0;
|
|
12
|
+
const maxSize = panel.maxSize ?? 100;
|
|
13
|
+
totalMinSize += minSize;
|
|
14
|
+
if (panel.size == null) {
|
|
15
|
+
numOfPanelsWithoutSize++;
|
|
16
|
+
} else {
|
|
17
|
+
totalSize += panel.size;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
...panel,
|
|
21
|
+
minSize,
|
|
22
|
+
maxSize
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
validateSize("minSize", totalMinSize);
|
|
26
|
+
validateSize("size", totalSize);
|
|
27
|
+
let end = 0;
|
|
28
|
+
let remainingSize = 0;
|
|
29
|
+
const result = panels.map((panel) => {
|
|
30
|
+
let start = end;
|
|
31
|
+
if (panel.size != null) {
|
|
32
|
+
end += panel.size;
|
|
33
|
+
remainingSize = panel.size - panel.minSize;
|
|
34
|
+
return {
|
|
35
|
+
...panel,
|
|
36
|
+
start,
|
|
37
|
+
end,
|
|
38
|
+
remainingSize
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
const size = (100 - totalSize) / numOfPanelsWithoutSize;
|
|
42
|
+
end += size;
|
|
43
|
+
remainingSize = size - panel.minSize;
|
|
44
|
+
return { ...panel, size, start, end, remainingSize };
|
|
45
|
+
});
|
|
46
|
+
return result;
|
|
47
|
+
}
|
|
48
|
+
function getHandlePanels(ctx, id = ctx.activeResizeId) {
|
|
49
|
+
const [beforeId, afterId] = id?.split(":") ?? [];
|
|
50
|
+
if (!beforeId || !afterId)
|
|
51
|
+
return;
|
|
52
|
+
const beforeIndex = ctx.previousPanels.findIndex((panel) => panel.id === beforeId);
|
|
53
|
+
const afterIndex = ctx.previousPanels.findIndex((panel) => panel.id === afterId);
|
|
54
|
+
if (beforeIndex === -1 || afterIndex === -1)
|
|
55
|
+
return;
|
|
56
|
+
const before = ctx.previousPanels[beforeIndex];
|
|
57
|
+
const after = ctx.previousPanels[afterIndex];
|
|
58
|
+
return {
|
|
59
|
+
before: {
|
|
60
|
+
...before,
|
|
61
|
+
index: beforeIndex
|
|
62
|
+
},
|
|
63
|
+
after: {
|
|
64
|
+
...after,
|
|
65
|
+
index: afterIndex
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
function getHandleBounds(ctx, id = ctx.activeResizeId) {
|
|
70
|
+
const panels = getHandlePanels(ctx, id);
|
|
71
|
+
if (!panels)
|
|
72
|
+
return;
|
|
73
|
+
const { before, after } = panels;
|
|
74
|
+
return {
|
|
75
|
+
min: Math.max(before.start + before.minSize, after.end - after.maxSize),
|
|
76
|
+
max: Math.min(after.end - after.minSize, before.maxSize + before.start)
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function getPanelBounds(ctx, id) {
|
|
80
|
+
const bounds = getHandleBounds(ctx, id);
|
|
81
|
+
const panels = getHandlePanels(ctx, id);
|
|
82
|
+
if (!bounds || !panels)
|
|
83
|
+
return;
|
|
84
|
+
const { before, after } = panels;
|
|
85
|
+
const beforeMin = Math.abs(before.start - bounds.min);
|
|
86
|
+
const afterMin = after.size + (before.size - beforeMin);
|
|
87
|
+
const beforeMax = Math.abs(before.start - bounds.max);
|
|
88
|
+
const afterMax = after.size - (beforeMax - before.size);
|
|
89
|
+
return {
|
|
90
|
+
before: {
|
|
91
|
+
index: before.index,
|
|
92
|
+
min: beforeMin,
|
|
93
|
+
max: beforeMax,
|
|
94
|
+
isAtMin: beforeMin === before.size,
|
|
95
|
+
isAtMax: beforeMax === before.size,
|
|
96
|
+
up(step) {
|
|
97
|
+
return Math.min(before.size + step, beforeMax);
|
|
98
|
+
},
|
|
99
|
+
down(step) {
|
|
100
|
+
return Math.max(before.size - step, beforeMin);
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
after: {
|
|
104
|
+
index: after.index,
|
|
105
|
+
min: afterMin,
|
|
106
|
+
max: afterMax,
|
|
107
|
+
isAtMin: afterMin === after.size,
|
|
108
|
+
isAtMax: afterMax === after.size,
|
|
109
|
+
up(step) {
|
|
110
|
+
return Math.min(after.size + step, afterMin);
|
|
111
|
+
},
|
|
112
|
+
down(step) {
|
|
113
|
+
return Math.max(after.size - step, afterMax);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
function clamp(value, min, max) {
|
|
119
|
+
return Math.min(Math.max(value, min), max);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export { clamp, getHandleBounds, getHandlePanels, getNormalizedPanels, getPanelBounds };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zag-js/splitter",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.3",
|
|
4
4
|
"description": "Core logic for the splitter widget implemented as a state machine",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"js",
|
|
@@ -28,13 +28,13 @@
|
|
|
28
28
|
"url": "https://github.com/chakra-ui/zag/issues"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@zag-js/anatomy": "0.10.
|
|
32
|
-
"@zag-js/core": "0.10.
|
|
33
|
-
"@zag-js/types": "0.10.
|
|
34
|
-
"@zag-js/dom-query": "0.10.
|
|
35
|
-
"@zag-js/dom-event": "0.10.
|
|
36
|
-
"@zag-js/number-utils": "0.10.
|
|
37
|
-
"@zag-js/utils": "0.10.
|
|
31
|
+
"@zag-js/anatomy": "0.10.3",
|
|
32
|
+
"@zag-js/core": "0.10.3",
|
|
33
|
+
"@zag-js/types": "0.10.3",
|
|
34
|
+
"@zag-js/dom-query": "0.10.3",
|
|
35
|
+
"@zag-js/dom-event": "0.10.3",
|
|
36
|
+
"@zag-js/number-utils": "0.10.3",
|
|
37
|
+
"@zag-js/utils": "0.10.3"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"clean-package": "2.2.0"
|
|
@@ -52,13 +52,8 @@
|
|
|
52
52
|
"./package.json": "./package.json"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
|
-
"build
|
|
56
|
-
"start": "pnpm build --watch",
|
|
57
|
-
"build": "tsup src --dts",
|
|
58
|
-
"test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
|
|
55
|
+
"build": "vite build -c ../../../vite.config.ts",
|
|
59
56
|
"lint": "eslint src --ext .ts,.tsx",
|
|
60
|
-
"test-ci": "pnpm test --ci --runInBand",
|
|
61
|
-
"test-watch": "pnpm test --watch -u",
|
|
62
57
|
"typecheck": "tsc --noEmit"
|
|
63
58
|
}
|
|
64
59
|
}
|
package/src/splitter.connect.ts
CHANGED
|
@@ -30,20 +30,20 @@ export function connect<T extends PropTypes>(state: State, send: Send, normalize
|
|
|
30
30
|
*/
|
|
31
31
|
setToMinSize(id: PanelId) {
|
|
32
32
|
const panel = panels.find((panel) => panel.id === id)
|
|
33
|
-
send({ type: "
|
|
33
|
+
send({ type: "SET_PANEL_SIZE", id, size: panel?.minSize, src: "setToMinSize" })
|
|
34
34
|
},
|
|
35
35
|
/**
|
|
36
36
|
* Function to set a panel to its maximum size.
|
|
37
37
|
*/
|
|
38
38
|
setToMaxSize(id: PanelId) {
|
|
39
39
|
const panel = panels.find((panel) => panel.id === id)
|
|
40
|
-
send({ type: "
|
|
40
|
+
send({ type: "SET_PANEL_SIZE", id, size: panel?.maxSize, src: "setToMaxSize" })
|
|
41
41
|
},
|
|
42
42
|
/**
|
|
43
43
|
* Function to set the size of a panel.
|
|
44
44
|
*/
|
|
45
45
|
setSize(id: PanelId, size: number) {
|
|
46
|
-
send({ type: "
|
|
46
|
+
send({ type: "SET_PANEL_SIZE", id, size })
|
|
47
47
|
},
|
|
48
48
|
/**
|
|
49
49
|
* Returns the state details for a resize trigger.
|
package/src/splitter.machine.ts
CHANGED
|
@@ -37,22 +37,7 @@ export function machine(userContext: UserDefinedContext) {
|
|
|
37
37
|
},
|
|
38
38
|
|
|
39
39
|
on: {
|
|
40
|
-
|
|
41
|
-
actions: "setStartPanelToMin",
|
|
42
|
-
},
|
|
43
|
-
EXPAND: {
|
|
44
|
-
actions: "setStartPanelToMax",
|
|
45
|
-
},
|
|
46
|
-
TOGGLE: [
|
|
47
|
-
{
|
|
48
|
-
guard: "isStartPanelAtMin",
|
|
49
|
-
actions: "setStartPanelToMax",
|
|
50
|
-
},
|
|
51
|
-
{
|
|
52
|
-
actions: "setStartPanelToMin",
|
|
53
|
-
},
|
|
54
|
-
],
|
|
55
|
-
SET_SIZE: {
|
|
40
|
+
SET_PANEL_SIZE: {
|
|
56
41
|
actions: "setPanelSize",
|
|
57
42
|
},
|
|
58
43
|
},
|
package/src/splitter.utils.ts
CHANGED
|
@@ -12,7 +12,7 @@ export function getNormalizedPanels(ctx: Ctx): NormalizedPanelData {
|
|
|
12
12
|
let totalMinSize = 0
|
|
13
13
|
|
|
14
14
|
const panels = ctx.size.map((panel) => {
|
|
15
|
-
const minSize = panel.minSize ??
|
|
15
|
+
const minSize = panel.minSize ?? 0
|
|
16
16
|
const maxSize = panel.maxSize ?? 100
|
|
17
17
|
|
|
18
18
|
totalMinSize += minSize
|