From 6a466a098650bb46154d0a293ae4bf0b88a1d3ef Mon Sep 17 00:00:00 2001 From: Ben Dwyer Date: Fri, 19 Jul 2024 10:01:38 +0100 Subject: [PATCH] Zoom Out: Only show the inserters when a block is selected or hovered (#63668) * Zoom Out: Only show the inserters when a block is selected or hovered * Correct logic for rendering the zoom out inserters We want to show the inserters on hover, even if there is no selection. * Update shouldRenderInsertionPoint condition * Make the inserters always present and just toggle visibility * Update docs/reference-guides/data/data-core-block-editor.md * Update packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js * Update packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js * Update packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js * Update packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js * Update packages/block-editor/src/store/reducer.js * Update packages/block-editor/src/store/reducer.js * Update packages/block-editor/src/store/selectors.js * fix the property name * Reduce number of select calls by lifting useSelect calls to ZoomOutModeInserters --------- Co-authored-by: Jerry Jones Co-authored-by: scruffian Co-authored-by: jeryj --- .../data/data-core-block-editor.md | 24 +++++++ .../block-list/use-block-props/index.js | 2 +- .../use-block-props/use-is-hovered.js | 37 +++++++---- .../src/components/block-tools/style.scss | 8 +++ .../zoom-out-mode-inserter-button.js | 47 ++++++++++++++ .../block-tools/zoom-out-mode-inserters.js | 63 ++++++++++++------- packages/block-editor/src/store/actions.js | 15 +++++ packages/block-editor/src/store/reducer.js | 18 ++++++ packages/block-editor/src/store/selectors.js | 10 +++ 9 files changed, 191 insertions(+), 33 deletions(-) create mode 100644 packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js diff --git a/docs/reference-guides/data/data-core-block-editor.md b/docs/reference-guides/data/data-core-block-editor.md index 4b66ad9eb6cb40..7eed5c8741288b 100644 --- a/docs/reference-guides/data/data-core-block-editor.md +++ b/docs/reference-guides/data/data-core-block-editor.md @@ -562,6 +562,18 @@ _Returns_ - `number`: Number of blocks in the post, or number of blocks with name equal to blockName. +### getHoveredBlockClientId + +Returns the currently hovered block. + +_Parameters_ + +- _state_ `Object`: Global application state. + +_Returns_ + +- `Object`: Client Id of the hovered block. + ### getInserterItems Determines the items that appear in the inserter. Includes both static items (e.g. a regular block type) and dynamic items (e.g. a reusable block). @@ -1257,6 +1269,18 @@ _Parameters_ Action that hides the insertion point. +### hoverBlock + +Returns an action object used in signalling that the block with the specified client ID has been hovered. + +_Parameters_ + +- _clientId_ `string`: Block client ID. + +_Returns_ + +- `Object`: Action object. + ### insertAfterBlock Action that inserts a default block after a given block. diff --git a/packages/block-editor/src/components/block-list/use-block-props/index.js b/packages/block-editor/src/components/block-list/use-block-props/index.js index 64e40559bb4735..6c44aa5c5d9705 100644 --- a/packages/block-editor/src/components/block-list/use-block-props/index.js +++ b/packages/block-editor/src/components/block-list/use-block-props/index.js @@ -115,7 +115,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) { useFocusHandler( clientId ), useEventHandlers( { clientId, isSelected } ), useNavModeExit( clientId ), - useIsHovered(), + useIsHovered( { clientId } ), useIntersectionObserver(), useMovingAnimation( { triggerAnimationOnChange: index, clientId } ), useDisabled( { isDisabled: ! hasOverlay } ), diff --git a/packages/block-editor/src/components/block-list/use-block-props/use-is-hovered.js b/packages/block-editor/src/components/block-list/use-block-props/use-is-hovered.js index 518ed583933acd..7c4b4aae8a70a1 100644 --- a/packages/block-editor/src/components/block-list/use-block-props/use-is-hovered.js +++ b/packages/block-editor/src/components/block-list/use-block-props/use-is-hovered.js @@ -2,23 +2,37 @@ * WordPress dependencies */ import { useRefEffect } from '@wordpress/compose'; +import { useDispatch } from '@wordpress/data'; -function listener( event ) { - if ( event.defaultPrevented ) { - return; - } - - const action = event.type === 'mouseover' ? 'add' : 'remove'; - - event.preventDefault(); - event.currentTarget.classList[ action ]( 'is-hovered' ); -} +/** + * Internal dependencies + */ +import { store as blockEditorStore } from '../../../store'; /* * Adds `is-hovered` class when the block is hovered and in navigation or * outline mode. */ -export function useIsHovered() { +export function useIsHovered( { clientId } ) { + const { hoverBlock } = useDispatch( blockEditorStore ); + + function listener( event ) { + if ( event.defaultPrevented ) { + return; + } + + const action = event.type === 'mouseover' ? 'add' : 'remove'; + + event.preventDefault(); + event.currentTarget.classList[ action ]( 'is-hovered' ); + + if ( action === 'add' ) { + hoverBlock( clientId ); + } else { + hoverBlock( null ); + } + } + return useRefEffect( ( node ) => { node.addEventListener( 'mouseout', listener ); node.addEventListener( 'mouseover', listener ); @@ -29,6 +43,7 @@ export function useIsHovered() { // Remove class in case it lingers. node.classList.remove( 'is-hovered' ); + hoverBlock( null ); }; }, [] ); } diff --git a/packages/block-editor/src/components/block-tools/style.scss b/packages/block-editor/src/components/block-tools/style.scss index 20c0ab4104204b..6bf1f91cb08682 100644 --- a/packages/block-editor/src/components/block-tools/style.scss +++ b/packages/block-editor/src/components/block-tools/style.scss @@ -285,3 +285,11 @@ border: none; } } + +.block-editor-block-tools__zoom-out-mode-inserter-button { + visibility: hidden; + + &.is-visible { + visibility: visible; + } +} diff --git a/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js b/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js new file mode 100644 index 00000000000000..8ea80a53830135 --- /dev/null +++ b/packages/block-editor/src/components/block-tools/zoom-out-mode-inserter-button.js @@ -0,0 +1,47 @@ +/** + * External dependencies + */ +import clsx from 'clsx'; + +/** + * WordPress dependencies + */ +import { useState } from '@wordpress/element'; +import { Button } from '@wordpress/components'; +import { plus } from '@wordpress/icons'; +import { _x } from '@wordpress/i18n'; + +function ZoomOutModeInserterButton( { isVisible, onClick } ) { + const [ + zoomOutModeInserterButtonHovered, + setZoomOutModeInserterButtonHovered, + ] = useState( false ); + + return ( +