Skip to content

Preferences: Share the toggle logic of the preference menu items - #87884

Open
t-hamano wants to merge 7 commits into
trunkfrom
update/more-menu-preference-item-hook
Open

t-hamano wants to merge 7 commits into
trunkfrom
update/more-menu-preference-item-hook

Conversation

@t-hamano

@t-hamano t-hamano commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What?

Moves the shared checked state, toggling, and announcement logic of the preference menu items into a private usePreferenceToggle hook in @wordpress/preferences.

Why?

After #87622, the same logic lived in four places:

  • PreferenceToggleMenuItem in @wordpress/preferences
  • MoreMenuPreferenceItem in @wordpress/editor
  • MoreMenuPreferenceItem in @wordpress/edit-widgets
  • MoreMenuPreferenceItem in @wordpress/customize-widgets

The copies are needed because @wordpress/ui is bundled into each package, so the Menu parts can't be rendered by the preferences package, but the logic itself can be shared.

How?

Adds a private usePreferenceToggle hook to @wordpress/preferences, which reads the preference, toggles it and announces the change, and uses it in PreferenceToggleMenuItem and the three MoreMenuPreferenceItem components.

There are no user-facing changes.

Testing Instructions

  1. Open the post editor, the site editor, the widgets screen and the widgets Customizer.
  2. Open the Options menu and toggle the preferences, such as "Top toolbar".
  3. Confirm the preferences toggle and that screen readers announce the change.

Use of AI Tools

This PR was authored with Claude Code, and reviewed by the author.


Generated by Claude Code

Move the checked state, toggling and announcement of the preference menu
items into a private `usePreferenceToggle` hook of the preferences package,
and use it in `PreferenceToggleMenuItem` and the `MoreMenuPreferenceItem`
copies of the editor, the widgets editor and the widgets Customizer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011g9URxg61iAc4ZxQiqFamu
@github-actions github-actions Bot added [Package] Editor /packages/editor [Package] Edit Widgets /packages/edit-widgets [Package] Preferences /packages/preferences labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

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: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: -26 B (0%)

Total Size: 8.85 MB

📦 View Changed
Filename Size Change
build/scripts/customize-widgets/index.min.js 79.2 kB -41 B (-0.05%)
build/scripts/edit-widgets/index.min.js 85.8 kB -30 B (-0.03%)
build/scripts/editor/index.min.js 575 kB -19 B (0%)
build/scripts/preferences/index.min.js 3.05 kB +64 B (+2.14%)

6c3ce0f Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 20262a5 trunk % Change
timeToFirstByte 40.6 ms +13.67% -4.31% 38 ms +13.68% -2.11% 6.84%
largestContentfulPaint 72 ms +0% -11.11% 64 ms +9.38% -6.25% 12.5%
lcpMinusTtfb 26.55 ms +15.63% -9.98% 25.85 ms +8.9% -14.89% 2.71%
wpBeforeTemplate 20.95 ms +12.46% -1.67% 19.99 ms +11.76% -2.55% 4.8%
wpTemplate 16.19 ms +6.18% -6.67% 14.48 ms +9.53% -1.38% 11.81%
wpTotal 37.25 ms +12.72% -4.24% 34.67 ms +14.65% -2.08% 7.44%
wpMemoryUsage 7.76 MB +0% -0% 7.72 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 20262a5 trunk % Change
timeToFirstByte 33.4 ms +8.68% -4.64% 33.2 ms +5.57% -4.52% 0.6%
largestContentfulPaint 60 ms +6.67% -0% 60 ms +6.67% -6.67% 0%
lcpMinusTtfb 27.55 ms +2.36% -6.17% 26 ms +11.54% -5.58% 5.96%
wpBeforeTemplate 18.24 ms +6.03% -1.86% 18.26 ms +7.06% -4% -0.11%
wpTemplate 11.98 ms +4.42% -8.93% 11.54 ms +7.89% -4.68% 3.81%
wpTotal 30.54 ms +9.1% -4.88% 30.27 ms +6.38% -5.02% 0.89%
wpMemoryUsage 6.44 MB +0% -0% 6.41 MB +0% -0% 0.56%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 20262a5 trunk % Change
mediaProcessingJpeg 238.34 ms +3.82% -2.74% 224.27 ms +2.92% -0.11% 6.27%
mediaProcessingAvif 3827.06 ms +1.53% -1.33% 3752.27 ms +2.34% -1.94% 1.99%
mediaProcessingJpegToAvif 2674.48 ms +1.6% -2.24% 2622.35 ms +1.53% -0.94% 1.99%

media-upload

Metric 20262a5 trunk % Change
jpegUploadProcessing 1467.3 ms +2.21% -1.57% 1470.88 ms +0.47% -1.86% -0.24%
pngUploadProcessing 222.07 ms +10.16% -1.65% 203.51 ms +2.17% -3.95% 9.12%
largeJpegUploadProcessing 1448.13 ms +0.93% -0.56% 1410.72 ms +1.77% -0.36% 2.65%
multipleImageUploadProcessing 1527 ms +0.82% -1.72% 1493.04 ms +0.67% -1.68% 2.27%

post-editor

Metric 20262a5 trunk % Change
serverResponse 315.17 ms +7.09% -6.3% 326.98 ms +2.34% -3.71% -3.61%
firstPaint 367.47 ms +6.19% -10.33% 358.47 ms +4.38% -7.16% 2.51%
domContentLoaded 681.93 ms +8.92% -4.18% 656.81 ms +2.9% -1.22% 3.82%
loaded 683.29 ms +8.95% -4.18% 658.2 ms +2.89% -1.23% 3.81%
firstContentfulPaint 390.31 ms +8.46% -5.96% 374.59 ms +2.54% -4.33% 4.2%
firstBlock 1810.4 ms +5.72% -1.77% 1762.39 ms +2.81% -1.9% 2.72%
type 16.69 ms +4.49% -11.74% 15.63 ms +2.56% -3.2% 6.78%
typeWithoutInspector 16.91 ms +7.1% -8.57% 16.74 ms +5.32% -15.59% 1.02%
typeWithTopToolbar 22.98 ms +1.74% -6.92% 23.54 ms +1.27% -5.31% -2.38%
typeContainer 7.04 ms +10.09% -9.38% 7.09 ms +5.64% -6.06% -0.71%
focus 98.91 ms +7.55% -6.32% 100.57 ms +8.27% -4.94% -1.65%
firstFocus 234.63 ms +0% -0% 225.12 ms +0% -0% 4.22%
selectAll 329.59 ms +0.72% -3.76% 318.33 ms +6.2% -1.09% 3.54%
undo 27.05 ms +5.47% -5.8% 27.04 ms +9.17% -3.25% 0.04%
listViewOpen 33.32 ms +9.42% -9.18% 32.67 ms +4.01% -9.4% 1.99%
inserterOpen 33.29 ms +8.92% -8.86% 34.48 ms +8.82% -6.93% -3.45%
inserterHover 9.5 ms +6.11% -7.47% 10.32 ms +8.53% -7.36% -7.95%
inserterSearch 6.81 ms +2.35% -2.35% 7 ms +4.14% -4.14% -2.71%
loadPatterns 519.86 ms +1.4% -2.49% 553.29 ms +8.31% -2.26% -6.04%
wpTotal 306.58 ms +7.24% -6.47% 318.24 ms +2.41% -3.82% -3.66%
wpMemoryUsage 14.80 MB +0% -0% 14.76 MB +0% -0% 0.25%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 20262a5 trunk % Change
serverResponse 492.1 ms +1.22% -5.04% 484.58 ms +0.94% -7.21% 1.55%
firstPaint 415.11 ms +18.6% -8.04% 426.8 ms +12.22% -10.69% -2.74%
domContentLoaded 867.24 ms +3.44% -1.64% 846.95 ms +1.47% -1.42% 2.4%
loaded 868.78 ms +3.44% -1.6% 848.78 ms +1.47% -1.45% 2.36%
firstContentfulPaint 480.65 ms +8.11% -1.15% 465.27 ms +2.94% -1.65% 3.31%
firstBlock 2709.71 ms +1.76% -0.34% 2644.58 ms +1.65% -0.89% 2.46%
type 18.34 ms +5.89% -8.02% 18.42 ms +22.64% -5.05% -0.43%
navigate 120.1 ms +1.49% -2.63% 122.68 ms +0.29% -8.18% -2.1%
loadPatterns 1652.08 ms +8.91% -5.92% 1696.37 ms +7.11% -3.25% -2.61%
loadPages 998.13 ms +3.3% -0.98% 1009 ms +4.33% -1.27% -1.08%
wpTotal 482.57 ms +1.19% -5.3% 475.01 ms +0.81% -7.31% 1.59%
wpMemoryUsage 12.84 MB +0% -0% 12.80 MB +0% -0% 0.28%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

