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

Navigation Link UI: Focus the parent block when a block is removed. #59466

Merged
merged 3 commits into from
Mar 4, 2024
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
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
Loading