From 0d5d016575ae70186853d2ffad2fe00ce245f902 Mon Sep 17 00:00:00 2001 From: Andrew Serong <14988353+andrewserong@users.noreply.github.com> Date: Mon, 18 Sep 2023 15:48:11 +1000 Subject: [PATCH] Also use focus logic for when the list view is closed via the keyboard shortcut --- .../secondary-sidebar/list-view-sidebar.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/edit-post/src/components/secondary-sidebar/list-view-sidebar.js b/packages/edit-post/src/components/secondary-sidebar/list-view-sidebar.js index c1606aa54d2823..4d0a4cce661661 100644 --- a/packages/edit-post/src/components/secondary-sidebar/list-view-sidebar.js +++ b/packages/edit-post/src/components/secondary-sidebar/list-view-sidebar.js @@ -101,8 +101,7 @@ export default function ListViewSidebar( { listViewToggleElement } ) { } } - // This only fires when the sidebar is open because of the conditional rendering. It is the same shortcut to open but that is defined as a global shortcut and only fires when the sidebar is closed. - useShortcut( 'core/edit-post/toggle-list-view', () => { + const handleToggleListViewShortcut = useCallback( () => { // If the sidebar has focus, it is safe to close. if ( sidebarRef.current.contains( @@ -110,12 +109,21 @@ export default function ListViewSidebar( { listViewToggleElement } ) { ) ) { setIsListViewOpened( false ); - // TODO: Should we set focus here on the list view button toggle, too? + if ( ! hasBlocksSelected ) { + listViewToggleElement?.focus(); + } } else { // If the list view or outline does not have focus, focus should be moved to it. handleSidebarFocus( tab ); } - } ); + }, [ hasBlocksSelected, listViewToggleElement, setIsListViewOpened, tab ] ); + + // This only fires when the sidebar is open because of the conditional rendering. + // It is the same shortcut to open but that is defined as a global shortcut and only fires when the sidebar is closed. + useShortcut( + 'core/edit-post/toggle-list-view', + handleToggleListViewShortcut + ); /** * Render tab content for a given tab name.