Skip to content

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

TypeScript

fix(TreeList): forward BaseProps attributes to the tree element (#5476) (#5493)

Gonzalo Blascob47ad01e4a15
diff --git a/.changeset/treelist-forward-baseprops.md b/.changeset/treelist-forward-baseprops.md
new file mode 100644
index 0000000000..514bede33e
--- /dev/null
+++ b/.changeset/treelist-forward-baseprops.md
@@ -0,0 +1,11 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] TreeList: forward `aria-label`/`aria-labelledby` to the `role="tree"` element so a tree can be named
+
+`TreeList` destructured a fixed set of props with no rest spread, so every `BaseProps` attribute (`aria-*`, `role`, `tabIndex`, `id`, event handlers) was dropped and never reached the DOM. A tree without a visible header could not be named at all - a screen reader announced an unnamed tree with no way to know what it was.
+
+The component now spreads the remaining props onto the root element and routes `aria-label`/`aria-labelledby` onto the `<ul role="tree">` itself, so `<TreeList aria-label="File tree">` names the tree. When a visible `header` is rendered, it keeps naming the tree (AT hears the same name the user sees); a consumer-supplied `aria-labelledby` only applies on the headerless path. The contract `role="tree"` is written after the rest spread so a consumer cannot displace it.
+
+@gonzoblasco
diff --git a/packages/core/src/TreeList/TreeList.test.tsx b/packages/core/src/TreeList/TreeList.test.tsx
index d8a19543cf..ea0dca44b5 100644
--- a/packages/core/src/TreeList/TreeList.test.tsx
+++ b/packages/core/src/TreeList/TreeList.test.tsx
@@ -145,6 +145,18 @@ describe('TreeList', () => {
expect(screen.getByTestId('tree')).toBeInTheDocument();
});
+ it('forwards aria-label to the tree element', () => {
+ render(<TreeList items={simpleItems} aria-label="File tree" />);
+ const tree = screen.getByRole('tree');
+ expect(tree).toHaveAttribute('aria-label', 'File tree');
+ });
+
+ it('forwards id to the root element', () => {
+ render(<TreeList items={simpleItems} id="file-tree" />);
+ const root = screen.getByRole('tree').parentElement;
+ expect(root).toHaveAttribute('id', 'file-tree');
+ });
+
it('renders description text', () => {
const items: TreeListItemData[] = [
{id: 'a', label: 'Label', description: 'Description text'},
@@ -194,6 +206,43 @@ describe('TreeList', () => {
expect(tree).not.toHaveAttribute('aria-labelledby');
});
+ it('prefers the visible header id over caller aria-labelledby when header exists', () => {
+ render(
+ <TreeList
+ items={simpleItems}
+ header={<span>File Tree</span>}
+ aria-labelledby="external-label"
+ />,
+ );
+ const tree = screen.getByRole('tree');
+ const headerId = tree.getAttribute('aria-labelledby');
+ expect(headerId).toBeTruthy();
+ expect(headerId).not.toBe('external-label');
+ const headerEl = document.getElementById(headerId!);
+ expect(headerEl?.textContent).toBe('File Tree');
+ });
+
+ it('uses caller aria-labelledby on the headerless path', () => {
+ render(<TreeList items={simpleItems} aria-labelledby="external-label" />);
+ const tree = screen.getByRole('tree');
+ expect(tree).toHaveAttribute('aria-labelledby', 'external-label');
+ });
+
+ it('ignores caller aria-label when a header names the tree', () => {
+ render(
+ <TreeList
+ items={simpleItems}
+ header={<span>File Tree</span>}
+ aria-label="External name"
+ />,
+ );
+ const tree = screen.getByRole('tree');
+ expect(tree).not.toHaveAttribute('aria-label');
+ const headerId = tree.getAttribute('aria-labelledby');
+ const headerEl = document.getElementById(headerId!);
+ expect(headerEl?.textContent).toBe('File Tree');
+ });
+
// ===========================================================================
// Expansion (internal state)
// ===========================================================================
diff --git a/packages/core/src/TreeList/TreeList.tsx b/packages/core/src/TreeList/TreeList.tsx
index c79c18b529..3578d9da6f 100644
--- a/packages/core/src/TreeList/TreeList.tsx
+++ b/packages/core/src/TreeList/TreeList.tsx
@@ -188,7 +188,10 @@ export function TreeList({
className,
style,
'data-testid': testId,
+ 'aria-label': ariaLabel,
+ 'aria-labelledby': ariaLabelledby,
ref,
+ ...restProps
}: TreeListProps) {
const headerId = useId();
@@ -336,7 +339,8 @@ export function TreeList({
stylex.props(styles.root, xstyle),
className,
style,
- )}>
+ )}
+ {...restProps}>
{header != null && (
<div id={headerId} {...stylex.props(styles.header)}>
{header}
@@ -345,7 +349,8 @@ export function TreeList({
<ul
ref={treeRef}
role="tree"
- aria-labelledby={header != null ? headerId : undefined}
+ aria-label={header != null ? undefined : ariaLabel}
+ aria-labelledby={header != null ? headerId : ariaLabelledby}
onKeyDown={handleKeyDown}
onFocus={handleFocus}
{...stylex.props(styles.list)}>