Skip to content

Commit

Permalink
fix 'jiggling' issues and debounce dragOver
Browse files Browse the repository at this point in the history
  • Loading branch information
bsholmes committed Jan 16, 2024
1 parent c140013 commit a8831c6
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/components/Table/GridTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ export function GridTable<R extends Kinded, X extends Only<GridTableXss, X> = an
evt.preventDefault();

// set flags for css spacer
recursiveSetDraggedOver(rows, DraggedOver.None);
// don't set none for the row we are entering
recursiveSetDraggedOver(rows.filter((r) => r.id !== row.id), DraggedOver.None);

if (draggedRowRef.current) {
if (draggedRowRef.current.id === row.id) {
Expand All @@ -373,7 +374,8 @@ export function GridTable<R extends Kinded, X extends Only<GridTableXss, X> = an
evt.preventDefault();

if (draggedRowRef.current) {
if (draggedRowRef.current.id === row.id) {
if (draggedRowRef.current.id === row.id || !evt.currentTarget) {
tableState.maybeSetRowDraggedOver(row.id, DraggedOver.None, draggedRowRef.current);
return;
}

Expand Down
24 changes: 19 additions & 5 deletions src/components/Table/components/Row.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { observer } from "mobx-react";
import { ReactElement, useContext, useRef } from "react";
import { ReactElement, useContext, useRef, useCallback } from "react";
import {
defaultRenderFn,
headerRenderFn,
Expand Down Expand Up @@ -31,6 +31,7 @@ import {
import { Css, Palette } from "src/Css";
import { AnyObject } from "src/types";
import { isFunction } from "src/utils";
import { useDebouncedCallback } from "use-debounce";

interface RowProps<R extends Kinded> {
as: RenderAs;
Expand Down Expand Up @@ -94,8 +95,8 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {

const containerCss = {
...Css.add("transition", "padding 0.25s ease-in-out").$,
...(rs.isDraggedOver === DraggedOver.Above && Css.ptPx(35).$),
...(rs.isDraggedOver === DraggedOver.Below && Css.pbPx(35).$),
...(rs.isDraggedOver === DraggedOver.Above && Css.ptPx(25).$),
...(rs.isDraggedOver === DraggedOver.Below && Css.pbPx(25).$),
};

const rowCss = {
Expand Down Expand Up @@ -133,6 +134,14 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
// used to render the whole row when dragging with the handle
const ref = useRef<HTMLTableRowElement>(null);

// debounce drag over callback to avoid excessive re-renders
const dragOverCallback = useCallback(
onDragOver ?? (() => {}),
[row],
);
// when the event is not called, we still need to call preventDefault
const onDragOverDebounced = useDebouncedCallback(dragOverCallback, 100);

const RowContent = () => (
<RowTag css={rowCss} {...others} data-gridrow {...getCount(row.id)}>
{isKeptGroupRow ? (
Expand Down Expand Up @@ -201,7 +210,7 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
onDragEnd,
onDrop,
onDragEnter,
onDragOver,
onDragOver: onDragOverDebounced,
});

// Only use the `numExpandedColumns` as the `colspan` when rendering the "Expandable Header"
Expand Down Expand Up @@ -354,7 +363,12 @@ function RowImpl<R extends Kinded, S>(props: RowProps<R>): ReactElement {
// and spacer styling
onDrop={(evt) => onDrop?.(row, evt)}
onDragEnter={(evt) => onDragEnter?.(row, evt)}
onDragOver={(evt) => onDragOver?.(row, evt)}
onDragOver={(evt) => {
// when the event isn't called due to debounce, we still need to
// call preventDefault for the drop event to fire
evt.preventDefault();
onDragOverDebounced(row, evt);
}}
ref={ref}
>
{RowContent()}
Expand Down
1 change: 1 addition & 0 deletions src/components/Table/utils/RowStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ export class RowStates<R extends Kinded> {
}

// this allows a single-row re-render
if(rs.isDraggedOver === draggedOver) return;
rs.isDraggedOver = draggedOver;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/Table/utils/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ export function isCursorBelowMidpoint(target: HTMLElement, clientY: number) {
const style = window.getComputedStyle(target);
const rect = target.getBoundingClientRect();

const pt = parseInt(style.getPropertyValue("padding-top")) / 2;
const pb = parseInt(style.getPropertyValue("padding-bottom"));
let pt = parseFloat(style.getPropertyValue("padding-top"));
const pb = parseFloat(style.getPropertyValue("padding-bottom"));

return clientY > rect.top + pt + (rect.height - pb) / 2;
}
Expand Down

0 comments on commit a8831c6

Please sign in to comment.