-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Sidebar: withFocusReturn
doesn't work when the Sidebar is already open
#1918
Comments
We may need to allow a fallback behavior to be specified, to manually control the Maybe a selector passed as an option to export default withFocusReturn( Sidebar, {
fallback: '[id^="editor-tools__sidebar-button-"]'
} ); |
Off the top of my head: what if "problematic" components define their own, specific, focus fallback? If the fallback could be stored somewhere in a way it can be accessed by |
Just tested again and still happens. |
This ticket was mentioned in Slack in #core-editor by jeffpaul. View the logs. |
Thinking at this, ideally they should be part of the same component 😬 However, that would require some refactoring in the placement of the toggle and sidebar. |
This need to be fixed, but also needs more research, so we move this to 5.0 |
Perhaps as part of the creation of the higher-order component of In this case, <Sidebar.FocusInitiator>
<IconButton
icon="admin-generic"
label={ __( 'Settings' ) }
onClick={ toggleGeneralSidebar }
isToggled={ isEditorSidebarOpened }
aria-expanded={ isEditorSidebarOpened }
shortcut={ shortcuts.toggleSidebar }
/>
</Sidebar.FocusInitiator> As far as implementation, there could be a randomly-generated unique ID shared between the initiator component and the The naming implies some two-way relationship of focus, but currently we don't have a continuation of tab stops from the initiator (settings button) to the thing it controls (sidebar). Maybe we should? |
Not sure. Remember months ago when I've recommended any toggled UI to be placed immediately after its "initiator" in the DOM? 🙂 That would have made things much simpler. We're now in a situation where major changes can't be made so we have to "repair" managing focus programmatically. Anything that helps in that direction would be very appreciated. |
That's a simplification that ignores the cross-component nature of some interactions as well as the CSS/DOM requirements of things like modals and menus. Keyboard shortcuts are available via a menu item or a keyboard shortcut. Should we show the menu when the keyboard shortcut is used directly from the editor? That would "restore" focus to the menu rather than editor when the modal is left. @aduth's idea implies we could restore focus based on where the keyboard was before the UI was toggled. That's not programmatically repairing focus, it's programmatically improving the handling of focus by being context-aware. An aside to the issue at hand: the terse reply "not sure" followed by commentary surrounding what we should have done months ago adds nothing to people trying to follow this issue and address it. That makes it harder to follow the issue and fix it, which is indeed bad for accessibility. |
Noting that this issue pops up when the Keyboard Shortcuts modal is dismissed as well. |
This is going to be an issue with Options modal when #10215 lands. |
Looking into pending issues, just tested again this and it appears there's one more issue: starting with Gutenberg 2.7.0, focus is not moved back to the "Settings" button even when the Sidebar is initially closed. This appears to be a regression, going to label the issue as such. Also noting this issue is open since more than one year and a half, and still needs to be addressed. |
* Update ref * Update ref to point to gutenberg master
withFocusReturn
is a pretty neat tool to handle focus, specifically designed for components that "appear" on the page after some user action, to move focus back to the UI control that opened them when they get unmounted.This is important for keyboard accessibility, to avoid focus losses.
However,
withFocusReturn
assumes the component is initially not mounted. When the sidebar is already open, closing it using the keyboard does not move focus back to the "Settings" button. That's because when the Sidebar is already open on the initial page load, there's nodocument.activeElement
thatwithFocusReturn
can get and use later.To reproduce:
Worth noting this is not exactly a bug, it's more an improper use of
withFocusReturn
as it was designed.withFocusReturn
works when a component is not initially mounted, so it's suited for modal dialogs, drop-down menus and the like given that they're not already mounted on page load. Instead, they should get mounted after an explicit user action. See also conversation about the Post Visibility "popover" #1886 (review) and #1204It appears the Sidebar needs a new mechanism to set focus back on the Settings button.
Note: worth noting
document.activeElement
doesn't work consistently across browsers/platforms when clicking with the mouse on button elements, see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus. Depending on the specific case, this might be acceptable since handling focus is mainly a concern for keyboard navigation. Something to be aware of though.The text was updated successfully, but these errors were encountered: