Skip to content

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

TypeScript

fix(AlertDialog): a released action fades on the theme's tempo, as other presses do (#8638)

Christopher Chedeau787b6967b152
diff --git a/.changeset/alertdialog-release-fade-tempo.md b/.changeset/alertdialog-release-fade-tempo.md
new file mode 100644
index 0000000000..4bf6bacc6c
--- /dev/null
+++ b/.changeset/alertdialog-release-fade-tempo.md
@@ -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
diff --git a/packages/core/src/hooks/actionGroupTracker.test.ts b/packages/core/src/hooks/actionGroupTracker.test.ts
index 54001b8c1c..ba190c765c 100644
--- a/packages/core/src/hooks/actionGroupTracker.test.ts
+++ b/packages/core/src/hooks/actionGroupTracker.test.ts
@@ -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);
diff --git a/packages/core/src/hooks/actionGroupTracker.ts b/packages/core/src/hooks/actionGroupTracker.ts
index 0bd3462cf9..60d839139e 100644
--- a/packages/core/src/hooks/actionGroupTracker.ts
+++ b/packages/core/src/hooks/actionGroupTracker.ts
@@ -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
@@ -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"]';
@@ -193,6 +192,8 @@ 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(() => {
@@ -200,7 +201,7 @@ export function createActionGroupTracker(
if (element.getAttribute(PRESSED_ATTRIBUTE) === 'fading') {
element.removeAttribute(PRESSED_ATTRIBUTE);
}
- }, PRESS_FADE_MS),
+ }, pressFadeMs(element)),
);
};
diff --git a/packages/core/src/utils/pressFeedback.ts b/packages/core/src/utils/pressFeedback.ts
index b580c56ca6..f783785068 100644
--- a/packages/core/src/utils/pressFeedback.ts
+++ b/packages/core/src/utils/pressFeedback.ts
@@ -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.
*
@@ -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);
@@ -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],
);
}
}