Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/alertdialog-release-fade-tempo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@astryxdesign/core': patch
---

[fix] AlertDialog: under the touch presentation, an action lifted while the alert stays open now ends its release fade on the theme's tempo-scaled clock, as the stylesheet's fade does, instead of at a fixed 200 ms.

@vjeux
49 changes: 49 additions & 0 deletions packages/core/src/hooks/actionGroupTracker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,55 @@ describe('createActionGroupTracker', () => {
]);
});

it('times the release fade by the tempo its stylesheet reads, as the press controller does (spec:AST-108 FR4)', () => {
// A rule writes the tempo; browsers inherit it from the theme, jsdom
// only reads it where it is declared.
const style = document.createElement('style');
style.textContent = '[data-action] { --_astryx-motion-tempo: 1.5; }';
document.head.append(style);
try {
const group = tracker();
group.begin(100, 20);
group.end();
vi.advanceTimersByTime(PRESS_FADE_MS);
expect(first.button.getAttribute(PRESSED_ATTRIBUTE)).toBe('fading');
vi.advanceTimersByTime(PRESS_FADE_MS / 2);
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
} finally {
style.remove();
}
});

it('under Reduce Motion a tempo may shorten the release fade but never lengthens it past 200 ms (spec:AST-108 FR4)', () => {
vi.stubGlobal(
'matchMedia',
(query: string): MediaQueryList =>
({
matches: /prefers-reduced-motion:\s*reduce/.test(query),
media: query,
onchange: null,
addListener: () => undefined,
removeListener: () => undefined,
addEventListener: () => undefined,
removeEventListener: () => undefined,
dispatchEvent: () => false,
}) as MediaQueryList,
);
const style = document.createElement('style');
style.textContent = '[data-action] { --_astryx-motion-tempo: 1.5; }';
document.head.append(style);
try {
const group = tracker();
group.begin(100, 20);
group.end();
vi.advanceTimersByTime(PRESS_FADE_MS);
expect(first.button.hasAttribute(PRESSED_ATTRIBUTE)).toBe(false);
} finally {
style.remove();
vi.unstubAllGlobals();
}
});

it('dispose finishes a pending release fade', () => {
const group = tracker();
group.begin(100, 20);
Expand Down
9 changes: 5 additions & 4 deletions packages/core/src/hooks/actionGroupTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/**
* @file actionGroupTracker.ts
* @input The tracked elements, the action selector and hit expansion, and the
* press paint and fade clocks
* press paint and the press controller's fade clock
* @output createActionGroupTracker, ActionGroupTracker,
* ActionGroupTrackerOptions, ActionGroupTarget
* @position Internal core tracker for a held finger that slides across a
Expand All @@ -14,8 +14,7 @@

import {clickAndReport, clickForLift} from '../utils/browserClickWait';
import {armClickGuard} from '../utils/clickGuard';
import {PRESSED_ATTRIBUTE} from '../utils/pressFeedback';
import {PRESS_FADE_MS} from '../utils/pressGesture';
import {PRESSED_ATTRIBUTE, pressFadeMs} from '../utils/pressFeedback';

/** An action that is not enabled takes no highlight and does not run. */
const DISABLED = ':disabled, [aria-disabled="true"]';
Expand Down Expand Up @@ -193,14 +192,16 @@ export function createActionGroupTracker(
}
clearFade(element);
element.setAttribute(PRESSED_ATTRIBUTE, 'fading');
// The stylesheet's release follows the theme's tempo, so the fade ends on
// the press controller's clock.
fades.set(
element,
setTimeout(() => {
fades.delete(element);
if (element.getAttribute(PRESSED_ATTRIBUTE) === 'fading') {
element.removeAttribute(PRESSED_ATTRIBUTE);
}
}, PRESS_FADE_MS),
}, pressFadeMs(element)),
);
};

Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/utils/pressFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
* @input The press state machine (pressGesture.ts), the menu-press and
* press-group markers and the document's pointer, keyboard, scroll and
* visibility events
* @output PRESSABLE_ATTRIBUTE, PRESSED_ATTRIBUTE, pressableProps and
* installPressFeedback
* @output PRESSABLE_ATTRIBUTE, PRESSED_ATTRIBUTE, pressableProps,
* pressFadeMs and installPressFeedback
* @position The DOM half of the touch press model. usePressFeedback installs
* it, and the CSS arms live in interactionOverlay.stylex.ts.
*
Expand Down Expand Up @@ -153,7 +153,7 @@ const TIMER_MS: Record<NonNullable<PressStep['timer']>, number> = {
* property its stylesheet reads: the theme's `--_astryx-motion-tempo`. Under
* Reduce Motion a tempo may shorten the fade, never lengthen it past 200 ms.
*/
function fadeMs(element: Element): number {
export function pressFadeMs(element: Element): number {
const style = getComputedStyle(element);
const value = style.getPropertyValue('--_astryx-motion-tempo').trim();
const read = value === '' ? Number.NaN : Number(value);
Expand Down Expand Up @@ -234,7 +234,7 @@ function apply(slot: Slot, step: PressStep): void {
},
// The release fade follows the theme's tempo (AST-108). The onset delay
// and the tap flash do not.
kind === 'fade' ? fadeMs(live.element) : TIMER_MS[kind],
kind === 'fade' ? pressFadeMs(live.element) : TIMER_MS[kind],
);
}
}
Expand Down
Loading