-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Inserter: Set initial active tab ID during render #67103
Conversation
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Size Change: -11 B (0%) Total Size: 1.82 MB
ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code change makes sense, however, I'm unable to make this work - both in trunk
and this PR.
Am I missing something? I expected "All" will get focused when I click on Patterns, but I didn't see it happening (I also tried navigating with tabbing).
Screen.Recording.2024-11-19.at.15.55.47.mov
Flaky tests detected in 1c7b946. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11909377374
|
@tyxla, sorry for the confusion. When navigating the category list after opening the Patterns, the first category should receive focus. It usually takes two Tab presses. I've updated the description and added the screencast. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as expected 👍
Thanks @Mamaduka 🚀
Thanks for testing and review, @tyxla 🙇 |
// when focus is moved to the tablist, the first tab will be focused | ||
// despite not being selected | ||
if ( selectedTabId === null && ! activeTabId && firstTabId ) { | ||
setActiveId( firstTabId ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't set state during render?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ellatrix see https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes for the pattern followed here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can if it's called conditionally; it prevents extra render.
Refs:
- https://react.dev/reference/react/useState#storing-information-from-previous-renders
- https://react.dev/learn/you-might-not-need-an-effect#adjusting-some-state-when-a-prop-changes
- https://blog.isquaredsoftware.com/2020/05/blogged-answers-a-mostly-complete-guide-to-react-rendering-behavior/#setting-state-while-rendering
What?
PR updates the
activeTabId
logic in theCategoryTabs
component and sets the state during render instead of using the effect.Initially, I planned to fix the effect dependencies (
setActiveId
can be omitted, and we could also removeactiveTabId
), but I realized we could use a different pattern.Why?
React will re-render
CategoryTabs
immediately and not render its children or update the DOM, avoiding rendering a tab list with an undefined active tab ID.Details:
Testing Instructions
Testing Instructions for Keyboard
Same.
Screencast
CleanShot.2024-11-19.at.18.08.33.mp4