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

useToolsPanel: calculate derived state in reducer to prevent too many renders #65564

Merged
merged 2 commits into from
Sep 24, 2024

Conversation

jsnajdr
Copy link
Member

@jsnajdr jsnajdr commented Sep 23, 2024

Second attempt at implementing #65494, this time with a very different approach. Instead of calculating the derived state (menuItems, areAllOptionalControlsHidden) in effects, layout- or normal, let's calculate all the updated state at once, in a reducer. If you register or unregister a panel, the new menuItems value is calculated instantly, within one render, and is immediately available.

This should prevent painting an unfinished intermediate state, because there is no longer an intermediate state. And also avoid untractable infinite effect loops that plagued #65494, because the effects are eliminated.

How to test:
Use test steps from #65494 to verify that the unwanted visual glitches in Global Styles editor are gone.

Test also all other usages of ToolsPanel especially in the post editor when switching blocks.

@jsnajdr jsnajdr added [Type] Bug An existing feature does not function as intended [Package] Components /packages/components labels Sep 23, 2024
@jsnajdr jsnajdr self-assigned this Sep 23, 2024
@jsnajdr jsnajdr requested a review from ajitbohra as a code owner September 23, 2024 08:44
Copy link

github-actions bot commented Sep 23, 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: jsnajdr <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ciampo <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: andrewserong <[email protected]>

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

Copy link
Contributor

@youknowriad youknowriad left a comment

Choose a reason for hiding this comment

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

Looks good in my tests. Would appreciate a second review.

@jsnajdr
Copy link
Member Author

jsnajdr commented Sep 23, 2024

I'm worried whether the isResetting state and ref still works. The code introduced in #34157 and similar PRs.

Copy link
Contributor

@ciampo ciampo left a comment

Choose a reason for hiding this comment

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

Also looking good on my machine.

I'm worried whether the isResetting state and ref still works. The code introduced in #34157 and similar PRs.

I followed testing instructions in #34157 and things seem to work as expected.

If we wanted to be more conservative, we could wait for @aaronrobertshaw and/or @andrewserong to have confirmation that the resetting logic still works as expected. But I'm tempted to merge it and watch out for any reports in the upcoming days.

@jsnajdr
Copy link
Member Author

jsnajdr commented Sep 23, 2024

But I'm tempted to merge it and watch out for any reports in the upcoming days.

Well that's what we did on Friday with #65494 and it ended badly 🙂 Let's wait for at least one "independent" review, from someone who actually worked on the code.

Copy link
Contributor

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

to have confirmation that the resetting logic still works as expected

Thanks for the ping 👍

I've given this some testing focused on the reset behaviour. It's looking good to me.

I did find a couple of bugs for different panels in the editor but they also exist on trunk (e.g. Image block's resolution control). I'll create separate issues for those as they are unrelated to the ToolsPanel itself.

✅ Component unit tests pass
✅ Global styles panels in Site Editor behave as expected
✅ Block inspector panels function as per trunk in both editors
✅ Individual control resets and reset all functionality looks to be working fine
✅ Ad hoc controls injected to ToolsPanels e.g. Cover overlay work as before
✅ Storybook examples that conditionally render controls or make them default, work too

🚢

Copy link
Contributor

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

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

Ah, Aaron beat me to it! 🙇 This is testing well for me, too, across the range of block supports used by blocks like Group, Cover, Heading, Paragraph etc, tested in the post and site editor and in global styles. I also really like how this reads now using a reducer, instead 👍

LGTM! 🚀

@jsnajdr
Copy link
Member Author

jsnajdr commented Sep 24, 2024

Thanks everyone for your testing and reviews 🚀

@jsnajdr jsnajdr merged commit 842f67d into trunk Sep 24, 2024
65 checks passed
@jsnajdr jsnajdr deleted the fix/tools-panel-use-reducer branch September 24, 2024 06:30
@github-actions github-actions bot added this to the Gutenberg 19.4 milestone Sep 24, 2024
@ciampo
Copy link
Contributor

ciampo commented Sep 24, 2024

I guess this should feels like a nice fix to backport to the next release, I'll add the label

@ciampo ciampo added the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Sep 24, 2024
@youknowriad
Copy link
Contributor

I guess you anticipated this but the performance regression is back here.

@github-actions github-actions bot removed the Backport to WP 6.7 Beta/RC Pull request that needs to be backported to the WordPress major release that's currently in beta label Sep 24, 2024
gutenbergplugin pushed a commit that referenced this pull request Sep 24, 2024
… renders (#65564)

Co-authored-by: jsnajdr <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ciampo <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: andrewserong <[email protected]>
@github-actions github-actions bot added the Backported to WP Core Pull request that has been successfully merged into WP Core label Sep 24, 2024
Copy link

I just cherry-picked this PR to the wp/6.7 branch to get it included in the next release: b26e274

@jsnajdr
Copy link
Member Author

jsnajdr commented Sep 24, 2024

Yes, the focus performance regression is also present here, just like in #65494:

Screenshot 2024-09-24 at 15 48 10

It's because the ToolsPanel is rendered right away, during the focus event handler, while previously it was fully rendered only after a cascade of effects and state updates.

I believe this is a deficiency of the measuring method, i.e., measuring the execution of the focus handler. There shouldn't be any user-visible change. Previously the work was postponed more or less after the next animation frame, outside the focus handler, and now it's done all at once.

But yes, maybe we could update the tools panel in some kind of "low priority" update, the kind that useDeferredValue offers. That way the rendered blocks and their selection are updated immediately, high priority, and sidebars are updated later. Maybe @ellatrix has some thoughs on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backported to WP Core Pull request that has been successfully merged into WP Core [Package] Components /packages/components [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants