@@ -124,37 +124,30 @@ function isLastChildVisible(container) {
124
124
}
125
125
126
126
function SplitPane ( { messageRef, topElement, bottomElement} ) {
127
- let [ dragBase , setDragBase ] = useState ( Number . NaN )
127
+ let [ dragOffset , setDragOffset ] = useState ( Number . NaN )
128
128
let [ splitPos , setSplitPos ] = useState ( 200 )
129
- let draggingRef = useRef ( )
129
+ let splitPosRef = useRef ( )
130
+ splitPosRef . current = splitPos
130
131
let containerRef = useRef ( )
131
- draggingRef . current = dragBase
132
132
useEffect ( ( ) => {
133
- let mousemove = ( e ) => {
134
- if ( ! draggingRef . current ) {
133
+ let onMouseMove = ( e ) => {
134
+ if ( Number . isNaN ( dragOffset ) ) {
135
135
return
136
136
}
137
- setSplitPos ( getSplitPos ( e . clientY , containerRef . current ) )
137
+ setSplitPos ( getSplitPos ( e . clientY + dragOffset , containerRef . current ) )
138
138
}
139
- let mouseup = ( e ) => {
140
- if ( ! draggingRef . current ) {
141
- return
142
- }
143
- setSplitPos ( getSplitPos ( e . clientY , containerRef . current ) )
144
- setDragBase ( Number . NaN )
145
- }
146
- window . document . addEventListener ( "mousemove" , mousemove )
147
- window . document . addEventListener ( "mouseup" , mouseup )
139
+ let onMouseUp = ( ) => setDragOffset ( Number . NaN )
140
+ window . document . addEventListener ( "mousemove" , onMouseMove )
141
+ window . document . addEventListener ( "mouseup" , onMouseUp )
148
142
return ( ) => {
149
- window . document . removeEventListener ( "mousemove" , mousemove )
150
- window . document . removeEventListener ( "mouseup" , mouseup )
143
+ window . document . removeEventListener ( "mousemove" , onMouseMove )
144
+ window . document . removeEventListener ( "mouseup" , onMouseUp )
151
145
}
152
- } , [ draggingRef , setDragBase ] )
146
+ } , [ dragOffset , setDragOffset ] )
153
147
let onMouseDown = useCallback ( ( e ) => {
154
148
e . preventDefault ( )
155
- setSplitPos ( getSplitPos ( e . clientY , containerRef . current ) )
156
- setDragBase ( e . clientY )
157
- } , [ setDragBase , setSplitPos ] )
149
+ setDragOffset ( splitPosRef . current - e . clientY )
150
+ } , [ setDragOffset ] )
158
151
let topElementHeight = Math . trunc ( splitPos )
159
152
if ( containerRef . current ) {
160
153
let rect = containerRef . current . getBoundingClientRect ( )
@@ -168,13 +161,13 @@ function SplitPane({messageRef, topElement, bottomElement}) {
168
161
}
169
162
if ( ! containerRef . current ) {
170
163
let rect = ref . getBoundingClientRect ( )
171
- setSplitPos ( getSplitPos ( rect . top , ref ) )
164
+ setSplitPos ( getSplitPos ( rect . top + 2.5 * getRemInPixel ( ) , ref ) )
172
165
}
173
166
containerRef . current = ref
174
167
} }
175
168
className = { twJoin (
176
169
"grow border-t border-x border-gray-500 bg-gray-900 flex flex-col overflow-y-hidden" ,
177
- ! Number . isNaN ( dragBase ) && "cursor-row-resize" ,
170
+ ! Number . isNaN ( dragOffset ) && "cursor-row-resize" ,
178
171
) } >
179
172
< div
180
173
style = { { height : topElementHeight + "px" } }
@@ -184,7 +177,7 @@ function SplitPane({messageRef, topElement, bottomElement}) {
184
177
< SplitBar
185
178
container = { containerRef . current }
186
179
splitPos = { splitPos }
187
- dragBase = { dragBase }
180
+ dragOffset = { dragOffset }
188
181
onMouseDown = { onMouseDown } />
189
182
< div ref = { messageRef } className = "px-1 pt-3 pb-1 overflow-y-scroll" >
190
183
{ bottomElement }
@@ -193,7 +186,7 @@ function SplitPane({messageRef, topElement, bottomElement}) {
193
186
)
194
187
}
195
188
196
- function SplitBar ( { splitPos, dragBase , onMouseDown, container} ) {
189
+ function SplitBar ( { splitPos, dragOffset , onMouseDown, container} ) {
197
190
if ( ! container ) {
198
191
return < div />
199
192
}
@@ -204,16 +197,16 @@ function SplitBar({splitPos, dragBase, onMouseDown, container}) {
204
197
let left = rect . left - parentRect . left
205
198
return < >
206
199
< div
207
- onMouseDown = { Number . isNaN ( dragBase ) ? onMouseDown : undefined }
208
- style = { { top : Math . trunc ( splitPos - 1 ) + 0.5 , height : 1 , width : width , left : left } }
200
+ onMouseDown = { Number . isNaN ( dragOffset ) ? onMouseDown : undefined }
201
+ style = { { top : Math . trunc ( splitPos - 1 ) , height : 1 , width : width , left : left } }
209
202
className = "absolute h-[1px] bg-gray-500 z-20 cursor-row-resize" />
210
203
< div
211
- onMouseDown = { Number . isNaN ( dragBase ) ? onMouseDown : undefined }
212
- style = { { top : Math . trunc ( splitPos ) + 0.5 , height : innerHeight , width : width , left : left } }
204
+ onMouseDown = { Number . isNaN ( dragOffset ) ? onMouseDown : undefined }
205
+ style = { { top : Math . trunc ( splitPos ) , height : innerHeight , width : width , left : left } }
213
206
className = "absolute bg-slate-800 z-20 cursor-row-resize" />
214
207
< div
215
- onMouseDown = { Number . isNaN ( dragBase ) ? onMouseDown : undefined }
216
- style = { { top : Math . trunc ( splitPos + innerHeight ) + 0.5 , height : 1 , width : width , left : left } }
208
+ onMouseDown = { Number . isNaN ( dragOffset ) ? onMouseDown : undefined }
209
+ style = { { top : Math . trunc ( splitPos + innerHeight ) , height : 1 , width : width , left : left } }
217
210
className = "absolute h-[1px] bg-gray-500 z-20 cursor-row-resize" />
218
211
</ >
219
212
}
@@ -223,8 +216,8 @@ function getSplitPos(clientY, container) {
223
216
return Math . trunc ( clientY )
224
217
}
225
218
let rect = container . getBoundingClientRect ( )
226
- let safety = 4 * getRemInPixel ( )
219
+ let safety = 1 * getRemInPixel ( )
227
220
let result = Math . max ( rect . top + safety , clientY )
228
- result = Math . min ( rect . top + rect . height - safety , result )
221
+ result = Math . min ( rect . bottom - safety - 5 , result )
229
222
return Math . trunc ( result )
230
223
}
0 commit comments