Skip to content

Commit a6f61d9

Browse files
committed
Refactor splitter drag functionality to restore cursor and user select styles
1 parent 5e9d9cf commit a6f61d9

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

packages/web/src/utility/splitterDrag.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
export default function splitterDrag(node, axes) {
22
let resizeStart = null;
3+
let prevCursor = null;
4+
let prevUserSelect = null;
35

46
const handleResizeDown = e => {
57
if (e.button !== 0) return;
68
e.preventDefault();
79
resizeStart = e[axes];
10+
prevCursor = document.body.style.cursor;
11+
prevUserSelect = document.body.style.userSelect;
812
document.addEventListener('mousemove', handleResizeMove, true);
913
document.addEventListener('mouseup', handleResizeEnd, true);
1014
document.body.style.cursor = axes === 'clientX' ? 'col-resize' : 'row-resize';
@@ -21,13 +25,20 @@ export default function splitterDrag(node, axes) {
2125
})
2226
);
2327
};
28+
29+
const restoreStyles = () => {
30+
document.body.style.cursor = prevCursor;
31+
document.body.style.userSelect = prevUserSelect;
32+
prevCursor = null;
33+
prevUserSelect = null;
34+
};
35+
2436
const handleResizeEnd = e => {
2537
e.preventDefault();
2638
resizeStart = null;
2739
document.removeEventListener('mousemove', handleResizeMove, true);
2840
document.removeEventListener('mouseup', handleResizeEnd, true);
29-
document.body.style.cursor = '';
30-
document.body.style.userSelect = '';
41+
restoreStyles();
3142
};
3243

3344
node.addEventListener('mousedown', handleResizeDown);
@@ -38,8 +49,7 @@ export default function splitterDrag(node, axes) {
3849
if (resizeStart != null) {
3950
document.removeEventListener('mousemove', handleResizeMove, true);
4051
document.removeEventListener('mouseup', handleResizeEnd, true);
41-
document.body.style.cursor = '';
42-
document.body.style.userSelect = '';
52+
restoreStyles();
4353
}
4454
},
4555
};

0 commit comments

Comments
 (0)