@zag-js/splitter 0.2.6 → 0.2.7

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,90 +1,53 @@
1
1
  import { StateMachine } from '@zag-js/core';
2
2
  import { RequiredBy, DirectionProperty, CommonProperties, Context } from '@zag-js/types';
3
3
 
4
- type ElementIds = Partial<{
5
- root: string;
6
- splitter: string;
7
- label: string;
8
- toggleBtn: string;
9
- primaryPane: string;
10
- secondaryPane: string;
11
- }>;
4
+ type PanelId = string | number;
5
+ type PanelSizeData = {
6
+ id: PanelId;
7
+ size?: number;
8
+ minSize?: number;
9
+ maxSize?: number;
10
+ };
11
+ type ResizeDetails = {
12
+ size: PanelSizeData[];
13
+ activeHandleId: string | null;
14
+ };
12
15
  type PublicContext = DirectionProperty & CommonProperties & {
13
- /**
14
- * The ids of the elements in the splitter. Useful for composition.
15
- */
16
- ids?: ElementIds;
17
- /**
18
- * Whether to allow the separator to be dragged.
19
- */
20
- fixed?: boolean;
21
- /**
22
- * The orientation of the split view.
23
- */
24
16
  orientation: "horizontal" | "vertical";
25
- /**
26
- * The minimum size of the primary pane.
27
- */
28
- min: number;
29
- /**
30
- * The maximum size of the primary pane.
31
- */
32
- max: number;
33
- /**
34
- * The size of the primary pane.
35
- */
36
- value: number;
37
- /**
38
- * The step increments of the primary pane when it is dragged
39
- * or resized with keyboard.
40
- */
41
- step: number;
42
- /**
43
- * Callback to be invoked when the primary pane is resized.
44
- */
45
- onChange?: (details: {
46
- value: number;
47
- }) => void;
48
- /**
49
- * Callback to be invoked when the primary pane's resize session starts
50
- */
51
- onChangeStart?: (details: {
52
- value: number;
53
- }) => void;
54
- /**
55
- * Callback to be invoked when the primary pane's resize session ends
56
- */
57
- onChangeEnd?: (details: {
58
- value: number;
59
- }) => void;
60
- /**
61
- * Whether the primary pane is disabled.
62
- */
63
- disabled?: boolean;
64
- /**
65
- * The minimum offset needed to snap the primary pane to its minimum or maximum size.
66
- */
67
- snapOffset: number;
17
+ size: PanelSizeData[];
18
+ onResize?: (details: ResizeDetails) => void;
19
+ onResizeStart?: (details: ResizeDetails) => void;
20
+ onResizeEnd?: (details: ResizeDetails) => void;
68
21
  };
69
22
  type UserDefinedContext = RequiredBy<PublicContext, "id">;
23
+ type NormalizedPanelData = Array<Required<PanelSizeData> & {
24
+ remainingSize: number;
25
+ minSize: number;
26
+ maxSize: number;
27
+ start: number;
28
+ end: number;
29
+ }>;
70
30
  type ComputedContext = Readonly<{
71
- /**
72
- * @computed
73
- * Whether the primary pane is at its minimum size.
74
- */
75
- isAtMin: boolean;
76
- /**
77
- * @computed
78
- * Whether the primary pane is at its maximum size.
79
- */
80
- isAtMax: boolean;
81
- /**
82
- * @computed
83
- * Whether the orientation is horizontal.
84
- */
85
31
  isHorizontal: boolean;
32
+ panels: NormalizedPanelData;
33
+ activeResizeBounds?: {
34
+ min: number;
35
+ max: number;
36
+ };
37
+ activeResizePanels?: {
38
+ before: PanelSizeData;
39
+ after: PanelSizeData;
40
+ };
41
+ }>;
42
+ type PrivateContext = Context<{
43
+ activeResizeId: string | null;
44
+ previousPanels: NormalizedPanelData;
45
+ activeResizeState: {
46
+ isAtMin: boolean;
47
+ isAtMax: boolean;
48
+ };
49
+ initialSize: Array<Required<Pick<PanelSizeData, "id" | "size">>>;
86
50
  }>;
87
- type PrivateContext = Context<{}>;
88
51
  type MachineContext = PublicContext & ComputedContext & PrivateContext;
89
52
  type MachineState = {
90
53
  value: "unknown" | "idle" | "hover:temp" | "hover" | "dragging" | "focused";
@@ -92,5 +55,14 @@ type MachineState = {
92
55
  };
93
56
  type State = StateMachine.State<MachineContext, MachineState>;
94
57
  type Send = StateMachine.Send<StateMachine.AnyEventObject>;
58
+ type PanelProps = {
59
+ id: PanelId;
60
+ snapSize?: number;
61
+ };
62
+ type ResizeTriggerProps = {
63
+ id: `${PanelId}:${PanelId}`;
64
+ step?: number;
65
+ disabled?: boolean;
66
+ };
95
67
 
96
- export { MachineContext, MachineState, Send, State, UserDefinedContext };
68
+ export { MachineContext, MachineState, NormalizedPanelData, PanelId, PanelProps, ResizeTriggerProps, Send, State, UserDefinedContext };
@@ -0,0 +1,54 @@
1
+ import { MachineContext, NormalizedPanelData, PanelId } from './splitter.types.js';
2
+ import '@zag-js/core';
3
+ import '@zag-js/types';
4
+
5
+ declare function getNormalizedPanels(ctx: MachineContext): NormalizedPanelData;
6
+ declare function getHandlePanels(ctx: MachineContext, id?: string | null): {
7
+ before: {
8
+ index: number;
9
+ id: PanelId;
10
+ size: number;
11
+ minSize: number;
12
+ maxSize: number;
13
+ remainingSize: number;
14
+ start: number;
15
+ end: number;
16
+ };
17
+ after: {
18
+ index: number;
19
+ id: PanelId;
20
+ size: number;
21
+ minSize: number;
22
+ maxSize: number;
23
+ remainingSize: number;
24
+ start: number;
25
+ end: number;
26
+ };
27
+ } | undefined;
28
+ declare function getHandleBounds(ctx: MachineContext, id?: string | null): {
29
+ min: number;
30
+ max: number;
31
+ } | undefined;
32
+ declare function getPanelBounds(ctx: MachineContext, id?: string | null): {
33
+ before: {
34
+ index: number;
35
+ min: number;
36
+ max: number;
37
+ isAtMin: boolean;
38
+ isAtMax: boolean;
39
+ up(step: number): number;
40
+ down(step: number): number;
41
+ };
42
+ after: {
43
+ index: number;
44
+ min: number;
45
+ max: number;
46
+ isAtMin: boolean;
47
+ isAtMax: boolean;
48
+ up(step: number): number;
49
+ down(step: number): number;
50
+ };
51
+ } | undefined;
52
+ declare function clamp(value: number, min: number, max: number): number;
53
+
54
+ export { clamp, getHandleBounds, getHandlePanels, getNormalizedPanels, getPanelBounds };
@@ -0,0 +1,159 @@
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);
19
+
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
+ function validateSize(key, size) {
31
+ if (Math.floor(size) > 100) {
32
+ throw new Error(`Total ${key} of panels cannot be greater than 100`);
33
+ }
34
+ }
35
+ function getNormalizedPanels(ctx) {
36
+ let numOfPanelsWithoutSize = 0;
37
+ let totalSize = 0;
38
+ let totalMinSize = 0;
39
+ const panels = ctx.size.map((panel) => {
40
+ var _a, _b;
41
+ const minSize = (_a = panel.minSize) != null ? _a : 10;
42
+ const maxSize = (_b = panel.maxSize) != null ? _b : 100;
43
+ totalMinSize += minSize;
44
+ if (panel.size == null) {
45
+ numOfPanelsWithoutSize++;
46
+ } else {
47
+ totalSize += panel.size;
48
+ }
49
+ return {
50
+ ...panel,
51
+ minSize,
52
+ maxSize
53
+ };
54
+ });
55
+ validateSize("minSize", totalMinSize);
56
+ validateSize("size", totalSize);
57
+ let end = 0;
58
+ let remainingSize = 0;
59
+ const result = panels.map((panel) => {
60
+ let start = end;
61
+ if (panel.size != null) {
62
+ end += panel.size;
63
+ remainingSize = panel.size - panel.minSize;
64
+ return {
65
+ ...panel,
66
+ start,
67
+ end,
68
+ remainingSize
69
+ };
70
+ }
71
+ const size = (100 - totalSize) / numOfPanelsWithoutSize;
72
+ end += size;
73
+ remainingSize = size - panel.minSize;
74
+ return { ...panel, size, start, end, remainingSize };
75
+ });
76
+ return result;
77
+ }
78
+ function getHandlePanels(ctx, id = ctx.activeResizeId) {
79
+ var _a;
80
+ const [beforeId, afterId] = (_a = id == null ? void 0 : id.split(":")) != null ? _a : [];
81
+ if (!beforeId || !afterId)
82
+ return;
83
+ const beforeIndex = ctx.previousPanels.findIndex((panel) => panel.id === beforeId);
84
+ const afterIndex = ctx.previousPanels.findIndex((panel) => panel.id === afterId);
85
+ if (beforeIndex === -1 || afterIndex === -1)
86
+ return;
87
+ const before = ctx.previousPanels[beforeIndex];
88
+ const after = ctx.previousPanels[afterIndex];
89
+ return {
90
+ before: {
91
+ ...before,
92
+ index: beforeIndex
93
+ },
94
+ after: {
95
+ ...after,
96
+ index: afterIndex
97
+ }
98
+ };
99
+ }
100
+ function getHandleBounds(ctx, id = ctx.activeResizeId) {
101
+ const panels = getHandlePanels(ctx, id);
102
+ if (!panels)
103
+ return;
104
+ const { before, after } = panels;
105
+ return {
106
+ min: Math.max(before.start + before.minSize, after.end - after.maxSize),
107
+ max: Math.min(after.end - after.minSize, before.maxSize + before.start)
108
+ };
109
+ }
110
+ function getPanelBounds(ctx, id) {
111
+ const bounds = getHandleBounds(ctx, id);
112
+ const panels = getHandlePanels(ctx, id);
113
+ if (!bounds || !panels)
114
+ return;
115
+ const { before, after } = panels;
116
+ const beforeMin = Math.abs(before.start - bounds.min);
117
+ const afterMin = after.size + (before.size - beforeMin);
118
+ const beforeMax = Math.abs(before.start - bounds.max);
119
+ const afterMax = after.size - (beforeMax - before.size);
120
+ return {
121
+ before: {
122
+ index: before.index,
123
+ min: beforeMin,
124
+ max: beforeMax,
125
+ isAtMin: beforeMin === before.size,
126
+ isAtMax: beforeMax === before.size,
127
+ up(step) {
128
+ return Math.min(before.size + step, beforeMax);
129
+ },
130
+ down(step) {
131
+ return Math.max(before.size - step, beforeMin);
132
+ }
133
+ },
134
+ after: {
135
+ index: after.index,
136
+ min: afterMin,
137
+ max: afterMax,
138
+ isAtMin: afterMin === after.size,
139
+ isAtMax: afterMax === after.size,
140
+ up(step) {
141
+ return Math.min(after.size + step, afterMin);
142
+ },
143
+ down(step) {
144
+ return Math.max(after.size - step, afterMax);
145
+ }
146
+ }
147
+ };
148
+ }
149
+ function clamp(value, min, max) {
150
+ return Math.min(Math.max(value, min), max);
151
+ }
152
+ // Annotate the CommonJS export names for ESM import in node:
153
+ 0 && (module.exports = {
154
+ clamp,
155
+ getHandleBounds,
156
+ getHandlePanels,
157
+ getNormalizedPanels,
158
+ getPanelBounds
159
+ });
@@ -0,0 +1,14 @@
1
+ import {
2
+ clamp,
3
+ getHandleBounds,
4
+ getHandlePanels,
5
+ getNormalizedPanels,
6
+ getPanelBounds
7
+ } from "./chunk-SQ3UMXCZ.mjs";
8
+ export {
9
+ clamp,
10
+ getHandleBounds,
11
+ getHandlePanels,
12
+ getNormalizedPanels,
13
+ getPanelBounds
14
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/splitter",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "Core logic for the splitter widget implemented as a state machine",
5
5
  "keywords": [
6
6
  "js",
@@ -28,12 +28,12 @@
28
28
  },
29
29
  "dependencies": {
30
30
  "@zag-js/anatomy": "0.1.3",
31
- "@zag-js/types": "0.3.2",
32
- "@zag-js/core": "0.2.4"
31
+ "@zag-js/core": "0.2.5",
32
+ "@zag-js/types": "0.3.3"
33
33
  },
34
34
  "devDependencies": {
35
35
  "clean-package": "2.2.0",
36
- "@zag-js/dom-utils": "0.2.2",
36
+ "@zag-js/dom-utils": "0.2.3",
37
37
  "@zag-js/number-utils": "0.2.2",
38
38
  "@zag-js/utils": "0.3.2"
39
39
  },
@@ -1,217 +0,0 @@
1
- import {
2
- parts
3
- } from "./chunk-DX6NGFGC.mjs";
4
- import {
5
- dom
6
- } from "./chunk-A3GF7W2N.mjs";
7
-
8
- // ../../utilities/dom/src/attrs.ts
9
- var dataAttr = (guard) => {
10
- return guard ? "" : void 0;
11
- };
12
-
13
- // ../../utilities/dom/src/keyboard-event.ts
14
- var rtlKeyMap = {
15
- ArrowLeft: "ArrowRight",
16
- ArrowRight: "ArrowLeft"
17
- };
18
- var sameKeyMap = {
19
- Up: "ArrowUp",
20
- Down: "ArrowDown",
21
- Esc: "Escape",
22
- " ": "Space",
23
- ",": "Comma",
24
- Left: "ArrowLeft",
25
- Right: "ArrowRight"
26
- };
27
- function getEventKey(event, options = {}) {
28
- var _a;
29
- const { dir = "ltr", orientation = "horizontal" } = options;
30
- let { key } = event;
31
- key = (_a = sameKeyMap[key]) != null ? _a : key;
32
- const isRtl = dir === "rtl" && orientation === "horizontal";
33
- if (isRtl && key in rtlKeyMap) {
34
- key = rtlKeyMap[key];
35
- }
36
- return key;
37
- }
38
- var PAGE_KEYS = /* @__PURE__ */ new Set(["PageUp", "PageDown"]);
39
- var ARROW_KEYS = /* @__PURE__ */ new Set(["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
40
- function getEventStep(event) {
41
- if (event.ctrlKey || event.metaKey) {
42
- return 0.1;
43
- } else {
44
- const isPageKey = PAGE_KEYS.has(event.key);
45
- const isSkipKey = isPageKey || event.shiftKey && ARROW_KEYS.has(event.key);
46
- return isSkipKey ? 10 : 1;
47
- }
48
- }
49
-
50
- // src/splitter.connect.ts
51
- function connect(state, send, normalize) {
52
- const isHorizontal = state.context.isHorizontal;
53
- const isDisabled = state.context.disabled;
54
- const isFocused = state.hasTag("focus");
55
- const isDragging = state.matches("dragging");
56
- const isAtMin = state.context.isAtMin;
57
- const isAtMax = state.context.isAtMax;
58
- const min = state.context.min;
59
- const max = state.context.max;
60
- const value = state.context.value;
61
- return {
62
- isCollapsed: isAtMin,
63
- isExpanded: isAtMax,
64
- isFocused,
65
- isDragging,
66
- value,
67
- collapse() {
68
- send("COLLAPSE");
69
- },
70
- expand() {
71
- send("EXPAND");
72
- },
73
- toggle() {
74
- send("TOGGLE");
75
- },
76
- setSize(size) {
77
- send({ type: "SET_SIZE", size });
78
- },
79
- rootProps: normalize.element({
80
- ...parts.root.attrs,
81
- "data-orientation": state.context.orientation,
82
- "data-disabled": dataAttr(isDisabled),
83
- id: dom.getRootId(state.context),
84
- style: {
85
- display: "flex",
86
- flex: "1 1 0%",
87
- flexDirection: isHorizontal ? "row" : "column"
88
- }
89
- }),
90
- secondaryPaneProps: normalize.element({
91
- ...parts.secondaryPane.attrs,
92
- "data-disabled": dataAttr(isDisabled),
93
- id: dom.getSecondaryPaneId(state.context),
94
- style: {
95
- height: isHorizontal ? "100%" : "auto",
96
- width: isHorizontal ? "auto" : "100%",
97
- flex: "1 1 auto",
98
- position: "relative"
99
- }
100
- }),
101
- primaryPaneProps: normalize.element({
102
- ...parts.primaryPane.attrs,
103
- id: dom.getPrimaryPaneId(state.context),
104
- "data-disabled": dataAttr(isDisabled),
105
- "data-state": isAtMax ? "at-max" : isAtMin ? "at-min" : "between",
106
- style: {
107
- visibility: "visible",
108
- flex: `0 0 ${value}px`,
109
- position: "relative",
110
- userSelect: isDragging ? "none" : "auto",
111
- ...isHorizontal ? { minWidth: `${min}px`, maxWidth: `${max}px` } : { minHeight: `${min}px`, maxHeight: `${max}px` }
112
- }
113
- }),
114
- toggleButtonProps: normalize.element({
115
- ...parts.toggleButton.attrs,
116
- id: dom.getToggleButtonId(state.context),
117
- "aria-label": state.context.isAtMin ? "Expand Primary Pane" : "Collapse Primary Pane",
118
- onClick() {
119
- send("TOGGLE");
120
- }
121
- }),
122
- labelProps: normalize.element({
123
- ...parts.label.attrs,
124
- id: dom.getLabelId(state.context)
125
- }),
126
- splitterProps: normalize.element({
127
- ...parts.splitter.attrs,
128
- id: dom.getSplitterId(state.context),
129
- role: "separator",
130
- tabIndex: isDisabled ? void 0 : 0,
131
- "aria-valuenow": value,
132
- "aria-valuemin": min,
133
- "aria-valuemax": max,
134
- "aria-orientation": state.context.orientation,
135
- "aria-labelledby": dom.getLabelId(state.context),
136
- "aria-controls": dom.getPrimaryPaneId(state.context),
137
- "data-orientation": state.context.orientation,
138
- "data-focus": dataAttr(isFocused),
139
- "data-disabled": dataAttr(isDisabled),
140
- style: {
141
- touchAction: "none",
142
- userSelect: "none",
143
- WebkitUserSelect: "none",
144
- msUserSelect: "none",
145
- flex: "0 0 auto",
146
- cursor: dom.getCursor(state.context),
147
- minHeight: isHorizontal ? "0px" : void 0,
148
- minWidth: isHorizontal ? void 0 : "0px"
149
- },
150
- onPointerDown(event) {
151
- if (isDisabled) {
152
- event.preventDefault();
153
- return;
154
- }
155
- send("POINTER_DOWN");
156
- event.preventDefault();
157
- event.stopPropagation();
158
- },
159
- onPointerOver() {
160
- if (isDisabled)
161
- return;
162
- send("POINTER_OVER");
163
- },
164
- onPointerLeave() {
165
- if (isDisabled)
166
- return;
167
- send("POINTER_LEAVE");
168
- },
169
- onBlur() {
170
- send("BLUR");
171
- },
172
- onFocus() {
173
- send("FOCUS");
174
- },
175
- onDoubleClick() {
176
- if (isDisabled)
177
- return;
178
- send("DOUBLE_CLICK");
179
- },
180
- onKeyDown(event) {
181
- if (isDisabled)
182
- return;
183
- const step = getEventStep(event) * state.context.step;
184
- const keyMap = {
185
- ArrowUp() {
186
- send({ type: "ARROW_UP", step });
187
- },
188
- ArrowDown() {
189
- send({ type: "ARROW_DOWN", step });
190
- },
191
- ArrowLeft() {
192
- send({ type: "ARROW_LEFT", step });
193
- },
194
- ArrowRight() {
195
- send({ type: "ARROW_RIGHT", step });
196
- },
197
- Home() {
198
- send("HOME");
199
- },
200
- End() {
201
- send("END");
202
- }
203
- };
204
- const key = getEventKey(event, state.context);
205
- const exec = keyMap[key];
206
- if (exec) {
207
- exec(event);
208
- event.preventDefault();
209
- }
210
- }
211
- })
212
- };
213
- }
214
-
215
- export {
216
- connect
217
- };