-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Ensure block toolbar doesn't overlap block by modifying forcePosition and shift popover props #42887
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
Merged
talldan
merged 20 commits into
trunk
from
try/flip-block-toolbar-by-modifying-placement
Aug 23, 2022
Merged
Ensure block toolbar doesn't overlap block by modifying forcePosition and shift popover props #42887
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
8224e2b
Flip popover placement in SelectedBlockPopover component
talldan 1b1e68b
Adjust shift/forcePosition props instead of placement
talldan b191f72
Remount popover when attached to a different block
talldan 558ce49
Remove key prop
talldan 161d2f1
Update hook to use an effect and handle window resize event
talldan db3c7bb
Fix props not changing when elements are changed. Tidy up code and ad…
talldan a7de679
Remove any unrelated changes
talldan 506ae6f
Handle content ref being undefined - fixes widget editor errors
talldan 5be2bd4
Update toolbar popover props on block movement
talldan fda157e
Use dynamic toolbar height in hook
talldan 6e2be7d
Improve comments
talldan 9ec515c
Add resize observer for block
talldan eb03600
Refactor into seperate hooks
talldan e5c20cb
Revert "Refactor into seperate hooks"
talldan 6f2e887
Ensure the resize observer is using the correct view
talldan bc43d19
Refactor update on block move to a different effect to reduce event b…
talldan 4c3add5
Fix doc block description
talldan ad97614
Add missing hook dependency
talldan 07ff4b0
Switch to useLayoutEffect
talldan d4c5772
Use lazy initial state
talldan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
123 changes: 123 additions & 0 deletions
123
packages/block-editor/src/components/block-tools/use-block-toolbar-popover-props.js
This file contains hidden or 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,123 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useRefEffect } from '@wordpress/compose'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { useCallback, useLayoutEffect, useState } from '@wordpress/element'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../store'; | ||
| import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; | ||
|
|
||
| // By default the toolbar sets the `shift` prop. If the user scrolls the page | ||
| // down the toolbar will stay on screen by adopting a sticky position at the | ||
| // top of the viewport. | ||
| const DEFAULT_PROPS = { __unstableForcePosition: true, __unstableShift: true }; | ||
|
|
||
| // When there isn't enough height between the top of the block and the editor | ||
| // canvas, the `shift` prop is set to `false`, as it will cause the block to be | ||
| // obscured. The `flip` behavior is enabled (by setting `forcePosition` to | ||
| // `false`), which positions the toolbar below the block. | ||
| const RESTRICTED_HEIGHT_PROPS = { | ||
| __unstableForcePosition: false, | ||
| __unstableShift: false, | ||
| }; | ||
|
|
||
| /** | ||
| * Get the popover props for the block toolbar, determined by the space at the top of the canvas and the toolbar height. | ||
| * | ||
| * @param {Element} contentElement The DOM element that represents the editor content or canvas. | ||
| * @param {Element} selectedBlockElement The outer DOM element of the first selected block. | ||
| * @param {number} toolbarHeight The height of the toolbar in pixels. | ||
| * | ||
| * @return {Object} The popover props used to determine the position of the toolbar. | ||
| */ | ||
| function getProps( contentElement, selectedBlockElement, toolbarHeight ) { | ||
| if ( ! contentElement || ! selectedBlockElement ) { | ||
| return DEFAULT_PROPS; | ||
| } | ||
|
|
||
| const blockRect = selectedBlockElement.getBoundingClientRect(); | ||
| const contentRect = contentElement.getBoundingClientRect(); | ||
|
|
||
| if ( blockRect.top - contentRect.top > toolbarHeight ) { | ||
| return DEFAULT_PROPS; | ||
| } | ||
|
|
||
| return RESTRICTED_HEIGHT_PROPS; | ||
| } | ||
|
|
||
| /** | ||
| * Determines the desired popover positioning behavior, returning a set of appropriate props. | ||
| * | ||
| * @param {Object} elements | ||
| * @param {Element} elements.contentElement The DOM element that represents the editor content or canvas. | ||
| * @param {string} elements.clientId The clientId of the first selected block. | ||
| * | ||
| * @return {Object} The popover props used to determine the position of the toolbar. | ||
| */ | ||
| export default function useBlockToolbarPopoverProps( { | ||
| contentElement, | ||
| clientId, | ||
| } ) { | ||
| const selectedBlockElement = useBlockElement( clientId ); | ||
| const [ toolbarHeight, setToolbarHeight ] = useState( 0 ); | ||
| const [ props, setProps ] = useState( () => | ||
| getProps( contentElement, selectedBlockElement, toolbarHeight ) | ||
| ); | ||
| const blockIndex = useSelect( | ||
| ( select ) => select( blockEditorStore ).getBlockIndex( clientId ), | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| const popoverRef = useRefEffect( ( popoverNode ) => { | ||
| setToolbarHeight( popoverNode.offsetHeight ); | ||
| }, [] ); | ||
|
|
||
| const updateProps = useCallback( | ||
| () => | ||
| setProps( | ||
| getProps( contentElement, selectedBlockElement, toolbarHeight ) | ||
| ), | ||
| [ contentElement, selectedBlockElement, toolbarHeight ] | ||
| ); | ||
|
|
||
| // Update props when the block is moved. This also ensures the props are | ||
| // correct on initial mount, and when the selected block or content element | ||
| // changes (since the callback ref will update). | ||
| useLayoutEffect( updateProps, [ blockIndex, updateProps ] ); | ||
|
|
||
| // Update props when the viewport is resized or the block is resized. | ||
| useLayoutEffect( () => { | ||
| if ( ! contentElement || ! selectedBlockElement ) { | ||
| return; | ||
| } | ||
|
|
||
| // Update the toolbar props on viewport resize. | ||
| const contentView = contentElement?.ownerDocument?.defaultView; | ||
| contentView?.addEventHandler?.( 'resize', updateProps ); | ||
|
|
||
| // Update the toolbar props on block resize. | ||
| let resizeObserver; | ||
| const blockView = selectedBlockElement?.ownerDocument?.defaultView; | ||
| if ( blockView.ResizeObserver ) { | ||
| resizeObserver = new blockView.ResizeObserver( updateProps ); | ||
| resizeObserver.observe( selectedBlockElement ); | ||
| } | ||
|
|
||
| return () => { | ||
| contentView?.removeEventHandler?.( 'resize', updateProps ); | ||
|
|
||
| if ( resizeObserver ) { | ||
| resizeObserver.disconnect(); | ||
| } | ||
| }; | ||
| }, [ updateProps, contentElement, selectedBlockElement ] ); | ||
|
|
||
| return { | ||
| ...props, | ||
| ref: popoverRef, | ||
| }; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.