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

Editor: when Toggle navigation receives state false, focus #37265

Merged
merged 3 commits into from
Dec 17, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect, useRef } from '@wordpress/element';
import {
Button,
Icon,
Expand Down Expand Up @@ -40,6 +41,15 @@ function NavigationToggle( { icon } ) {

const disableMotion = useReducedMotion();

const navigationToggleRef = useRef();

useEffect( () => {
// Focus the trigger button if close
alexstine marked this conversation as resolved.
Show resolved Hide resolved
if ( ! isNavigationOpen ) {
navigationToggleRef.current.focus();
}
}, [ isNavigationOpen ] );
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for making this PR! However, I don't think this is the right solution. We don't want to focus the button whenever it gets closed, we only want to focus it whenever route changes. This could have some unwanted side-effects (for instance, if we want to close this one to open another sidebar, the focus could be moved to this button). I opened #37314 for an alternative approach which I believe could be more robust, but also requires a lot of changes 😅 .


const toggleNavigationPanel = () =>
setIsNavigationPanelOpened( ! isNavigationOpen );

Expand Down Expand Up @@ -79,6 +89,7 @@ function NavigationToggle( { icon } ) {
<Button
className="edit-site-navigation-toggle__button has-icon"
label={ __( 'Toggle navigation' ) }
ref={ navigationToggleRef }
onClick={ toggleNavigationPanel }
showTooltip
>
Expand Down