@zag-js/splitter 0.2.5 → 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.
@@ -0,0 +1,8 @@
1
+ import {
2
+ machine
3
+ } from "./chunk-IOEWCDUH.mjs";
4
+ import "./chunk-T56NFB6E.mjs";
5
+ import "./chunk-SQ3UMXCZ.mjs";
6
+ export {
7
+ machine
8
+ };
@@ -0,0 +1,68 @@
1
+ import { StateMachine } from '@zag-js/core';
2
+ import { RequiredBy, DirectionProperty, CommonProperties, Context } from '@zag-js/types';
3
+
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
+ };
15
+ type PublicContext = DirectionProperty & CommonProperties & {
16
+ orientation: "horizontal" | "vertical";
17
+ size: PanelSizeData[];
18
+ onResize?: (details: ResizeDetails) => void;
19
+ onResizeStart?: (details: ResizeDetails) => void;
20
+ onResizeEnd?: (details: ResizeDetails) => void;
21
+ };
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
+ }>;
30
+ type ComputedContext = Readonly<{
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">>>;
50
+ }>;
51
+ type MachineContext = PublicContext & ComputedContext & PrivateContext;
52
+ type MachineState = {
53
+ value: "unknown" | "idle" | "hover:temp" | "hover" | "dragging" | "focused";
54
+ tags: "focus";
55
+ };
56
+ type State = StateMachine.State<MachineContext, MachineState>;
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
+ };
67
+
68
+ export { MachineContext, MachineState, NormalizedPanelData, PanelId, PanelProps, ResizeTriggerProps, Send, State, UserDefinedContext };
@@ -0,0 +1,18 @@
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 __copyProps = (to, from, except, desc) => {
7
+ if (from && typeof from === "object" || typeof from === "function") {
8
+ for (let key of __getOwnPropNames(from))
9
+ if (!__hasOwnProp.call(to, key) && key !== except)
10
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
11
+ }
12
+ return to;
13
+ };
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+
16
+ // src/splitter.types.ts
17
+ var splitter_types_exports = {};
18
+ module.exports = __toCommonJS(splitter_types_exports);
File without changes
@@ -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,10 +1,7 @@
1
1
  {
2
2
  "name": "@zag-js/splitter",
3
- "version": "0.2.5",
3
+ "version": "0.2.7",
4
4
  "description": "Core logic for the splitter widget implemented as a state machine",
5
- "main": "dist/index.js",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.ts",
8
5
  "keywords": [
9
6
  "js",
10
7
  "machine",
@@ -30,19 +27,32 @@
30
27
  "url": "https://github.com/chakra-ui/zag/issues"
31
28
  },
32
29
  "dependencies": {
33
- "@zag-js/anatomy": "0.1.2",
34
- "@zag-js/core": "0.2.3",
35
- "@zag-js/types": "0.3.1"
30
+ "@zag-js/anatomy": "0.1.3",
31
+ "@zag-js/core": "0.2.5",
32
+ "@zag-js/types": "0.3.3"
36
33
  },
37
34
  "devDependencies": {
38
- "@zag-js/dom-utils": "0.2.1",
39
- "@zag-js/number-utils": "0.2.1",
40
- "@zag-js/utils": "0.3.1"
35
+ "clean-package": "2.2.0",
36
+ "@zag-js/dom-utils": "0.2.3",
37
+ "@zag-js/number-utils": "0.2.2",
38
+ "@zag-js/utils": "0.3.2"
39
+ },
40
+ "clean-package": "../../../clean-package.config.json",
41
+ "main": "dist/index.js",
42
+ "module": "dist/index.mjs",
43
+ "types": "dist/index.d.ts",
44
+ "exports": {
45
+ ".": {
46
+ "types": "./dist/index.d.ts",
47
+ "import": "./dist/index.mjs",
48
+ "require": "./dist/index.js"
49
+ },
50
+ "./package.json": "./package.json"
41
51
  },
42
52
  "scripts": {
43
- "build-fast": "tsup src/index.ts --format=esm,cjs",
53
+ "build-fast": "tsup src",
44
54
  "start": "pnpm build --watch",
45
- "build": "tsup src/index.ts --format=esm,cjs --dts",
55
+ "build": "tsup src --dts",
46
56
  "test": "jest --config ../../../jest.config.js --rootDir . --passWithNoTests",
47
57
  "lint": "eslint src --ext .ts,.tsx",
48
58
  "test-ci": "pnpm test --ci --runInBand",