'Always open list view' preference sets initial focus and starts tab sequence from the List View
Description
When the 'Always open list view' preference is enabled, initial keyboard focus in both the Post Editor and in the Site Editor is set to the first item within the List view.
This is clrearly unexpected and makes the tab sequence start from the wrong place, as a relevant part of the UI is just 'skipped',
Expected:
- initial focs to not be set on any specific element.
- The tab sequence should start from the document root.
- Only when creating a new post, initial focus is set to the post title.
I do realize initial focus is a behaevior that is built-in in the List view component. With the default preferences, it is good that when the List view opens, fofucs is set to the List view. I'd argue that focus shold be set to the List view role="region" instead of to the first item but that's a separate issue. However, the List view should not get focus on editor load when 'Always open list view' is enabled.
Screenshot of initial focus in the Post editor (same happens in the SIte editor):
Step-by-step reproduction instructions
- First test with default preferences.
- Edit a post.
- Observe that when the post editor loads, initial focus is not set anywhere.
- Press the Tav key and observe the first element that gets focus is the 'View Posts' link.
- Enable the 'Always open list view' preference.
- Exit the editor and edit a post again.
- Observe that when the post editor loads, initial focus is set to the List view first item.
- The same thing happens in the site editor.
Note: initial focus in the Site editor > edit mode is set to the editor canvas. That's wrong, see https://github.com/WordPress/gutenberg/issues/51570
Screenshots, screen recording, code snippet
No response
Environment info
No response
Please confirm that you have searched existing issues in the repo.
Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
Yes
To my understanding, this is one more case where some components behaviors (or behaviors of hooks attached to components) need to depend on the context rather than being built-in. Cc @ciampo
the List view should not get focus on editor load when 'Always open list view' is enabled.
That's a good find, thank you for flagging it.
this is one more case where some components behaviors (or behaviors of hooks attached to components) need to depend on the context rather than being built-in
This scenario is quite different from what discussed in https://github.com/WordPress/gutenberg/pull/56217#issuecomment-1838431637, mostly because the component responsible for rendering the list view and moving keyboard focus is not part of the @wordpress/components package, but is instead part of the edit-post package (see code), which means that it's probably ok for it to access the editor preferences and behave accordingly.
The same behavior is also implemented for the site editor in the edit-site package (see code).
I don't currently have the capacity to work on this at the moment, but I'm happy to help with the PR review.
I don't currently have the capacity to work on this at the moment, but I'm happy to help with the PR review.
I'm gonna work on this! 👍🏻
I'm sorry, unfortunately I won't have the bandwidth to tackle this issue in the near future
I was exploring this and found that accessing the showListViewByDefault preference helps avoid focusing the List View when it’s opened automatically via the preference. However, I assume we still want focus to move to the List View when the user manually opens it, even if the preference is enabled.
To solve this, I experimented with adding a source to the setIsListViewOpened action to track whether the List View was opened via user interaction or preferences. This allows the focus logic to do something like:
--- a/packages/editor/src/components/list-view-sidebar/index.js
+++ b/packages/editor/src/components/list-view-sidebar/index.js
@@ -23,11 +23,27 @@ import { store as editorStore } from '../../store';
const { TabbedSidebar } = unlock( blockEditorPrivateApis );
export default function ListViewSidebar() {
+ const { isListViewOpened, listViewOpenedSource } = useSelect(
+ ( select ) => {
+ return {
+ isListViewOpened: select( editorStore ).isListViewOpened(),
+ listViewOpenedSource:
+ select( editorStore ).getListViewOpenedSource(),
+ };
+ },
+ []
+ );
+
const { setIsListViewOpened } = useDispatch( editorStore );
const { getListViewToggleRef } = unlock( useSelect( editorStore ) );
// This hook handles focus when the sidebar first renders.
+ const shouldFocusOnMount =
+ isListViewOpened && listViewOpenedSource.source === 'user';
const focusOnMountRef = useFocusOnMount( 'firstElement' );
+ const noOpRef = useRef( null );
+
+ const activeFocusRef = shouldFocusOnMount ? focusOnMountRef : noOpRef;
This works as expected: auto-opened via preference doesn’t focus (tab sequence start from document root), manual open does. I just wanted to confirm if adding this source metadata to the store is something we’d want here, or if there’s a better pattern we should follow. Please let me know if I’ve missed anything important. Thanks!
Here's a quick screencast of it working:
https://github.com/user-attachments/assets/3e937edc-1f30-456d-8f66-eac1764edf2a
Hey @t-hamano, I have a working patch for this, but wanted to get your thoughts too. It involves changes in editor store for correct focus behavior (comment). Before I move forward with the PR, do you think this approach makes sense? Thanks!
Thanks for the ping!
I don't think it's ideal to add a selector like getListViewOpenedSource just to solve one problem.
I haven't tested it, but one idea might be to move the focus handling from within the List View to the process that triggers isListViewOpened. Below is a rough code example, but I think something like this might work.
const openListView = useCallback( () => {
setIsListViewOpened( true );
window.requestAnimationFrame( () => {
const [ firstFocusable ] = focus.focusable.find(
listViewRef?.current
);
if ( firstFocusable ) {
firstFocusable?.focus();
}
} );
}, [ setIsListViewOpened, isListViewOpen ] );
Pinging @andrewserong @ramonjd for the technical feedback!
one idea might be to move the focus handling from within the List View to the process that triggers isListViewOpened
Where would this code live? Would it have to be in multiple places?
Could an alternative be overriding default focus behaviour in ListViewSidebar itself? I'm not sure, just throwing it out there.
export default function ListViewSidebar() {
const { setIsListViewOpened } = useDispatch( editorStore );
const { getListViewToggleRef } = unlock( useSelect( editorStore ) );
const { get: getPreference } = useSelect( preferencesStore );
const isListViewByDefault = getPreference(
'core',
'showListViewByDefault'
);
/*
* This hook handles focus when the sidebar first renders.
*
* If the list view is not by default, focus on the first element.
* If the list view is by default, focus on the first content element.
*/
const focusOnMountRef = useFocusOnMount(
! isListViewByDefault ? 'firstElement' : null
);
....