An open source design system that's fully customizable and agent ready
TypeScript
feat(tree-list): add astryx-tree-list-chevron theme target (#4305)
freddymeta68afe6995b3e
diff --git a/.changeset/tree-list-chevron-theme-target.md b/.changeset/tree-list-chevron-theme-target.mdnew file mode 100644index 0000000000..1a876c215c--- /dev/null+++ b/.changeset/tree-list-chevron-theme-target.md@@ -0,0 +1,7 @@+---+'@astryxdesign/core': patch+---++[feat] TreeList: add a dedicated `astryx-tree-list-chevron` theme target for the expand/collapse toggle, so consumers can theme the chevron (color, per open/closed state) via `defineTheme` instead of reaching it through the functional `[data-tree-toggle]` attribute. Reflects the open/closed state as a `data-state` attribute (`expanded`/`collapsed`); the functional `data-tree-toggle` hook is unchanged.++@freddymetadiff --git a/apps/storybook/stories/TreeList.stories.tsx b/apps/storybook/stories/TreeList.stories.tsxindex 0e9c3260c6..c67da19706 100644--- a/apps/storybook/stories/TreeList.stories.tsx+++ b/apps/storybook/stories/TreeList.stories.tsx@@ -5,6 +5,7 @@ import {TreeList} from '@astryxdesign/core/TreeList';import type {TreeListItemData} from '@astryxdesign/core/TreeList';import {Icon} from '@astryxdesign/core/Icon';import {Badge} from '@astryxdesign/core/Badge';+import {Theme, defineTheme} from '@astryxdesign/core/theme';import {FolderIcon,DocumentIcon,@@ -265,12 +266,22 @@ export const WithEndContent: Story = {isExpanded: true,endContent: <Badge label="3" />,children: [- {id: 'unread', label: 'Unread', onClick: noop, endContent: <Badge label="3" />},+ {+ id: 'unread',+ label: 'Unread',+ onClick: noop,+ endContent: <Badge label="3" />,+ },{id: 'starred', label: 'Starred', onClick: noop},],},{id: 'sent', label: 'Sent', onClick: noop},- {id: 'drafts', label: 'Drafts', onClick: noop, endContent: <Badge label="1" />},+ {+ id: 'drafts',+ label: 'Drafts',+ onClick: noop,+ endContent: <Badge label="1" />,+ },],},};@@ -292,7 +303,12 @@ export const DisabledItems: Story = {},],},- {id: 'disabled-parent', label: 'Disabled Parent', onClick: noop, isDisabled: true},+ {+ id: 'disabled-parent',+ label: 'Disabled Parent',+ onClick: noop,+ isDisabled: true,+ },],},};@@ -313,3 +329,52 @@ export const SelectedItems: Story = {],},};++/**+ * Theme the expand/collapse chevron precisely via `defineTheme`.+ *+ * - `components['tree-list-chevron'].base` scopes overrides to the toggle+ * control only (via the `astryx-tree-list-chevron` target), instead of+ * reaching it through the functional `[data-tree-toggle]` attribute.+ * - `state:expanded` / `state:collapsed` restyle each open/closed state,+ * which the toggle reflects as a `data-state` attribute.+ *+ * Defaults are unchanged; this story only demonstrates the override channel.+ */+const chevronTheme = defineTheme({+ name: 'tree-list-chevron-demo',+ components: {+ 'tree-list-chevron': {+ base: {+ color: 'var(--color-accent)',+ },+ 'state:expanded': {+ color: 'var(--color-success)',+ },+ },+ },+});++export const ThemedChevron: Story = {+ render: () => (+ <Theme theme={chevronTheme} mode="light">+ <TreeList+ items={[+ {+ id: 'root',+ label: 'Expanded branch (accent → success)',+ isExpanded: true,+ children: [+ {id: 'leaf-1', label: 'Leaf 1', onClick: noop},+ {+ id: 'nested',+ label: 'Collapsed branch (accent)',+ children: [{id: 'leaf-2', label: 'Leaf 2', onClick: noop}],+ },+ ],+ },+ ]}+ />+ </Theme>+ ),+};diff --git a/packages/core/src/TreeList/TreeList.doc.mjs b/packages/core/src/TreeList/TreeList.doc.mjsindex dabb328f3b..5a4180e882 100644--- a/packages/core/src/TreeList/TreeList.doc.mjs+++ b/packages/core/src/TreeList/TreeList.doc.mjs@@ -26,6 +26,7 @@ export const docs = {targets: [{className: 'astryx-tree-list', visualProps: ['density']},{className: 'astryx-tree-list-item', visualProps: ['density'], states: ['selected', 'disabled']},+ {className: 'astryx-tree-list-chevron', states: ['state']},],},components: [@@ -90,6 +91,7 @@ export const docsZh = {targets: [{className: 'astryx-tree-list', visualProps: ['density']},{className: 'astryx-tree-list-item', visualProps: ['density'], states: ['selected', 'disabled']},+ {className: 'astryx-tree-list-chevron', states: ['state']},],},components: [diff --git a/packages/core/src/TreeList/TreeList.test.tsx b/packages/core/src/TreeList/TreeList.test.tsxindex 9e5eb07f7e..ab059772be 100644--- a/packages/core/src/TreeList/TreeList.test.tsx+++ b/packages/core/src/TreeList/TreeList.test.tsx@@ -14,6 +14,8 @@ import {render, screen, fireEvent} from '@testing-library/react';import userEvent from '@testing-library/user-event';import {TreeList} from './TreeList';import type {TreeListItemData} from './TreeListTypes';+import {defineTheme} from '../theme/defineTheme';+import {generateThemeCSSFlat} from '../theme/generateThemeRules';const simpleItems: TreeListItemData[] = [{id: 'a', label: 'Item A'},@@ -403,6 +405,69 @@ describe('TreeList', () => {expect(root.className).toContain('astryx-tree-list');});+ // ===========================================================================+ // Chevron theme target+ // ===========================================================================++ describe('chevron theme target', () => {+ it('renders the astryx-tree-list-chevron target on the toggle button', () => {+ render(<TreeList items={nestedItems} />);+ const toggle = screen+ .getByText('Parent')+ .closest('li')!+ .querySelector('[data-tree-toggle]')!;++ // Dedicated, stable theme target on the expand/collapse control, so a+ // theme can restyle the chevron without a fragile [data-tree-toggle] hook.+ expect(toggle).toHaveClass('astryx-tree-list-chevron');+ // Open/closed state is reflected so a theme can target each state alone.+ expect(toggle).toHaveAttribute('data-state', 'collapsed');+ });++ it('reflects the expanded state on the toggle when open', () => {+ render(<TreeList items={nestedItemsExpanded} />);+ const toggle = screen+ .getByText('Parent')+ .closest('li')!+ .querySelector('[data-tree-toggle]')!;++ expect(toggle).toHaveClass('astryx-tree-list-chevron');+ expect(toggle).toHaveAttribute('data-state', 'expanded');+ });++ it('keeps the functional data-tree-toggle hook alongside the new target', () => {+ // The theme target is additive — the toggle is still a real <button> and+ // still carries the functional activation attribute TreeList relies on.+ render(<TreeList items={nestedItems} />);+ const toggle = screen+ .getByText('Parent')+ .closest('li')!+ .querySelector('[data-tree-toggle]')!;+ expect(toggle.tagName).toBe('BUTTON');+ expect(toggle).toHaveAttribute('data-tree-toggle');+ });++ it('exposes tree-list-chevron as a themeable defineTheme target', () => {+ // The generated CSS is what proves the target is reachable by a theme:+ // jsdom cannot resolve the @layer cascade, so the DOM-class assertions+ // above and this generation assertion together cover the seam.+ const theme = defineTheme({+ name: 'tree-list-chevron-test',+ components: {+ 'tree-list-chevron': {+ base: {color: 'var(--color-accent)'},+ 'state:expanded': {color: 'var(--color-text-primary)'},+ },+ },+ });+ const css = generateThemeCSSFlat(theme);+ expect(css).toContain('.astryx-tree-list-chevron {');+ expect(css).toContain('color: var(--color-accent)');+ expect(css).toContain('.astryx-tree-list-chevron.expanded');+ expect(css).toContain('color: var(--color-text-primary)');+ });+ });+// ===========================================================================// APG structural ARIA (aria-level / aria-posinset / aria-setsize)// ===========================================================================diff --git a/packages/core/src/TreeList/TreeListItem.tsx b/packages/core/src/TreeList/TreeListItem.tsxindex 3e1fed947f..8aeb3d6b9d 100644--- a/packages/core/src/TreeList/TreeListItem.tsx+++ b/packages/core/src/TreeList/TreeListItem.tsx@@ -394,12 +394,30 @@ export function TreeListItem({// a separate tab stop. Row-level Enter/Space forwards to this button.tabIndex={-1}onClick={handleToggle}- {...stylex.props(styles.chevronButton)}>+ {...mergeProps(+ // Stable theme target for the expand/collapse control. `data-tree-toggle`+ // stays as the functional activation hook; this adds a themeable+ // `astryx-tree-list-chevron` class and reflects the open/closed state so+ // a theme can restyle the toggle (and each state) without a fragile+ // `[data-tree-toggle]` selector.+ themeProps('tree-list-chevron', {+ state: isExpanded ? 'expanded' : 'collapsed',+ }),+ stylex.props(styles.chevronButton),+ )}>{chevronIcon}</button>) : (// Non-interactive chevron only when toggling is not wired up at all- <span {...stylex.props(styles.chevronContainer)}>{chevronIcon}</span>+ <span+ {...mergeProps(+ themeProps('tree-list-chevron', {+ state: isExpanded ? 'expanded' : 'collapsed',+ }),+ stylex.props(styles.chevronContainer),+ )}>+ {chevronIcon}+ </span>)) : null;