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

Focus first section root block if no selected block and tabbing to zoom out canvas #65843

Merged
merged 2 commits into from
Oct 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ export default function useTabNav() {
const focusCaptureBeforeRef = useRef();
const focusCaptureAfterRef = useRef();

const { hasMultiSelection, getSelectedBlockClientId, getBlockCount } =
useSelect( blockEditorStore );
const {
hasMultiSelection,
getSelectedBlockClientId,
getBlockCount,
getBlockOrder,
getLastFocus,
getSectionRootClientId,
isZoomOut,
__unstableGetEditorMode,
} = unlock( useSelect( blockEditorStore ) );
const { setLastFocus } = unlock( useDispatch( blockEditorStore ) );
const { getLastFocus } = unlock( useSelect( blockEditorStore ) );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved this up to the other useSelect, as there shouldn't be a reason to use them in separate places.


// Reference that holds the a flag for enabling or disabling
// capturing on the focus capture elements.
Expand All @@ -45,6 +52,24 @@ export default function useTabNav() {
)
.focus();
}
}
// In "compose" mode without a selected ID, we want to place focus on the section root when tabbing to the canvas.
else if ( __unstableGetEditorMode() === 'zoom-out' && isZoomOut() ) {
const sectionRootClientId = getSectionRootClientId();
const sectionBlocks = getBlockOrder( sectionRootClientId );

// If we have section within the section root, focus the first one.
if ( sectionBlocks.length ) {
container.current
.querySelector( `[data-block="${ sectionBlocks[ 0 ] }"]` )
.focus();
}
// If we don't have any section blocks, focus the section root.
else {
container.current
.querySelector( `[data-block="${ sectionRootClientId }"]` )
.focus();
}
} else {
const canvasElement =
container.current.ownerDocument === event.target.ownerDocument
Expand All @@ -61,7 +86,6 @@ export default function useTabNav() {
const next = isBefore
? tabbables[ 0 ]
: tabbables[ tabbables.length - 1 ];

next.focus();
}
}
Expand Down
Loading