Skip to content

Commit b206f63

Browse files
cixzhangvjeux
andauthored
fix(core): a touch press's release lasts until its fade has played (#9758)
* fix(core): a touch press's release lasts until its fade has played Under a finger, the touch press controller ended a press's release on a clock that starts at the lift. When script delays the first frame of the release animation, the animation ends later, but the clock does not, so the highlight was cut off mid-fade. When the clock runs out, the controller now waits for a release animation that still runs or still waits for its first frame: on the surface, its ::before or ::after, or on a part inside it whose nearest press owner is the surface. With none, the release ends at the clock, as before. Rebased onto main after #8360 landed: main's theme tempo already caps the release under Reduce Motion, so only the release-ends-with-its-animation part remains. Co-authored-by: Cindy Zhang <cindyxz@meta.com> * docs(core): the press clock's comments say the release waits for its animation CodeBunny's notes on the first round: pressGesture.ts still described PRESS_FADE_MS as the timer that removes the attribute, and two new comments in pressFeedback.ts had sentences over 25 words. * Merge main into the press release change Main's #8638 renamed the release clock helper fadeMs to an exported pressFadeMs. The release call here now uses pressFadeMs; nothing else in this pull request changes. Co-authored-by: Christopher Chedeau <vjeuxx@gmail.com>
1 parent cc034ed commit b206f63

5 files changed

Lines changed: 271 additions & 25 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+
[fix] Under a finger, a press's release now lasts until its fade has played. The touch press controller ended the release on a clock started at the lift, so when the page's script delayed the fade's first frame, the release was cut short mid-fade. It now ends when the press's release animation ends, on the surface or on a part inside it that a component draws the release on, or on its clock where no release animation runs (an instant release, a surface without one, or an engine without Web Animations). This applies to every surface the touch press controller drives: buttons, switches, checkboxes, cards, menu rows and list rows.
6+
7+
@vjeux

