Skip to content

[autocomplete] Keyboard navigation does not scroll a listbox whose options are positioned with CSS transform #49653

Closed

Description

@micmcg

Steps to reproduce

Live example: https://stackblitz.com/edit/vitejs-vite-qrvv2tm3?file=src%2FApp.tsx

The example renders react-window 2's List as the listbox element, so the listbox is the element that scrolls:

import Autocomplete from '@mui/material/Autocomplete';
import TextField from '@mui/material/TextField';
import * as React from 'react';
import { List, type RowComponentProps } from 'react-window';

type OptionElement = React.ReactElement<React.HTMLAttributes<HTMLElement>>;

function Row({ index, style, options }: RowComponentProps<{ options: OptionElement[] }>) {
  const option = options[index];
  return React.cloneElement(option, { style: { ...option.props.style, ...style } });
}

function Listbox({
  children,
  style,
  ref,
  ...other
}: React.HTMLAttributes<HTMLElement> & { ref?: React.Ref<HTMLElement> }) {
  const options = React.Children.toArray(children) as OptionElement[];
  return (
    <List
      {...other}
      listRef={(api) => {
        if (typeof ref === 'function') ref(api?.element ?? null);
      }}
      rowComponent={Row}
      rowProps={{ options }}
      rowCount={options.length}
      rowHeight={36}
      tagName="ul"
      style={{ ...style, maxHeight: undefined }}
    />
  );
}

const OPTIONS = Array.from({ length: 1000 }, (_, i) => `Option ${i + 1}`);

export default function Demo() {
  return (
    <Autocomplete
      sx={{ width: 300 }}
      options={OPTIONS}
      renderInput={(params) => <TextField {...params} label="1,000 options" />}
      slotProps={{ listbox: { component: Listbox } }}
    />
  );
}
  1. Open the preview in its own browser tab, so that the key presses reach it.
  2. Click the input.
  3. Press the Down arrow about 20 times.

Current behavior

The highlighted option moves below the visible part of the listbox, and the listbox does not scroll. When the highlight passes the last option that react-window has rendered, the next key press moves the highlight back to the first option.

Expected behavior

The listbox scrolls to keep the highlighted option in view, as it does for an Autocomplete that is not virtualised.

Context

useAutocomplete scrolls the highlighted option into view with the option's offsetTop (source). offsetTop ignores CSS transforms.

react-window 2 positions each row with transform: translateY(...). The TanStack Virtual examples position rows the same way. In such a listbox, every option has an offsetTop of 0. The scroll code then finds that the option is already in view and does not scroll.

The Virtualization demo avoids the scroll code. Its List scrolls inside the listbox element, so the listbox element itself never scrolls. The demo then passes onHighlightChange, keeps a map from each option to its row index, and calls react-window's scrollToRow. Each app that virtualises an Autocomplete copies that code.

A possible fix is to measure the option with getBoundingClientRect() relative to the listbox, because that measurement includes transforms:

const listboxRect = listboxNode.getBoundingClientRect();
const optionRect = option.getBoundingClientRect();
const optionTop = optionRect.top - listboxRect.top - listboxNode.clientTop + listboxNode.scrollTop;
const optionBottom = optionTop + optionRect.height;

With this change, the listbox in the example keeps the highlighted option in view with no onHighlightChange code. We have not tested this patch in MUI. We tested the effect from the other side: when the row component converts react-window's translateY to top, the example scrolls correctly with the current MUI code.

This issue is only about the scroll. A separate limit stays: Home, End and wrap-around skip options that are not in the DOM, because validOptionIndex treats a missing option as disabled.

Your environment

  • @mui/material 9.5.0
  • react-window 2.5.0
  • React 19.3.1
  • Chrome 154, macOS

Search keywords: autocomplete virtualization offsetTop transform react-window scroll keyboard

Activity

  1. added
    status: waiting for maintainer
    on Nov 3, 2026
  2. pavstev commented on Nov 3, 2026

    @pavstev
    Contributor

    I can reproduce this on current master with react-window 2.3.4 (the version in docs/package.json) in Chromium, Firefox and WebKit: every row gets transform: translateY(...) and the same offsetTop of 8, so the highlight leaves the listbox after about 10 ArrowDown presses. I'd measure the option against the listbox in syncHighlightedIndexToDOM and keep the rest of the scroll block as it is; scale keeps layout pixels when an ancestor is scaled, and the < 1 check is there because offsetHeight is rounded:

    const scale = Math.abs(listboxRect.height - listboxNode.offsetHeight) < 1 ? 1 : listboxRect.height / listboxNode.offsetHeight;
    const elementTop = (optionRect.top - listboxRect.top) / scale - listboxNode.clientTop + listboxNode.scrollTop;

    I compared it with master on the default, groupBy, bordered and disablePortal listboxes, and the scrollTop after every arrow, Page, Home and End key is the same as before in all three browsers. I left out scrollIntoView({ block: 'nearest' }) because it would also scroll the page and ignore the group header offset. @silviuaavram, would you take a PR with this change and two browser tests (plain, and inside a scaled ancestor)?

  3. added
    scope: autocomplete
    on Nov 3, 2026
  4. jiwoo1222 commented on Nov 7, 2026

    @jiwoo1222

    I’m going to prepare a focused fix and regression coverage for transform-positioned virtualized listbox options, based on the reproduction and geometry approach discussed here.

  5. pavstev commented on Nov 10, 2026

    @pavstev
    Contributor

    There is already a PR for this, #49663, open since 3 Nov. It has the getBoundingClientRect change from the issue and two browser tests (plain transform, and inside a scaled ancestor). All tests and builds pass, and it merges cleanly into current master. @jiwoo1222, if you see a gap in it, a review comment there would help most, and I will adjust the PR.

  6. added
    type: bug
    and removed
    status: waiting for maintainer
    on Nov 12, 2026
  7. ZeeshanTamboli commented on Nov 12, 2026

    @ZeeshanTamboli
    Member

    Thanks for reporting. It seems like a bug.

  8. github-actions commented on Nov 13, 2026

    @github-actions
    Contributor

    This issue has been closed. If you have a similar problem but not exactly the same, please open a new issue.
    Now, if you have additional information related to this issue or things that could help future readers, feel free to leave a comment.


    @micmcg Thanks again for creating this issue! If you have a moment, we would love to hear your thoughts on how we handled it with this short feedback form.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    scope: autocompletetype: bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions