Skip to content

Commit

Permalink
Delay showing root drop targets. Fixes #156
Browse files Browse the repository at this point in the history
  • Loading branch information
nomcopter committed May 2, 2021
1 parent 602b0d5 commit 570efc6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
60 changes: 45 additions & 15 deletions src/RootDropTargets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,50 @@ export interface RootDropTargetsProps {
isDragging: boolean;
}

class RootDropTargetsClass extends React.PureComponent<RootDropTargetsProps> {
render() {
return (
<div
className={classNames('drop-target-container', {
'-dragging': this.props.isDragging,
})}
>
{values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map((position) => (
<MosaicDropTarget position={position} path={[]} key={position} />
))}
</div>
);
}
const RootDropTargetsComponent = React.memo((props: RootDropTargetsProps) => {
const delayedIsDragging = useDelayedTrue(props.isDragging, 0);
return (
<div
className={classNames('drop-target-container', {
'-dragging': delayedIsDragging,
})}
>
{values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map((position) => (
<MosaicDropTarget position={position} path={[]} key={position} />
))}
</div>
);
});
RootDropTargetsComponent.displayName = 'RootDropTargetsComponent';

function useDelayedTrue(currentValue: boolean, delay: number): boolean {
const delayedRef = React.useRef(currentValue);

const [, setCounter] = React.useState(0);
const setAndRender = (newValue: boolean) => {
delayedRef.current = newValue;
setCounter((count) => count + 1);
};

React.useEffect(() => {
if (delayedRef.current === currentValue) {
return;
}

let timer: number | undefined;
if (currentValue) {
timer = window.setTimeout(() => setAndRender(true), delay);
} else {
setAndRender(false);
}
return () => {
if (timer) {
window.clearTimeout(timer);
}
};
}, [currentValue]);

return delayedRef.current;
}

const dropTarget = {};
Expand All @@ -34,4 +64,4 @@ export const RootDropTargets = DropTarget(
(_connect, monitor): RootDropTargetsProps => ({
isDragging: monitor.getItem() !== null && monitor.getItemType() === MosaicDragType.WINDOW,
}),
)(RootDropTargetsClass as any) as React.ComponentType<{}>;
)(RootDropTargetsComponent as any) as React.ComponentType;
3 changes: 3 additions & 0 deletions styles/mosaic-window.less
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
}

.mosaic-window-title {
display: flex;
align-items: center;
height: 100%;
padding-left: 15px;
flex: 1;
text-overflow: ellipsis;
Expand Down

0 comments on commit 570efc6

Please sign in to comment.