Skip to content

Commit 570efc6

Browse files
committed
Delay showing root drop targets. Fixes #156
1 parent 602b0d5 commit 570efc6

File tree

2 files changed

+48
-15
lines changed

2 files changed

+48
-15
lines changed

src/RootDropTargets.tsx

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,50 @@ export interface RootDropTargetsProps {
1111
isDragging: boolean;
1212
}
1313

14-
class RootDropTargetsClass extends React.PureComponent<RootDropTargetsProps> {
15-
render() {
16-
return (
17-
<div
18-
className={classNames('drop-target-container', {
19-
'-dragging': this.props.isDragging,
20-
})}
21-
>
22-
{values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map((position) => (
23-
<MosaicDropTarget position={position} path={[]} key={position} />
24-
))}
25-
</div>
26-
);
27-
}
14+
const RootDropTargetsComponent = React.memo((props: RootDropTargetsProps) => {
15+
const delayedIsDragging = useDelayedTrue(props.isDragging, 0);
16+
return (
17+
<div
18+
className={classNames('drop-target-container', {
19+
'-dragging': delayedIsDragging,
20+
})}
21+
>
22+
{values<MosaicDropTargetPosition>(MosaicDropTargetPosition).map((position) => (
23+
<MosaicDropTarget position={position} path={[]} key={position} />
24+
))}
25+
</div>
26+
);
27+
});
28+
RootDropTargetsComponent.displayName = 'RootDropTargetsComponent';
29+
30+
function useDelayedTrue(currentValue: boolean, delay: number): boolean {
31+
const delayedRef = React.useRef(currentValue);
32+
33+
const [, setCounter] = React.useState(0);
34+
const setAndRender = (newValue: boolean) => {
35+
delayedRef.current = newValue;
36+
setCounter((count) => count + 1);
37+
};
38+
39+
React.useEffect(() => {
40+
if (delayedRef.current === currentValue) {
41+
return;
42+
}
43+
44+
let timer: number | undefined;
45+
if (currentValue) {
46+
timer = window.setTimeout(() => setAndRender(true), delay);
47+
} else {
48+
setAndRender(false);
49+
}
50+
return () => {
51+
if (timer) {
52+
window.clearTimeout(timer);
53+
}
54+
};
55+
}, [currentValue]);
56+
57+
return delayedRef.current;
2858
}
2959

3060
const dropTarget = {};
@@ -34,4 +64,4 @@ export const RootDropTargets = DropTarget(
3464
(_connect, monitor): RootDropTargetsProps => ({
3565
isDragging: monitor.getItem() !== null && monitor.getItemType() === MosaicDragType.WINDOW,
3666
}),
37-
)(RootDropTargetsClass as any) as React.ComponentType<{}>;
67+
)(RootDropTargetsComponent as any) as React.ComponentType;

styles/mosaic-window.less

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
}
2626

2727
.mosaic-window-title {
28+
display: flex;
29+
align-items: center;
30+
height: 100%;
2831
padding-left: 15px;
2932
flex: 1;
3033
text-overflow: ellipsis;

0 commit comments

Comments
 (0)