Skip to content

Commit c68e13e

Browse files
committed
fix(SelectableCard): under an iOS 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. iOS changes a selection mark in the frame the selection lands (motion/uikit/selection.ts, selection.mark.changeMs). Under the iOS idiom (the nested coarse-pointer and touch-callout keys, IOS_TOUCH_STYLE_KEYS) 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. Android and a mouse keep the theme's fade.
1 parent ecdce12 commit c68e13e

5 files changed

Lines changed: 463 additions & 3 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] In iOS Safari with a coarse primary pointer (a touchscreen), a SelectableCard's selection mark now changes in one frame, as iOS'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. Elsewhere, Android and fine pointers included, the card keeps the theme's fade. Under Reduce Motion the mark changes at once on every pointer, as the reduced-motion design record asks.
6+
7+
@vjeux

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const docs = {
2222
},
2323
props: [
2424
{name: 'label', type: 'string', description: 'Accessibility label.', required: true},
25-
{name: 'isSelected', type: 'boolean', description: 'Controlled selection state.', required: true},
25+
{name: 'isSelected', type: 'boolean', description: 'Controlled selection state. In iOS Safari with a coarse primary pointer (a touchscreen) the selection ring and border change in one frame; elsewhere, Android and fine pointers included, they fade over the theme’s fast duration.', required: true},
2626
{name: 'onChange', type: '(isSelected: boolean) => void', description: 'Called when toggled.', required: true},
2727
{name: 'isDisabled', type: 'boolean', description: 'Disables the card.', default: 'false'},
2828
{name: 'children', type: 'ReactNode', description: 'Card content.'},

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

Lines changed: 23 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';
@@ -61,6 +63,9 @@ import {useMergedRefs} from '../hooks/useMergedRefs';
6163
// `::after` that paints it. See interactionOverlay.stylex.ts.
6264
const pressedOverlayColor = 'var(--_press-paint)';
6365

66+
/** How long the selection mark takes to change under an iOS finger: one frame. */
67+
const MARK_CHANGE = `${selectionMotionConsts.markChangeMs}ms`;
68+
6469
const styles = stylex.create({
6570
interactive: {
6671
// Declared here, on the element that carries the `astryx-selectable-card`
@@ -73,7 +78,23 @@ const styles = stylex.create({
7378
':is(:disabled,[aria-disabled="true"])': 'default',
7479
},
7580
transitionProperty: 'box-shadow, border-color',
76-
transitionDuration: durationVars['--duration-fast'],
81+
transitionDuration: {
82+
default: durationVars['--duration-fast'],
83+
// Under an iOS finger the selection mark (the inset ring and the
84+
// border) changes in the frame the selection lands, as iOS's selection
85+
// marks do (motion/uikit/selection.ts). The ring cannot interpolate
86+
// with the card's resting shadow, so a fade would split it from the
87+
// border. The iOS gate is the nested media + supports keys
88+
// (IOS_TOUCH_STYLE_KEYS); Android and a mouse keep the theme's
89+
// duration.
90+
'@media (pointer: coarse)': {
91+
'@supports (-webkit-touch-callout: none)': MARK_CHANGE,
92+
},
93+
// Reduce Motion: the mark changes at once on every pointer
94+
// (design:reduced-motion DR3; DR2's cross-fade cap on a touch screen
95+
// allows it). Last, so StyleX's media ordering keeps it plain.
96+
'@media (prefers-reduced-motion: reduce)': '0s',
97+
},
7798
transitionTimingFunction: easeVars['--ease-standard'],
7899
},
79100
// Hover and pressed overlay. Hover is guarded by @media (hover: hover) so
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 the iOS idiom (the nested
9+
* coarse-pointer and touch-callout keys) and keeps the theme's duration
10+
* token with a mouse or an Android finger
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+
describe('SelectableCard selection mark', () => {
59+
it('changes its ring and border in one frame under an iOS finger only, and keeps the theme’s duration with a mouse or an Android finger', () => {
60+
const {container} = render(
61+
<SelectableCard label="Pro plan" isSelected onChange={() => {}}>
62+
Pro
63+
</SelectableCard>,
64+
);
65+
const card = container.querySelector('.astryx-selectable-card');
66+
if (card == null) {
67+
throw new Error('the card rendered no root');
68+
}
69+
const durations = [...durationsOf(card)];
70+
const ios = durations.filter(
71+
([media]) =>
72+
media?.includes('(-webkit-touch-callout: none)') &&
73+
media.includes('(pointer: coarse)'),
74+
);
75+
expect(ios.map(([, value]) => value)).toEqual([
76+
`${selection.mark.changeMs}ms`,
77+
]);
78+
// No arm for a coarse pointer alone: Android keeps the theme's fade.
79+
expect(
80+
durations.filter(
81+
([media]) =>
82+
media?.includes('(pointer: coarse)') &&
83+
!media.includes('-webkit-touch-callout'),
84+
),
85+
).toEqual([]);
86+
expect(new Map(durations).get(null)).toMatch(/^var\(--duration-fast/);
87+
// Reduce Motion: at once on every pointer (design:reduced-motion DR2,
88+
// DR3), in a plain rule, not narrowed by the iOS arm's media.
89+
expect(new Map(durations).get('(prefers-reduced-motion: reduce)')).toBe(
90+
'0s',
91+
);
92+
});
93+
});

0 commit comments

Comments
 (0)