Skip to content

Commit

Permalink
Navigation Link UI: Focus the parent block when a block is removed. (#…
Browse files Browse the repository at this point in the history
…59466)

Co-authored-by: scruffian <[email protected]>
Co-authored-by: alexstine <[email protected]>
Co-authored-by: jeryj <[email protected]>
Co-authored-by: getdave <[email protected]>
  • Loading branch information
5 people authored Mar 4, 2024
1 parent 68aa464 commit c386949
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ export default function NavigationLinkEdit( {

const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
const { selectBlock } = useDispatch( blockEditorStore );
const [ isLinkOpen, setIsLinkOpen ] = useState( false );
// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
Expand All @@ -192,6 +193,7 @@ export default function NavigationLinkEdit( {
isTopLevelLink,
isParentOfSelectedBlock,
hasChildren,
firstParentClientId,
} = useSelect(
( select ) => {
const {
Expand All @@ -201,8 +203,13 @@ export default function NavigationLinkEdit( {
getBlockRootClientId,
hasSelectedInnerBlock,
getBlockParentsByBlockName,
getBlockParents,
getSelectedBlockClientId,
} = select( blockEditorStore );

const selectedBlockClientId = getSelectedBlockClientId();
const parents = getBlockParents( selectedBlockClientId );

return {
innerBlocks: getBlocks( clientId ),
isAtMaxNesting:
Expand All @@ -218,6 +225,7 @@ export default function NavigationLinkEdit( {
true
),
hasChildren: !! getBlockCount( clientId ),
firstParentClientId: parents[ parents.length - 1 ],
};
},
[ clientId ]
Expand Down Expand Up @@ -567,8 +575,9 @@ export default function NavigationLinkEdit( {
// If there is no link then remove the auto-inserted block.
// This avoids empty blocks which can provided a poor UX.
if ( ! url ) {
// Need to handle refocusing the Nav block or the inserter?
onReplace( [] );
// Focus the parent block.
selectBlock( firstParentClientId );
}
} }
anchor={ popoverAnchor }
Expand Down

0 comments on commit c386949

Please sign in to comment.