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

Tabs: Fallback to first enabled tab if no active tab Id #60681

Merged
merged 5 commits into from
Apr 12, 2024

Conversation

jeryj
Copy link
Contributor

@jeryj jeryj commented Apr 11, 2024

Alternative to #60573. More context in #52997 (comment)

What?

If there is no selectedTabId, focus would be placed on the tab container which requires an arrow keypress to move focus to the first tab. This behavior isn't as user friendly as focus should be placed on a tab. To prevent focus on the wrapper, if no element is focused and there is no activeId, fallback to the first enabled tab.

Why?

Sometimes you don't want any tab pane selected by default. This was possible before, but the focus was set to the wrapping element, not the first tab. Also, we'd like a tab to always be selected rather than focus placed on the wrapping container.

How?

Testing Instructions

Test the patterns inserter in #60257.

  • Open Block inserter (in post editor and in site editor)
  • Select Pattern Tab
  • Tab to patten categories panel
  • Focus should be on first tab
  • Use arrows to navigate between the patterns
  • Select a pattern
  • Tab into patterns panel
  • Shift + Tab out
  • Focus should be on the selected category
  • Arrow between patterns and repeat switching between categories

Testing Instructions for Keyboard

Screenshots or screencast

If there is no selectedTabId, focus will be placed on the tab container which requires an arrow keypress to move focus to the first tab. This behavior isn't as user friendly as focus should be placed on a tab. To prevent focus on the wrapper, if no element is focused and there is no activeId, fallback to the first enabled tab.
@jeryj jeryj added [Type] Enhancement A suggestion for improvement. [Feature] UI Components Impacts or related to the UI component system labels Apr 11, 2024
@jeryj jeryj self-assigned this Apr 11, 2024
@jeryj jeryj requested a review from ajitbohra as a code owner April 11, 2024 17:47
@jeryj jeryj requested review from mirka and chad1008 April 11, 2024 17:47
Copy link

github-actions bot commented Apr 11, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: jeryj <[email protected]>
Co-authored-by: mirka <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@mirka mirka changed the title Fallback to first enabled tab if no active tab Id Tabs: Fallback to first enabled tab if no active tab Id Apr 12, 2024
Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

Thank you so much for looking into this! I went ahead and added a test and a changelog. This is good to go on my end if you're ok with the code changes I suggested — otherwise let's continue tweaking it 🔧

Comment on lines +156 to +162
useEffect( () => {
// If there is no active tab, fallback to place focus on the first enabled tab
// so there is always an active element
if ( selectedTabId === null && ! activeId && firstEnabledTab?.id ) {
setActiveId( firstEnabledTab.id );
}
}, [ selectedTabId, activeId, firstEnabledTab?.id, setActiveId ] );
Copy link
Member

Choose a reason for hiding this comment

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

I feel like it'd be safer to move this out to a designated effect like so, instead of piggybacking on another one. I'd want to avoid unintended complications down the line. In that vein, I think we'd be safer to add a selectedTabId === null condition, since that's the only time we need this logic, as far as we know. (And it doubles as a isControlled check.)

Does that sound reasonable? Anything I'm missing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was considering this too from a code readability standpoint. I wasn't sure performance and debugging-wise if it would be worse to add another useEffect though. I'm totally fine with this route though if you are!

@jeryj jeryj merged commit e9bccc8 into trunk Apr 12, 2024
63 checks passed
@jeryj jeryj deleted the add/active-tab-fallback branch April 12, 2024 20:49
@github-actions github-actions bot added this to the Gutenberg 18.2 milestone Apr 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] UI Components Impacts or related to the UI component system [Type] Enhancement A suggestion for improvement.
Projects
Development

Successfully merging this pull request may close these issues.

2 participants