Skip to content

Commit

Permalink
refactor: make overlay syncing more deterministic
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvxd committed Dec 8, 2024
1 parent dab9425 commit 2d82115
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
38 changes: 28 additions & 10 deletions packages/core/components/DraggableComponent/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
CSSProperties,
ReactNode,
Ref,
SyntheticEvent,
Expand Down Expand Up @@ -196,8 +197,8 @@ export const DraggableComponent = ({
);
}, [iframe.enabled]);

const sync = useCallback(() => {
if (!ref.current || !overlayRef.current) return;
const getStyle = useCallback(() => {
if (!ref.current) return;

const rect = ref.current!.getBoundingClientRect();

Expand All @@ -214,12 +215,20 @@ export const DraggableComponent = ({
y: (view?.scrollY || 0) - (portalContainerRect?.top ?? 0),
};

overlayRef.current!.style.left = `${rect.left + scroll.x}px`;
overlayRef.current!.style.top = `${rect.top + scroll.y}px`;
overlayRef.current!.style.height = `${rect.height}px`;
overlayRef.current!.style.width = `${rect.width}px`;
const style: CSSProperties = {
left: `${rect.left + scroll.x}px`,
top: `${rect.top + scroll.y}px`,
height: `${rect.height}px`,
width: `${rect.width}px`,
};

return style;
}, [ref, overlayRef, iframe]);

const [style, setStyle] = useState<CSSProperties>();

overlayRef.current!.style.opacity = "1";
const sync = useCallback(() => {
setStyle(getStyle());
}, [ref, overlayRef, iframe]);

useEffect(() => {
Expand Down Expand Up @@ -347,7 +356,17 @@ export const DraggableComponent = ({
}
}, [disabled, ref]);

const isVisible = (isSelected || hover || indicativeHover) && !userIsDragging;
const [isVisible, setIsVisible] = useState(false);

useEffect(() => {
sync();

if ((isSelected || hover || indicativeHover) && !userIsDragging) {
setIsVisible(true);
} else {
setIsVisible(false);
}
}, [isSelected, hover, indicativeHover, iframe, state.data, userIsDragging]);

const [actionsWidth, setActionsWidth] = useState(250);
const actionsRef = useRef<HTMLDivElement>(null);
Expand Down Expand Up @@ -388,8 +407,6 @@ export const DraggableComponent = ({
setDragAxis(autoDragAxis);
}, [ref, userDragAxis, autoDragAxis]);

useEffect(sync, [ref, overlayRef, isVisible, hover, iframe, state.data]);

return (
<DropZoneProvider
value={{
Expand All @@ -412,6 +429,7 @@ export const DraggableComponent = ({
hover: hover || indicativeHover,
})}
ref={overlayRef}
style={{ ...style }}
>
{debug}
{isLoading && (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.DraggableComponent {
opacity: 0;
position: absolute;
pointer-events: none;

Expand Down

0 comments on commit 2d82115

Please sign in to comment.