From 7075e3a6869eb13d4d287585b63a574068ac35bd Mon Sep 17 00:00:00 2001 From: Shaun Andrews Date: Thu, 11 Jun 2020 17:51:01 -0400 Subject: [PATCH] Select Tool: Show block borders on `:hover` while using the Select tool (#22508) * Playing with adding a block hover border when in select mode. * Trying to bring back the `.is-hovered` class. * Porting over some changes from Ella. * Only add listeners in navigation mode * Making the linter happy. Co-authored-by: Ella van Durpe --- .../components/block-list/block-wrapper.js | 49 ++++++++++++++++++- .../src/components/block-list/style.scss | 6 ++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/components/block-list/block-wrapper.js b/packages/block-editor/src/components/block-list/block-wrapper.js index 201276d151514b..ae6565e7aea271 100644 --- a/packages/block-editor/src/components/block-list/block-wrapper.js +++ b/packages/block-editor/src/components/block-list/block-wrapper.js @@ -7,7 +7,13 @@ import { first, last, omit } from 'lodash'; /** * WordPress dependencies */ -import { useRef, useEffect, useContext, forwardRef } from '@wordpress/element'; +import { + useRef, + useEffect, + useState, + useContext, + forwardRef, +} from '@wordpress/element'; import { focus, isTextField, placeCaretAtHorizontalEdge } from '@wordpress/dom'; import { BACKSPACE, DELETE, ENTER } from '@wordpress/keycodes'; import { __, sprintf } from '@wordpress/i18n'; @@ -65,8 +71,11 @@ const BlockComponent = forwardRef( 'core/block-editor' ); const fallbackRef = useRef(); + const isAligned = wrapperProps && !! wrapperProps[ 'data-align' ]; wrapper = wrapper || fallbackRef; + const [ isHovered, setHovered ] = useState( false ); + // Provide the selected node, or the first and last nodes of a multi- // selection, so it can be used to position the contextual block toolbar. // We only provide what is necessary, and remove the nodes again when they @@ -190,7 +199,36 @@ const BlockComponent = forwardRef( mode === 'html' && ! __unstableIsHtml ? '-visual' : ''; const blockElementId = `block-${ clientId }${ htmlSuffix }`; + function onMouseOver( event ) { + if ( event.defaultPrevented ) { + return; + } + + event.preventDefault(); + + if ( isHovered ) { + return; + } + + setHovered( true ); + } + + function onMouseOut( event ) { + if ( event.defaultPrevented ) { + return; + } + + event.preventDefault(); + + if ( ! isHovered ) { + return; + } + + setHovered( false ); + } + return ( + // eslint-disable-next-line jsx-a11y/mouse-events-have-key-events