Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(TreeList): scope focus-visible outline to the focused row only (#4156)

Ishan Jain43c2dc9a6d00
diff --git a/.changeset/treelist-focus-ring-leak.md b/.changeset/treelist-focus-ring-leak.md
new file mode 100644
index 0000000000..1b1f1bdb8f
--- /dev/null
+++ b/.changeset/treelist-focus-ring-leak.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] TreeList: focusing a parent row no longer leaks the focus-visible outline onto its descendant rows — each row's ring now resolves from its own nearest treeitem instead of matching any focused ancestor (#4130)
+@is-jain
diff --git a/packages/core/src/TreeList/TreeList.test.tsx b/packages/core/src/TreeList/TreeList.test.tsx
index 4247a4759b..9e5eb07f7e 100644
--- a/packages/core/src/TreeList/TreeList.test.tsx
+++ b/packages/core/src/TreeList/TreeList.test.tsx
@@ -10,7 +10,7 @@
*/
import {describe, it, expect, vi} from 'vitest';
-import {render, screen} from '@testing-library/react';
+import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {TreeList} from './TreeList';
import type {TreeListItemData} from './TreeListTypes';
@@ -251,6 +251,37 @@ describe('TreeList', () => {
expect(screen.getByText('Leaf')).toBeInTheDocument();
});
+ // ===========================================================================
+ // Focus-visible outline scoping (regression: focusing a parent row must not
+ // leak the ring onto descendant rows — see #4130)
+ // ===========================================================================
+
+ it('scopes the focus-visible outline to the focused row, not its descendants', () => {
+ render(<TreeList items={deepItems} />);
+ const root = screen.getByText('Root').closest('li')!;
+ const mid = screen.getByText('Mid').closest('li')!;
+ const leaf = screen.getByText('Leaf').closest('li')!;
+
+ // A keydown before .focus() establishes keyboard modality so jsdom's
+ // :focus-visible heuristic applies deterministically, regardless of
+ // pointer events left over from other tests in this file.
+ fireEvent.keyDown(document.body, {key: 'Tab'});
+ root.focus();
+ expect(root).toHaveFocus();
+
+ expect(
+ getComputedStyle(root).getPropertyValue('--_tree-focus-outline'),
+ ).toContain('solid');
+ // Mid and Leaf are DOM descendants of Root's <li> (nested <ul role="group">
+ // subtrees) — their own outline var must stay unset, not inherit Root's.
+ expect(
+ getComputedStyle(mid).getPropertyValue('--_tree-focus-outline'),
+ ).toBe('none');
+ expect(
+ getComputedStyle(leaf).getPropertyValue('--_tree-focus-outline'),
+ ).toBe('none');
+ });
+
// ===========================================================================
// Interactive items
// ===========================================================================
diff --git a/packages/core/src/TreeList/TreeListItem.tsx b/packages/core/src/TreeList/TreeListItem.tsx
index 14f0d14cc0..3e1fed947f 100644
--- a/packages/core/src/TreeList/TreeListItem.tsx
+++ b/packages/core/src/TreeList/TreeListItem.tsx
@@ -29,7 +29,6 @@ import {mergeProps} from '../utils';
import {useLinkComponent} from '../Link/useLinkComponent';
import {TreeListBranches} from './TreeListBranches';
import type {TreeListDensity} from './TreeListTypes';
-import {treeItemScope} from './treeListItem.markers.stylex';
import {themeProps} from '../utils/themeProps';
import {useTranslator} from '../i18n';
@@ -47,6 +46,19 @@ const styles = stylex.create({
// The treeitem row is the roving-tabindex focus owner; suppress the
// native focus ring in favor of the row's :focus-visible outline below.
outline: 'none',
+ // Publish this row's own focus state as an inheritable CSS variable
+ // instead of matching it via an ancestor selector. Every nested <li>
+ // redeclares these vars (default: 'none' / '0'), so a descendant row's
+ // default shadows an ancestor's active value — the ring can never leak
+ // past the nearest containing treeitem, however deep the tree nests.
+ '--_tree-focus-outline': {
+ default: 'none',
+ ':focus-visible': `2px solid ${colorVars['--color-accent']}`,
+ },
+ '--_tree-focus-outline-offset': {
+ default: '0',
+ ':focus-visible': '2px',
+ },
},
childGroup: {
margin: 0,
@@ -86,17 +98,14 @@ const styles = stylex.create({
},
focusVisibleOutline: {
outline: {
- default: 'none',
- // Focus lives on the treeitem row (the <li>) via roving tabindex.
- // Scoped to the row's OWN treeitem so focusing a parent does not leak
- // the ring onto descendant rows. Also support inner focusable actions.
- [stylex.when.ancestor(':focus-visible', treeItemScope)]:
- `2px solid ${colorVars['--color-accent']}`,
+ // Reads the row's own --_tree-focus-outline (published on the <li> in
+ // `wrapper`), which resolves to the nearest containing treeitem only.
+ default: 'var(--_tree-focus-outline, none)',
+ // Also support inner focusable actions.
':has(:focus-visible)': `2px solid ${colorVars['--color-accent']}`,
},
outlineOffset: {
- default: '0',
- [stylex.when.ancestor(':focus-visible', treeItemScope)]: '2px',
+ default: 'var(--_tree-focus-outline-offset, 0)',
':has(:focus-visible)': '2px',
},
},
@@ -451,7 +460,7 @@ export function TreeListItem({
data-tree-id={id}
data-tree-level={nestedLevel + 1}
data-tree-disabled={isDisabled || undefined}
- {...stylex.props(styles.wrapper, treeItemScope)}>
+ {...stylex.props(styles.wrapper)}>
<div {...stylex.props(styles.treeBranches)}>
<TreeListBranches
ancestorsIsLast={ancestorsIsLast}
diff --git a/packages/core/src/TreeList/treeListItem.markers.stylex.ts b/packages/core/src/TreeList/treeListItem.markers.stylex.ts
deleted file mode 100644
index 1f47036fa7..0000000000
--- a/packages/core/src/TreeList/treeListItem.markers.stylex.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-// Copyright (c) Meta Platforms, Inc. and affiliates.
-
-/**
- * @file treeListItem.markers.stylex.ts
- * @input Uses StyleX defineMarker
- * @output Exports treeItemScope marker
- * @position Scoped marker; consumed by TreeListItem.tsx
- *
- * SYNC: When modified, update /packages/core/src/TreeList/TreeListItem.tsx
- */
-
-import * as stylex from '@stylexjs/stylex';
-
-/**
- * Scoped marker placed on each treeitem row (`<li role="treeitem">`).
- * Binds the row's `:focus-visible` outline to its OWN treeitem, so focusing
- * a parent treeitem does not leak the focus ring onto descendant rows.
- */
-export const treeItemScope: ReturnType<typeof stylex.defineMarker> =
- stylex.defineMarker();