An open source design system that's fully customizable and agent ready
TypeScript
fix(List): wrap header + list in column container for flex parents (#2017)
Cindy Zhangf7b7210787b6
diff --git a/apps/storybook/stories/List.stories.tsx b/apps/storybook/stories/List.stories.tsxindex a139c20331..6a336c68bf 100644--- a/apps/storybook/stories/List.stories.tsx+++ b/apps/storybook/stories/List.stories.tsx@@ -1,8 +1,11 @@+import {useState} from 'react';import type {Meta, StoryObj} from '@storybook/react';import {XDSList, XDSListItem} from '@xds/core/List';import {XDSAvatar} from '@xds/core/Avatar';import {XDSBadge} from '@xds/core/Badge';import {XDSIcon} from '@xds/core/Icon';+import {XDSSwitch} from '@xds/core/Switch';+import {XDSText} from '@xds/core/Text';import {Cog6ToothIcon,BellIcon,@@ -217,3 +220,89 @@ export const WithMedia: Story = {</XDSList>),};++function ToggleHeaderDemo() {+ const [showArchived, setShowArchived] = useState(false);+ return (+ <XDSList+ hasDividers+ header={+ <div+ style={{+ display: 'flex',+ alignItems: 'center',+ justifyContent: 'space-between',+ }}>+ <XDSText variant="label" size="large">+ Notifications+ </XDSText>+ <XDSSwitch+ label="Show archived"+ isSelected={showArchived}+ onChange={setShowArchived}+ />+ </div>+ }>+ <XDSListItem+ label="New deployment succeeded"+ description="Production v2.4.1 deployed"+ startContent={<XDSIcon icon={BellIcon} />}+ />+ <XDSListItem+ label="Security alert"+ description="Unusual login detected"+ startContent={<XDSIcon icon={ShieldCheckIcon} />}+ />+ {showArchived && (+ <XDSListItem+ label="Archived: Build completed"+ description="CI pipeline finished"+ startContent={<XDSIcon icon={Cog6ToothIcon} />}+ />+ )}+ </XDSList>+ );+}++export const HeaderWithToggle: Story = {+ render: () => <ToggleHeaderDemo />,+ parameters: {+ docs: {+ description: {+ story:+ 'A list with a toggle in the header. The header and list are ' ++ 'wrapped so they stack correctly even inside flex parents.',+ },+ },+ },+};++export const HeaderInFlexParent: Story = {+ render: () => (+ <div+ style={{+ display: 'flex',+ gap: 24,+ border: '1px dashed #888',+ padding: 16,+ }}>+ <div style={{flex: 1}}>+ <XDSText variant="label" size="large">+ Sidebar+ </XDSText>+ </div>+ <div style={{flex: 2}}>+ <ToggleHeaderDemo />+ </div>+ </div>+ ),+ parameters: {+ docs: {+ description: {+ story:+ 'Demonstrates the list with a header inside a flex parent. ' ++ 'The header should appear above the list, not beside it.',+ },+ },+ },+};diff --git a/packages/core/src/List/XDSList.test.tsx b/packages/core/src/List/XDSList.test.tsxindex a3d2b030a0..2360458d5a 100644--- a/packages/core/src/List/XDSList.test.tsx+++ b/packages/core/src/List/XDSList.test.tsx@@ -170,6 +170,29 @@ describe('XDSList', () => {expect(ul).not.toHaveAttribute('aria-labelledby');});+ it('wraps header and list in a column container', () => {+ const {container} = render(+ <XDSList header={<span>Group</span>}>+ <XDSListItem label="Item" />+ </XDSList>,+ );+ const header = screen.getByText('Group');+ const wrapper = header.parentElement?.parentElement;+ const ul = container.querySelector('ul');+ expect(wrapper).toContainElement(header.parentElement!);+ expect(wrapper).toContainElement(ul!);+ });++ it('does not add a wrapper div when header is absent', () => {+ const {container} = render(+ <XDSList data-testid="my-list">+ <XDSListItem label="Item" />+ </XDSList>,+ );+ const ul = container.querySelector('ul');+ expect(ul?.parentElement).toBe(container);+ });+// ===========================================================================// Density variants// ===========================================================================diff --git a/packages/core/src/List/XDSList.tsx b/packages/core/src/List/XDSList.tsxindex 77cee6bd24..7128966cc3 100644--- a/packages/core/src/List/XDSList.tsx+++ b/packages/core/src/List/XDSList.tsx@@ -80,6 +80,10 @@ export interface XDSListProps extends XDSBaseProps<// =============================================================================const styles = stylex.create({+ root: {+ display: 'flex',+ flexDirection: 'column',+ },list: {margin: 0,paddingInlineStart: 0,@@ -142,31 +146,43 @@ export function XDSList({[density, hasDividers, listStyle],);+ const listElement = (+ <Tag+ ref={ref as React.Ref<HTMLUListElement & HTMLOListElement>}+ data-testid={testId}+ aria-labelledby={header != null ? headerId : undefined}+ {...(listStyle === 'none' && !isOrdered ? {role: 'list'} : {})}+ {...mergeProps(+ xdsClassName('list', {density, listStyle}),+ stylex.props(+ styles.list,+ hasDividers && styles.withDividers,+ listStyle !== 'none' && styles.withCounter,+ xstyle,+ ),+ className,+ style,+ )}>+ {children}+ </Tag>+ );++ if (header == null) {+ return (+ <XDSListContext.Provider value={contextValue}>+ {listElement}+ </XDSListContext.Provider>+ );+ }+return (<XDSListContext.Provider value={contextValue}>- {header != null && (+ <div {...stylex.props(styles.root)}><div id={headerId} {...stylex.props(styles.header)}>{header}</div>- )}- <Tag- ref={ref as React.Ref<HTMLUListElement & HTMLOListElement>}- data-testid={testId}- aria-labelledby={header != null ? headerId : undefined}- {...(listStyle === 'none' && !isOrdered ? {role: 'list'} : {})}- {...mergeProps(- xdsClassName('list', {density, listStyle}),- stylex.props(- styles.list,- hasDividers && styles.withDividers,- listStyle !== 'none' && styles.withCounter,- xstyle,- ),- className,- style,- )}>- {children}- </Tag>+ {listElement}+ </div></XDSListContext.Provider>);}