Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/item-theme-states.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] Item: the `item` theme target now reflects the row's state as `data-highlighted`, `data-selected`, and `data-disabled`, so a theme can restyle a highlighted, selected, or disabled row. A row that composes Item reflects the states it passes to Item: for example, ListItem reflects selected and disabled, and DropdownMenuItem reflects disabled. The docs list `className` and `style`, give `ref` the root element type, and give `xstyle` its standard description.

@cixzhang
31 changes: 31 additions & 0 deletions apps/storybook/stories/Item.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ export const Disabled: Story = {
),
};

/**
* Each row state on one interactive row: rest, highlighted (the active row of
* a menu or a picker), selected, disabled, and selected while disabled. The
* row reflects each state on its `item` theme target.
*/
export const States: Story = {
render: () => (
<Stack gap={0}>
{(
[
['Rest', {}],
['Highlighted', {isHighlighted: true}],
['Selected', {isSelected: true}],
['Disabled', {isDisabled: true}],
['Selected and disabled', {isSelected: true, isDisabled: true}],
] as const
).map(([label, state]) => (
<Item
key={label}
{...state}
startContent={<Icon icon={DocumentIcon} size="sm" />}
label={label}
description="Edited 2 hours ago"
endContent={<Badge label="Draft" />}
onClick={() => {}}
/>
))}
</Stack>
),
};

/** Top-aligned layout for multi-line content. */
export const AlignStart: Story = {
render: () => (
Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/Item/Item.doc.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export const docs = {
},
theming: {
targets: [
{className: 'astryx-item', visualProps: ['density', 'align']},
{
className: 'astryx-item',
visualProps: ['density', 'align'],
states: ['highlighted', 'selected', 'disabled'],
},
{className: 'astryx-item-swipe-panel', visualProps: ['side']},
{className: 'astryx-item-swipe-action', visualProps: ['variant']},
],
Expand Down Expand Up @@ -210,14 +214,25 @@ export const docs = {
},
{
name: 'ref',
type: 'React.Ref<HTMLDivElement>',
description: 'Ref forwarded to the root element.',
type: 'React.Ref<HTMLElement>',
description:
'Ref forwarded to the root element, whose type follows `as`.',
},
{
name: 'xstyle',
type: 'StyleXStyles',
description:
'StyleX styles for layout customization. Must be a stylex.create() value.',
'StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value: not an inline style object like style={{}}.',
},
{
name: 'className',
type: 'string',
description: 'CSS class name(s) for the root element.',
},
{
name: 'style',
type: 'React.CSSProperties',
description: 'Inline styles for the root element.',
},
{
name: 'data-testid',
Expand Down
73 changes: 73 additions & 0 deletions packages/core/src/Item/Item.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,79 @@ describe('Item', () => {
}
});

it('truncates the label and description to the lines it is given', () => {
// The clamp count reaches CSS through a custom property, so resolve it.
const lineClamp = (element: HTMLElement) => {
const value = getComputedStyle(element)
.getPropertyValue('-webkit-line-clamp')
.trim();
const variable = /^var\((--[^),]+)\)$/.exec(value)?.[1];
return variable == null
? value
: getComputedStyle(element).getPropertyValue(variable).trim() ||
element.style.getPropertyValue(variable).trim();
};
// A node label wraps by default, so one line comes from labelLines.
const {rerender} = render(
<Item
label={<span>A long label</span>}
labelLines={1}
description={<span>A long description</span>}
descriptionLines={1}
/>,
);
for (const text of ['A long label', 'A long description']) {
const style = getComputedStyle(screen.getByText(text).parentElement!);
expect(style.overflow).toBe('hidden');
expect(style.textOverflow).toBe('ellipsis');
expect(style.whiteSpace).toBe('nowrap');
}
rerender(
<Item
label="A long label"
labelLines={3}
description="A long description"
descriptionLines={2}
/>,
);
const label = screen.getByText('A long label');
const description = screen.getByText('A long description');
for (const element of [label, description]) {
const style = getComputedStyle(element);
expect(style.overflow).toBe('hidden');
expect(style.display).toBe('-webkit-box');
expect(style.whiteSpace).not.toBe('nowrap');
}
expect(lineClamp(label)).toBe('3');
expect(lineClamp(description)).toBe('2');
});

it('reflects highlighted, selected, and disabled on the item theme target', () => {
const {rerender} = render(<Item label="Item" data-testid="item" />);
const item = () => screen.getByTestId('item');
for (const state of ['highlighted', 'selected', 'disabled']) {
expect(item()).not.toHaveAttribute(`data-${state}`);
}
rerender(
<Item
label="Item"
isHighlighted
isSelected
isDisabled
onClick={() => {}}
data-testid="item"
/>,
);
expect(item()).toHaveClass('astryx-item');
expect(item()).toHaveAttribute('data-highlighted', 'highlighted');
expect(item()).toHaveAttribute('data-selected', 'selected');
expect(item()).toHaveAttribute('data-disabled', 'disabled');
rerender(<Item label="Item" role="option" isSelected data-testid="item" />);
expect(item()).toHaveAttribute('data-selected', 'selected');
expect(item()).not.toHaveAttribute('data-highlighted');
expect(item()).not.toHaveAttribute('data-disabled');
});

it('aligns the row on first baselines only under first-line', () => {
const {rerender} = render(<Item label="Item" data-testid="item" />);
const declares = (value: string) =>
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/Item/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1006,7 +1006,13 @@ export function Item({
aria-disabled={isDisabled || undefined}
{...(isInteractive ? pressable : undefined)}
{...mergeProps(
themeProps('item', {density, align}),
themeProps('item', {
density,
align,
highlighted: isHighlighted ? 'highlighted' : null,
selected: isSelected ? 'selected' : null,
disabled: isDisabled ? 'disabled' : null,
}),
focusOutlineProps.focusWithin(
styles.root,
densityStyles[density],
Expand Down
Loading
Loading