Skip to content

Commit 3d018fb

Browse files
authored
[Component Audit] Item: a theme can restyle highlighted, selected, and disabled rows (#10073)
* [Component Audit] Item: a theme can restyle highlighted, selected, and disabled rows * [Component Audit] Item: receipts find the text cell by layout, move the pointer onto the disabled row, and read the focus width from the theme; resolve private self-review findings * [Component Audit] Item: the disabled hover frame fails closed when the row has no box * [Component Audit] Item: labelLines and descriptionLines truncation is tested, and the clamped label is proven in a browser * [Component Audit] Item: the truncation test proves the clamp counts, and the clamped frame proves its fixture runs past two lines * [Component Audit] Item: the changeset says which composing rows reflect which states * [Component Audit] Item: the changeset's second example is DropdownMenuItem * [Component Audit] Item: receipts expect each row's own direction, which a story case may set * [Component Audit] Item: the frame direction is read from the preview's direction wrapper
1 parent 8c0e95b commit 3d018fb

6 files changed

Lines changed: 799 additions & 5 deletions

File tree

‎.changeset/item-theme-states.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[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.
6+
7+
@cixzhang

‎apps/storybook/stories/Item.stories.tsx‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,37 @@ export const Disabled: Story = {
342342
),
343343
};
344344

345+
/**
346+
* Each row state on one interactive row: rest, highlighted (the active row of
347+
* a menu or a picker), selected, disabled, and selected while disabled. The
348+
* row reflects each state on its `item` theme target.
349+
*/
350+
export const States: Story = {
351+
render: () => (
352+
<Stack gap={0}>
353+
{(
354+
[
355+
['Rest', {}],
356+
['Highlighted', {isHighlighted: true}],
357+
['Selected', {isSelected: true}],
358+
['Disabled', {isDisabled: true}],
359+
['Selected and disabled', {isSelected: true, isDisabled: true}],
360+
] as const
361+
).map(([label, state]) => (
362+
<Item
363+
key={label}
364+
{...state}
365+
startContent={<Icon icon={DocumentIcon} size="sm" />}
366+
label={label}
367+
description="Edited 2 hours ago"
368+
endContent={<Badge label="Draft" />}
369+
onClick={() => {}}
370+
/>
371+
))}
372+
</Stack>
373+
),
374+
};
375+
345376
/** Top-aligned layout for multi-line content. */
346377
export const AlignStart: Story = {
347378
render: () => (

‎packages/core/src/Item/Item.doc.mjs‎

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@ export const docs = {
2727
},
2828
theming: {
2929
targets: [
30-
{className: 'astryx-item', visualProps: ['density', 'align']},
30+
{
31+
className: 'astryx-item',
32+
visualProps: ['density', 'align'],
33+
states: ['highlighted', 'selected', 'disabled'],
34+
},
3135
{className: 'astryx-item-swipe-panel', visualProps: ['side']},
3236
{className: 'astryx-item-swipe-action', visualProps: ['variant']},
3337
],
@@ -210,14 +214,25 @@ export const docs = {
210214
},
211215
{
212216
name: 'ref',
213-
type: 'React.Ref<HTMLDivElement>',
214-
description: 'Ref forwarded to the root element.',
217+
type: 'React.Ref<HTMLElement>',
218+
description:
219+
'Ref forwarded to the root element, whose type follows `as`.',
215220
},
216221
{
217222
name: 'xstyle',
218223
type: 'StyleXStyles',
219224
description:
220-
'StyleX styles for layout customization. Must be a stylex.create() value.',
225+
'StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value: not an inline style object like style={{}}.',
226+
},
227+
{
228+
name: 'className',
229+
type: 'string',
230+
description: 'CSS class name(s) for the root element.',
231+
},
232+
{
233+
name: 'style',
234+
type: 'React.CSSProperties',
235+
description: 'Inline styles for the root element.',
221236
},
222237
{
223238
name: 'data-testid',

‎packages/core/src/Item/Item.test.tsx‎

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -874,6 +874,79 @@ describe('Item', () => {
874874
}
875875
});
876876

877+
it('truncates the label and description to the lines it is given', () => {
878+
// The clamp count reaches CSS through a custom property, so resolve it.
879+
const lineClamp = (element: HTMLElement) => {
880+
const value = getComputedStyle(element)
881+
.getPropertyValue('-webkit-line-clamp')
882+
.trim();
883+
const variable = /^var\((--[^),]+)\)$/.exec(value)?.[1];
884+
return variable == null
885+
? value
886+
: getComputedStyle(element).getPropertyValue(variable).trim() ||
887+
element.style.getPropertyValue(variable).trim();
888+
};
889+
// A node label wraps by default, so one line comes from labelLines.
890+
const {rerender} = render(
891+
<Item
892+
label={<span>A long label</span>}
893+
labelLines={1}
894+
description={<span>A long description</span>}
895+
descriptionLines={1}
896+
/>,
897+
);
898+
for (const text of ['A long label', 'A long description']) {
899+
const style = getComputedStyle(screen.getByText(text).parentElement!);
900+
expect(style.overflow).toBe('hidden');
901+
expect(style.textOverflow).toBe('ellipsis');
902+
expect(style.whiteSpace).toBe('nowrap');
903+
}
904+
rerender(
905+
<Item
906+
label="A long label"
907+
labelLines={3}
908+
description="A long description"
909+
descriptionLines={2}
910+
/>,
911+
);
912+
const label = screen.getByText('A long label');
913+
const description = screen.getByText('A long description');
914+
for (const element of [label, description]) {
915+
const style = getComputedStyle(element);
916+
expect(style.overflow).toBe('hidden');
917+
expect(style.display).toBe('-webkit-box');
918+
expect(style.whiteSpace).not.toBe('nowrap');
919+
}
920+
expect(lineClamp(label)).toBe('3');
921+
expect(lineClamp(description)).toBe('2');
922+
});
923+
924+
it('reflects highlighted, selected, and disabled on the item theme target', () => {
925+
const {rerender} = render(<Item label="Item" data-testid="item" />);
926+
const item = () => screen.getByTestId('item');
927+
for (const state of ['highlighted', 'selected', 'disabled']) {
928+
expect(item()).not.toHaveAttribute(`data-${state}`);
929+
}
930+
rerender(
931+
<Item
932+
label="Item"
933+
isHighlighted
934+
isSelected
935+
isDisabled
936+
onClick={() => {}}
937+
data-testid="item"
938+
/>,
939+
);
940+
expect(item()).toHaveClass('astryx-item');
941+
expect(item()).toHaveAttribute('data-highlighted', 'highlighted');
942+
expect(item()).toHaveAttribute('data-selected', 'selected');
943+
expect(item()).toHaveAttribute('data-disabled', 'disabled');
944+
rerender(<Item label="Item" role="option" isSelected data-testid="item" />);
945+
expect(item()).toHaveAttribute('data-selected', 'selected');
946+
expect(item()).not.toHaveAttribute('data-highlighted');
947+
expect(item()).not.toHaveAttribute('data-disabled');
948+
});
949+
877950
it('aligns the row on first baselines only under first-line', () => {
878951
const {rerender} = render(<Item label="Item" data-testid="item" />);
879952
const declares = (value: string) =>

‎packages/core/src/Item/Item.tsx‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,13 @@ export function Item({
10061006
aria-disabled={isDisabled || undefined}
10071007
{...(isInteractive ? pressable : undefined)}
10081008
{...mergeProps(
1009-
themeProps('item', {density, align}),
1009+
themeProps('item', {
1010+
density,
1011+
align,
1012+
highlighted: isHighlighted ? 'highlighted' : null,
1013+
selected: isSelected ? 'selected' : null,
1014+
disabled: isDisabled ? 'disabled' : null,
1015+
}),
10101016
focusOutlineProps.focusWithin(
10111017
styles.root,
10121018
densityStyles[density],

0 commit comments

Comments
 (0)