Skip to content

Commit d506a84

Browse files
committed
fix d&d flicker on column reorder
1 parent 64a30fc commit d506a84

1 file changed

Lines changed: 39 additions & 49 deletions

File tree

source/src/components/InfiniteTable/hooks/useColumnPointerEvents.ts

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as React from 'react';
22
import { useCallback, useState } from 'react';
3+
import { flushSync } from 'react-dom';
34
import { useInfiniteTableSelector } from './useInfiniteTableSelector';
45

56
import {
@@ -196,14 +197,6 @@ export const useColumnPointerEvents = ({
196197
const currentComputedColumnOrder = getComputed().computedColumnOrder;
197198

198199
if (JSON.stringify(columnOrder, currentComputedColumnOrder)) {
199-
computedVisibleColumns.forEach((col) => {
200-
setInfiniteColumnOffsetWhileReordering(
201-
col.computedVisibleIndex,
202-
'',
203-
rootRef.current,
204-
);
205-
});
206-
207200
// componentActions.columnOrder = columnOrder;
208201
// we can't simply do the above line
209202
// as it would discard non visible columns from the column order
@@ -283,7 +276,8 @@ export const useColumnPointerEvents = ({
283276
requestAnimationFrame(() => {
284277
const { multiSortBehavior } = getState();
285278
const target = domRef.current!;
286-
rootRef.current?.classList.remove(InfiniteClsShiftingColumns);
279+
const rootNode = rootRef.current;
280+
rootNode?.classList.remove(InfiniteClsShiftingColumns);
287281

288282
dragger.stop();
289283

@@ -293,34 +287,6 @@ export const useColumnPointerEvents = ({
293287
discardAlwaysRenderedColumn?.();
294288
restoreRenderRange?.();
295289

296-
computedVisibleColumns.forEach((col) => {
297-
clearInfiniteColumnReorderDuration(
298-
col.computedVisibleIndex,
299-
rootRef.current,
300-
);
301-
setInfiniteColumnVisibility(
302-
col.computedVisibleIndex,
303-
'',
304-
rootRef.current,
305-
);
306-
setInfiniteColumnOffsetWhileReordering(
307-
col.computedVisibleIndex,
308-
'',
309-
rootRef.current,
310-
);
311-
});
312-
313-
// setInfiniteColumnVisibility(dragColumnIndex, '', rootRef.current);
314-
315-
setInfiniteColumnZIndex(
316-
dragColumnIndex,
317-
getColumnZIndex(dragColumn, {
318-
pinnedStartColsCount: computedPinnedStartColumns.length,
319-
visibleColsCount: computedVisibleColumns.length,
320-
}),
321-
rootRef.current,
322-
);
323-
324290
target.style.cursor = initialCursor as string;
325291
target.releasePointerCapture(pointerId);
326292

@@ -341,20 +307,44 @@ export const useColumnPointerEvents = ({
341307
});
342308
}
343309

344-
if (reorderDragResult) {
345-
persistColumnOrder(reorderDragResult);
346-
} else if (didDragAtLeastOnce) {
347-
if (allowColumnHideOnDrag) {
348-
// the column was dropped outside
349-
// so we need to hide it
350-
api.setVisibilityForColumn(dragColumn.id, false);
310+
// Make sure state commit happens before we clear drag-only CSS vars.
311+
flushSync(() => {
312+
if (reorderDragResult) {
313+
persistColumnOrder(reorderDragResult);
314+
} else if (didDragAtLeastOnce) {
315+
if (allowColumnHideOnDrag) {
316+
// the column was dropped outside
317+
// so we need to hide it
318+
api.setVisibilityForColumn(dragColumn.id, false);
319+
}
351320
}
352-
}
321+
actions.columnReorderDragColumnId = false;
322+
if (horizontalLayoutPageIndex != null) {
323+
actions.columnReorderInPageIndex = null;
324+
}
325+
});
353326

354-
actions.columnReorderDragColumnId = false;
355-
if (horizontalLayoutPageIndex != null) {
356-
actions.columnReorderInPageIndex = null;
357-
}
327+
computedVisibleColumns.forEach((col) => {
328+
clearInfiniteColumnReorderDuration(
329+
col.computedVisibleIndex,
330+
rootNode,
331+
);
332+
setInfiniteColumnVisibility(col.computedVisibleIndex, '', rootNode);
333+
setInfiniteColumnOffsetWhileReordering(
334+
col.computedVisibleIndex,
335+
'',
336+
rootNode,
337+
);
338+
});
339+
340+
setInfiniteColumnZIndex(
341+
dragColumnIndex,
342+
getColumnZIndex(dragColumn, {
343+
pinnedStartColsCount: computedPinnedStartColumns.length,
344+
visibleColsCount: computedVisibleColumns.length,
345+
}),
346+
rootNode,
347+
);
358348
});
359349
};
360350

0 commit comments

Comments
 (0)