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

Apply hover class in outline mode #27714

Merged
merged 5 commits into from
Dec 15, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,28 @@ import { useSelect } from '@wordpress/data';
/** @typedef {import('@wordpress/element').RefObject} RefObject */

/**
* Returns true when the block is hovered and in navigation mode, false if not.
* Returns true when the block is hovered and in navigation or outline mode, false if not.
*
* @param {RefObject} ref React ref with the block element.
*
* @return {boolean} Hovered state.
*/
export function useIsHovered( ref ) {
const [ isHovered, setHovered ] = useState( false );
const isNavigationMode = useSelect(
( select ) => select( 'core/block-editor' ).isNavigationMode(),
[]
);

useEffect( () => {
if ( ! isNavigationMode ) {
return;
}
const { isNavigationMode, isOutlineMode } = useSelect( ( select ) => {
const {
isNavigationMode: selectIsNavigationMode,
getSettings,
} = select( 'core/block-editor' );

return {
isNavigationMode: selectIsNavigationMode(),
isOutlineMode: getSettings().outlineMode,
};
}, [] );

useEffect( () => {
function addListener( eventType, value ) {
function listener( event ) {
if ( event.defaultPrevented ) {
Expand All @@ -45,8 +49,10 @@ export function useIsHovered( ref ) {
return addListener( 'mouseout', false );
}

return addListener( 'mouseover', true );
}, [ isNavigationMode, isHovered, setHovered ] );
if ( isOutlineMode || isNavigationMode ) {
return addListener( 'mouseover', true );
}
}, [ isNavigationMode, isOutlineMode, isHovered, setHovered ] );

return isHovered;
}