forked from mui/mui-x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
useColumnResize.tsx
281 lines (233 loc) · 9.17 KB
/
useColumnResize.tsx
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
import * as React from 'react';
import { ownerDocument } from '@material-ui/core/utils';
import { ColDef } from '../../models/colDef';
import { useLogger } from '../utils';
import { useEventCallback } from '../../utils/material-ui-utils';
import { COL_RESIZE_START, COL_RESIZE_STOP } from '../../constants/eventsConstants';
import {
HEADER_CELL_CSS_CLASS,
HEADER_CELL_SEPARATOR_RESIZABLE_CSS_CLASS,
} from '../../constants/cssClassesConstants';
import {
findCellElementsFromCol,
findParentElementFromClassName,
getFieldFromHeaderElem,
findHeaderElementFromField,
} from '../../utils/domUtils';
import { ApiRef } from '../../models';
import { CursorCoordinates } from '../../models/api/columnReorderApi';
const MIN_COL_WIDTH = 50;
let cachedSupportsTouchActionNone = false;
// TODO: remove support for Safari < 13.
// https://caniuse.com/#search=touch-action
//
// Safari, on iOS, supports touch action since v13.
// Over 80% of the iOS phones are compatible
// in August 2020.
function doesSupportTouchActionNone(): boolean {
if (!cachedSupportsTouchActionNone) {
const element = document.createElement('div');
element.style.touchAction = 'none';
document.body.appendChild(element);
cachedSupportsTouchActionNone = window.getComputedStyle(element).touchAction === 'none';
element.parentElement!.removeChild(element);
}
return cachedSupportsTouchActionNone;
}
function trackFinger(event, currentTouchId): CursorCoordinates | boolean {
if (currentTouchId !== undefined && event.changedTouches) {
for (let i = 0; i < event.changedTouches.length; i += 1) {
const touch = event.changedTouches[i];
if (touch.identifier === currentTouchId) {
return {
x: touch.clientX,
y: touch.clientY,
};
}
}
return false;
}
return {
x: event.clientX,
y: event.clientY,
};
}
// TODO improve experience for last column
export const useColumnResize = (columnsRef: React.RefObject<HTMLDivElement>, apiRef: ApiRef) => {
const logger = useLogger('useColumnResize');
const colDefRef = React.useRef<ColDef>();
const colElementRef = React.useRef<HTMLDivElement>();
const colCellElementsRef = React.useRef<NodeListOf<Element>>();
const initialOffset = React.useRef<number>();
const stopResizeEventTimeout = React.useRef<number>();
const touchId = React.useRef<number>();
const columnsHeaderElement = columnsRef.current;
const updateWidth = (newWidth: number) => {
logger.debug(`Updating width to ${newWidth} for col ${colDefRef.current!.field}`);
colDefRef.current!.width = newWidth;
colElementRef.current!.style.width = `${newWidth}px`;
colElementRef.current!.style.minWidth = `${newWidth}px`;
colElementRef.current!.style.maxWidth = `${newWidth}px`;
colCellElementsRef.current!.forEach((element) => {
const div = element as HTMLDivElement;
div.style.width = `${newWidth}px`;
div.style.minWidth = `${newWidth}px`;
div.style.maxWidth = `${newWidth}px`;
});
};
const handleResizeMouseUp = useEventCallback(() => {
// eslint-disable-next-line @typescript-eslint/no-use-before-define
stopListening();
apiRef.current!.updateColumn(colDefRef.current as ColDef);
clearTimeout(stopResizeEventTimeout.current);
stopResizeEventTimeout.current = setTimeout(() => {
apiRef.current.publishEvent(COL_RESIZE_STOP);
});
logger.debug(
`Updating col ${colDefRef.current!.field} with new width: ${colDefRef.current!.width}`,
);
});
const handleResizeMouseMove = useEventCallback((nativeEvent) => {
// Cancel move in case some other element consumed a mouseup event and it was not fired.
if (nativeEvent.buttons === 0) {
handleResizeMouseUp();
return;
}
let newWidth =
initialOffset.current +
nativeEvent.clientX -
colElementRef.current!.getBoundingClientRect().left;
newWidth = Math.max(MIN_COL_WIDTH, newWidth);
updateWidth(newWidth);
});
const handleMouseDown = useEventCallback((event: React.MouseEvent<HTMLDivElement>) => {
// Only handle left clicks
if (event.button !== 0) {
return;
}
// Skip if the column isn't resizable
if (!event.currentTarget.classList.contains('MuiDataGrid-columnSeparatorResizable')) {
return;
}
// Avoid text selection
event.preventDefault();
colElementRef.current = findParentElementFromClassName(
event.currentTarget,
HEADER_CELL_CSS_CLASS,
) as HTMLDivElement;
const field = colElementRef.current.getAttribute('data-field') as string;
const colDef = apiRef.current.getColumnFromField(field);
logger.debug(`Start Resize on col ${colDef.field}`);
apiRef.current.publishEvent(COL_RESIZE_START, { field });
colDefRef.current = colDef;
colElementRef.current = columnsHeaderElement!.querySelector(
`[data-field="${colDef.field}"]`,
) as HTMLDivElement;
colCellElementsRef.current = findCellElementsFromCol(colElementRef.current) as NodeListOf<
Element
>;
const doc = ownerDocument(apiRef.current.rootElementRef!.current as HTMLElement);
doc.body.style.cursor = 'col-resize';
initialOffset.current =
(colDefRef.current.width as number) -
(event.clientX - colElementRef.current!.getBoundingClientRect().left);
doc.addEventListener('mousemove', handleResizeMouseMove);
doc.addEventListener('mouseup', handleResizeMouseUp);
});
const handleTouchEnd = useEventCallback((nativeEvent) => {
const finger = trackFinger(nativeEvent, touchId.current);
if (!finger) {
return;
}
// eslint-disable-next-line @typescript-eslint/no-use-before-define
stopListening();
apiRef.current!.updateColumn(colDefRef.current as ColDef);
clearTimeout(stopResizeEventTimeout.current);
stopResizeEventTimeout.current = setTimeout(() => {
apiRef.current.publishEvent(COL_RESIZE_STOP);
});
logger.debug(
`Updating col ${colDefRef.current!.field} with new width: ${colDefRef.current!.width}`,
);
});
const handleTouchMove = useEventCallback((nativeEvent) => {
const finger = trackFinger(nativeEvent, touchId.current);
if (!finger) {
return;
}
// Cancel move in case some other element consumed a touchmove event and it was not fired.
if (nativeEvent.type === 'mousemove' && nativeEvent.buttons === 0) {
handleTouchEnd(nativeEvent);
return;
}
let newWidth =
initialOffset.current! +
(finger as CursorCoordinates).x -
colElementRef.current!.getBoundingClientRect().left;
newWidth = Math.max(MIN_COL_WIDTH, newWidth);
updateWidth(newWidth);
});
const handleTouchStart = useEventCallback((event) => {
const cellSeparator = findParentElementFromClassName(
event.target,
HEADER_CELL_SEPARATOR_RESIZABLE_CSS_CLASS,
);
// Let the event bubble if the target is not a col separator
if (!cellSeparator) return;
// If touch-action: none; is not supported we need to prevent the scroll manually.
if (!doesSupportTouchActionNone()) {
event.preventDefault();
}
const touch = event.changedTouches[0];
if (touch != null) {
// A number that uniquely identifies the current finger in the touch session.
touchId.current = touch.identifier;
}
colElementRef.current = findParentElementFromClassName(
event.target,
HEADER_CELL_CSS_CLASS,
) as HTMLDivElement;
const field = getFieldFromHeaderElem(colElementRef.current!);
const colDef = apiRef.current.getColumnFromField(field);
logger.debug(`Start Resize on col ${colDef.field}`);
apiRef.current.publishEvent(COL_RESIZE_START, { field });
colDefRef.current = colDef;
colElementRef.current = findHeaderElementFromField(
columnsHeaderElement!,
colDef.field,
) as HTMLDivElement;
colCellElementsRef.current = findCellElementsFromCol(colElementRef.current) as NodeListOf<
Element
>;
initialOffset.current =
(colDefRef.current.width as number) -
(touch.clientX - colElementRef.current!.getBoundingClientRect().left);
const doc = ownerDocument(event.currentTarget as HTMLElement);
doc.addEventListener('touchmove', handleTouchMove);
doc.addEventListener('touchend', handleTouchEnd);
});
const stopListening = React.useCallback(() => {
const doc = ownerDocument(apiRef.current.rootElementRef!.current as HTMLElement);
doc.body.style.removeProperty('cursor');
doc.removeEventListener('mousemove', handleResizeMouseMove);
doc.removeEventListener('mouseup', handleResizeMouseUp);
doc.removeEventListener('touchmove', handleTouchMove);
doc.removeEventListener('touchend', handleTouchEnd);
}, [apiRef, handleResizeMouseMove, handleResizeMouseUp, handleTouchMove, handleTouchEnd]);
React.useEffect(() => {
columnsHeaderElement?.addEventListener('touchstart', handleTouchStart, {
passive: doesSupportTouchActionNone(),
});
return () => {
columnsHeaderElement?.removeEventListener('touchstart', handleTouchStart);
clearTimeout(stopResizeEventTimeout.current);
stopListening();
};
}, [columnsHeaderElement, handleTouchStart, stopListening]);
return React.useMemo(
() => ({
onMouseDown: handleMouseDown,
}),
[handleMouseDown],
);
};