Skip to content

[Bug] TreeList consumer onKeyDown cannot cancel built-in navigation #5584

Closed

Description

@cixzhang

Impact

A builder who uses TreeList's accepted onKeyDown prop to reserve a key cannot cancel the built-in tree behavior. Calling preventDefault() for ArrowDown still moves focus to the next tree item before the consumer handler runs, so a keyboard user gets navigation the app explicitly tried to suppress. The handler type-checks and fires; nothing warns that cancellation is too late.

Current main

At cddf57e5, ...restProps puts the consumer handler on the outer <div>, while handleKeyDown runs on the inner ul[role="tree"]. The event reaches the inner handler first and bubbles outward only after focus and roving tabindex have changed.

Reproduction

it('lets a consumer prevent built-in TreeList keyboard navigation', () => {
  render(
    <TreeList
      items={[
        {id: 'one', label: 'One'},
        {id: 'two', label: 'Two'},
      ]}
      onKeyDown={event => event.preventDefault()}
    />,
  );

  const items = screen.getAllByRole('treeitem');
  items[0].focus();
  fireEvent.keyDown(items[0], {key: 'ArrowDown'});

  expect(items[0]).toHaveFocus();
  expect(items[0]).toHaveAttribute('tabindex', '0');
  expect(items[1]).toHaveAttribute('tabindex', '-1');
});

On current main this fails: focus lands on Two, with tabindex="0"; One becomes tabindex="-1".

Acceptance

  • The consumer onKeyDown runs before TreeList's navigation/toggle behavior.
  • preventDefault() leaves focus and the roving tab stop unchanged.
  • A non-canceling consumer handler still fires while the APG keyboard model works.
  • Add the focused regression test above.

Related: #3343 tracks the broader composite-navigation program; this issue is scoped to the already-public TreeList handler contract.

Activity

  1. added
    bug
    on Aug 27, 2026
  2. self-assigned this
    on Sep 2, 2026
  3. added 3 commits that reference this issue on Sep 3, 2026
    34160b6
    473391c
    8cef714
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions