Skip to content

Commit

Permalink
Improve inbetween inserter behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 24, 2022
1 parent 5966562 commit fc28658
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions packages/block-editor/src/components/block-popover/inbetween.js
Expand Up @@ -12,7 +12,7 @@ import {
useMemo,
createContext,
useState,
useEffect,
useLayoutEffect,
} from '@wordpress/element';
import { Popover } from '@wordpress/components';
import { isRTL } from '@wordpress/i18n';
Expand All @@ -36,10 +36,6 @@ function BlockPopoverInbetween( {
} ) {
// This is a temporary hack to get the inbetween inserter to recompute properly.
const [ positionRecompute, forceRecompute ] = useState( {} );
useEffect( () => {
const intervalHandle = setInterval( forceRecompute, 500 );
return () => clearInterval( intervalHandle );
}, [] );

const { orientation, rootClientId, isVisible } = useSelect(
( select ) => {
Expand All @@ -60,7 +56,7 @@ function BlockPopoverInbetween( {
isBlockVisible( nextClientId ),
};
},
[ previousClientId ]
[ previousClientId, nextClientId ]
);
const previousElement = useBlockElement( previousClientId );
const nextElement = useBlockElement( nextClientId );
Expand Down Expand Up @@ -169,6 +165,31 @@ function BlockPopoverInbetween( {

const popoverScrollRef = usePopoverScroll( __unstableContentRef );

// This is only needed for a smoth transition when moving blocks.
useLayoutEffect( () => {
if ( ! previousElement ) {
return;
}
const observer = new window.MutationObserver( forceRecompute );
observer.observe( previousElement, { attributes: true } );

return () => {
observer.disconnect();
};
}, [ previousElement ] );

useLayoutEffect( () => {
if ( ! nextElement ) {
return;
}
const observer = new window.MutationObserver( forceRecompute );
observer.observe( nextElement, { attributes: true } );

return () => {
observer.disconnect();
};
}, [ nextElement ] );

if ( ! previousElement || ! nextElement || ! isVisible ) {
return null;
}
Expand Down

0 comments on commit fc28658

Please sign in to comment.