Skip to content

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

TypeScript

fix(core): a calc() duration token times the press release clock (#10069)

Christopher Chedeaue855791f09f2
diff --git a/.changeset/press-release-clock-calc.md b/.changeset/press-release-clock-calc.md
new file mode 100644
index 0000000000..6c085c847d
--- /dev/null
+++ b/.changeset/press-release-clock-calc.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A theme duration token written with `calc()` now times a press's release as the browser resolves it. Before, the release clock fell back to 175 ms while the fade played the token's value.
+
+@vjeux
diff --git a/packages/core/src/motion/__tests__/stylexDeclarationsPlugin.ts b/packages/core/src/motion/__tests__/stylexDeclarationsPlugin.ts
index 8a469b7a9d..ede05e900e 100644
--- a/packages/core/src/motion/__tests__/stylexDeclarationsPlugin.ts
+++ b/packages/core/src/motion/__tests__/stylexDeclarationsPlugin.ts
@@ -4,12 +4,14 @@
* @file stylexDeclarationsPlugin.ts
* @input esbuild's plugin interface
* @output `stylexDeclarations`, an esbuild plugin that loads
- * `@stylexjs/stylex` as a stand-in whose `defineVars` returns its values
+ * `@stylexjs/stylex` as a stand-in. Its `defineVars`, `defineConsts` and
+ * `create` return their values; `defineMarker` and `props` give empty
+ * objects
* @position Shared by the motion browser specs that bundle a module which
- * imports the theme's token file
+ * imports the theme's token file or declares styles
*
- * The token file declares its variables through StyleX, which the build
- * compiles away. A page needs only the plain defaults beside them.
+ * The modules declare their variables and styles through StyleX, which the
+ * build compiles away. A page needs only the plain values beside them.
*/
import type {Plugin} from 'esbuild';
@@ -22,7 +24,14 @@ export const stylexDeclarations: Plugin = {
namespace: 'stylex-declarations',
}));
builder.onLoad({filter: /.*/, namespace: 'stylex-declarations'}, () => ({
- contents: 'export const defineVars = values => values;',
+ contents: [
+ 'const same = value => value;',
+ 'export const defineVars = same;',
+ 'export const defineConsts = same;',
+ 'export const create = same;',
+ 'export const defineMarker = () => ({});',
+ 'export const props = () => ({});',
+ ].join('\n'),
loader: 'js',
}));
},
diff --git a/packages/core/src/motion/__tests__/themeTiming.interaction.browser.spec.ts b/packages/core/src/motion/__tests__/themeTiming.interaction.browser.spec.ts
index 0514a9ae97..1a01385478 100644
--- a/packages/core/src/motion/__tests__/themeTiming.interaction.browser.spec.ts
+++ b/packages/core/src/motion/__tests__/themeTiming.interaction.browser.spec.ts
@@ -2,18 +2,18 @@
/**
* @file themeTiming.interaction.browser.spec.ts
- * @input themeTiming.ts bundled with esbuild onto a blank fixture page;
- * Chromium and WebKit on the phone profile
+ * @input themeTiming.ts and the press release clock, bundled with esbuild
+ * onto a blank fixture page; Chromium and WebKit on the phone profile
* @output Proof that readThemeTiming resolves a motion's timing as the
* browser resolves a transition. A hand-written `calc()` token, a nested
* theme scope and a tempo-scaled value give one length in both of its
* patterns. In one, the element's own transition carries the token. In the
* other, the token is read where the element is. That length is the
* element's computed transition. A `calc()` value resolves the same way
- * for a caller that needs the values. An easing that is not a cubic
- * Bézier, `linear()` or `steps()`, plays as the browser plays it in both
- * patterns. With no stylesheet, `--duration-exit` reads its declared
- * default where the element is
+ * for the press release clock and for a caller that needs the values. An
+ * easing that is not a cubic Bézier, `linear()` or `steps()`, plays as the
+ * browser plays it in both patterns. With no stylesheet, `--duration-exit`
+ * reads its declared default where the element is
* @position Browser proof for themeTiming.ts, run by `pnpm test:interaction`;
* jsdom resolves no `calc()`
*/
@@ -21,14 +21,16 @@
import {fileURLToPath} from 'node:url';
import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
import {build} from 'esbuild';
-import type * as ThemeTimingModule from '../themeTiming';
+import type * as ThemeTimingModule from './themeTimingBundle';
import {stylexDeclarations} from './stylexDeclarationsPlugin';
let script = '';
test.beforeAll(async () => {
const result = await build({
- entryPoints: [fileURLToPath(new URL('../themeTiming.ts', import.meta.url))],
+ entryPoints: [
+ fileURLToPath(new URL('./themeTimingBundle.ts', import.meta.url)),
+ ],
plugins: [stylexDeclarations],
bundle: true,
write: false,
@@ -205,6 +207,27 @@ test('a duration that is not a positive length gives no curve in either pattern'
/** The page's bundle. */
type Bundle = typeof ThemeTimingModule;
+test('a calc() --duration-exit times the press release clock as its release animation resolves it', async ({
+ page,
+}) => {
+ await open(page);
+ const read = await page.evaluate(() => {
+ const {pressFadeMs, readThemeTokens} = (
+ window as unknown as {ThemeTiming: Bundle}
+ ).ThemeTiming;
+ const plain = document.querySelector<HTMLElement>('#plain');
+ if (plain == null) {
+ throw new Error('the fixture has no #plain');
+ }
+ plain.style.setProperty('--duration-exit', 'calc(0.1s * 2)');
+ return {
+ clock: pressFadeMs(plain),
+ token: readThemeTokens(plain, {duration: '--duration-exit'}).ms,
+ };
+ });
+ expect(read).toEqual({clock: 200, token: 200});
+});
+
test('resolveTiming resolves calc() values for a caller that needs them', async ({
page,
}) => {
diff --git a/packages/core/src/motion/__tests__/themeTimingBundle.ts b/packages/core/src/motion/__tests__/themeTimingBundle.ts
new file mode 100644
index 0000000000..abb1a6e6a3
--- /dev/null
+++ b/packages/core/src/motion/__tests__/themeTimingBundle.ts
@@ -0,0 +1,12 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file themeTimingBundle.ts
+ * @input themeTiming.ts and the press release clock that reads it
+ * @output Re-exports them as one entry point
+ * @position The esbuild entry the theme-timing proof bundles into its page
+ * (global `ThemeTiming`), and the type of that global
+ */
+
+export {readThemeTiming, readThemeTokens, resolveTiming} from '../themeTiming';
+export {pressFadeMs} from '../../utils/pressFeedback';
diff --git a/packages/core/src/motion/themeTiming.ts b/packages/core/src/motion/themeTiming.ts
index 55154a4167..e7069f7da0 100644
--- a/packages/core/src/motion/themeTiming.ts
+++ b/packages/core/src/motion/themeTiming.ts
@@ -101,7 +101,6 @@ export function easedProgress(
};
}
-/** The first transition's duration in a computed style, in ms. */
function firstDurationMs(style: CSSStyleDeclaration): number | null {
return parseMotionRoleValue(
'duration',
@@ -181,7 +180,7 @@ function progressOf(
* them where `element` is. A plain time or easing parses as written. Other
* text, such as `calc()`, resolves on a hidden probe's transition. The
* duration is null when it is not a time of zero or more. The easing is null
- * when it is not a cubic Bézier, and a text left out gives null.
+ * when it is not a cubic Bézier. A value left out gives null.
*/
export function resolveTiming(
element: Element,
diff --git a/packages/core/src/utils/pressFeedback.test.ts b/packages/core/src/utils/pressFeedback.test.ts
index f7b107c689..1e7bca06f1 100644
--- a/packages/core/src/utils/pressFeedback.test.ts
+++ b/packages/core/src/utils/pressFeedback.test.ts
@@ -29,6 +29,7 @@ import {
installPressFeedback,
PRESSABLE_ATTRIBUTE,
PRESSED_ATTRIBUTE,
+ pressFadeMs,
} from './pressFeedback';
import {
isTrackedByGroup,
@@ -697,6 +698,34 @@ describe('the release clock without a theme', () => {
});
});
+describe('the release clock on the pressed element', () => {
+ afterEach(() => {
+ document.body.replaceChildren();
+ });
+
+ /** An element with `tokens` set on it, in the document. */
+ function pressed(tokens: Record<string, string> = {}): HTMLElement {
+ const element = document.createElement('div');
+ for (const [name, value] of Object.entries(tokens)) {
+ element.style.setProperty(name, value);
+ }
+ document.body.appendChild(element);
+ return element;
+ }
+
+ it('reads --duration-exit where the element is, a zero included', () => {
+ expect(pressFadeMs(pressed({'--duration-exit': '250ms'}))).toBe(250);
+ expect(pressFadeMs(pressed({'--duration-exit': '0ms'}))).toBe(0);
+ });
+
+ it("gives the token's default for no value or a value that is not a time", () => {
+ expect(pressFadeMs(pressed())).toBe(PRESS_FADE_MS);
+ expect(pressFadeMs(pressed({'--duration-exit': 'fast'}))).toBe(
+ PRESS_FADE_MS,
+ );
+ });
+});
+
/** The Reduce Motion fade, ms: the longest a finger's release plays there. */
const REDUCED_FADE_MS = Number.parseFloat(reducedMotionFadeConsts.duration);
diff --git a/packages/core/src/utils/pressFeedback.ts b/packages/core/src/utils/pressFeedback.ts
index db3378c230..57115e9b4e 100644
--- a/packages/core/src/utils/pressFeedback.ts
+++ b/packages/core/src/utils/pressFeedback.ts
@@ -51,6 +51,7 @@ import {
import {isCoarsePointer, watchCoarsePointer} from './coarsePointer';
import {reducedMotionFadeConsts} from '../motion/reducedMotion.stylex';
import {parseMotionRoleValue} from '../motion/roles';
+import {readThemeTokens} from '../motion/themeTiming';
import {subscribeInput} from './inputStream';
import {isInsideScrollView} from './scrollView';
import {msSinceInput} from './inputAge';
@@ -203,16 +204,14 @@ const TIMER_MS: Record<NonNullable<PressStep['timer']>, number> = {
/**
* The release fade's clock on `element`, read once, at the release, from the
- * token its stylesheet reads: the theme's `--duration-exit`. Without a
- * readable value it is the token's default. Under Reduce Motion it is no
- * longer than the Reduce Motion fade, as in the stylesheet.
+ * token its stylesheet reads. That token is the theme's `--duration-exit`,
+ * resolved as the release animation's duration resolves it. A value that is
+ * not a time gives the token's default. Under Reduce Motion it is no longer
+ * than the Reduce Motion fade, as in the stylesheet.
*/
export function pressFadeMs(element: Element): number {
const exit =
- parseMotionRoleValue(
- 'duration',
- getComputedStyle(element).getPropertyValue('--duration-exit'),
- ) ?? PRESS_FADE_MS;
+ readThemeTokens(element, {duration: '--duration-exit'}).ms ?? PRESS_FADE_MS;
const reduced =
typeof window.matchMedia === 'function' &&
window.matchMedia('(prefers-reduced-motion: reduce)').matches;