Skip to content

Commit

Permalink
Keep inserters visible on scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 19, 2022
1 parent aea561a commit 232b6f9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 54 deletions.
10 changes: 8 additions & 2 deletions packages/block-editor/src/components/block-popover/inbetween.js
Expand Up @@ -98,7 +98,13 @@ function BlockPopoverInbetween( {
width,
height: previousRect ? previousRect.height : nextRect.height,
};
}, [ previousElement, nextElement, isVertical, positionRecompute ] );
}, [
previousElement,
nextElement,
isVertical,
positionRecompute,
isVisible,
] );

const getAnchorRect = useCallback( () => {
if ( ( ! previousElement && ! nextElement ) || ! isVisible ) {
Expand Down Expand Up @@ -159,7 +165,7 @@ function BlockPopoverInbetween( {
width: 0,
ownerDocument,
};
}, [ previousElement, nextElement, positionRecompute ] );
}, [ previousElement, nextElement, positionRecompute, isVisible ] );

const popoverScrollRef = usePopoverScroll( __unstableContentRef );

Expand Down
5 changes: 3 additions & 2 deletions packages/block-editor/src/components/block-popover/style.scss
Expand Up @@ -6,6 +6,9 @@
// like the popover is impacted by the block gap margin.
margin: 0 !important;

// Allow clicking through the toolbar holder.
pointer-events: none;

.components-popover__content {
margin: 0 !important;
min-width: auto;
Expand All @@ -16,8 +19,6 @@
box-shadow: none;
overflow-y: visible;

// Allow clicking through the toolbar holder.
pointer-events: none;
> div > * {
pointer-events: all;
}
Expand Down
@@ -1,70 +1,20 @@
/**
* External dependencies
*/
import { throttle } from 'lodash';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useState, useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import BlockPopoverInbetween from '../block-popover/inbetween';
import { store as blockEditorStore } from '../../store';
import Inserter from '../inserter';
import { __unstableUseBlockElement } from '../block-list/use-block-props/use-block-refs';

function useIsScrolling() {
const [ isScrolling, setIsScrolling ] = useState( false );

const clientId = useSelect( ( select ) => {
return select( blockEditorStore ).getBlockOrder()?.[ 0 ];
}, [] );
const element = __unstableUseBlockElement( clientId );

useEffect( () => {
let timeout;
setIsScrolling( false );
if ( ! element ) {
return;
}
const onScroll = throttle( () => {
setIsScrolling( true );
clearTimeout( timeout );
timeout = setTimeout( () => {
setIsScrolling( false );
}, 100 );
}, 100 );

element.ownerDocument.defaultView.addEventListener(
'scroll',
onScroll
);
return () => {
clearTimeout( timeout );
element.ownerDocument.defaultView.removeEventListener(
'scroll',
onScroll
);
};
}, [ element ] );

return isScrolling;
}

function ExplodedModeInserters( { __unstableContentRef } ) {
const isScrolling = useIsScrolling();
const blockOrder = useSelect( ( select ) => {
return select( blockEditorStore ).getBlockOrder();
}, [] );

if ( isScrolling ) {
return null;
}

return blockOrder.map( ( clientId, index ) => {
if ( index === blockOrder.length - 1 ) {
return null;
Expand Down

0 comments on commit 232b6f9

Please sign in to comment.