Skip to content

Commit

Permalink
Select mode: fix navigate by pointer (#31321)
Browse files Browse the repository at this point in the history
* Select mode: fix navigate by pointer

* Fix inner blocks
  • Loading branch information
ellatrix authored and youknowriad committed Apr 29, 2021
1 parent 5fa778a commit 83f8734
Showing 1 changed file with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,24 @@ import { store as blockEditorStore } from '../../../store';
* @param {string} clientId Block client ID.
*/
export function useNavModeExit( clientId ) {
const isEnabled = useSelect( ( select ) => {
const { isNavigationMode, isBlockSelected } = select(
blockEditorStore
);
return isNavigationMode() && isBlockSelected( clientId );
}, [] );
const { setNavigationMode } = useDispatch( blockEditorStore );

const { isNavigationMode, isBlockSelected } = useSelect( blockEditorStore );
const { setNavigationMode, selectBlock } = useDispatch( blockEditorStore );
return useRefEffect(
( node ) => {
if ( ! isEnabled ) {
return;
}
function onMouseDown( event ) {
// Don't select a block if it's already handled by a child
// block.
if ( isNavigationMode() && ! event.defaultPrevented ) {
// Prevent focus from moving to the block.
event.preventDefault();

function onMouseDown() {
setNavigationMode( false );
// When clicking on a selected block, exit navigation mode.
if ( isBlockSelected( clientId ) ) {
setNavigationMode( false );
} else {
selectBlock( clientId );
}
}
}

node.addEventListener( 'mousedown', onMouseDown );
Expand All @@ -39,6 +41,6 @@ export function useNavModeExit( clientId ) {
node.addEventListener( 'mousedown', onMouseDown );
};
},
[ isEnabled ]
[ clientId, isNavigationMode, isBlockSelected, setNavigationMode ]
);
}

0 comments on commit 83f8734

Please sign in to comment.