Repository navigation
Rich Text: Only run the autocomplete for the selected field - #85570
youknowriad wants to merge 1 commit into
Conversation
Completions are only offered while typing in a field, which is then the selected field, yet every rich text field ran the autocomplete hook, its effects and keydown listener, and the effect mirroring its ARIA attributes. Move them into a component that renders as the field's child, where the autocomplete popover rendered, only while the field is selected. It attaches to the field as its ref did, and hands the ARIA attributes back to the field to render, so the field's markup is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
🤖 PR meta 🤖🎉 PropsIf 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: -587 B (-0.01%) Total Size: 8.55 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome 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. Backspace in
|
|
I think Ella tried something similar in #78304. |
|
This did produce a result. 2% improvement in block mounting but it's small so not sure worth the weird pattern (sub component that calls a state on the parent component) |
What?
Only the selected rich text field runs the autocomplete. Every other field skips its hook, effects, keydown listener and completers. The field's markup and the autocomplete's behavior are unchanged.
This is a first step in reducing the per-field cost of
RichText, which dominates rendering long posts.Why?
Every block's rich text field mounts the full
RichTextWrapper: about 100 hooks and 26 effects per field on trunk. With 1,234 fields in the large post used by the performance tests, that is most of the cost of rendering the blocks. A prototype that rendered unselected fields as static HTML (keeping themcontenteditable) put an upper bound of roughly 60ms on what reducing this per-field cost could gain for the first block metric there.Part of it is machinery that only matters for the selected field. The autocomplete is the clearest case: completions are only offered while typing in a field, which is then the selected one.
How?
The autocomplete (
useBlockEditorAutocompleteProps) and the effect mirroring its ARIA attributes onto an editing host move into aSelectedFieldAutocompletecomponent.keydownlistener is therefore registered at the same point relative to the other field listeners as before: after the rich text package's, before the block editor's selected-only ones.aria-autocomplete,aria-haspopup,aria-controls,aria-owns,aria-activedescendant) back toRichTextWrapper. The wrapper spreads them on the field where the autocomplete props were spread, so React still renders them, in the same commit as the popover.Mounting on selection rather than with the field changes nothing visible:
useAutocompletePropsonly shows the popover and the ARIA attributes after the text changes, and a freshly mounted autocomplete starts from the current value, so nothing opens on selection.Not moved, because they don't only matter for the selected field:
useMarkPersistentalso marks changes made from outside a field (formatting across multi-selected blocks, undo), and would cancel a pending typing debounce on deselection.useRichText's own layout effects, which a child's effects can't.Per unselected field,
RichTextWrappergoes from 100 hooks and 26 effects to 81 hooks and 23 effects.On the large performance-test post (1,234 rich text fields), measured locally over 4 interleaved rounds of 10 loads, first block went from 579/577/581/575ms to 569/570/577/574ms: about −5ms, roughly 1%. In CPU profiles,
RichTextWrapperdrops from ~20.5ms to ~18.2ms per load. The larger gains need lazier activation of the field itself (useRichTextand its listeners), which is a bigger change.Testing Instructions
/head: the block list opens, arrow keys move through it,Enterinserts a heading, andEscapedismisses it.@followed by a user name: the mention list works as before.aria-autocomplete,aria-haspopup,aria-controls,aria-ownsandaria-activedescendant. They are removed when it closes. In a block supportingeditableRoot, they are mirrored onto the editing host.The behavior and resulting content of the slash inserter were identical to trunk in a scripted check. The
autocomplete-and-mentions,rich-text,writing-flow,splitting-merging,multi-block-selection,inserting-blocks,footnotesandblock-editor-keyboard-shortcutse2e specs, plus the paragraph, heading, list and quote block specs, pass in Chromium locally. The only failures were twomulti-block-selectiontests and the "newlines after multiple mention completions" test, which also fail intermittently on trunk locally.Use of AI Tools
Profiled, implemented and measured with Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code