Skip to content

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

TypeScript

fix(Item): a selected row shows hover and press over its fill; a highlighted row shows press (#10111)

Cindy Zhang9e7567c48048
diff --git a/.changeset/item-state-overlay.md b/.changeset/item-state-overlay.md
new file mode 100644
index 0000000000..4757e71c45
--- /dev/null
+++ b/.changeset/item-state-overlay.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A selected interactive `Item` or `ListItem` row now shows hover and mouse-press paint over its fill, and a highlighted `Item` row shows mouse-press paint. Before, the fill hid both. A highlighted row shows no extra hover, because its fill is already the hover tone.
+
+@cixzhang
diff --git a/packages/core/src/Item/Item.tsx b/packages/core/src/Item/Item.tsx
index 0b3fdc3a1b..7867977d2c 100644
--- a/packages/core/src/Item/Item.tsx
+++ b/packages/core/src/Item/Item.tsx
@@ -692,6 +692,7 @@ export function Item({
);
const isInteractive = onClick != null || href != null || isDelegate;
+ const hasStateFill = isHighlighted || isSelected;
const {target, rel} = computeTargetAndRel(targetFromProps, relFromProps);
// When a semantic role is provided (e.g. "menuitem"), a parent component
// handles keyboard access. Skip the invisible button/anchor and put
@@ -1019,11 +1020,23 @@ export function Item({
align === 'start' && styles.alignStart,
align === 'first-line' && styles.alignFirstLine,
isInteractive && styles.interactive,
- isInteractive && interactionOverlayStyles.backgroundColor,
+ // A row with a state fill keeps the fill as its background colour.
+ // Its overlay paints as an image layer over the fill.
+ isInteractive &&
+ (hasStateFill
+ ? interactionOverlayStyles.backgroundImage
+ : interactionOverlayStyles.backgroundColor),
// The touch press on a compositor layer (after the overlay, whose
// own touch paint it turns off): the release keeps fading while
// script runs.
isInteractive && rowPressLayerStyles.cell,
+ // A highlighted row's fill is already the hover tone, so it shows
+ // only its press. A selected row also shows its hover.
+ isInteractive &&
+ hasStateFill &&
+ (isSelected
+ ? rowPressLayerStyles.cellOnImage
+ : rowPressLayerStyles.cellOnImagePressOnly),
isLinkRoot && styles.linkRoot,
isHighlighted && styles.highlighted,
isSelected && styles.selected,
diff --git a/packages/core/src/Item/__tests__/ItemStateOverlay.a11y.chromium.spec.ts b/packages/core/src/Item/__tests__/ItemStateOverlay.a11y.chromium.spec.ts
new file mode 100644
index 0000000000..8ddea3b883
--- /dev/null
+++ b/packages/core/src/Item/__tests__/ItemStateOverlay.a11y.chromium.spec.ts
@@ -0,0 +1,139 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+// @pr-a11y scope core/Item core/List ^apps/storybook/stories/Item
+
+/**
+ * @file ItemStateOverlay.a11y.chromium.spec.ts
+ * @input The built Core/Item "States" story; real Chromium mouse input
+ * @output Browser evidence that a highlighted or selected row still paints
+ * the hover and a mouse press's `:active` overlay over its own fill, as a
+ * row at rest does, and that its fill returns once the pointer leaves
+ * @position Chromium proof for component:Item FR9 and design:user-states (the
+ * pressed overlay composes over a persistent state fill). jsdom resolves no
+ * cascade, so only a browser shows which paint wins.
+ */
+
+import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
+import {holdMotionStill} from '@astryxdesign/a11y-spec/chromium';
+import {
+ DEFAULT_STORYBOOK_DIR,
+ serveStorybook,
+ type StaticServer,
+} from '@astryxdesign/a11y-spec/storybook';
+// @ts-expect-error -- pngjs ships no declarations; runtime support is pinned.
+import {PNG} from 'pngjs';
+
+let storybook: StaticServer;
+test.beforeAll(async () => {
+ storybook = await serveStorybook(
+ process.env.ASTRYX_STORYBOOK_DIR ?? DEFAULT_STORYBOOK_DIR,
+ );
+});
+test.afterAll(async () => {
+ await storybook?.close();
+});
+
+test.use({viewport: {width: 640, height: 480}});
+
+type Rgb = [number, number, number];
+
+// The colour at one point of the page, from a 1 × 1 screenshot.
+async function colourAt(page: Page, x: number, y: number): Promise<Rgb> {
+ const bytes = await page.screenshot({clip: {x, y, width: 1, height: 1}});
+ const png = PNG.sync.read(bytes);
+ return [png.data[0], png.data[1], png.data[2]];
+}
+
+const differs = (a: Rgb, b: Rgb) => a.some((channel, i) => channel !== b[i]);
+
+for (const mode of ['light', 'dark'] as const) {
+ // Rest is the control: a row with no state fill, which has always painted
+ // both. A highlighted row's fill is already the hover tone, so it shows
+ // only its press.
+ for (const label of ['Rest', 'Highlighted', 'Selected']) {
+ const showsHover = label !== 'Highlighted';
+ test(`a ${label.toLowerCase()} row paints ${showsHover ? 'hover and press' : 'press, not hover,'} over its fill (${mode})`, async ({
+ page,
+ }) => {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-item--states&viewMode=story&globals=colorMode:${mode};astryxTheme:neutral`,
+ );
+ await holdMotionStill(page);
+ const row = page
+ .locator('#storybook-root .astryx-item')
+ .filter({hasText: label})
+ .first();
+ const box = await row.boundingBox();
+ if (box == null) {
+ throw new Error(`no ${label} row`);
+ }
+ // A point in the row clear of its text, icon and badge.
+ const x = Math.round(box.x + box.width * 0.6);
+ const y = Math.round(box.y + 4);
+ await page.mouse.move(1, 470);
+ const rest = await colourAt(page, x, y);
+ await page.mouse.move(x, y);
+ if (showsHover) {
+ await expect
+ .poll(async () => differs(await colourAt(page, x, y), rest))
+ .toBe(true);
+ } else {
+ // Give a hover transition time to show before reading no change.
+ await page.waitForTimeout(400);
+ expect(differs(await colourAt(page, x, y), rest)).toBe(false);
+ }
+ const hover = await colourAt(page, x, y);
+ await page.mouse.down();
+ await expect
+ .poll(async () => differs(await colourAt(page, x, y), hover))
+ .toBe(true);
+ await page.mouse.up();
+ await page.mouse.move(1, 470);
+ await expect
+ .poll(async () => differs(await colourAt(page, x, y), rest))
+ .toBe(false);
+ });
+ }
+}
+
+test.describe('under a finger', () => {
+ // A phone: coarse primary pointer. The press paints on the row's compositor
+ // layer, above the state fill.
+ test.use({hasTouch: true, isMobile: true});
+
+ for (const mode of ['light', 'dark'] as const) {
+ for (const label of ['Rest', 'Highlighted', 'Selected']) {
+ test(`a finger press paints a ${label.toLowerCase()} row (${mode})`, async ({
+ page,
+ context,
+ }) => {
+ await page.goto(
+ `${storybook.origin}/iframe.html?id=core-item--states&viewMode=story&globals=colorMode:${mode};astryxTheme:neutral`,
+ );
+ await holdMotionStill(page);
+ const row = page
+ .locator('#storybook-root .astryx-item')
+ .filter({hasText: label})
+ .first();
+ const box = await row.boundingBox();
+ if (box == null) {
+ throw new Error(`no ${label} row`);
+ }
+ const x = Math.round(box.x + box.width * 0.6);
+ const y = Math.round(box.y + 4);
+ const rest = await colourAt(page, x, y);
+ const cdp = await context.newCDPSession(page);
+ await cdp.send('Input.dispatchTouchEvent', {
+ type: 'touchStart',
+ touchPoints: [{x, y, id: 1}],
+ });
+ await expect
+ .poll(async () => differs(await colourAt(page, x, y), rest))
+ .toBe(true);
+ await cdp.send('Input.dispatchTouchEvent', {
+ type: 'touchEnd',
+ touchPoints: [],
+ });
+ });
+ }
+ }
+});
diff --git a/packages/core/src/privateAttributes/press.stylex.ts b/packages/core/src/privateAttributes/press.stylex.ts
index 05dd0369ef..15f0dacf03 100644
--- a/packages/core/src/privateAttributes/press.stylex.ts
+++ b/packages/core/src/privateAttributes/press.stylex.ts
@@ -8,7 +8,8 @@
* @input StyleX, the overlay color tokens, the touch press strength
* (`pressVars`) and the row layer's clocks (utils/rowPressLayer.stylex.ts)
* @output `rowPressLayerStyles`, the compositor press layer for list rows
- * (`cell`, `cellOnImage` for a `backgroundImage` row), and
+ * (`cell`, `cellOnImage` for a `backgroundImage` row,
+ * `cellOnImagePressOnly` for a row whose fill is the hover tone), and
* `rowPressPaintStyles`, a CheckboxList row's press paint for its box
* @position Owner module of the private press attribute: the press controller
* (utils/pressFeedback.ts) writes it, and this file reads it. No package
@@ -240,6 +241,25 @@ export const rowPressLayerStyles = stylex.create({
},
},
},
+ /**
+ * `cellOnImage` without the hover arm, for a row whose fill is already the
+ * hover tone (a highlighted Item row). A hover layer on it would match a
+ * pressed row, so the row shows only its press.
+ */
+ cellOnImagePressOnly: {
+ backgroundImage: {
+ default: null,
+ [ENABLED]: {
+ default: null,
+ ':active': {
+ default: pressedImage,
+ [COARSE]: 'none',
+ },
+ [PRESSED_ON]: {default: pressedOverlayImage, [COARSE]: 'none'},
+ [PRESSED_FADING]: {default: pressedOverlayImage, [COARSE]: 'none'},
+ },
+ },
+ },
});
export const rowPressPaintStyles = stylex.create({