Skip to content

Commit c30794b

Browse files
committed
feat(SelectableCard): under a finger the selection ring and border change in one frame
A selected card's mark is an inset ring, drawn in the card's shadow slot, and the accent border. The ring cannot interpolate with the card's resting shadow, so it appeared or vanished in one frame while the border faded over the theme's fast duration: the mark changed in two steps. UIKit changes a selection mark in the frame the selection lands (motion/uikit/selection.ts, selection.mark.changeMs). Under a finger (a coarse primary pointer, the touch resolver's TOUCH_QUERY, on every touch device) the card's box-shadow and border-color transition now takes that clock, through the same build-time mirror the checkbox and radio marks read (Indicator/selectionMotion.stylex.ts), so ring and border change together. A mouse keeps the theme's fade, and under Reduce Motion changes at once (design:reduced-motion DR3).
1 parent b5999ef commit c30794b

5 files changed

Lines changed: 562 additions & 28 deletions

File tree

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+
[feat] Under a finger (a coarse primary pointer, on every touch device), a SelectableCard's selection mark now changes in one frame, as UIKit's selection marks do. Before, the inset ring appeared or vanished at once while the border faded over the theme's fast duration, so the mark changed in two steps. With a mouse the card keeps the theme's fade, and under Reduce Motion a mouse's change is now immediate.
6+
7+
@vjeux

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

Lines changed: 126 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,38 +8,118 @@ export const docs = {
88
category: 'Container',
99
keywords: ['card', 'selectable', 'toggle', 'checkbox', 'radio', 'selection'],
1010
usage: {
11-
description: 'A card that toggles between selected and unselected states with an accent border. For navigation use ClickableCard.',
11+
description:
12+
'A card that toggles between selected and unselected states with an accent border. For navigation use ClickableCard.',
1213
bestPractices: [
13-
{guidance: true, description: 'Use for plan pickers, filter chips, or option grids.'},
14-
{guidance: true, description: 'For single-select track one ID; for multi-select use a Set.'},
15-
{guidance: true, description: 'When focused, toggle selection with Space or Enter.'},
16-
{guidance: false, description: 'Use for navigation; use ClickableCard for that.'},
14+
{
15+
guidance: true,
16+
description: 'Use for plan pickers, filter chips, or option grids.',
17+
},
18+
{
19+
guidance: true,
20+
description:
21+
'For single-select track one ID; for multi-select use a Set.',
22+
},
23+
{
24+
guidance: true,
25+
description: 'When focused, toggle selection with Space or Enter.',
26+
},
27+
{
28+
guidance: false,
29+
description: 'Use for navigation; use ClickableCard for that.',
30+
},
1731
],
1832
anatomy: [
19-
{name: 'Container', required: true, description: 'Interactive div with accent border on selection.'},
20-
{name: 'Content', required: true, description: 'Children rendered inside the card.'},
33+
{
34+
name: 'Container',
35+
required: true,
36+
description: 'Interactive div with accent border on selection.',
37+
},
38+
{
39+
name: 'Content',
40+
required: true,
41+
description: 'Children rendered inside the card.',
42+
},
2143
],
2244
},
2345
props: [
24-
{name: 'label', type: 'string', description: 'Accessibility label.', required: true},
25-
{name: 'isSelected', type: 'boolean', description: 'Controlled selection state.', required: true},
26-
{name: 'onChange', type: '(isSelected: boolean) => void', description: 'Called when toggled.', required: true},
27-
{name: 'isDisabled', type: 'boolean', description: 'Disables the card.', default: 'false'},
46+
{
47+
name: 'label',
48+
type: 'string',
49+
description: 'Accessibility label.',
50+
required: true,
51+
},
52+
{
53+
name: 'isSelected',
54+
type: 'boolean',
55+
description:
56+
'Controlled selection state. Under a finger (a coarse primary pointer) the selection ring and border change in one frame; with a mouse they fade over the theme’s fast duration.',
57+
required: true,
58+
},
59+
{
60+
name: 'onChange',
61+
type: '(isSelected: boolean) => void',
62+
description: 'Called when toggled.',
63+
required: true,
64+
},
65+
{
66+
name: 'isDisabled',
67+
type: 'boolean',
68+
description: 'Disables the card.',
69+
default: 'false',
70+
},
2871
{name: 'children', type: 'ReactNode', description: 'Card content.'},
29-
{name: 'padding', type: '0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10', description: 'Inner padding.', default: '4'},
30-
{name: 'variant', type: "'default' | 'transparent' | 'muted' | 'blue' | 'cyan' | 'gray' | 'green' | 'orange' | 'pink' | 'purple' | 'red' | 'teal' | 'yellow'", description: 'Background color variant.', default: "'default'"},
31-
{name: 'elevation', type: "'none' | 'low' | 'med' | 'high'", description: 'Resting shadow depth. The selection ring composes on top, so a selected card keeps its shadow.', default: "'none'"},
72+
{
73+
name: 'padding',
74+
type: '0 | 0.5 | 1 | 1.5 | 2 | 3 | 4 | 5 | 6 | 8 | 10',
75+
description: 'Inner padding.',
76+
default: '4',
77+
},
78+
{
79+
name: 'variant',
80+
type: "'default' | 'transparent' | 'muted' | 'blue' | 'cyan' | 'gray' | 'green' | 'orange' | 'pink' | 'purple' | 'red' | 'teal' | 'yellow'",
81+
description: 'Background color variant.',
82+
default: "'default'",
83+
},
84+
{
85+
name: 'elevation',
86+
type: "'none' | 'low' | 'med' | 'high'",
87+
description:
88+
'Resting shadow depth. The selection ring composes on top, so a selected card keeps its shadow.',
89+
default: "'none'",
90+
},
3291
{name: 'width', type: 'SizeValue', description: 'Card width.'},
3392
{name: 'height', type: 'SizeValue', description: 'Card height.'},
3493
{name: 'maxWidth', type: 'SizeValue', description: 'Maximum card width.'},
35-
{name: 'xstyle', type: 'StyleXStyles', description: 'StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value, not an inline style object like style={{}}.'},
94+
{
95+
name: 'xstyle',
96+
type: 'StyleXStyles',
97+
description:
98+
'StyleX styles for layout customization (margins, positioning, sizing). Must be a stylex.create() value, not an inline style object like style={{}}.',
99+
},
36100
],
37101
theming: {
38102
container: true,
39-
targets: [{className: 'astryx-selectable-card', visualProps: ['selected', 'variant']}],
103+
targets: [
104+
{
105+
className: 'astryx-selectable-card',
106+
visualProps: ['selected', 'variant'],
107+
},
108+
],
40109
vars: [
41-
{name: '--selectable-card-ring-color', description: 'Colour of the selection ring drawn for a variant a theme added. The built-in variants each ring in their own border token and ignore this; a theme that adds a variant sets it in the same rule as that variant\'s `backgroundColor`, because no token the component could pick is guaranteed to contrast with a fill it cannot know.', default: 'var(--color-accent)'},
42-
{name: '--_press-overlay', description: 'Colour of the ::after interaction overlay for the hover and mouse states: transparent at rest, the hover overlay token on hover (hover-capable pointers), the pressed overlay token while a mouse holds `:active`. A press the touch press controller paints (`data-astryx-press`) is the overlay\'s image instead, at the press\'s strength, and the colour yields to it. Set on the card element by its own interaction arms.', default: 'transparent', private: true},
110+
{
111+
name: '--selectable-card-ring-color',
112+
description:
113+
"Colour of the selection ring drawn for a variant a theme added. The built-in variants each ring in their own border token and ignore this; a theme that adds a variant sets it in the same rule as that variant's `backgroundColor`, because no token the component could pick is guaranteed to contrast with a fill it cannot know.",
114+
default: 'var(--color-accent)',
115+
},
116+
{
117+
name: '--_press-overlay',
118+
description:
119+
"Colour of the ::after interaction overlay for the hover and mouse states: transparent at rest, the hover overlay token on hover (hover-capable pointers), the pressed overlay token while a mouse holds `:active`. A press the touch press controller paints (`data-astryx-press`) is the overlay's image instead, at the press's strength, and the colour yields to it. Set on the card element by its own interaction arms.",
120+
default: 'transparent',
121+
private: true,
122+
},
43123
],
44124
},
45125
playground: {
@@ -52,7 +132,11 @@ export const docs = {
52132
props: {gap: 1},
53133
children: [
54134
{__element: 'XDSHeading', props: {level: 3}, children: 'Pro plan'},
55-
{__element: 'XDSText', props: {type: 'body'}, children: '$29/month, unlimited projects and priority support.'},
135+
{
136+
__element: 'XDSText',
137+
props: {type: 'body'},
138+
children: '$29/month, unlimited projects and priority support.',
139+
},
56140
],
57141
},
58142
},
@@ -61,14 +145,29 @@ export const docs = {
61145

62146
/** @type {import('@astryxdesign/cli/authoring').ComponentTranslationDoc} */
63147
export const docsDense = {
64-
description: 'Card toggling between selected/unselected states w/ accent border. For navigation use ClickableCard instead.',
148+
description:
149+
'Card toggling between selected/unselected states w/ accent border. For navigation use ClickableCard instead.',
65150
usage: {
66-
description: 'Card toggling between selected/unselected states w/ accent border. For navigation use ClickableCard instead.',
151+
description:
152+
'Card toggling between selected/unselected states w/ accent border. For navigation use ClickableCard instead.',
67153
bestPractices: [
68-
{guidance: true, description: 'Use for plan pickers, filter chips, option grids.'},
69-
{guidance: true, description: 'For single-select track one ID; for multi-select use a Set.'},
70-
{guidance: true, description: 'When focused, toggle selection with Space or Enter.'},
71-
{guidance: false, description: 'Use for navigation; use ClickableCard instead.'},
154+
{
155+
guidance: true,
156+
description: 'Use for plan pickers, filter chips, option grids.',
157+
},
158+
{
159+
guidance: true,
160+
description:
161+
'For single-select track one ID; for multi-select use a Set.',
162+
},
163+
{
164+
guidance: true,
165+
description: 'When focused, toggle selection with Space or Enter.',
166+
},
167+
{
168+
guidance: false,
169+
description: 'Use for navigation; use ClickableCard instead.',
170+
},
72171
],
73172
},
74173
propDescriptions: {
@@ -78,7 +177,8 @@ export const docsDense = {
78177
isDisabled: 'disables card',
79178
padding: 'inner padding',
80179
variant: 'background color variant',
81-
elevation: 'resting shadow depth: none|low|med|high; selection ring composes on top',
180+
elevation:
181+
'resting shadow depth: none|low|med|high; selection ring composes on top',
82182
width: 'card width',
83183
height: 'card height',
84184
maxWidth: 'max card width',

‎packages/core/src/SelectableCard/SelectableCard.tsx‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
/**
66
* @file SelectableCard.tsx
7-
* @input Uses Card, useClickableContainer, StyleX
7+
* @input Uses Card, useClickableContainer, StyleX, the selection mark's
8+
* touch timing (Indicator/selectionMotion.stylex.ts)
89
* @output Exports SelectableCard component and SelectableCardProps
910
* @position Interactive card for toggle selection
1011
*
@@ -43,6 +44,7 @@ import type {Elevation, SizeValue, SpacingStep} from '../utils/types';
4344
import {mergeProps} from '../utils';
4445
import {Card} from '../Card/Card';
4546
import type {CardVariant} from '../Card/Card';
47+
import {selectionMotionConsts} from '../Indicator/selectionMotion.stylex';
4648
import {useClickableContainer} from '../hooks/useClickableContainer';
4749
import type {BaseProps} from '../BaseProps';
4850
import {themeProps} from '../utils/themeProps';
@@ -65,6 +67,9 @@ const PRESSED_ON = '[data-astryx-press="on"]';
6567
/** Written by the press controller for the press's release. */
6668
const PRESSED_FADING = '[data-astryx-press="fading"]';
6769

70+
/** How long the selection mark takes to change under a finger: one frame. */
71+
const MARK_CHANGE = `${selectionMotionConsts.markChangeMs}ms`;
72+
6873
const styles = stylex.create({
6974
interactive: {
7075
// Declared here, on the element that carries the `astryx-selectable-card`
@@ -77,7 +82,21 @@ const styles = stylex.create({
7782
':is(:disabled,[aria-disabled="true"])': 'default',
7883
},
7984
transitionProperty: 'box-shadow, border-color',
80-
transitionDuration: durationVars['--duration-fast'],
85+
transitionDuration: {
86+
default: durationVars['--duration-fast'],
87+
// Under a finger (a coarse primary pointer, the touch resolver's
88+
// TOUCH_QUERY) the selection mark (the inset ring and the border)
89+
// changes in the frame the selection lands, as UIKit's selection marks
90+
// do (motion/uikit/selection.ts). The ring cannot interpolate with the
91+
// card's resting shadow, so a fade would split it from the border. A
92+
// mouse keeps the theme's duration, and under Reduce Motion changes at
93+
// once (design:reduced-motion DR3); a finger's change already is.
94+
// The resolver's and the policy's keys, spelled: an imported at-rule
95+
// key lands in a different StyleX priority layer than a literal one.
96+
'@media (pointer: coarse)': MARK_CHANGE,
97+
'@media (prefers-reduced-motion: reduce) and (not (pointer: coarse))':
98+
'0s',
99+
},
81100
transitionTimingFunction: easeVars['--ease-standard'],
82101
},
83102
// Hover and pressed overlay. Hover is guarded by @media (hover: hover) so
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file SelectableCardMark.test.tsx
5+
* @input vitest, Testing Library, SelectableCard and the UIKit constants
6+
* table (motion/uikit/selection.ts)
7+
* @output Asserts the card's selection mark (its inset ring and its border)
8+
* changes on the table's one frame under a finger (a coarse primary
9+
* pointer, on every touch device), keeps the theme's duration token with a
10+
* mouse, and is immediate with a mouse under Reduce Motion
11+
* @position Unit test of SelectableCard's touch mark timing; the real-engine
12+
* proof is __tests__/SelectableCardMark.interaction.browser.spec.ts
13+
*/
14+
15+
import {describe, expect, it} from 'vitest';
16+
import {render} from '@testing-library/react';
17+
import {selection} from '../motion/uikit/selection';
18+
import {SelectableCard} from './SelectableCard';
19+
20+
/**
21+
* The `transition-duration` each condition declares on `el`, keyed by its
22+
* enclosing media and supports conditions (outermost first, " | "-joined).
23+
*/
24+
function durationsOf(el: Element): Map<string | null, string> {
25+
const classes = el.className
26+
.split(/\s+/)
27+
.filter(Boolean)
28+
.map(name => `.${name}`);
29+
const durations = new Map<string | null, string>();
30+
const walk = (rules: CSSRuleList, conditions: string[]) => {
31+
for (const rule of Array.from(rules)) {
32+
if (rule instanceof CSSMediaRule) {
33+
walk(rule.cssRules, [...conditions, rule.media.mediaText]);
34+
} else if (rule instanceof CSSSupportsRule) {
35+
walk(rule.cssRules, [...conditions, rule.conditionText]);
36+
} else if (
37+
rule instanceof CSSStyleRule &&
38+
// The card itself, not its press layer (`::after`).
39+
!rule.selectorText.includes('::') &&
40+
classes.some(cls => rule.selectorText.startsWith(cls))
41+
) {
42+
const match = /transition-duration:\s*([^;]+)/.exec(rule.style.cssText);
43+
if (match) {
44+
durations.set(
45+
conditions.length === 0 ? null : conditions.join(' | '),
46+
match[1].trim(),
47+
);
48+
}
49+
}
50+
}
51+
};
52+
for (const sheet of Array.from(document.styleSheets)) {
53+
walk(sheet.cssRules, []);
54+
}
55+
return durations;
56+
}
57+
58+
/** The instant key of the Reduce Motion policy (motion/reducedMotion.ts). */
59+
const REDUCED_MOTION_INSTANT =
60+
'(prefers-reduced-motion: reduce) and (not (pointer: coarse))';
61+
62+
describe('SelectableCard selection mark', () => {
63+
it('changes its ring and border in one frame under a finger, keeps the theme’s duration with a mouse, and changes at once with a mouse under Reduce Motion', () => {
64+
const {container} = render(
65+
<SelectableCard label="Pro plan" isSelected onChange={() => {}}>
66+
Pro
67+
</SelectableCard>,
68+
);
69+
const card = container.querySelector('.astryx-selectable-card');
70+
if (card == null) {
71+
throw new Error('the card rendered no root');
72+
}
73+
const durations = [...durationsOf(card)];
74+
// TOUCH_QUERY with no platform gate (StyleX keeps it exclusive of the
75+
// later reduced-motion key, which still leaves every coarse pointer).
76+
const touch = durations.filter(
77+
([media]) =>
78+
media != null &&
79+
media !== REDUCED_MOTION_INSTANT &&
80+
media.includes('(pointer: coarse)'),
81+
);
82+
expect(touch.map(([, value]) => value)).toEqual([
83+
`${selection.mark.changeMs}ms`,
84+
]);
85+
expect(
86+
durations.filter(([media]) => media?.includes('-webkit-touch-callout')),
87+
).toEqual([]);
88+
expect(new Map(durations).get(null)).toMatch(/^var\(--duration-fast/);
89+
// design:reduced-motion DR3, in the instant key the policy spells.
90+
expect(new Map(durations).get(REDUCED_MOTION_INSTANT)).toBe('0s');
91+
});
92+
});

0 commit comments

Comments
 (0)