-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathsplitter.connect.ts
179 lines (167 loc) · 5.59 KB
/
splitter.connect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { getEventKey, getEventStep, type EventKeyMap } from "@zag-js/dom-event"
import { dataAttr } from "@zag-js/dom-query"
import type { NormalizeProps, PropTypes } from "@zag-js/types"
import { parts } from "./splitter.anatomy"
import { dom } from "./splitter.dom"
import type { MachineApi, ResizeTriggerProps, ResizeTriggerState, Send, State } from "./splitter.types"
import { getHandleBounds } from "./splitter.utils"
export function connect<T extends PropTypes>(state: State, send: Send, normalize: NormalizeProps<T>): MachineApi<T> {
const horizontal = state.context.isHorizontal
const focused = state.hasTag("focus")
const dragging = state.matches("dragging")
const panels = state.context.panels
function getResizeTriggerState(props: ResizeTriggerProps): ResizeTriggerState {
const { id, disabled } = props
const ids = id.split(":")
const panelIds = ids.map((id) => dom.getPanelId(state.context, id))
const panels = getHandleBounds(state.context, id)
return {
disabled: !!disabled,
focused: state.context.activeResizeId === id && focused,
panelIds,
min: panels?.min,
max: panels?.max,
value: 0,
}
}
return {
focused: focused,
dragging: dragging,
getResizeTriggerState,
bounds: getHandleBounds(state.context),
setToMinSize(id) {
const panel = panels.find((panel) => panel.id === id)
send({ type: "SET_PANEL_SIZE", id, size: panel?.minSize, src: "setToMinSize" })
},
setToMaxSize(id) {
const panel = panels.find((panel) => panel.id === id)
send({ type: "SET_PANEL_SIZE", id, size: panel?.maxSize, src: "setToMaxSize" })
},
setSize(id, size) {
send({ type: "SET_PANEL_SIZE", id, size })
},
getRootProps() {
return normalize.element({
...parts.root.attrs,
"data-orientation": state.context.orientation,
id: dom.getRootId(state.context),
dir: state.context.dir,
style: {
display: "flex",
flexDirection: horizontal ? "row" : "column",
height: "100%",
width: "100%",
overflow: "hidden",
},
})
},
getPanelProps(props) {
const { id } = props
return normalize.element({
...parts.panel.attrs,
"data-orientation": state.context.orientation,
dir: state.context.dir,
id: dom.getPanelId(state.context, id),
"data-ownedby": dom.getRootId(state.context),
style: dom.getPanelStyle(state.context, id),
})
},
getResizeTriggerProps(props) {
const { id, disabled, step = 1 } = props
const triggerState = getResizeTriggerState(props)
return normalize.element({
...parts.resizeTrigger.attrs,
dir: state.context.dir,
id: dom.getResizeTriggerId(state.context, id),
role: "separator",
"data-ownedby": dom.getRootId(state.context),
tabIndex: disabled ? undefined : 0,
"aria-valuenow": triggerState.value,
"aria-valuemin": triggerState.min,
"aria-valuemax": triggerState.max,
"data-orientation": state.context.orientation,
"aria-orientation": state.context.orientation,
"aria-controls": triggerState.panelIds.join(" "),
"data-focus": dataAttr(triggerState.focused),
"data-disabled": dataAttr(disabled),
style: {
touchAction: "none",
userSelect: "none",
WebkitUserSelect: "none",
flex: "0 0 auto",
pointerEvents: dragging && !triggerState.focused ? "none" : undefined,
cursor: horizontal ? "col-resize" : "row-resize",
[horizontal ? "minHeight" : "minWidth"]: "0",
},
onPointerDown(event) {
if (disabled) {
event.preventDefault()
return
}
send({ type: "POINTER_DOWN", id })
event.currentTarget.setPointerCapture(event.pointerId)
event.preventDefault()
event.stopPropagation()
},
onPointerUp(event) {
if (disabled) return
if (event.currentTarget.hasPointerCapture(event.pointerId)) {
event.currentTarget.releasePointerCapture(event.pointerId)
}
},
onPointerOver() {
if (disabled) return
send({ type: "POINTER_OVER", id })
},
onPointerLeave() {
if (disabled) return
send({ type: "POINTER_LEAVE", id })
},
onBlur() {
send("BLUR")
},
onFocus() {
send({ type: "FOCUS", id })
},
onDoubleClick() {
if (disabled) return
send({ type: "DOUBLE_CLICK", id })
},
onKeyDown(event) {
if (event.defaultPrevented) return
if (disabled) return
const moveStep = getEventStep(event) * step
const keyMap: EventKeyMap = {
Enter() {
send("ENTER")
},
ArrowUp() {
send({ type: "ARROW_UP", step: moveStep })
},
ArrowDown() {
send({ type: "ARROW_DOWN", step: moveStep })
},
ArrowLeft() {
send({ type: "ARROW_LEFT", step: moveStep })
},
ArrowRight() {
send({ type: "ARROW_RIGHT", step: moveStep })
},
Home() {
send("HOME")
},
End() {
send("END")
},
}
const key = getEventKey(event, state.context)
const exec = keyMap[key]
if (exec) {
exec(event)
event.preventDefault()
}
},
})
},
}
}