Skip to content

fix(TreeList): respect consumer onKeyDown preventDefault cancellation - #5606

Merged
cixzhang merged 5 commits into
meta:mainfrom
Geervan:fix/treelist-onkeydown-cancellation
Sep 3, 2026
Merged

cixzhang merged 5 commits into
meta:mainfrom
Geervan:fix/treelist-onkeydown-cancellation

Conversation

@Geervan

@Geervan Geervan commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Fixes #5584

A builder using TreeList's accepted onKeyDown prop to intercept or reserve specific keys (such as ArrowDown, ArrowUp, or Enter) could not cancel the built-in WAI-ARIA tree keyboard navigation.

Calling event.preventDefault() in onKeyDown executed and type-checked without warning, but focus and roving tabindex moved to the adjacent tree item anyway. This broke the standard React component event contract and prevented applications from implementing custom keyboard shortcuts or drag-and-drop reordering without triggering unwanted list navigation.

Root Cause

In TreeList.tsx, ...restProps placed consumer attributes (including onKeyDown) onto the outer <div class="astryx-tree-list"> wrapper, while internal APG keyboard navigation (handleKeyDown from useTreeFocus) was attached to the inner <ul role="tree">.

Because DOM keyboard events originate on <li role="treeitem"> elements inside the <ul>, event bubbling delivered the event to <ul role="tree"> first. useTreeFocus updated tabindex and called .focus() on the target item before the event bubbled up to the outer <div>. By the time the consumer's onKeyDown executed event.preventDefault(), focus had already jumped.

Solution

  1. Extract & Compose onKeyDown: Destructured onKeyDown from TreeListProps and composed it directly on <ul role="tree"> using composeEventHandlers(onKeyDownProp, handleKeyDown).
  2. Execution Order: composeEventHandlers invokes the consumer's onKeyDown first. If event.preventDefault() is called, composeEventHandlers halts execution before handleKeyDown runs, keeping focus and roving tabindex locked on the active item.
  3. Type Safety: Applied type casting (onKeyDownProp as unknown as React.KeyboardEventHandler<HTMLUListElement>) to resolve TypeScript element type mismatches between HTMLDivElement props and HTMLUListElement listeners.

Verification

  • Added regression unit tests to TreeList.test.tsx verifying:
    • event.preventDefault() blocks built-in navigation and preserves active item focus/tabindex.
    • Non-canceling consumer onKeyDown handlers still fire alongside built-in keyboard navigation.
  • Executed Vitest test suite (83/83 tests passing).
  • Tested interactive behavior in Storybook.

Changeset

Added patch changeset .changeset/treelist-onkeydown-cancellation.md for @astryxdesign/core.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Aug 27, 2026
@vercel

vercel Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
astryx Ready Ready Preview Sep 3, 2026 12:16pm UTC

Request Review

@github-actions github-actions Bot added community Authored by a community contributor (not on the eng/design team) needs:code-review High-risk change (new package/component/API) — needs human code review before merge labels Aug 27, 2026
github-actions Bot added a commit that referenced this pull request Aug 27, 2026
@github-actions

github-actions Bot commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

PR Analysis Report

📚 Storybook Preview

View Storybook for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

🧪 Sandbox Preview

View Sandbox for this PR
GitHub Pages may take up to a minute to hydrate after deploy.

Modified Components

TreeList (@astryxdesign/core) · View in Storybook
Metric Before After Delta
Bundle Size (ESM) N/A N/A N/A
Lines of Code N/A 805 -
Complexity N/A Very High (86) -

Bundle Size Summary

Package Size (ESM) Size (CJS) Gzipped
@astryxdesign/core N/A 4.8KB 1.2KB

Accessibility Audit

Status: No accessibility violations detected.

Visual Regression

12 added · 0 removed. View the report

A repository maintainer can accept these exact frames: /accept-visual 33753971655/1 <why every changed frame is correct>

Added — After
Added — After visual regression frame

Added — After
Added — After visual regression frame

Added — After
Added — After visual regression frame


Generated by PR Enrichment workflow | Storybook | Sandbox | View full report

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing the cancellation order. One compatibility issue remains: a builder's onKeyDown now receives the inner <ul> as event.currentTarget, although TreeListProps and the ref promise the root <div> (TreeList.tsx:356). Chromium and a focused test confirm event.currentTarget !== ref.current. Please preserve the root handler contract while letting consumers cancel built-in tree navigation, and assert both cancellation and currentTarget === ref.current. If you'd rather talk it through with someone, we're in Discord.

[Reviewed by Robohands]

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the root currentTarget contract is fixed, but moving TreeList’s internal keyboard handler to the root also captures keys from the public header slot. In Chromium at this head, focusing a header button and pressing ArrowDown moves focus to the first tree item; the pre-PR base leaves focus on the button. Please keep internal tree navigation scoped to events originating inside ul[role="tree"] while preserving the root consumer handler and its preventDefault() cancellation. If you’d rather talk it through with someone, we’re in Discord.

[Reviewed by Robohands]

github-actions Bot added a commit that referenced this pull request Sep 3, 2026

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this now preserves the root handler contract, consumer cancellation, and header keyboard behavior. Please update the stale <ul>/composeEventHandlers changeset wording before merge.

[Reviewed by Robohands]

@github-actions github-actions Bot removed the needs:code-review High-risk change (new package/component/API) — needs human code review before merge label Sep 3, 2026
@Geervan

Geervan commented Sep 3, 2026

Copy link
Copy Markdown
Contributor Author

done, updated the changeset as requested!

@cixzhang cixzhang left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, canceled keys now keep focus in place while preserving the root currentTarget, normal tree navigation, and header behavior. The changeset matches the implementation.

[Reviewed by Robohands]

@cixzhang

cixzhang commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

/accept-visual 33753971655/1 All 12 exact-head TreeList baseline frames render the intended unchanged hierarchy across the six light/dark themes; the PR changes keyboard event handling, not appearance.

@cixzhang
cixzhang merged commit f0e2b49 into meta:main Sep 3, 2026
27 of 28 checks passed
@github-actions

github-actions Bot commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Visual acceptance refused: the pull request is not open against main.

josephfarina pushed a commit that referenced this pull request Sep 4, 2026
…#5606)

* fix(TreeList): respect consumer onKeyDown preventDefault cancellation

* docs(changeset): add author tag @Geervan to TreeList changeset

* fix(TreeList): bind onKeyDown to root div and assert cancellation and currentTarget === ref.current

* fix(TreeList): scope internal tree navigation to keydown events originating inside ul role=tree

* docs(changeset): update TreeList changeset to reflect root container onKeyDown handling
@github-actions
github-actions Bot deleted the fix/treelist-onkeydown-cancellation branch September 4, 2026 10:59
AKnassa added a commit to AKnassa/astryx that referenced this pull request Sep 26, 2026
Resolved conflicts:
- packages/core/src/TreeList/TreeList.test.tsx: both sides appended tests at
  the end of the top-level describe('TreeList'). Kept both: this branch's
  describe('renderExpandIcon') block (with its closing brace), followed by
  upstream's three consumer onKeyDown tests (meta#5606).

TreeList.tsx, TreeListItem.tsx, TreeListTypes.ts and TreeList.doc.mjs
auto-merged; the merged diff against upstream/main is only this PR's delta.

This branch was successfully deployed

1 active deployment
Preview — 258c17dd Deployed Sep 3, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. community Authored by a community contributor (not on the eng/design team)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants