-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reusable BlockPopover and BlockPopoverInbetween components (#40441)
- Loading branch information
1 parent
5af694c
commit 4d9af43
Showing
17 changed files
with
390 additions
and
406 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/block-editor/src/components/block-popover/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
180
packages/block-editor/src/components/block-popover/inbetween.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
73
packages/block-editor/src/components/block-popover/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
24
packages/block-editor/src/components/block-popover/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.