6c3ce0f Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

region-scoped focus that refrains on traversals in /test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts, passed after 1 failed attempt.
Error: expect(locator).toHaveCount(expected) failed

Locator:  locator('[data-wp-router-region="lifecycle-a"] :focus')
Expected: 0
Received: 1
Timeout:  5000ms

Call log:
  - Expect "toHaveCount" locator('[data-wp-router-region="lifecycle-a"] :focus') with timeout 5000ms
  - waiting for locator('[data-wp-router-region="lifecycle-a"] :focus')
    14 × locator resolved to 1 element
       - unexpected value "1"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/interactivity/router-navigation-lifecycle.spec.ts:1674:37
should give the image its context menu while selected @WebKit @Firefox in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Test timeout of 100000ms exceeded.
should undo without broken temporary state in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:image -->
- <figure class="wp-block-image"><img alt=""/></figure>
+ <!-- wp:image {"id":20,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8891/wp-content/uploads/2026/12/daf4a77c-d3ae-4bc3-a154-3e8e2baf370d.png" alt="" class="wp-image-20"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49

6c3ce0f Run

@t-hamano t-hamano added the [Type] Code Quality Issues or PRs that relate to code quality label Dec 3, 2026
@t-hamano
t-hamano marked this pull request as ready for review December 3, 2026 08:27
@t-hamano
t-hamano requested review from Mamaduka and ciampo December 3, 2026 08:28
* @param options.label Label of the preference.
* @param options.messageActivated Message announced on activation.
* @param options.messageDeactivated Message announced on deactivation.
* @param options.handleToggling Whether the hook toggles the preference.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The handleToggling kind of conflicts with the hook's name/purpose, which implies that preferences are toggled.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point. In 8ae1bf6, I replaced handleToggling and onToggle with a toggle option, which defaults to toggling the preference in the store.

@ciampo ciampo left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the follow-up!. The hook keeps the behavior of all four copies. I left a few small suggestions inline, none of them blocking.

t-hamano and others added 5 commits December 3, 2026 10:11
The hook now always toggles the preference and announces it. The `toggle`
option only replaces how the preference is toggled, which distraction free
mode needs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011g9URxg61iAc4ZxQiqFamu
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011g9URxg61iAc4ZxQiqFamu
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011g9URxg61iAc4ZxQiqFamu

@ciampo ciampo left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM 🚀

Comment on lines +35 to 47
toggle: handleToggling ? undefined : () => onToggle?.(),
} );

return (
<MenuItem
icon={ isActive ? check : null }
isSelected={ isActive }
onClick={ () => {
onToggle();
if ( handleToggling ) {
toggle( scope, name );
onToggle?.();
}
speakMessage();
toggle();
} }

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nit, optional: onToggle gets called from two places here. We could pass toggle: handleToggling ? undefined : () => {} and always call onToggle?.(); toggle(); instead. Same order, and the editor copy could do the same.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Edit Widgets /packages/edit-widgets [Package] Editor /packages/editor [Package] Preferences /packages/preferences [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants