Skip to content

Commit

Permalink
fix: don't flicker root DropZone when dragging
Browse files Browse the repository at this point in the history
There's a slight delay between the user starting the drag and draggedItem being set, which meant that the root dropzone was being enabled too late after the user would start dragging. This fixes that by using an `onMouseDown` and `onMouseUp` event handler to introduce a new `userWillDrag` state within the current context.
  • Loading branch information
chrisvxd committed Oct 31, 2023
1 parent 2faf90d commit 358435c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/core/components/DraggableComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const DraggableComponent = ({
isSelected = false,
onClick = () => null,
onMount = () => null,
onMouseDown = () => null,
onMouseUp = () => null,
onMouseOver = () => null,
onMouseOut = () => null,
onDelete = () => null,
Expand All @@ -32,6 +34,8 @@ export const DraggableComponent = ({
isSelected?: boolean;
onClick?: (e: SyntheticEvent) => void;
onMount?: () => void;
onMouseDown?: (e: SyntheticEvent) => void;
onMouseUp?: (e: SyntheticEvent) => void;
onMouseOver?: (e: SyntheticEvent) => void;
onMouseOut?: (e: SyntheticEvent) => void;
onDelete?: (e: SyntheticEvent) => void;
Expand Down Expand Up @@ -75,6 +79,8 @@ export const DraggableComponent = ({
}}
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
onClick={onClick}
>
{debug}
Expand Down
14 changes: 12 additions & 2 deletions packages/core/components/DropZone/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CSSProperties, useContext, useEffect } from "react";
import { CSSProperties, useContext, useEffect, useState } from "react";
import { DraggableComponent } from "../DraggableComponent";
import DroppableStrictMode from "../DroppableStrictMode";
import { getItem } from "../../lib/get-item";
Expand Down Expand Up @@ -79,6 +79,8 @@ function DropZoneEdit({ zone, style }: DropZoneProps) {
// we use the index rather than spread to prevent down-level iteration warnings: https://stackoverflow.com/questions/53441292/why-downleveliteration-is-not-on-by-default
const [draggedSourceArea] = getZoneId(draggedSourceId);

const [userWillDrag, setUserWillDrag] = useState(false);

const userIsDragging = !!draggedItem;
const draggingOverArea = userIsDragging && zoneArea === draggedSourceArea;
const draggingNewComponent = draggedSourceId?.startsWith("component-list");
Expand Down Expand Up @@ -108,7 +110,7 @@ function DropZoneEdit({ zone, style }: DropZoneProps) {
: isRootZone;
const hoveringOverZone = hoveringZone === zoneCompound;

let isEnabled = false;
let isEnabled = userWillDrag;

/**
* We enable zones when:
Expand Down Expand Up @@ -234,6 +236,14 @@ function DropZoneEdit({ zone, style }: DropZoneProps) {
});
e.stopPropagation();
}}
onMouseDown={(e) => {
e.stopPropagation();
setUserWillDrag(true);
}}
onMouseUp={(e) => {
e.stopPropagation();
setUserWillDrag(false);
}}
onMouseOver={(e) => {
e.stopPropagation();

Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/get-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Data, MappedItem } from "../types/Config";
import { Data } from "../types/Config";
import { rootDroppableId } from "./root-droppable-id";
import { setupZone } from "./setup-zone";

Expand Down

0 comments on commit 358435c

Please sign in to comment.