-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
resize.ts
306 lines (271 loc) · 8.88 KB
/
resize.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import {
useCallback,
useEffect,
useLayoutEffect,
useMemo,
useRef,
useState,
} from 'react';
import { useStorageContext } from '../storage';
import debounce from './debounce';
type ResizableElement = 'first' | 'second';
type UseDragResizeArgs = {
/**
* Set the default sizes for the two resizable halves by passing their ratio
* (first divided by second).
*/
defaultSizeRelation?: number;
/**
* The direction in which the two halves should be resizable.
*/
direction: 'horizontal' | 'vertical';
/**
* Choose one of the two halves that should initially be hidden.
*/
initiallyHidden?: ResizableElement;
/**
* Invoked when the visibility of one of the halves changes.
* @param hiddenElement The element that is now hidden after the change
* (`null` if both are visible).
*/
onHiddenElementChange?(hiddenElement: ResizableElement | null): void;
/**
* The minimum width in pixels for the first half. If it is resized to a
* width smaller than this threshold, the half will be hidden.
*/
sizeThresholdFirst?: number;
/**
* The minimum width in pixels for the second half. If it is resized to a
* width smaller than this threshold, the half will be hidden.
*/
sizeThresholdSecond?: number;
/**
* A key for which the state of resizing is persisted in storage (if storage
* is available).
*/
storageKey?: string;
};
export function useDragResize({
defaultSizeRelation = DEFAULT_FLEX,
direction,
initiallyHidden,
onHiddenElementChange,
sizeThresholdFirst = 100,
sizeThresholdSecond = 100,
storageKey,
}: UseDragResizeArgs) {
const storage = useStorageContext();
const store = useMemo(
() =>
debounce(500, (value: string) => {
if (storage && storageKey) {
storage.set(storageKey, value);
}
}),
[storage, storageKey],
);
const [hiddenElement, setHiddenElement] = useState<ResizableElement | null>(
() => {
const storedValue =
storage && storageKey ? storage.get(storageKey) : null;
if (storedValue === HIDE_FIRST || initiallyHidden === 'first') {
return 'first';
}
if (storedValue === HIDE_SECOND || initiallyHidden === 'second') {
return 'second';
}
return null;
},
);
const setHiddenElementWithCallback = useCallback(
(element: ResizableElement | null) => {
if (element !== hiddenElement) {
setHiddenElement(element);
onHiddenElementChange?.(element);
}
},
[hiddenElement, onHiddenElementChange],
);
const firstRef = useRef<HTMLDivElement>(null);
const dragBarRef = useRef<HTMLDivElement>(null);
const secondRef = useRef<HTMLDivElement>(null);
const defaultFlexRef = useRef(`${defaultSizeRelation}`);
/**
* Set initial flex values
*/
useLayoutEffect(() => {
const storedValue =
storage && storageKey
? storage.get(storageKey) || defaultFlexRef.current
: defaultFlexRef.current;
const flexDirection = direction === 'horizontal' ? 'row' : 'column';
if (firstRef.current) {
firstRef.current.style.display = 'flex';
firstRef.current.style.flexDirection = flexDirection;
firstRef.current.style.flex =
storedValue === HIDE_FIRST || storedValue === HIDE_SECOND
? defaultFlexRef.current
: storedValue;
}
if (secondRef.current) {
secondRef.current.style.display = 'flex';
secondRef.current.style.flexDirection = flexDirection;
secondRef.current.style.flex = '1';
}
if (dragBarRef.current) {
dragBarRef.current.style.display = 'flex';
dragBarRef.current.style.flexDirection = flexDirection;
}
}, [direction, storage, storageKey]);
const hide = useCallback((resizableElement: ResizableElement) => {
const element =
resizableElement === 'first' ? firstRef.current : secondRef.current;
if (!element) {
return;
}
// We hide elements off screen because of codemirror. If the page is loaded
// and the codemirror container would have zero width, the layout isn't
// instant pretty. By always giving the editor some width we avoid any
// layout shifts when the editor reappears.
element.style.left = '-1000px';
element.style.position = 'absolute';
element.style.opacity = '0';
element.style.height = '500px';
element.style.width = '500px';
// Make sure that the flex value of the first item is at least equal to one
// so that the entire space of the parent element is filled up
if (firstRef.current) {
const flex = parseFloat(firstRef.current.style.flex);
if (!Number.isFinite(flex) || flex < 1) {
firstRef.current.style.flex = '1';
}
firstRef.current.style.flex;
}
}, []);
const show = useCallback(
(resizableElement: ResizableElement) => {
const element =
resizableElement === 'first' ? firstRef.current : secondRef.current;
if (!element) {
return;
}
element.style.width = '';
element.style.height = '';
element.style.opacity = '';
element.style.position = '';
element.style.left = '';
if (firstRef.current && storage && storageKey) {
const storedValue = storage?.get(storageKey);
if (storedValue !== HIDE_FIRST && storedValue !== HIDE_SECOND) {
firstRef.current.style.flex = storedValue || defaultFlexRef.current;
}
}
},
[storage, storageKey],
);
/**
* Hide and show items when state changes
*/
useLayoutEffect(() => {
if (hiddenElement === 'first') {
hide('first');
} else {
show('first');
}
if (hiddenElement === 'second') {
hide('second');
} else {
show('second');
}
}, [hiddenElement, hide, show]);
useEffect(() => {
if (!dragBarRef.current || !firstRef.current || !secondRef.current) {
return;
}
const dragBarContainer = dragBarRef.current;
const firstContainer = firstRef.current;
const wrapper = firstContainer.parentElement!;
const eventProperty = direction === 'horizontal' ? 'clientX' : 'clientY';
const rectProperty = direction === 'horizontal' ? 'left' : 'top';
const adjacentRectProperty =
direction === 'horizontal' ? 'right' : 'bottom';
const sizeProperty =
direction === 'horizontal' ? 'clientWidth' : 'clientHeight';
function handleMouseDown(downEvent: MouseEvent) {
downEvent.preventDefault();
// Distance between the start of the drag bar and the exact point where
// the user clicked on the drag bar.
const offset =
downEvent[eventProperty] -
dragBarContainer.getBoundingClientRect()[rectProperty];
function handleMouseMove(moveEvent: MouseEvent) {
if (moveEvent.buttons === 0) {
return handleMouseUp();
}
const firstSize =
moveEvent[eventProperty] -
wrapper.getBoundingClientRect()[rectProperty] -
offset;
const secondSize =
wrapper.getBoundingClientRect()[adjacentRectProperty] -
moveEvent[eventProperty] +
offset -
dragBarContainer[sizeProperty];
if (firstSize < sizeThresholdFirst) {
// Hide the first display
setHiddenElementWithCallback('first');
store(HIDE_FIRST);
} else if (secondSize < sizeThresholdSecond) {
// Hide the second display
setHiddenElementWithCallback('second');
store(HIDE_SECOND);
} else {
// Show both and adjust the flex value of the first one (the flex
// value for the second one is always `1`)
setHiddenElementWithCallback(null);
const newFlex = `${firstSize / secondSize}`;
firstContainer.style.flex = newFlex;
store(newFlex);
}
}
function handleMouseUp() {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
}
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
}
dragBarContainer.addEventListener('mousedown', handleMouseDown);
function reset() {
if (firstRef.current) {
firstRef.current.style.flex = defaultFlexRef.current;
}
store(defaultFlexRef.current);
setHiddenElementWithCallback(null);
}
dragBarContainer.addEventListener('dblclick', reset);
return () => {
dragBarContainer.removeEventListener('mousedown', handleMouseDown);
dragBarContainer.removeEventListener('dblclick', reset);
};
}, [
direction,
setHiddenElementWithCallback,
sizeThresholdFirst,
sizeThresholdSecond,
store,
]);
return useMemo(
() => ({
dragBarRef,
hiddenElement,
firstRef,
setHiddenElement,
secondRef,
}),
[hiddenElement, setHiddenElement],
);
}
const DEFAULT_FLEX = 1;
const HIDE_FIRST = 'hide-first';
const HIDE_SECOND = 'hide-second';