Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reusable BlockPopover and BlockPopoverInbetween components, remove default block appender button for empty paragraphs #40441

Merged
merged 4 commits into from
Apr 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions packages/base-styles/_z-index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,7 @@ $z-layers: (
".edit-site-navigation-panel__preview": 32,

// Above the block list and the header.
".block-editor-block-list__block-popover": 31,

// Under the block popover (block toolbar).
".block-editor-block-list__insertion-point-popover": 28,
".block-editor-block-popover": 31,

// Show snackbars above everything (similar to popovers)
".components-snackbar-list": 100000,
Expand Down
41 changes: 41 additions & 0 deletions packages/block-editor/src/components/block-popover/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# BlockPopover and BlockPopoverInbetween

These two components allow rendering editor UI by the block (in a popover) but outside the canvas. This is important to avoid messing with the style and layout of the block list.

For example, it's used to render the contextual block toolbar and the in-between block inserter.

## BlockPopover

### Props

#### clientId

The client ID of the block representing the top position of the popover.

- Type: `String`
- Required: Yes

#### bottomClientId

The client ID of the block representing the bottom position of the popover.

- Type: `String`
- Required: No

## BlockPopoverInbetween

### Props

#### previousClientId

The client ID of the block before the popover.

- Type: `String`
- Required: Yes

#### nextClientId

The client ID of the block after the popover.

- Type: `String`
- Required: Yes
180 changes: 180 additions & 0 deletions packages/block-editor/src/components/block-popover/inbetween.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useCallback, useMemo, createContext } from '@wordpress/element';
import { Popover } from '@wordpress/components';
import { isRTL } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';
import usePopoverScroll from './use-popover-scroll';

export const InsertionPointOpenRef = createContext();

function BlockPopoverInbetween( {
previousClientId,
nextClientId,
children,
__unstablePopoverSlot,
__unstableContentRef,
...props
} ) {
const { orientation, rootClientId } = useSelect(
( select ) => {
const { getBlockListSettings, getBlockRootClientId } = select(
blockEditorStore
);

const _rootClientId = getBlockRootClientId( previousClientId );
return {
orientation:
getBlockListSettings( _rootClientId )?.orientation ||
'vertical',
rootClientId: _rootClientId,
};
},
[ previousClientId ]
);
const previousElement = useBlockElement( previousClientId );
const nextElement = useBlockElement( nextClientId );
const isVertical = orientation === 'vertical';
const style = useMemo( () => {
if ( ! previousElement && ! nextElement ) {
return {};
}

const previousRect = previousElement
? previousElement.getBoundingClientRect()
: null;
const nextRect = nextElement
? nextElement.getBoundingClientRect()
: null;

if ( isVertical ) {
return {
width: previousElement
? previousElement.offsetWidth
: nextElement.offsetWidth,
height:
nextRect && previousRect
? nextRect.top - previousRect.bottom
: 0,
};
}

let width = 0;
if ( previousRect && nextRect ) {
width = isRTL()
? previousRect.left - nextRect.right
: nextRect.left - previousRect.right;
}

return {
width,
height: previousElement
? previousElement.offsetHeight
: nextElement.offsetHeight,
};
}, [ previousElement, nextElement, isVertical ] );

const getAnchorRect = useCallback( () => {
if ( ! previousElement && ! nextElement ) {
return {};
}

const { ownerDocument } = previousElement || nextElement;

const previousRect = previousElement
? previousElement.getBoundingClientRect()
: null;
const nextRect = nextElement
? nextElement.getBoundingClientRect()
: null;

if ( isVertical ) {
if ( isRTL() ) {
return {
top: previousRect ? previousRect.bottom : nextRect.top,
left: previousRect ? previousRect.right : nextRect.right,
right: previousRect ? previousRect.left : nextRect.left,
bottom: nextRect ? nextRect.top : previousRect.bottom,
ownerDocument,
};
}

return {
top: previousRect ? previousRect.bottom : nextRect.top,
left: previousRect ? previousRect.left : nextRect.left,
right: previousRect ? previousRect.right : nextRect.right,
bottom: nextRect ? nextRect.top : previousRect.bottom,
ownerDocument,
};
}

if ( isRTL() ) {
return {
top: previousRect ? previousRect.top : nextRect.top,
left: previousRect ? previousRect.left : nextRect.right,
right: nextRect ? nextRect.right : previousRect.left,
bottom: previousRect ? previousRect.bottom : nextRect.bottom,
ownerDocument,
};
}

return {
top: previousRect ? previousRect.top : nextRect.top,
left: previousRect ? previousRect.right : nextRect.left,
right: nextRect ? nextRect.left : previousRect.right,
bottom: previousRect ? previousRect.bottom : nextRect.bottom,
ownerDocument,
};
}, [ previousElement, nextElement ] );

const popoverScrollRef = usePopoverScroll( __unstableContentRef );

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

/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
// While ideally it would be enough to capture the
// bubbling focus event from the Inserter, due to the
// characteristics of click focusing of `button`s in
// Firefox and Safari, it is not reliable.
//
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
return (
<Popover
ref={ popoverScrollRef }
noArrow
animate={ false }
getAnchorRect={ getAnchorRect }
focusOnMount={ false }
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the the default popover slot).
__unstableSlotName={ __unstablePopoverSlot || null }
// Forces a remount of the popover when its position changes
// This makes sure the popover doesn't animate from its previous position.
key={ nextClientId + '--' + rootClientId }
{ ...props }
className={ classnames(
'block-editor-block-popover',
props.className
) }
>
<div style={ style }>{ children }</div>
</Popover>
);
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
}

export default BlockPopoverInbetween;
73 changes: 73 additions & 0 deletions packages/block-editor/src/components/block-popover/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { Popover } from '@wordpress/components';
import { getScrollContainer } from '@wordpress/dom';

/**
* Internal dependencies
*/
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';
import usePopoverScroll from './use-popover-scroll';

export default function BlockPopover( {
clientId,
bottomClientId,
children,
__unstablePopoverSlot,
__unstableContentRef,
...props
} ) {
const selectedElement = useBlockElement( clientId );
const lastSelectedElement = useBlockElement( bottomClientId ?? clientId );
const popoverScrollRef = usePopoverScroll( __unstableContentRef );

if ( ! selectedElement || ( bottomClientId && ! lastSelectedElement ) ) {
return null;
}

const anchorRef = {
top: selectedElement,
bottom: lastSelectedElement,
};

const { ownerDocument } = selectedElement;
const stickyBoundaryElement =
ownerDocument.defaultView.frameElement ||
getScrollContainer( selectedElement ) ||
ownerDocument.body;

return (
<Popover
ref={ popoverScrollRef }
noArrow
animate={ false }
position="top right left"
focusOnMount={ false }
anchorRef={ anchorRef }
__unstableStickyBoundaryElement={ stickyBoundaryElement }
// Render in the old slot if needed for backward compatibility,
// otherwise render in place (not in the the default popover slot).
__unstableSlotName={ __unstablePopoverSlot || null }
__unstableBoundaryParent
// Observe movement for block animations (especially horizontal).
__unstableObserveElement={ selectedElement }
shouldAnchorIncludePadding
// Used to safeguard sticky position behavior against cases where it would permanently
// obscure specific sections of a block.
__unstableEditorCanvasWrapper={ __unstableContentRef?.current }
{ ...props }
className={ classnames(
'block-editor-block-popover',
props.className
) }
>
{ children }
</Popover>
);
}
24 changes: 24 additions & 0 deletions packages/block-editor/src/components/block-popover/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

.components-popover.block-editor-block-popover {
z-index: z-index(".block-editor-block-popover");
position: absolute;

.components-popover__content {
margin: 0 !important;
min-width: auto;
width: max-content;
background: none;
border: none;
box-shadow: none;
overflow-y: visible;

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

// Position the block toolbar.
> * {
pointer-events: all;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useRefEffect } from '@wordpress/compose';
*
* @param {Object} scrollableRef
*/
export function usePopoverScroll( scrollableRef ) {
function usePopoverScroll( scrollableRef ) {
return useRefEffect(
( node ) => {
if ( ! scrollableRef ) {
Expand All @@ -32,3 +32,5 @@ export function usePopoverScroll( scrollableRef ) {
[ scrollableRef ]
);
}

export default usePopoverScroll;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import InsertionPoint, { InsertionPointOpenRef } from './insertion-point';
import BlockPopover from './block-popover';
import BlockPopover from './selected-block-popover';

export default function BlockToolsBackCompat( { children } ) {
const openRef = useContext( InsertionPointOpenRef );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import BlockIcon from '../block-icon';
import { store as blockEditorStore } from '../../store';
import BlockDraggable from '../block-draggable';
import useBlockDisplayInformation from '../use-block-display-information';
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs';

/**
* Block selection button component, displaying the label of the block. If the block
Expand All @@ -49,7 +50,7 @@ import useBlockDisplayInformation from '../use-block-display-information';
*
* @return {WPComponent} The component to be rendered.
*/
function BlockSelectionButton( { clientId, rootClientId, blockElement } ) {
function BlockSelectionButton( { clientId, rootClientId } ) {
const blockInformation = useBlockDisplayInformation( clientId );
const selected = useSelect(
( select ) => {
Expand Down Expand Up @@ -90,6 +91,7 @@ function BlockSelectionButton( { clientId, rootClientId, blockElement } ) {

speak( label );
}, [ label ] );
const blockElement = useBlockElement( clientId );

const {
hasBlockMovingClientId,
Expand Down
Loading