Skip to content

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

TypeScript

feat(TreeList): let a tree item carry row styles (#6238)

Ernest8112fcc621d8
diff --git a/.changeset/tree-list-item-classname.md b/.changeset/tree-list-item-classname.md
new file mode 100644
index 0000000000..c2b43b903a
--- /dev/null
+++ b/.changeset/tree-list-item-classname.md
@@ -0,0 +1,18 @@
+---
+'@astryxdesign/core': patch
+---
+
+[feat] Let a TreeList item carry row styles (#6238)
+@ernestt
+
+`TreeListItemData` gains `xstyle`, `className`, and `style`, applied to the
+item's row element, as the TreeList contract's per-item styling seam requires.
+
+`TreeList` takes rows as data, so nothing previously handed back the row's
+element. That prevents row-scoped primitives such as `useContainerReveal` from
+installing the class and inline custom properties that jointly isolate each
+row's hover and focus state; applying the reveal to the whole tree would expose
+every row action at once.
+
+`Item` already accepts the same row styling props, so a list converting to a
+tree keeps its reveal wiring instead of having to mark the whole tree.
diff --git a/packages/core/src/TreeList/TreeList.doc.mjs b/packages/core/src/TreeList/TreeList.doc.mjs
index dd6c91c835..ad4d81b317 100644
--- a/packages/core/src/TreeList/TreeList.doc.mjs
+++ b/packages/core/src/TreeList/TreeList.doc.mjs
@@ -101,7 +101,7 @@ export const docs = {
name: 'items',
type: 'TreeListItemData[]',
description:
- 'Recursive tree item data. Each item has id, label, optional children array, and optional isExpanded boolean for initial state.',
+ 'Recursive tree item data with id, label, optional children and isExpanded, plus optional xstyle, className, and style applied to that item row.',
required: true,
},
{
@@ -244,7 +244,7 @@ export const docsDense = {
],
},
propDescriptions: {
- items: 'Recursive tree item data w/ id, label, optional children + isExpanded.',
+ items: 'Recursive tree item data w/ id, label, optional children + isExpanded, plus optional xstyle/className/style on its row.',
density: 'Spacing density for items.',
variant: 'Guide-line treatment: lineGuides shows connectors, noGuides hides them (indent kept). Orthogonal to density.',
header: 'Header content, linked to tree via aria-labelledby.',
@@ -256,7 +256,7 @@ export const docsDense = {
displayName: 'Tree List',
description: 'Tree list container. Accepts items data + rendering config. Expansion managed internally.',
propDescriptions: {
- items: 'Recursive tree item data w/ id, label, optional children + isExpanded.',
+ items: 'Recursive tree item data w/ id, label, optional children + isExpanded, plus optional xstyle/className/style on its row.',
density: 'Spacing density for items.',
variant: 'Guide-line treatment: lineGuides shows connectors, noGuides hides them (indent kept). Orthogonal to density.',
header: 'Header content, linked to tree via aria-labelledby.',
diff --git a/packages/core/src/TreeList/TreeList.test.tsx b/packages/core/src/TreeList/TreeList.test.tsx
index 9c556c40d3..9c1543b9c3 100644
--- a/packages/core/src/TreeList/TreeList.test.tsx
+++ b/packages/core/src/TreeList/TreeList.test.tsx
@@ -12,6 +12,7 @@
import {describe, it, expect, vi} from 'vitest';
import {render, screen, fireEvent} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
+import * as stylex from '@stylexjs/stylex';
import {TreeList} from './TreeList';
import type {TreeListItemData} from './TreeListTypes';
import {defineTheme} from '../theme/defineTheme';
@@ -45,6 +46,10 @@ function collectCssText(): string {
.join('\n');
return out;
}
+const rowStyles = stylex.create({
+ custom: {opacity: 0.75},
+});
+
const simpleItems: TreeListItemData[] = [
{id: 'a', label: 'Item A'},
{id: 'b', label: 'Item B'},
@@ -458,6 +463,30 @@ describe('TreeList', () => {
expect(screen.getByTestId('badge')).toBeInTheDocument();
});
+ it('forwards xstyle, className, and style to an item row', () => {
+ const items: TreeListItemData[] = [
+ {
+ id: 'a',
+ label: 'Styled row',
+ xstyle: rowStyles.custom,
+ className: 'consumer-row',
+ style: {visibility: 'visible'},
+ },
+ ];
+ render(<TreeList items={items} />);
+
+ const row = screen
+ .getByText('Styled row')
+ .closest('li')!
+ .querySelector('.astryx-tree-list-item')!;
+ expect(row).toHaveClass('consumer-row');
+ expect(row.className).toContain(
+ stylex.props(rowStyles.custom).className!.split(' ')[0],
+ );
+ expect(row).toHaveStyle({visibility: 'visible'});
+ expect(row.getAttribute('style')).toContain('--_tree-indent');
+ });
+
// ===========================================================================
// Density
// ===========================================================================
diff --git a/packages/core/src/TreeList/TreeList.tsx b/packages/core/src/TreeList/TreeList.tsx
index eb800f21ba..9fd700a6ee 100644
--- a/packages/core/src/TreeList/TreeList.tsx
+++ b/packages/core/src/TreeList/TreeList.tsx
@@ -325,6 +325,9 @@ export function TreeList({
description={item.description}
startContent={item.startContent}
endContent={item.endContent}
+ xstyle={item.xstyle}
+ className={item.className}
+ style={item.style}
hasChildren={hasChildren}
hasExpandableItems={hasExpandableItems}
onClick={item.onClick}
diff --git a/packages/core/src/TreeList/TreeListItem.tsx b/packages/core/src/TreeList/TreeListItem.tsx
index 166fcb66d6..7268b0c2a8 100644
--- a/packages/core/src/TreeList/TreeListItem.tsx
+++ b/packages/core/src/TreeList/TreeListItem.tsx
@@ -292,6 +292,10 @@ export interface TreeListItemInternalProps {
target?: string;
isDisabled?: boolean;
isSelected?: boolean;
+ /** Consumer styles for the row element. See `TreeListItemData`. */
+ xstyle?: stylex.StyleXStyles;
+ className?: string;
+ style?: React.CSSProperties;
hasChildren: boolean;
/**
* Whether the tree contains at least one expandable item anywhere (i.e. a
@@ -339,6 +343,9 @@ export function TreeListItem({
description,
startContent,
endContent,
+ xstyle,
+ className,
+ style,
onClick,
href,
target,
@@ -589,15 +596,22 @@ export function TreeListItem({
interactionOverlayStyles.backgroundImage,
isDisabled && styles.disabled,
isSelected && styles.selected,
+ xstyle,
)
: stylex.props(
styles.contentWrapper,
densityStyles[density],
isDisabled && styles.disabled,
isSelected && styles.selected,
+ xstyle,
),
+ // Consumer row props are merged last so useContainerReveal can
+ // publish both its classes and inline custom properties. Seed the
+ // private indent first, then preserve the standard inline-style
+ // precedence promised by the TreeList contract.
+ className,
+ {...indentStyle, ...style},
)}
- style={indentStyle}
onClick={handleClick}>
{innerContent}
</div>
diff --git a/packages/core/src/TreeList/TreeListTypes.ts b/packages/core/src/TreeList/TreeListTypes.ts
index 2ad40168f2..da623b4771 100644
--- a/packages/core/src/TreeList/TreeListTypes.ts
+++ b/packages/core/src/TreeList/TreeListTypes.ts
@@ -11,7 +11,8 @@
* - /packages/core/src/TreeList/index.ts
*/
-import type {ReactNode} from 'react';
+import type {CSSProperties, ReactNode} from 'react';
+import type {StyleXStyles} from '@stylexjs/stylex';
import type {TreeListVariantMap} from './index';
/** Spacing density for tree list items. */
@@ -62,4 +63,13 @@ export interface TreeListItemData {
/** Whether the item is initially expanded. Only meaningful for items with children. */
isExpanded?: boolean;
+
+ /** StyleX styles applied to the item's row element. */
+ xstyle?: StyleXStyles;
+
+ /** Class applied to the item's row element. */
+ className?: string;
+
+ /** Inline styles applied to the item's row element. */
+ style?: CSSProperties;
}