Skip to content

fix(ui): scroll only CMenu's own list when it shows the focused item - #395

Merged
JuroOravec merged 1 commit into
mainfrom
fix/menu-scroll-stays-in-menu
Oct 25, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/menu-scroll-stays-in-menu

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Fixes #383.

Cause

CMenu showed its focused item with entry.element.scrollIntoView({ block: "nearest" }). That call scrolls every scrolling ancestor: the menu's list, the page, and any page that holds this one in a frame.

The menu calls it each time it updates its list of items, not only after a key press. That includes a re-render, a settings change, and a submenu that closes when the pointer leaves its trigger.

So with a submenu open, scrolling the page moved the pointer off the submenu trigger. The menu then updated, refocused its item, and scrolled the page straight back.

On the docs menu page with Chromium 156 (Playwright 1.64.1), this happened on every scroll. The docs e2e test test_menu_ui_examples_cover_choices_submenus_control_and_theme opens the nested-submenu demo and then clicks a preview about 4,000 px up the page. The page kept snapping back, Chromium runs no animation frames in an offscreen iframe, and Playwright's click waited forever. With Playwright 1.62.0, the same test passed on current main.

Fix

  • Within the menu: focusEntry scrolls only the scrollable elements between the item and the menu's popup (revealInSurface). It measures their visible area inside the border, beside any scrollbar, and scrolls by the least distance, as block: "nearest" does.
  • On the page: a key press that moves focus to an item still off screen also scrolls the page, as in a menu taller than the viewport at high zoom. The menu takes the keys a reader would scroll with, so it has to. A keyboardReveal flag is set while the key handler runs. A key that opens the menu carries that permission with its open request, because the menu focuses its first or last item after the handler returns. Nothing else scrolls the page.
  • Other components: CContextMenu and CSplitButton use the same menu code, so they get the fix too.

Known limits:

  • The page-scroll check looks at the menu's own window only, so a frame partly outside the page that holds it does not scroll that page.
  • A CContextMenu opened with Shift+F10 opens from the contextmenu event, not the key handler, so it scrolls only inside the menu. I didn't verify this path.

Left for a follow-up (same class of bug)

These components call scrollIntoView on paths that run without a key press, so they can pull the page back the same way:

  • CSelect (runtime.source.js:350) and CMultiSelect: when a server re-render reopens an open popup.
  • CCombobox, through _active_descendant.py:140: when late loadOptions results arrive or the server pushes new items.
  • CTabs (ctabs.py:591): when a re-render removes the current tab.

Not checked: focus() calls without preventScroll in CTag and CToolbar on re-render paths.

These are separate components, two of them with built runtimes, so I kept this PR to the menu.

Tests

Four new browser tests in cmenu/tests/e2e/test_cmenu_e2e.py:

  • test_an_open_menu_does_not_scroll_the_page_back_to_itself: a settings change while the page is scrolled away.
  • test_scrolling_the_page_while_a_submenu_is_open_keeps_its_position: the reported path, with the pointer resting on a submenu trigger.
  • test_a_key_scrolls_the_page_to_an_item_below_the_screen[end, open-on-last]: a 320x256 viewport.
  • test_keyboard_moves_reveal_items_inside_a_menu_that_scrolls: measured inside the border.

The first two fail on main. The key and border tests fail against an earlier draft of this change that never scrolled the page.

Evidence (local)

  • CMenu, CContextMenu and CSplitButton tests: 191 passed.
  • The docs menu e2e tests pass, including the one that timed out.
  • The phases below pass: uv lock, ruff, ruff format, mypy (also --platform linux), pyright, citry-client, codemirror Fluent, the protocol phases, the events protocol JS, vscode-extension, citry-ui assets and the validators.
  • docs-playground fails on main too: the committed docs_site/static/playground/portable_ide.py is stale since perf: walk parsed templates in check and the LSP without one Python call per level #377. This PR doesn't touch it.

PR CI runs the cargo phases and the full matrix.

🤖 Generated with Claude Code

CMenu brought its focused item into view with scrollIntoView, which also
scrolls the page and any page that holds this one in a frame. It does
that each time it updates its list of items, such as after a re-render
or after the pointer leaves a submenu trigger, so a reader who scrolled
the page with a submenu open was pulled back to the menu. On the docs
menu page, with Chromium 156, the page snapped back on every scroll, and
the docs end-to-end test that then clicks a preview far up the page
timed out.

The menu now scrolls only the scrollable elements between the item and
the menu's popup, measured against their visible area inside the border.
A key press that moves focus to an item still off screen, as in a menu
taller than the viewport at high zoom, also scrolls the page, since the
menu takes the keys a reader would scroll with. CContextMenu and
CSplitButton use the same menu code and get the same fix.

Left for a follow-up: CSelect, CMultiSelect, CCombobox (through the
shared active-option code) and CTabs also call scrollIntoView on paths
that run without a key press, such as a re-render while open or options
that arrive late, so they can pull the page back the same way.

Fixes #383

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 2fc91b2 into main Oct 25, 2026
13 of 25 checks passed
@JuroOravec
JuroOravec deleted the fix/menu-scroll-stays-in-menu branch October 25, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CMenu: with a submenu open, scrolling snaps the page back to the menu (Chromium 156)

1 participant