‎packages/core/src/utils/interactionOverlay.stylex.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ export const pressVars = stylex.defineVars({
7676
*
7777
* `defineConsts`, so the one number is inlined here at build time and is the
7878
* controller's constant at run time (`PRESS_FADE_MS` in pressGesture.ts):
79-
* the release animation and the timer that removes the attribute after it
80-
* cannot drift apart.
79+
* the release animation and the controller's clock cannot drift apart. When
80+
* that clock runs out, the controller waits for a release animation that a
81+
* busy main thread started late, then removes the attribute.
8182
*/
8283
export const pressConsts = stylex.defineConsts({
8384
/** How long the release takes, in ms. UIKit's deselect clock. */

‎packages/core/src/utils/pressFeedback.test.ts‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,164 @@ describe('the release', () => {
656656
});
657657
});
658658

659+
/**
660+
* A CSS animation as a browser reports it from `getAnimations()` (jsdom runs
661+
* none): by default the release the stylesheet starts on the surface's
662+
* `::before` at the `fading` write, still waiting for its first frame.
663+
* `end()` finishes it; `cancel()` cancels it, as unmounting or restyling the
664+
* surface does.
665+
*/
666+
function cssAnimation(
667+
element: Element,
668+
{
669+
target = element,
670+
playState = 'running',
671+
endTime = PRESS_FADE_MS,
672+
}: {target?: Element; playState?: string; endTime?: number} = {},
673+
) {
674+
let end: () => void = () => undefined;
675+
let cancel: () => void = () => undefined;
676+
const finished = new Promise<void>((resolve, reject) => {
677+
end = resolve;
678+
cancel = () => reject(new DOMException('cancelled', 'AbortError'));
679+
});
680+
finished.catch(() => undefined);
681+
return {
682+
animation: {
683+
animationName: 'release',
684+
playState,
685+
finished,
686+
effect: {
687+
target,
688+
pseudoElement: '::before',
689+
getComputedTiming: () => ({endTime}),
690+
},
691+
},
692+
end: () => end(),
693+
cancel: () => cancel(),
694+
};
695+
}
696+
697+
/** The animations a browser would report running on `element` and its pseudo-elements. */
698+
function animate(element: Element, animations: ReadonlyArray<object>): void {
699+
Object.defineProperty(element, 'getAnimations', {
700+
configurable: true,
701+
value: () => animations,
702+
});
703+
}
704+
705+
/** Let the callbacks a finished or cancelled animation queued run. */
706+
async function settled(): Promise<void> {
707+
for (let i = 0; i < 4; i += 1) {
708+
await Promise.resolve();
709+
}
710+
}
711+
712+
/** A finger's held press on the first row, lifted: the release is written. */
713+
function liftHeldPress(): void {
714+
press(rows[0], 'pointerdown', 10, 10);
715+
vi.advanceTimersByTime(PRESS_ONSET_MS);
716+
press(rows[0], 'pointerup', 10, 10);
717+
expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
718+
}
719+
720+
describe('the release ends with its animation, not its clock', () => {
721+
it('keeps `fading` until the release animation ends, however late a busy main thread let its first frame come', async () => {
722+
liftHeldPress();
723+
// 150 ms of script held the main thread after the lift: the release
724+
// started at the first frame after it, and is still running.
725+
const release = cssAnimation(rows[0]);
726+
animate(rows[0], [release.animation]);
727+
vi.advanceTimersByTime(PRESS_FADE_MS);
728+
await settled();
729+
expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
730+
release.end();
731+
await settled();
732+
expect(rows[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
733+
});
734+
735+
it('waits for a release still waiting for its first frame when the clock ran out', async () => {
736+
liftHeldPress();
737+
const release = cssAnimation(rows[0], {playState: 'pending'});
738+
animate(rows[0], [release.animation]);
739+
vi.advanceTimersByTime(PRESS_FADE_MS * 3);
740+
await settled();
741+
expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
742+
release.end();
743+
await settled();
744+
expect(rows[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
745+
});
746+
747+
it('ends when the release animation is cancelled: the surface was unmounted or restyled', async () => {
748+
liftHeldPress();
749+
const release = cssAnimation(rows[0]);
750+
animate(rows[0], [release.animation]);
751+
vi.advanceTimersByTime(PRESS_FADE_MS);
752+
release.cancel();
753+
await settled();
754+
expect(rows[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
755+
});
756+
757+
it('ends on its clock where no release animation runs: an instant release, a surface without one, or an engine without Web Animations', async () => {
758+
liftHeldPress();
759+
animate(rows[0], []);
760+
vi.advanceTimersByTime(PRESS_FADE_MS);
761+
await settled();
762+
expect(rows[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
763+
});
764+
765+
it('waits for a release a component draws inside the surface, on a descendant whose nearest press owner is the surface', async () => {
766+
liftHeldPress();
767+
// The box wrapper's `::after` plays the release for the row's press,
768+
// 50 ms longer than the row's clock.
769+
const box = document.createElement('span');
770+
rows[0].append(box);
771+
const release = cssAnimation(rows[0], {
772+
target: box,
773+
endTime: PRESS_FADE_MS + 50,
774+
});
775+
animate(rows[0], [release.animation]);
776+
vi.advanceTimersByTime(PRESS_FADE_MS);
777+
await settled();
778+
expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
779+
release.end();
780+
await settled();
781+
expect(rows[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
782+
});
783+
784+
it('waits for no animation that is not this press’s running release: a nested press owner’s (on it or inside it), an endless one, or one already finished', async () => {
785+
liftHeldPress();
786+
// A press owner inside the row: its release is its own press's, never
787+
// the row's.
788+
const nested = pressable('span');
789+
const inside = document.createElement('span');
790+
nested.append(inside);
791+
rows[0].append(nested);
792+
animate(rows[0], [
793+
cssAnimation(rows[0], {target: nested}).animation,
794+
cssAnimation(rows[0], {target: inside}).animation,
795+
cssAnimation(rows[0], {endTime: Infinity}).animation,
796+
cssAnimation(rows[0], {playState: 'finished'}).animation,
797+
]);
798+
vi.advanceTimersByTime(PRESS_FADE_MS);
799+
await settled();
800+
expect(rows[0].hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
801+
});
802+
803+
it('a new press on the surface owns its paint: the old release ending later does not clear it', async () => {
804+
liftHeldPress();
805+
const release = cssAnimation(rows[0]);
806+
animate(rows[0], [release.animation]);
807+
vi.advanceTimersByTime(PRESS_FADE_MS);
808+
press(rows[0], 'pointerdown', 10, 10);
809+
vi.advanceTimersByTime(PRESS_ONSET_MS);
810+
expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
811+
release.end();
812+
await settled();
813+
expect(rows[0].getAttribute(PRESSED_ATTRIBUTE)).toBe('on');
814+
});
815+
});
816+
659817
describe('two touches in a row', () => {
660818
it('answers the second tap while the first is still fading', () => {
661819
press(rows[0], 'pointerdown', 10, 10);

‎packages/core/src/utils/pressFeedback.ts‎

Lines changed: 99 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ interface LiveGesture {
109109
* for a press of the Space key.
110110
*/
111111
pointerId: number | null;
112-
timer: ReturnType<typeof setTimeout> | null;
112+
/** Cancels the clock the current phase owes, or the release it waits for. */
113+
timer: (() => void) | null;
113114
}
114115

115116
/**
@@ -165,6 +166,76 @@ export function pressFadeMs(element: Element): number {
165166
return reduced ? Math.min(scaled, PRESS_FADE_MS) : scaled;
166167
}
167168

169+
/**
170+
* The release animations that still run for `element`'s press. Each is a
171+
* finite CSS animation that has not finished. It runs on the element, on its
172+
* `::before` or `::after`, or on a descendant whose nearest press owner is
173+
* the element, because a component can draw its release on a part inside the
174+
* surface. A press owner nested inside plays the release of its own press,
175+
* so it never extends this one. The list is empty where the engine has no Web
176+
* Animations.
177+
*/
178+
function releaseAnimationsOf(element: Element): Animation[] {
179+
if (typeof element.getAnimations !== 'function') {
180+
return [];
181+
}
182+
return element.getAnimations({subtree: true}).filter(animation => {
183+
// The effect of a CSS animation is a KeyframeEffect. Its target is the
184+
// element, also when the animation runs on a pseudo-element.
185+
const effect = animation.effect as KeyframeEffect | null;
186+
const target = effect?.target ?? null;
187+
if (
188+
effect == null ||
189+
target == null ||
190+
!('animationName' in animation) ||
191+
(target !== element &&
192+
target.closest(`[${PRESSABLE_ATTRIBUTE}]`) !== element)
193+
) {
194+
return false;
195+
}
196+
if (!Number.isFinite(Number(effect.getComputedTiming().endTime))) {
197+
return false;
198+
}
199+
return animation.playState !== 'finished';
200+
});
201+
}
202+
203+
/**
204+
* Calls `end` when the release on `element` is over. A busy main thread
205+
* delays the first frame of the release animation, and with it the end. So a
206+
* clock that starts at the `fading` write can cut the release short. When
207+
* `clockMs` runs out, the release waits for each release animation that still
208+
* runs or still waits for its first frame. It ends when those finish or are
209+
* cancelled, as when the surface is unmounted or restyled. With no release
210+
* animation (an instant release, a surface without one, an engine without Web
211+
* Animations), the release ends at the clock. Returns a cancel.
212+
*/
213+
function endAfterRelease(
214+
element: Element,
215+
clockMs: number,
216+
end: () => void,
217+
): () => void {
218+
let waiting = true;
219+
const timer = setTimeout(() => {
220+
const running = releaseAnimationsOf(element);
221+
if (running.length === 0) {
222+
end();
223+
return;
224+
}
225+
void Promise.allSettled(
226+
running.map(async animation => animation.finished),
227+
).then(() => {
228+
if (waiting) {
229+
end();
230+
}
231+
});
232+
}, clockMs);
233+
return () => {
234+
waiting = false;
235+
clearTimeout(timer);
236+
};
237+
}
238+
168239
const TIMER_EVENT = {
169240
fade: 'fadeEnd',
170241
flash: 'flashEnd',
@@ -181,7 +252,7 @@ function paintElement(element: HTMLElement, paint: PressPaint): void {
181252

182253
function clearTimer(slot: Slot): void {
183254
if (slot.live?.timer != null) {
184-
clearTimeout(slot.live.timer);
255+
slot.live.timer();
185256
slot.live.timer = null;
186257
}
187258
}
@@ -217,25 +288,32 @@ function apply(slot: Slot, step: PressStep): void {
217288
}
218289
if (step.timer != null) {
219290
const kind = step.timer;
220-
// A TIMER, never `requestAnimationFrame`: iOS throttles frame callbacks
221-
// while a scroll runs, so a frame-based delay would fire late, after the
222-
// scroll, and paint a control nobody is pressing any more.
223-
live.timer = setTimeout(
224-
() => {
225-
if (slot.live != null) {
226-
slot.live.timer = null;
227-
}
228-
apply(
229-
slot,
230-
pressGestureStep(slot.live?.gesture ?? idlePressGesture(), {
231-
kind: TIMER_EVENT[kind],
232-
}),
233-
);
234-
},
235-
// The release fade follows the theme's tempo (AST-108). The onset delay
236-
// and the tap flash do not.
237-
kind === 'fade' ? pressFadeMs(live.element) : TIMER_MS[kind],
238-
);
291+
const fire = () => {
292+
if (slot.live != null) {
293+
slot.live.timer = null;
294+
}
295+
apply(
296+
slot,
297+
pressGestureStep(slot.live?.gesture ?? idlePressGesture(), {
298+
kind: TIMER_EVENT[kind],
299+
}),
300+
);
301+
};
302+
if (kind === 'fade') {
303+
// The release fade follows the theme's tempo (AST-108). It ends when
304+
// its animation ends, however late a busy main thread let it start.
305+
live.timer = endAfterRelease(
306+
live.element,
307+
pressFadeMs(live.element),
308+
fire,
309+
);
310+
} else {
311+
// A TIMER, never `requestAnimationFrame`: iOS throttles frame callbacks
312+
// while a scroll runs, so a frame-based delay would fire late, after the
313+
// scroll, and paint a control nobody is pressing any more.
314+
const timer = setTimeout(fire, TIMER_MS[kind]);
315+
live.timer = () => clearTimeout(timer);
316+
}
239317
}
240318
}
241319

‎packages/core/src/utils/pressGesture.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ export const PRESS_FLASH_MS = 100;
6262
* How long the paint takes to leave after a release: UIKit's deselect
6363
* crossfade. The one number, shared with the stylesheet: the release
6464
* animation in interactionOverlay.stylex.ts runs on the same constant, so the
65-
* timer that removes the attribute and the fade cannot drift apart.
65+
* controller's release clock and the fade cannot drift apart. When the clock
66+
* runs out, the controller waits for a release animation that still runs,
67+
* then removes the attribute.
6668
*/
6769
export const PRESS_FADE_MS: number = pressConsts.releaseMs;
6870

@@ -119,7 +121,7 @@ export type PressEvent =
119121
| {kind: 'onset'}
120122
/** {@link PRESS_FLASH_MS} elapsed on a too-quick tap. */
121123
| {kind: 'flashEnd'}
122-
/** {@link PRESS_FADE_MS} elapsed; the paint is gone. */
124+
/** The release is over: its animation ended, or its clock ran out with none running. */
123125
| {kind: 'fadeEnd'};
124126

125127
export interface PressStep {

0 commit comments

Comments
 (0)