@@ -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
3060const 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 ;
0 commit comments