Skip to content

Writing flow: enter the Search block's side by side fields at the caret column in both directions - #85555

Draft
mtias wants to merge 3 commits into
trunkfrom
auto/fix/38261-search-block-arrow-nav
Draft

mtias wants to merge 3 commits into
trunkfrom
auto/fix/38261-search-block-arrow-nav

Conversation

@mtias

@mtias mtias commented Oct 26, 2026

Copy link
Copy Markdown
Member

What?

Closes #38261.

When the up or down arrow key moves the caret into a row of side-by-side fields that belong to one block (the Search block's input and button), writing flow now lands on the field under the caret instead of the first one in DOM order going down and the last one going up. The same fields are visited in both directions.

Why?

In the Search block, pressing ArrowDown from the paragraph above visits label → input → next paragraph, while pressing ArrowUp from the paragraph below visits button → label. The button can never be reached with the arrow keys from above, the input never from below, and the round trip is not a mirror image (reported in #38261, re-confirmed on trunk in August 2026).

The cause is in getClosestTabbable (packages/block-editor/src/components/writing-flow/use-arrow-nav.ts): it returns the first focusable in DOM order (reversed when going up) that horizontally overlaps the current element. The label above the row and the paragraph below it span the full width, so both the input and the button overlap them, and DOM order decides: the input wins going down, the button going up. The direction, not the caret position, picks the field.

How?

The hook already remembers the caret's horizontal position for vertical moves (verticalRect, used by placeCaretAtVerticalEdge to keep the x column, "recreate browser behaviour"). This PR passes that x into getClosestTabbable as an optional last argument. After finding the DOM-first candidate, the function scans the focusables that follow it within the same block's own content (isInsideRootBlock: inner blocks and appenders are excluded; the walk stops as soon as it leaves the block, since a block's descendants are contiguous in DOM order) for elements in the same visual row, and keeps the one horizontally closest to x. Strict < means DOM order breaks ties; with Alt, or when there is no caret rect, x is undefined and behaviour is unchanged.

Scoping to one block is deliberate. A first version applied the rule to any row (Columns, Buttons, Gallery…) and changed the long-standing '2nd col' expectation in writing-flow.spec.js (re-asserted in #75141 and #81456). Review found it could land ArrowDown on the Buttons block's inline appender and skip a paragraph in uneven Columns, and that entry order for rows of blocks is a design decision for the writing-flow maintainers, so this PR leaves it alone: rows of blocks enter at the DOM-first block exactly as on trunk. The general "enter a row at the caret column" idea is a possible follow-up, not part of this change.

Besides the Search block, the other "side-by-side fields of one block" affected are the cells of a Table row and the buttons of a block placeholder (e.g. an empty Image block's Upload / Media Library / Insert from URL); they are now entered at the caret column too.

Considered and rejected: always DOM-first in both directions (symmetric, but the Search button becomes unreachable with Up/Down), and a Search-specific special case (the asymmetry is generic to the hook, not the block).

Reviewer notes: getClosestTabbable resolves the block wrapper with ownerDocument.getElementById( 'block-' + clientId )!; first.closest( BLOCK_SELECTOR ) would be the same element without the non-null assertion, if preferred.

Testing Instructions

  1. Insert a Paragraph with some text, a Search block, and another Paragraph with text.
  2. Put the caret at the start of the first paragraph and press ArrowDown repeatedly: focus goes to the Search block, the label, the input, then the second paragraph.
  3. Put the caret at the start of the second paragraph and press ArrowUp repeatedly: focus goes to the input, the label, then the first paragraph (on trunk it goes to the button instead).
  4. Click into the first line of the first paragraph roughly under the Search button, press ArrowDown repeatedly: focus goes to the label, then the button, then the second paragraph (on trunk: the input).
  5. From the second paragraph with the caret under the button, press ArrowUp: button, label, first paragraph.
  6. Check that Columns, Buttons and Table navigate as before; Alt+ArrowUp/Down is unchanged.

Testing Instructions for Keyboard

The steps above are keyboard-only apart from placing the caret.

Automated: npm run test:unit -- packages/block-editor/src/components/writing-flow (new getClosestTabbable tests, including the appender exclusion and DOM order for a row of blocks), and the new e2e test should navigate side by side fields in the same order up and down in test/e2e/specs/editor/various/writing-flow.spec.js, which fails on trunk.

Screenshots or screencast

Recorded in Chromium on the built plugin; each clip is ArrowDown from the first paragraph to the second, then ArrowUp back.

Before After
Caret at the left edge, ArrowUp from the paragraph below lands on the button (ArrowDown had visited the input) Lands on the input, same as ArrowDown
Caret under the button, ArrowDown from the label lands on the input (ArrowUp visits the button) Lands on the button, under the caret

Verification and what was not run

  • Writing-flow unit tests: 11 pass; the new ones fail with the source change reverse-applied.
  • npm run lint:js, tsc --noEmit, npm run build: clean.
  • Chromium, on the built plugin: the repo's writing-flow.spec.js passes all arrow-key tests (two failures unrelated to arrow keys are identical on trunk and specific to the local theme); a sequence-recording spec over Columns, uneven Columns, Buttons and Search is identical to trunk except for the Search block's up-from-below step.
  • Not run locally: Firefox and WebKit (the test's describe is tagged @firefox, @webkit, so CI covers them); Navigation, Social Links, Gallery and block placeholders were reasoned about from the code, not driven.

🤖 Generated with Claude Code

mtias and others added 2 commits October 21, 2026 19:40
Arrowing down through a Search block visited label, input, next block,
while arrowing up from the next block visited button, label: the fields
reached depended on the direction.

`getClosestTabbable` returned the first focusable in DOM order (reversed
when going up) that horizontally overlaps the current element. The Search
label and the paragraph after the block span the full width, so both
fields of the input + button row overlap them and DOM order picked the
input going down and the button going up. The same happens for every
side by side layout (columns, buttons, flex groups).

The hook already remembers the caret's horizontal position on the first
vertical key press to restore it in the target, recreating how a caret
keeps its column in a browser. Use that position to choose the target
too: among the candidates that form the row reached (the first one in
DOM order and the later ones that overlap it vertically without being
nested in it), prefer the one under the caret; ties, an unknown position
and Alt (which goes to the furthest edge) keep DOM order. This is
symmetric by construction for a given column.

"Should navigate inner blocks with arrow keys" now enters the first
column from the end of "Second paragraph", which lies under it, instead
of the last column; the expectation is updated. Considered and rejected:
always taking the DOM-first field of the row in both directions (makes
the button unreachable with the arrow keys and is not what a caret does),
and a Search-specific special case.

Refs #38261

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The previous commit chose the field under the caret among every candidate
in the row reached, which changed how the arrow keys enter rows of
blocks: Columns were entered at the column under the caret instead of the
DOM-first/last one (a writing-flow expectation re-asserted in #75141 and
#81456), ArrowDown into a Buttons block could land on its inline appender
(rendered once the wrapper is selected, and the closest focusable to the
caret), and uneven Columns could skip a paragraph on the way up.

Scope the scan to the content of the block the DOM-first candidate
belongs to (`isInsideRootBlock`: not its inner blocks, not its
appenders), and stop once the walk leaves that block, since a block's
descendants are contiguous in DOM order. The Search block's input and
button are one block, so #38261 is fixed as reported; rows of blocks
(Columns, Buttons, Gallery) and block placeholders behave exactly as
before, and `Should navigate inner blocks with arrow keys` keeps its
"2nd col" expectation.

Entering a row of blocks at the caret column is a reasonable extension
of the same idea, but it is the writing-flow maintainers' call and needs
its own design pass (which element bounds the row, appenders, uneven
columns); it is left as a proposal.

The new e2e test also covers the caret under the button, the issue's
second complaint ("Add button text" skipped on the way down).

Refs #38261

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Oct 26, 2026
@github-actions

github-actions Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

📦 Bundle size

Size Change: +192 B (0%)

Total Size: 9.07 MB

📦 View Changed
Filename Size Change
build/scripts/block-editor/index.min.js 607 kB +192 B (+0.03%)

a458968 Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 320af76 trunk % Change
timeToFirstByte 47.15 ms +4.88% -3.5% 47.2 ms +2.22% -3.5% -0.11%
largestContentfulPaint 80 ms +7.5% -5% 80 ms +7.5% -5% 0%
lcpMinusTtfb 31.55 ms +4.12% -3.33% 31.95 ms +14.4% -4.07% -1.25%
wpBeforeTemplate 24.29 ms +7.04% -4.65% 24.12 ms +3.32% -2.57% 0.7%
wpTemplate 19.18 ms +3.55% -3.75% 19.06 ms +6.09% -4.62% 0.63%
wpTotal 43.51 ms +5.4% -4.09% 43.36 ms +2.95% -3.02% 0.35%
wpMemoryUsage 7.68 MB +0% -0% 7.65 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 320af76 trunk % Change
timeToFirstByte 39.15 ms +3.83% -2.04% 39.5 ms +5.95% -2.91% -0.89%
largestContentfulPaint 88 ms +4.55% -0% 88 ms +4.55% -0% 0%
lcpMinusTtfb 49.35 ms +1.72% -1.32% 49.85 ms +2.91% -2.71% -1%
wpBeforeTemplate 21.1 ms +2.37% -1.23% 21.37 ms +4.26% -2.48% -1.26%
wpTemplate 14.83 ms +3.57% -3.98% 14.89 ms +6.04% -2.01% -0.4%
wpTotal 36.19 ms +3.32% -2.54% 36.41 ms +5.96% -2.5% -0.6%
wpMemoryUsage 6.37 MB +0% -0% 6.34 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 320af76 trunk % Change
mediaProcessingJpeg 383.67 ms +0.51% -0.5% 388.23 ms +4.53% -0.94% -1.17%
mediaProcessingAvif 5689.8 ms +0.19% -0.53% 5657.63 ms +0.26% -0.44% 0.57%
mediaProcessingJpegToAvif 3875.43 ms +0.45% -0.37% 3853.57 ms +0.31% -0.31% 0.57%

media-upload

Metric 320af76 trunk % Change
jpegUploadProcessing 1412.46 ms +37.73% -0.64% 1405.67 ms +0.68% -0.61% 0.48%
pngUploadProcessing 198.26 ms +2.06% -4.1% 204.65 ms +7.13% -3.75% -3.12%
largeJpegUploadProcessing 1407.54 ms +0.79% -0.35% 1401.21 ms +1.3% -0.17% 0.45%
multipleImageUploadProcessing 1561.43 ms +0.09% -1.45% 1527.04 ms +1.45% -2.09% 2.25%

post-editor

Metric 320af76 trunk % Change
serverResponse 301.83 ms +2.85% -7.8% 295.22 ms +2.54% -10.23% 2.24%
firstPaint 287.45 ms +31.66% -30.02% 246.17 ms +50.63% -31.72% 16.77%
domContentLoaded 861.75 ms +2.89% -2.69% 816.8 ms +1.87% -0.68% 5.5%
loaded 862.59 ms +2.91% -2.67% 817.6 ms +1.87% -0.66% 5.5%
firstContentfulPaint 376.43 ms +8.25% -3.55% 370.86 ms +1.23% -2.01% 1.5%
firstBlock 2387.18 ms +2.47% -1.96% 2296.54 ms +1.84% -1.13% 3.95%
type 16.58 ms +6.63% -4.89% 15 ms +4.4% -0.73% 10.53%
typeWithoutInspector 16.12 ms +5.58% -1.24% 15.15 ms +2.71% -3.7% 6.4%
typeWithTopToolbar 20.87 ms +5.94% -4.89% 20.29 ms +2.07% -1.13% 2.86%
typeContainer 7.14 ms +2.52% -3.64% 7.26 ms +7.16% -3.17% -1.65%
focus 95.42 ms +2.56% -4.79% 90.22 ms +3.9% -5.55% 5.76%
firstFocus 169.27 ms +0% -0% 197.76 ms +0% -0% -14.41%
selectAll 374.38 ms +0.08% -1.03% 361.64 ms +1.71% -4.57% 3.52%
listViewOpen 57.78 ms +11.06% -12.72% 54.92 ms +7.21% -3.4% 5.21%
inserterOpen 32.83 ms +7.8% -9.81% 33.9 ms +5.58% -1.18% -3.16%
inserterHover 9.53 ms +9.65% -3.88% 9.8 ms +12.14% -5.61% -2.76%
inserterSearch 7.82 ms +5.37% -3.58% 7.75 ms +13.42% -4.26% 0.9%
loadPatterns 493.21 ms +6.48% -4.29% 523.5 ms +0.87% -7.93% -5.79%
wpTotal 294.32 ms +2.98% -7.99% 287.65 ms +2.19% -10.34% 2.32%
wpMemoryUsage 14.71 MB +0% -0% 14.68 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 320af76 trunk % Change
serverResponse 368.07 ms +3.52% -4.72% 378 ms +5.2% -3.64% -2.63%
firstPaint 222.1 ms +13.1% -15.58% 294.79 ms +12.44% -13.19% -24.66%
domContentLoaded 1078.12 ms +1.35% -4.18% 1094.46 ms +1.87% -1.23% -1.49%
loaded 1079.18 ms +1.35% -4.18% 1095.52 ms +1.84% -1.22% -1.49%
firstContentfulPaint 440.72 ms +4.04% -5.44% 464.22 ms +1.38% -2.23% -5.06%
firstBlock 3743.69 ms +2.1% -1.53% 3827.22 ms +1.18% -1.23% -2.18%
type 16.76 ms +1.79% -1.37% 17.41 ms +5.4% -2.35% -3.73%
navigate 95.82 ms +14.79% -0.88% 102.42 ms +1.99% -1.57% -6.44%
loadPatterns 1091.98 ms +1.75% -4% 1068.62 ms +8.03% -5.5% 2.19%
loadPages 1025.33 ms +3.23% -1.92% 1019.56 ms +3.39% -3.33% 0.57%
wpTotal 359.49 ms +3.6% -4.86% 368.37 ms +5.58% -3.56% -2.41%
wpMemoryUsage 12.73 MB +0% -0% 12.70 MB +0% -0% 0.24%
wpDbQueries 44 +2.27% -0% 44.5 +1.12% -1.12% -1.12%

a458968 Run

The Unreleased entry linked issue #38261, and CI's changelog validation
requires every Unreleased entry to link a Gutenberg pull request. Point it
at #85555 now that the PR exists, and name the other side by side fields
of one block the change applies to (Table cells, placeholder buttons), as
the review asked.

Refs #38261

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

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] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent keyboard behaviour in Search block

1 participant