@zag-js/splitter 0.70.0 → 0.71.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.js +36 -82
- package/dist/index.mjs +9 -30
- package/package.json +9 -10
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/src/index.ts +0 -14
- package/src/splitter.anatomy.ts +0 -5
- package/src/splitter.connect.ts +0 -179
- package/src/splitter.dom.ts +0 -61
- package/src/splitter.machine.ts +0 -285
- package/src/splitter.props.ts +0 -22
- package/src/splitter.types.ts +0 -156
- package/src/splitter.utils.ts +0 -143
package/src/splitter.connect.ts
DELETED
|
@@ -1,179 +0,0 @@
|
|
|
1
|
-
import { getEventKey, getEventStep, type EventKeyMap } from "@zag-js/dom-event"
|
|
2
|
-
import { dataAttr } from "@zag-js/dom-query"
|
|
3
|
-
import type { NormalizeProps, PropTypes } from "@zag-js/types"
|
|
4
|
-
import { parts } from "./splitter.anatomy"
|
|
5
|
-
import { dom } from "./splitter.dom"
|
|
6
|
-
import type { MachineApi, ResizeTriggerProps, ResizeTriggerState, Send, State } from "./splitter.types"
|
|
7
|
-
import { getHandleBounds } from "./splitter.utils"
|
|
8
|
-
|
|
9
|
-
export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
|
|
10
|
-
const horizontal = state.context.isHorizontal
|
|
11
|
-
const focused = state.hasTag("focus")
|
|
12
|
-
const dragging = state.matches("dragging")
|
|
13
|
-
const panels = state.context.panels
|
|
14
|
-
|
|
15
|
-
function getResizeTriggerState(props: ResizeTriggerProps): ResizeTriggerState {
|
|
16
|
-
const { id, disabled } = props
|
|
17
|
-
const ids = id.split(":")
|
|
18
|
-
const panelIds = ids.map((id) => dom.getPanelId(state.context, id))
|
|
19
|
-
const panels = getHandleBounds(state.context, id)
|
|
20
|
-
|
|
21
|
-
return {
|
|
22
|
-
disabled: !!disabled,
|
|
23
|
-
focused: state.context.activeResizeId === id && focused,
|
|
24
|
-
panelIds,
|
|
25
|
-
min: panels?.min,
|
|
26
|
-
max: panels?.max,
|
|
27
|
-
value: 0,
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
return {
|
|
32
|
-
focused: focused,
|
|
33
|
-
dragging: dragging,
|
|
34
|
-
getResizeTriggerState,
|
|
35
|
-
bounds: getHandleBounds(state.context),
|
|
36
|
-
setToMinSize(id) {
|
|
37
|
-
const panel = panels.find((panel) => panel.id === id)
|
|
38
|
-
send({ type: "SET_PANEL_SIZE", id, size: panel?.minSize, src: "setToMinSize" })
|
|
39
|
-
},
|
|
40
|
-
setToMaxSize(id) {
|
|
41
|
-
const panel = panels.find((panel) => panel.id === id)
|
|
42
|
-
send({ type: "SET_PANEL_SIZE", id, size: panel?.maxSize, src: "setToMaxSize" })
|
|
43
|
-
},
|
|
44
|
-
setSize(id, size) {
|
|
45
|
-
send({ type: "SET_PANEL_SIZE", id, size })
|
|
46
|
-
},
|
|
47
|
-
|
|
48
|
-
getRootProps() {
|
|
49
|
-
return normalize.element({
|
|
50
|
-
...parts.root.attrs,
|
|
51
|
-
"data-orientation": state.context.orientation,
|
|
52
|
-
id: dom.getRootId(state.context),
|
|
53
|
-
dir: state.context.dir,
|
|
54
|
-
style: {
|
|
55
|
-
display: "flex",
|
|
56
|
-
flexDirection: horizontal ? "row" : "column",
|
|
57
|
-
height: "100%",
|
|
58
|
-
width: "100%",
|
|
59
|
-
overflow: "hidden",
|
|
60
|
-
},
|
|
61
|
-
})
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
getPanelProps(props) {
|
|
65
|
-
const { id } = props
|
|
66
|
-
return normalize.element({
|
|
67
|
-
...parts.panel.attrs,
|
|
68
|
-
"data-orientation": state.context.orientation,
|
|
69
|
-
dir: state.context.dir,
|
|
70
|
-
id: dom.getPanelId(state.context, id),
|
|
71
|
-
"data-ownedby": dom.getRootId(state.context),
|
|
72
|
-
style: dom.getPanelStyle(state.context, id),
|
|
73
|
-
})
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
getResizeTriggerProps(props) {
|
|
77
|
-
const { id, disabled, step = 1 } = props
|
|
78
|
-
const triggerState = getResizeTriggerState(props)
|
|
79
|
-
|
|
80
|
-
return normalize.element({
|
|
81
|
-
...parts.resizeTrigger.attrs,
|
|
82
|
-
dir: state.context.dir,
|
|
83
|
-
id: dom.getResizeTriggerId(state.context, id),
|
|
84
|
-
role: "separator",
|
|
85
|
-
"data-ownedby": dom.getRootId(state.context),
|
|
86
|
-
tabIndex: disabled ? undefined : 0,
|
|
87
|
-
"aria-valuenow": triggerState.value,
|
|
88
|
-
"aria-valuemin": triggerState.min,
|
|
89
|
-
"aria-valuemax": triggerState.max,
|
|
90
|
-
"data-orientation": state.context.orientation,
|
|
91
|
-
"aria-orientation": state.context.orientation,
|
|
92
|
-
"aria-controls": triggerState.panelIds.join(" "),
|
|
93
|
-
"data-focus": dataAttr(triggerState.focused),
|
|
94
|
-
"data-disabled": dataAttr(disabled),
|
|
95
|
-
style: {
|
|
96
|
-
touchAction: "none",
|
|
97
|
-
userSelect: "none",
|
|
98
|
-
WebkitUserSelect: "none",
|
|
99
|
-
flex: "0 0 auto",
|
|
100
|
-
pointerEvents: dragging && !triggerState.focused ? "none" : undefined,
|
|
101
|
-
cursor: horizontal ? "col-resize" : "row-resize",
|
|
102
|
-
[horizontal ? "minHeight" : "minWidth"]: "0",
|
|
103
|
-
},
|
|
104
|
-
onPointerDown(event) {
|
|
105
|
-
if (disabled) {
|
|
106
|
-
event.preventDefault()
|
|
107
|
-
return
|
|
108
|
-
}
|
|
109
|
-
send({ type: "POINTER_DOWN", id })
|
|
110
|
-
event.currentTarget.setPointerCapture(event.pointerId)
|
|
111
|
-
event.preventDefault()
|
|
112
|
-
event.stopPropagation()
|
|
113
|
-
},
|
|
114
|
-
onPointerUp(event) {
|
|
115
|
-
if (disabled) return
|
|
116
|
-
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
|
|
117
|
-
event.currentTarget.releasePointerCapture(event.pointerId)
|
|
118
|
-
}
|
|
119
|
-
},
|
|
120
|
-
onPointerOver() {
|
|
121
|
-
if (disabled) return
|
|
122
|
-
send({ type: "POINTER_OVER", id })
|
|
123
|
-
},
|
|
124
|
-
onPointerLeave() {
|
|
125
|
-
if (disabled) return
|
|
126
|
-
send({ type: "POINTER_LEAVE", id })
|
|
127
|
-
},
|
|
128
|
-
onBlur() {
|
|
129
|
-
send("BLUR")
|
|
130
|
-
},
|
|
131
|
-
onFocus() {
|
|
132
|
-
send({ type: "FOCUS", id })
|
|
133
|
-
},
|
|
134
|
-
onDoubleClick() {
|
|
135
|
-
if (disabled) return
|
|
136
|
-
send({ type: "DOUBLE_CLICK", id })
|
|
137
|
-
},
|
|
138
|
-
onKeyDown(event) {
|
|
139
|
-
if (event.defaultPrevented) return
|
|
140
|
-
if (disabled) return
|
|
141
|
-
|
|
142
|
-
const moveStep = getEventStep(event) * step
|
|
143
|
-
|
|
144
|
-
const keyMap: EventKeyMap = {
|
|
145
|
-
Enter() {
|
|
146
|
-
send("ENTER")
|
|
147
|
-
},
|
|
148
|
-
ArrowUp() {
|
|
149
|
-
send({ type: "ARROW_UP", step: moveStep })
|
|
150
|
-
},
|
|
151
|
-
ArrowDown() {
|
|
152
|
-
send({ type: "ARROW_DOWN", step: moveStep })
|
|
153
|
-
},
|
|
154
|
-
ArrowLeft() {
|
|
155
|
-
send({ type: "ARROW_LEFT", step: moveStep })
|
|
156
|
-
},
|
|
157
|
-
ArrowRight() {
|
|
158
|
-
send({ type: "ARROW_RIGHT", step: moveStep })
|
|
159
|
-
},
|
|
160
|
-
Home() {
|
|
161
|
-
send("HOME")
|
|
162
|
-
},
|
|
163
|
-
End() {
|
|
164
|
-
send("END")
|
|
165
|
-
},
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
const key = getEventKey(event, state.context)
|
|
169
|
-
const exec = keyMap[key]
|
|
170
|
-
|
|
171
|
-
if (exec) {
|
|
172
|
-
exec(event)
|
|
173
|
-
event.preventDefault()
|
|
174
|
-
}
|
|
175
|
-
},
|
|
176
|
-
})
|
|
177
|
-
},
|
|
178
|
-
}
|
|
179
|
-
}
|
package/src/splitter.dom.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import { createScope, queryAll } from "@zag-js/dom-query"
|
|
2
|
-
import type { JSX, Style } from "@zag-js/types"
|
|
3
|
-
import type { MachineContext as Ctx, PanelId } from "./splitter.types"
|
|
4
|
-
|
|
5
|
-
export const dom = createScope({
|
|
6
|
-
getRootId: (ctx: Ctx) => ctx.ids?.root ?? `splitter:${ctx.id}`,
|
|
7
|
-
getResizeTriggerId: (ctx: Ctx, id: string) => ctx.ids?.resizeTrigger?.(id) ?? `splitter:${ctx.id}:splitter:${id}`,
|
|
8
|
-
getLabelId: (ctx: Ctx) => ctx.ids?.label ?? `splitter:${ctx.id}:label`,
|
|
9
|
-
getPanelId: (ctx: Ctx, id: string | number) => ctx.ids?.panel?.(id) ?? `splitter:${ctx.id}:panel:${id}`,
|
|
10
|
-
getGlobalCursorId: (ctx: Ctx) => `splitter:${ctx.id}:global-cursor`,
|
|
11
|
-
|
|
12
|
-
getRootEl: (ctx: Ctx) => dom.getById(ctx, dom.getRootId(ctx)),
|
|
13
|
-
getResizeTriggerEl: (ctx: Ctx, id: string) => dom.getById(ctx, dom.getResizeTriggerId(ctx, id)),
|
|
14
|
-
getPanelEl: (ctx: Ctx, id: string | number) => dom.getById(ctx, dom.getPanelId(ctx, id)),
|
|
15
|
-
|
|
16
|
-
getCursor(ctx: Ctx) {
|
|
17
|
-
const x = ctx.isHorizontal
|
|
18
|
-
let cursor: Style["cursor"] = x ? "col-resize" : "row-resize"
|
|
19
|
-
if (ctx.activeResizeState.isAtMin) cursor = x ? "e-resize" : "s-resize"
|
|
20
|
-
if (ctx.activeResizeState.isAtMax) cursor = x ? "w-resize" : "n-resize"
|
|
21
|
-
return cursor
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
getPanelStyle(ctx: Ctx, id: PanelId): JSX.CSSProperties {
|
|
25
|
-
const flexGrow = ctx.panels.find((panel) => panel.id === id)?.size ?? "0"
|
|
26
|
-
return {
|
|
27
|
-
flexBasis: 0,
|
|
28
|
-
flexGrow,
|
|
29
|
-
flexShrink: 1,
|
|
30
|
-
overflow: "hidden",
|
|
31
|
-
}
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
getActiveHandleEl(ctx: Ctx) {
|
|
35
|
-
const activeId = ctx.activeResizeId
|
|
36
|
-
if (activeId == null) return
|
|
37
|
-
return dom.getById(ctx, dom.getResizeTriggerId(ctx, activeId))
|
|
38
|
-
},
|
|
39
|
-
|
|
40
|
-
getResizeTriggerEls(ctx: Ctx) {
|
|
41
|
-
const ownerId = CSS.escape(dom.getRootId(ctx))
|
|
42
|
-
return queryAll(dom.getRootEl(ctx), `[role=separator][data-ownedby='${ownerId}']`)
|
|
43
|
-
},
|
|
44
|
-
|
|
45
|
-
setupGlobalCursor(ctx: Ctx) {
|
|
46
|
-
const styleEl = dom.getById(ctx, dom.getGlobalCursorId(ctx))
|
|
47
|
-
const textContent = `* { cursor: ${dom.getCursor(ctx)} !important; }`
|
|
48
|
-
if (styleEl) {
|
|
49
|
-
styleEl.textContent = textContent
|
|
50
|
-
} else {
|
|
51
|
-
const style = dom.getDoc(ctx).createElement("style")
|
|
52
|
-
style.id = dom.getGlobalCursorId(ctx)
|
|
53
|
-
style.textContent = textContent
|
|
54
|
-
dom.getDoc(ctx).head.appendChild(style)
|
|
55
|
-
}
|
|
56
|
-
},
|
|
57
|
-
|
|
58
|
-
removeGlobalCursor(ctx: Ctx) {
|
|
59
|
-
dom.getById(ctx, dom.getGlobalCursorId(ctx))?.remove()
|
|
60
|
-
},
|
|
61
|
-
})
|
package/src/splitter.machine.ts
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
import { createMachine } from "@zag-js/core"
|
|
2
|
-
import { getRelativePoint, trackPointerMove } 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"
|
|
6
|
-
import type { MachineContext, MachineState, UserDefinedContext } from "./splitter.types"
|
|
7
|
-
import { clamp, getHandleBounds, getHandlePanels, getNormalizedPanels, getPanelBounds } from "./splitter.utils"
|
|
8
|
-
|
|
9
|
-
export function machine(userContext: UserDefinedContext) {
|
|
10
|
-
const ctx = compact(userContext)
|
|
11
|
-
return createMachine<MachineContext, MachineState>(
|
|
12
|
-
{
|
|
13
|
-
id: "splitter",
|
|
14
|
-
initial: "idle",
|
|
15
|
-
context: {
|
|
16
|
-
orientation: "horizontal",
|
|
17
|
-
activeResizeId: null,
|
|
18
|
-
previousPanels: [],
|
|
19
|
-
size: [],
|
|
20
|
-
initialSize: [],
|
|
21
|
-
activeResizeState: {
|
|
22
|
-
isAtMin: false,
|
|
23
|
-
isAtMax: false,
|
|
24
|
-
},
|
|
25
|
-
...ctx,
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
created: ["setPreviousPanels", "setInitialSize"],
|
|
29
|
-
|
|
30
|
-
watch: {
|
|
31
|
-
size: ["setActiveResizeState"],
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
computed: {
|
|
35
|
-
isHorizontal: (ctx) => ctx.orientation === "horizontal",
|
|
36
|
-
panels: (ctx) => getNormalizedPanels(ctx),
|
|
37
|
-
},
|
|
38
|
-
|
|
39
|
-
on: {
|
|
40
|
-
SET_PANEL_SIZE: {
|
|
41
|
-
actions: "setPanelSize",
|
|
42
|
-
},
|
|
43
|
-
},
|
|
44
|
-
states: {
|
|
45
|
-
idle: {
|
|
46
|
-
entry: ["clearActiveHandleId"],
|
|
47
|
-
on: {
|
|
48
|
-
POINTER_OVER: {
|
|
49
|
-
target: "hover:temp",
|
|
50
|
-
actions: ["setActiveHandleId"],
|
|
51
|
-
},
|
|
52
|
-
FOCUS: {
|
|
53
|
-
target: "focused",
|
|
54
|
-
actions: ["setActiveHandleId"],
|
|
55
|
-
},
|
|
56
|
-
DOUBLE_CLICK: {
|
|
57
|
-
actions: ["resetStartPanel", "setPreviousPanels"],
|
|
58
|
-
},
|
|
59
|
-
},
|
|
60
|
-
},
|
|
61
|
-
|
|
62
|
-
"hover:temp": {
|
|
63
|
-
after: {
|
|
64
|
-
HOVER_DELAY: "hover",
|
|
65
|
-
},
|
|
66
|
-
on: {
|
|
67
|
-
POINTER_DOWN: {
|
|
68
|
-
target: "dragging",
|
|
69
|
-
actions: ["setActiveHandleId"],
|
|
70
|
-
},
|
|
71
|
-
POINTER_LEAVE: "idle",
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
|
|
75
|
-
hover: {
|
|
76
|
-
tags: ["focus"],
|
|
77
|
-
on: {
|
|
78
|
-
POINTER_DOWN: "dragging",
|
|
79
|
-
POINTER_LEAVE: "idle",
|
|
80
|
-
},
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
focused: {
|
|
84
|
-
tags: ["focus"],
|
|
85
|
-
on: {
|
|
86
|
-
BLUR: "idle",
|
|
87
|
-
POINTER_DOWN: {
|
|
88
|
-
target: "dragging",
|
|
89
|
-
actions: ["setActiveHandleId"],
|
|
90
|
-
},
|
|
91
|
-
ARROW_LEFT: {
|
|
92
|
-
guard: "isHorizontal",
|
|
93
|
-
actions: ["shrinkStartPanel", "setPreviousPanels"],
|
|
94
|
-
},
|
|
95
|
-
ARROW_RIGHT: {
|
|
96
|
-
guard: "isHorizontal",
|
|
97
|
-
actions: ["expandStartPanel", "setPreviousPanels"],
|
|
98
|
-
},
|
|
99
|
-
ARROW_UP: {
|
|
100
|
-
guard: "isVertical",
|
|
101
|
-
actions: ["shrinkStartPanel", "setPreviousPanels"],
|
|
102
|
-
},
|
|
103
|
-
ARROW_DOWN: {
|
|
104
|
-
guard: "isVertical",
|
|
105
|
-
actions: ["expandStartPanel", "setPreviousPanels"],
|
|
106
|
-
},
|
|
107
|
-
ENTER: [
|
|
108
|
-
{
|
|
109
|
-
guard: "isStartPanelAtMax",
|
|
110
|
-
actions: ["setStartPanelToMin", "setPreviousPanels"],
|
|
111
|
-
},
|
|
112
|
-
{ actions: ["setStartPanelToMax", "setPreviousPanels"] },
|
|
113
|
-
],
|
|
114
|
-
HOME: {
|
|
115
|
-
actions: ["setStartPanelToMin", "setPreviousPanels"],
|
|
116
|
-
},
|
|
117
|
-
END: {
|
|
118
|
-
actions: ["setStartPanelToMax", "setPreviousPanels"],
|
|
119
|
-
},
|
|
120
|
-
},
|
|
121
|
-
},
|
|
122
|
-
|
|
123
|
-
dragging: {
|
|
124
|
-
tags: ["focus"],
|
|
125
|
-
entry: "focusResizeHandle",
|
|
126
|
-
activities: ["trackPointerMove"],
|
|
127
|
-
on: {
|
|
128
|
-
POINTER_MOVE: {
|
|
129
|
-
actions: ["setPointerValue", "setGlobalCursor", "invokeOnResize"],
|
|
130
|
-
},
|
|
131
|
-
POINTER_UP: {
|
|
132
|
-
target: "focused",
|
|
133
|
-
actions: ["setPreviousPanels", "clearGlobalCursor", "blurResizeHandle", "invokeOnResizeEnd"],
|
|
134
|
-
},
|
|
135
|
-
},
|
|
136
|
-
},
|
|
137
|
-
},
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
activities: {
|
|
141
|
-
trackPointerMove: (ctx, _evt, { send }) => {
|
|
142
|
-
const doc = dom.getDoc(ctx)
|
|
143
|
-
return trackPointerMove(doc, {
|
|
144
|
-
onPointerMove(info) {
|
|
145
|
-
send({ type: "POINTER_MOVE", point: info.point })
|
|
146
|
-
},
|
|
147
|
-
onPointerUp() {
|
|
148
|
-
send("POINTER_UP")
|
|
149
|
-
},
|
|
150
|
-
})
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
guards: {
|
|
154
|
-
isStartPanelAtMin: (ctx) => ctx.activeResizeState.isAtMin,
|
|
155
|
-
isStartPanelAtMax: (ctx) => ctx.activeResizeState.isAtMax,
|
|
156
|
-
isHorizontal: (ctx) => ctx.isHorizontal,
|
|
157
|
-
isVertical: (ctx) => !ctx.isHorizontal,
|
|
158
|
-
},
|
|
159
|
-
delays: {
|
|
160
|
-
HOVER_DELAY: 250,
|
|
161
|
-
},
|
|
162
|
-
actions: {
|
|
163
|
-
setGlobalCursor(ctx) {
|
|
164
|
-
dom.setupGlobalCursor(ctx)
|
|
165
|
-
},
|
|
166
|
-
clearGlobalCursor(ctx) {
|
|
167
|
-
dom.removeGlobalCursor(ctx)
|
|
168
|
-
},
|
|
169
|
-
invokeOnResize(ctx) {
|
|
170
|
-
ctx.onSizeChange?.({ size: Array.from(ctx.size), activeHandleId: ctx.activeResizeId })
|
|
171
|
-
},
|
|
172
|
-
invokeOnResizeEnd(ctx) {
|
|
173
|
-
ctx.onSizeChangeEnd?.({ size: Array.from(ctx.size), activeHandleId: ctx.activeResizeId })
|
|
174
|
-
},
|
|
175
|
-
setActiveHandleId(ctx, evt) {
|
|
176
|
-
ctx.activeResizeId = evt.id
|
|
177
|
-
},
|
|
178
|
-
clearActiveHandleId(ctx) {
|
|
179
|
-
ctx.activeResizeId = null
|
|
180
|
-
},
|
|
181
|
-
setInitialSize(ctx) {
|
|
182
|
-
ctx.initialSize = ctx.panels.slice().map((panel) => ({
|
|
183
|
-
id: panel.id,
|
|
184
|
-
size: panel.size,
|
|
185
|
-
}))
|
|
186
|
-
},
|
|
187
|
-
setPanelSize(ctx, evt) {
|
|
188
|
-
const { id, size } = evt
|
|
189
|
-
ctx.size = ctx.size.map((panel) => {
|
|
190
|
-
const panelSize = clamp(size, panel.minSize ?? 0, panel.maxSize ?? 100)
|
|
191
|
-
return panel.id === id ? { ...panel, size: panelSize } : panel
|
|
192
|
-
})
|
|
193
|
-
},
|
|
194
|
-
setStartPanelToMin(ctx) {
|
|
195
|
-
const bounds = getPanelBounds(ctx)
|
|
196
|
-
if (!bounds) return
|
|
197
|
-
const { before, after } = bounds
|
|
198
|
-
ctx.size[before.index].size = before.min
|
|
199
|
-
ctx.size[after.index].size = after.min
|
|
200
|
-
},
|
|
201
|
-
setStartPanelToMax(ctx) {
|
|
202
|
-
const bounds = getPanelBounds(ctx)
|
|
203
|
-
if (!bounds) return
|
|
204
|
-
const { before, after } = bounds
|
|
205
|
-
ctx.size[before.index].size = before.max
|
|
206
|
-
ctx.size[after.index].size = after.max
|
|
207
|
-
},
|
|
208
|
-
expandStartPanel(ctx, evt) {
|
|
209
|
-
const bounds = getPanelBounds(ctx)
|
|
210
|
-
if (!bounds) return
|
|
211
|
-
const { before, after } = bounds
|
|
212
|
-
ctx.size[before.index].size = before.up(evt.step)
|
|
213
|
-
ctx.size[after.index].size = after.down(evt.step)
|
|
214
|
-
},
|
|
215
|
-
shrinkStartPanel(ctx, evt) {
|
|
216
|
-
const bounds = getPanelBounds(ctx)
|
|
217
|
-
if (!bounds) return
|
|
218
|
-
const { before, after } = bounds
|
|
219
|
-
ctx.size[before.index].size = before.down(evt.step)
|
|
220
|
-
ctx.size[after.index].size = after.up(evt.step)
|
|
221
|
-
},
|
|
222
|
-
resetStartPanel(ctx, evt) {
|
|
223
|
-
const bounds = getPanelBounds(ctx, evt.id)
|
|
224
|
-
if (!bounds) return
|
|
225
|
-
const { before, after } = bounds
|
|
226
|
-
ctx.size[before.index].size = ctx.initialSize[before.index].size
|
|
227
|
-
ctx.size[after.index].size = ctx.initialSize[after.index].size
|
|
228
|
-
},
|
|
229
|
-
focusResizeHandle(ctx) {
|
|
230
|
-
raf(() => {
|
|
231
|
-
dom.getActiveHandleEl(ctx)?.focus({ preventScroll: true })
|
|
232
|
-
})
|
|
233
|
-
},
|
|
234
|
-
blurResizeHandle(ctx) {
|
|
235
|
-
raf(() => {
|
|
236
|
-
dom.getActiveHandleEl(ctx)?.blur()
|
|
237
|
-
})
|
|
238
|
-
},
|
|
239
|
-
setPreviousPanels(ctx) {
|
|
240
|
-
ctx.previousPanels = ctx.panels.slice()
|
|
241
|
-
},
|
|
242
|
-
setActiveResizeState(ctx) {
|
|
243
|
-
const panels = getPanelBounds(ctx)
|
|
244
|
-
if (!panels) return
|
|
245
|
-
const { before } = panels
|
|
246
|
-
ctx.activeResizeState = {
|
|
247
|
-
isAtMin: before.isAtMin,
|
|
248
|
-
isAtMax: before.isAtMax,
|
|
249
|
-
}
|
|
250
|
-
},
|
|
251
|
-
setPointerValue(ctx, evt) {
|
|
252
|
-
const panels = getHandlePanels(ctx)
|
|
253
|
-
const bounds = getHandleBounds(ctx)
|
|
254
|
-
|
|
255
|
-
if (!panels || !bounds) return
|
|
256
|
-
|
|
257
|
-
const rootEl = dom.getRootEl(ctx)
|
|
258
|
-
if (!rootEl) return
|
|
259
|
-
|
|
260
|
-
const relativePoint = getRelativePoint(evt.point, rootEl)
|
|
261
|
-
const percentValue = relativePoint.getPercentValue({
|
|
262
|
-
dir: ctx.dir,
|
|
263
|
-
orientation: ctx.orientation,
|
|
264
|
-
})
|
|
265
|
-
|
|
266
|
-
let pointValue = percentValue * 100
|
|
267
|
-
|
|
268
|
-
// update active resize state here because we use `previousPanels` in the calculations
|
|
269
|
-
ctx.activeResizeState = {
|
|
270
|
-
isAtMin: pointValue < bounds.min,
|
|
271
|
-
isAtMax: pointValue > bounds.max,
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
pointValue = clamp(pointValue, bounds.min, bounds.max)
|
|
275
|
-
|
|
276
|
-
const { before, after } = panels
|
|
277
|
-
|
|
278
|
-
const offset = pointValue - before.end
|
|
279
|
-
ctx.size[before.index].size = before.size + offset
|
|
280
|
-
ctx.size[after.index].size = after.size - offset
|
|
281
|
-
},
|
|
282
|
-
},
|
|
283
|
-
},
|
|
284
|
-
)
|
|
285
|
-
}
|
package/src/splitter.props.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { createProps } from "@zag-js/types"
|
|
2
|
-
import { createSplitProps } from "@zag-js/utils"
|
|
3
|
-
import type { PanelProps, ResizeTriggerProps, UserDefinedContext } from "./splitter.types"
|
|
4
|
-
|
|
5
|
-
export const props = createProps<UserDefinedContext>()([
|
|
6
|
-
"dir",
|
|
7
|
-
"getRootNode",
|
|
8
|
-
"id",
|
|
9
|
-
"ids",
|
|
10
|
-
"onSizeChange",
|
|
11
|
-
"onSizeChangeEnd",
|
|
12
|
-
"orientation",
|
|
13
|
-
"size",
|
|
14
|
-
])
|
|
15
|
-
|
|
16
|
-
export const splitProps = createSplitProps<Partial<UserDefinedContext>>(props)
|
|
17
|
-
|
|
18
|
-
export const panelProps = createProps<PanelProps>()(["id", "snapSize"])
|
|
19
|
-
export const splitPanelProps = createSplitProps<PanelProps>(panelProps)
|
|
20
|
-
|
|
21
|
-
export const resizeTriggerProps = createProps<ResizeTriggerProps>()(["disabled", "id", "step"])
|
|
22
|
-
export const splitResizeTriggerProps = createSplitProps<ResizeTriggerProps>(resizeTriggerProps)
|
package/src/splitter.types.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
import type { Machine, StateMachine as S } from "@zag-js/core"
|
|
2
|
-
import type { CommonProperties, DirectionProperty, PropTypes, RequiredBy } from "@zag-js/types"
|
|
3
|
-
|
|
4
|
-
/* -----------------------------------------------------------------------------
|
|
5
|
-
* Callback details
|
|
6
|
-
* -----------------------------------------------------------------------------*/
|
|
7
|
-
|
|
8
|
-
export type PanelId = string | number
|
|
9
|
-
|
|
10
|
-
export interface PanelSizeData {
|
|
11
|
-
id: PanelId
|
|
12
|
-
size?: number
|
|
13
|
-
minSize?: number
|
|
14
|
-
maxSize?: number
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export interface SizeChangeDetails {
|
|
18
|
-
size: PanelSizeData[]
|
|
19
|
-
activeHandleId: string | null
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/* -----------------------------------------------------------------------------
|
|
23
|
-
* Machine context
|
|
24
|
-
* -----------------------------------------------------------------------------*/
|
|
25
|
-
|
|
26
|
-
type ElementIds = Partial<{
|
|
27
|
-
root: string
|
|
28
|
-
resizeTrigger(id: string): string
|
|
29
|
-
label(id: string): string
|
|
30
|
-
panel(id: string | number): string
|
|
31
|
-
}>
|
|
32
|
-
|
|
33
|
-
interface PublicContext extends DirectionProperty, CommonProperties {
|
|
34
|
-
/**
|
|
35
|
-
* The orientation of the splitter. Can be `horizontal` or `vertical`
|
|
36
|
-
*/
|
|
37
|
-
orientation: "horizontal" | "vertical"
|
|
38
|
-
/**
|
|
39
|
-
* The size data of the panels
|
|
40
|
-
*/
|
|
41
|
-
size: PanelSizeData[]
|
|
42
|
-
/**
|
|
43
|
-
* Function called when the splitter is resized.
|
|
44
|
-
*/
|
|
45
|
-
onSizeChange?: (details: SizeChangeDetails) => void
|
|
46
|
-
/**
|
|
47
|
-
* Function called when the splitter resize ends.
|
|
48
|
-
*/
|
|
49
|
-
onSizeChangeEnd?: (details: SizeChangeDetails) => void
|
|
50
|
-
/**
|
|
51
|
-
* The ids of the elements in the splitter. Useful for composition.
|
|
52
|
-
*/
|
|
53
|
-
ids?: ElementIds
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export type UserDefinedContext = RequiredBy<PublicContext, "id">
|
|
57
|
-
|
|
58
|
-
export type NormalizedPanelData = Array<
|
|
59
|
-
Required<PanelSizeData> & {
|
|
60
|
-
remainingSize: number
|
|
61
|
-
minSize: number
|
|
62
|
-
maxSize: number
|
|
63
|
-
start: number
|
|
64
|
-
end: number
|
|
65
|
-
}
|
|
66
|
-
>
|
|
67
|
-
|
|
68
|
-
type ComputedContext = Readonly<{
|
|
69
|
-
isHorizontal: boolean
|
|
70
|
-
panels: NormalizedPanelData
|
|
71
|
-
activeResizeBounds?: { min: number; max: number }
|
|
72
|
-
activeResizePanels?: { before: PanelSizeData; after: PanelSizeData }
|
|
73
|
-
}>
|
|
74
|
-
|
|
75
|
-
interface PrivateContext {
|
|
76
|
-
activeResizeId: string | null
|
|
77
|
-
previousPanels: NormalizedPanelData
|
|
78
|
-
activeResizeState: { isAtMin: boolean; isAtMax: boolean }
|
|
79
|
-
initialSize: Array<Required<Pick<PanelSizeData, "id" | "size">>>
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export interface MachineContext extends PublicContext, ComputedContext, PrivateContext {}
|
|
83
|
-
|
|
84
|
-
export interface MachineState {
|
|
85
|
-
value: "idle" | "hover:temp" | "hover" | "dragging" | "focused"
|
|
86
|
-
tags: "focus"
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export type State = S.State<MachineContext, MachineState>
|
|
90
|
-
|
|
91
|
-
export type Send = S.Send<S.AnyEventObject>
|
|
92
|
-
|
|
93
|
-
export type Service = Machine<MachineContext, MachineState, S.AnyEventObject>
|
|
94
|
-
|
|
95
|
-
/* -----------------------------------------------------------------------------
|
|
96
|
-
* Component API
|
|
97
|
-
* -----------------------------------------------------------------------------*/
|
|
98
|
-
|
|
99
|
-
export interface PanelProps {
|
|
100
|
-
id: PanelId
|
|
101
|
-
snapSize?: number
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
export interface ResizeTriggerProps {
|
|
105
|
-
id: `${PanelId}:${PanelId}`
|
|
106
|
-
step?: number
|
|
107
|
-
disabled?: boolean
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
export interface ResizeTriggerState {
|
|
111
|
-
disabled: boolean
|
|
112
|
-
focused: boolean
|
|
113
|
-
panelIds: string[]
|
|
114
|
-
min: number | undefined
|
|
115
|
-
max: number | undefined
|
|
116
|
-
value: number
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export interface PanelBounds {
|
|
120
|
-
min: number
|
|
121
|
-
max: number
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
export interface MachineApi<T extends PropTypes = PropTypes> {
|
|
125
|
-
/**
|
|
126
|
-
* Whether the splitter is focused.
|
|
127
|
-
*/
|
|
128
|
-
focused: boolean
|
|
129
|
-
/**
|
|
130
|
-
* Whether the splitter is being dragged.
|
|
131
|
-
*/
|
|
132
|
-
dragging: boolean
|
|
133
|
-
/**
|
|
134
|
-
* The bounds of the currently dragged splitter handle.
|
|
135
|
-
*/
|
|
136
|
-
bounds: PanelBounds | undefined
|
|
137
|
-
/**
|
|
138
|
-
* Function to set a panel to its minimum size.
|
|
139
|
-
*/
|
|
140
|
-
setToMinSize(id: PanelId): void
|
|
141
|
-
/**
|
|
142
|
-
* Function to set a panel to its maximum size.
|
|
143
|
-
*/
|
|
144
|
-
setToMaxSize(id: PanelId): void
|
|
145
|
-
/**
|
|
146
|
-
* Function to set the size of a panel.
|
|
147
|
-
*/
|
|
148
|
-
setSize(id: PanelId, size: number): void
|
|
149
|
-
/**
|
|
150
|
-
* Returns the state details for a resize trigger.
|
|
151
|
-
*/
|
|
152
|
-
getResizeTriggerState(props: ResizeTriggerProps): ResizeTriggerState
|
|
153
|
-
getRootProps(): T["element"]
|
|
154
|
-
getPanelProps(props: PanelProps): T["element"]
|
|
155
|
-
getResizeTriggerProps(props: ResizeTriggerProps): T["element"]
|
|
156
|
-
}
|