Skip to content

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

TypeScript

fix(theme): add the --duration-exit token, defaulting to --duration-fast (#9798)

Cindy Zhang0d54fd3e711e
diff --git a/.changeset/duration-exit-token.md b/.changeset/duration-exit-token.md
new file mode 100644
index 0000000000..924c803a58
--- /dev/null
+++ b/.changeset/duration-exit-token.md
@@ -0,0 +1,8 @@
+---
+'@astryxdesign/core': patch
+'@astryxdesign/cli': patch
+---
+
+[fix] The theme token `--duration-exit`, the standard dismissal duration, now exists. It defaults to `var(--duration-fast)`, so it follows a theme's `tempo` through that token, and a theme can set it like any duration token. Before, a theme or component that read `var(--duration-exit)` found no value. The motion and token docs, and the theme template's token list, name it.
+
+@cixzhang
diff --git a/packages/cli/assets/docs/motion.doc.mjs b/packages/cli/assets/docs/motion.doc.mjs
index 23b07aa6a0..5c6ca59ceb 100644
--- a/packages/cli/assets/docs/motion.doc.mjs
+++ b/packages/cli/assets/docs/motion.doc.mjs
@@ -35,7 +35,7 @@ export const docs = {
content: [
{
type: 'prose',
- text: 'Nine duration tokens in fast, medium, and slow bands.',
+ text: 'Nine duration tokens in fast, medium, and slow bands, and `--duration-exit`, the standard dismissal duration, which follows `--duration-fast`.',
},
{
type: 'token-ref',
diff --git a/packages/cli/assets/docs/tree/tokens-duration.doc.mjs b/packages/cli/assets/docs/tree/tokens-duration.doc.mjs
index 052ad3b241..8d0fc68491 100644
--- a/packages/cli/assets/docs/tree/tokens-duration.doc.mjs
+++ b/packages/cli/assets/docs/tree/tokens-duration.doc.mjs
@@ -68,6 +68,10 @@ export const docs = {
[
"--duration-slow-max",
"1300ms"
+ ],
+ [
+ "--duration-exit",
+ "var(--duration-fast)"
]
]
}
diff --git a/packages/cli/assets/docs/tree/tokens.doc.mjs b/packages/cli/assets/docs/tree/tokens.doc.mjs
index 7fbe9625f6..0f9f1ed537 100644
--- a/packages/cli/assets/docs/tree/tokens.doc.mjs
+++ b/packages/cli/assets/docs/tree/tokens.doc.mjs
@@ -2,7 +2,7 @@
// AUTO-GENERATED — do not edit manually.
// Run: node scripts/generate-token-docs.mjs
-// Total: 285 tokens across 15 categories.
+// Total: 286 tokens across 15 categories.
/** @type {import('@astryxdesign/cli/authoring').NamespaceDoc} */
export const docs = {
diff --git a/packages/cli/assets/theme.template.ts b/packages/cli/assets/theme.template.ts
index 376ce388e3..0c484f1f55 100644
--- a/packages/cli/assets/theme.template.ts
+++ b/packages/cli/assets/theme.template.ts
@@ -230,7 +230,8 @@ export const myTheme = defineTheme({
// --border-width hairline thickness shared by bordered surfaces
// --radius-* none · inner · element · container · page · chat · full
// --shadow-* low/med/high + inset-{hover,selected,success,warning,error}
- // --duration-* {fast,medium,slow} × {-min, base, -max}
+ // --duration-* {fast,medium,slow} × {-min, base, -max} · exit
+ // (dismissal; defaults to fast)
// --ease-* standard (state) · entrance (arriving) · move (on
// screen) · exit (leaving)
// --font-family-* body · heading · code
diff --git a/packages/cli/test/__golden__/no-icon-set/docs-topics.json b/packages/cli/test/__golden__/no-icon-set/docs-topics.json
index 4c0cbf5067..cf55b2f9ba 100644
--- a/packages/cli/test/__golden__/no-icon-set/docs-topics.json
+++ b/packages/cli/test/__golden__/no-icon-set/docs-topics.json
@@ -202,7 +202,7 @@
"theme-motion",
"best-practices"
],
- "skeleton": "f1ffd7fc39f49d407c6caa563c25eadd8d79bf4d3c2fc6837f06297c37f714eb",
+ "skeleton": "fd00e4ddb07bfdb6b20ba2d257fe04877d634ce3906fa2fae2a0c21da4161e2f",
"mentions": []
},
"principles": {
diff --git a/packages/core/src/theme/generateThemeRules.ts b/packages/core/src/theme/generateThemeRules.ts
index 9d5e7cb543..d6d15499eb 100644
--- a/packages/core/src/theme/generateThemeRules.ts
+++ b/packages/core/src/theme/generateThemeRules.ts
@@ -463,17 +463,18 @@ function joinDeclarations(
}
/**
- * Public status roles whose canonical default references an existing semantic
- * source token. StyleX owns the document defaults. A theme that authors a
- * source repeats its omitted role at the theme root so nested Theme regions
- * bind the alias locally without changing authored-token metadata.
+ * Public tokens whose canonical default references another public token: the
+ * status roles and `--duration-exit`. StyleX owns the document defaults. A
+ * theme that authors a source repeats its omitted token at the theme root so
+ * nested Theme regions bind the alias locally without changing authored-token
+ * metadata.
*
* Conditional scopes repeat an omitted alias only when the source changes on
* that scope. Explicit adaptation role writes are replayed after generated
* aliases (and on media descendants) so authored role precedence survives
* overlapping conditions without disabling the alias in non-matching states.
*/
-const SCOPED_STATUS_ROLE_DEFAULTS: ReadonlyArray<
+const SCOPED_REFERENCE_DEFAULTS: ReadonlyArray<
readonly [role: string, source: string]
> = [
['--color-text-info', '--color-text-blue'],
@@ -493,16 +494,17 @@ const SCOPED_STATUS_ROLE_DEFAULTS: ReadonlyArray<
['--color-border-success', '--color-text-success'],
['--color-border-warning', '--color-text-warning'],
['--color-border-error', '--color-text-error'],
+ ['--duration-exit', '--duration-fast'],
];
-const SCOPED_STATUS_ROLE_SOURCES = new Map(SCOPED_STATUS_ROLE_DEFAULTS);
+const SCOPED_REFERENCE_SOURCES = new Map(SCOPED_REFERENCE_DEFAULTS);
/** Colours INV17's derivation measures; a scope that sets one re-derives. */
const STATUS_MEASURED =
/^--color-(?:info|success|warning|error|on-|background-surface|background-card)/;
const STATUS_ROLE_ORDER = new Map(
- SCOPED_STATUS_ROLE_DEFAULTS.map(([role], index) => [role, index]),
+ SCOPED_REFERENCE_DEFAULTS.map(([role], index) => [role, index]),
);
type TokenValues = Record<string, unknown>;
@@ -740,7 +742,7 @@ export function generatedScopedStatusRoleDefaults(
),
}));
- for (const [role, source] of SCOPED_STATUS_ROLE_DEFAULTS) {
+ for (const [role, source] of SCOPED_REFERENCE_DEFAULTS) {
const alias = `var(${source})`;
if (
authoredRoot[role] !== undefined ||
@@ -775,7 +777,7 @@ function authoredStatusRoleEntries(
tokens: Record<string, string>,
): [string, string][] {
const canonicalTokens = createTokenCascade(tokens).values;
- return SCOPED_STATUS_ROLE_DEFAULTS.flatMap(([role]) => {
+ return SCOPED_REFERENCE_DEFAULTS.flatMap(([role]) => {
const value = canonicalTokens[role];
return value === undefined ? [] : [[role, stringifyTokenValue(value)]];
});
@@ -789,7 +791,7 @@ function tokenValueIsAliasOf(value: unknown, source: string): boolean {
}
function mediaRoleRestorationValue(role: string, value: string): string {
- const source = SCOPED_STATUS_ROLE_SOURCES.get(role);
+ const source = SCOPED_REFERENCE_SOURCES.get(role);
return source !== undefined && tokenValueIsAliasOf(value, source)
? value
: 'inherit';
@@ -843,7 +845,7 @@ function generatedAdaptationStatusRoleDefaults(
// alias, and roles that follow them (icon and border) alias them here.
const ruleRoles = new Map(aliases[index]);
const ruleSources = {...authoredRule, ...Object.fromEntries(ruleRoles)};
- for (const [role, source] of SCOPED_STATUS_ROLE_DEFAULTS) {
+ for (const [role, source] of SCOPED_REFERENCE_DEFAULTS) {
const alias = `var(${source})`;
const rootRole = rootValues[role];
if (
@@ -921,7 +923,7 @@ function generatedMediaStatusRoleDefaults(
return stringifyTokenValue(candidate.values[role]);
};
- for (const [role, source] of SCOPED_STATUS_ROLE_DEFAULTS) {
+ for (const [role, source] of SCOPED_REFERENCE_DEFAULTS) {
const alias = `var(${source})`;
const rootRole = rootValues[role];
if (
diff --git a/packages/core/src/theme/themeMotion.test.ts b/packages/core/src/theme/themeMotion.test.ts
index 7c0236bb1b..71d9339ba9 100644
--- a/packages/core/src/theme/themeMotion.test.ts
+++ b/packages/core/src/theme/themeMotion.test.ts
@@ -15,7 +15,7 @@ import fs from 'node:fs';
import path from 'node:path';
import {describe, expect, it} from 'vitest';
import {defineTheme} from './defineTheme';
-import {generateAdaptationCSS} from './generateThemeRules';
+import {generateAdaptationCSS, generateThemeRules} from './generateThemeRules';
import {expandMotionScale} from './expandMotionScale';
import {
CHARACTER_EASE,
@@ -29,10 +29,13 @@ import {durationDefaults, easeDefaults} from './tokens.stylex';
const TODAY_MS = [130, 175, 230, 310, 410, 550, 730, 975, 1300];
const STANDARD = 'cubic-bezier(0.24, 1, 0.4, 1)';
+/** The duration tokens whose default is a literal, not a reference. */
+const BAND_DURATIONS = Object.entries(durationDefaults)
+ .filter(([, value]) => !value.startsWith('var('))
+ .map(([name]) => name);
+
function durationsMs(tokens: Readonly<Record<string, string>>): number[] {
- return Object.keys(durationDefaults).map(name =>
- Number.parseFloat(tokens[name]),
- );
+ return BAND_DURATIONS.map(name => Number.parseFloat(tokens[name]));
}
describe('the motion knobs (spec:AST-108 FR1)', () => {
@@ -243,6 +246,71 @@ describe('existing motion configuration (spec:AST-108 FR5)', () => {
});
});
+describe('--duration-exit (spec:AST-090 FR12)', () => {
+ it('defaults to --duration-fast', () => {
+ expect(durationDefaults['--duration-exit']).toBe('var(--duration-fast)');
+ expect(resolveThemeMotion({}).durations['--duration-exit']).toBe('175ms');
+ });
+
+ it('follows tempo through --duration-fast, and tempo writes no value for it', () => {
+ const tokens = expandThemeMotion({tempo: 1.2});
+ expect(tokens).not.toHaveProperty('--duration-exit');
+ expect(tokens).not.toHaveProperty('--_astryx-pre-tempo-duration-exit');
+ expect(Object.values(tokens).some(value => value.includes('NaN'))).toBe(
+ false,
+ );
+ expect(
+ resolveThemeMotion({motion: {tempo: 1.2}}).durations['--duration-exit'],
+ ).toBe(`${Math.round(175 * 1.2)}ms`);
+ });
+
+ it.each([
+ ['a tempo', {name: 'ExitTempo', motion: {tempo: 1.2}}],
+ [
+ 'a duration scale',
+ {name: 'ExitScale', motion: {fast: 100, medium: 250, ratio: 0.75}},
+ ],
+ [
+ 'an explicit --duration-fast',
+ {name: 'ExitFast', tokens: {'--duration-fast': '120ms'}},
+ ],
+ ] as const)(
+ 'is declared again in a theme scope that sets --duration-fast through %s, so a nested theme follows its own value',
+ (_label, config) => {
+ const css = generateThemeRules(defineTheme(config)).join('\n');
+ expect(css).toContain('--duration-exit: var(--duration-fast);');
+ },
+ );
+
+ it('is not declared again where the theme sets it, or sets no --duration-fast', () => {
+ const own = generateThemeRules(
+ defineTheme({
+ name: 'ExitOwn',
+ motion: {tempo: 1.2},
+ tokens: {'--duration-exit': '90ms'},
+ }),
+ ).join('\n');
+ expect(own).not.toContain('--duration-exit: var(--duration-fast);');
+ const plain = generateThemeRules(defineTheme({name: 'ExitPlain'})).join(
+ '\n',
+ );
+ expect(plain).not.toContain('--duration-exit');
+ });
+
+ it('takes a theme override', () => {
+ expect(
+ resolveThemeMotion({tokens: {'--duration-exit': '90ms'}}).durations[
+ '--duration-exit'
+ ],
+ ).toBe('90ms');
+ const theme = defineTheme({
+ name: 'ExitOverride',
+ tokens: {'--duration-exit': '90ms'},
+ });
+ expect(theme.tokens['--duration-exit']).toBe('90ms');
+ });
+});
+
describe('resolveThemeMotion (spec:AST-108 FR6)', () => {
it('returns the knobs, durations and easings with explicit tokens applied', () => {
const motion = resolveThemeMotion({
diff --git a/packages/core/src/theme/themeMotion.ts b/packages/core/src/theme/themeMotion.ts
index 7dc4e68a20..666bd1a76b 100644
--- a/packages/core/src/theme/themeMotion.ts
+++ b/packages/core/src/theme/themeMotion.ts
@@ -94,10 +94,18 @@ export const MOTION_TEMPO_PROPERTY = '--_astryx-motion-tempo';
*/
export const REDUCED_MOTION_PROPERTY = '--_astryx-reduced-motion';
+/**
+ * A reference such as `--duration-exit` follows the token it names, so tempo
+ * writes no value for it.
+ */
+const TIMED_DURATION_DEFAULTS = Object.entries(durationDefaults).filter(
+ ([, value]) => !value.startsWith('var('),
+);
+
/** The duration tokens at `tempo`: today's values times it, to the ms. */
export function durationTokensAt(tempo: number): Record<string, string> {
const tokens: Record<string, string> = {};
- for (const [name, value] of Object.entries(durationDefaults)) {
+ for (const [name, value] of TIMED_DURATION_DEFAULTS) {
tokens[name] = `${Math.round(msOf(value) * tempo)}ms`;
}
return tokens;
@@ -130,7 +138,7 @@ export function expandThemeMotion(
const tokens: Record<string, string> = {};
if (knobs.tempoIsSet) {
Object.assign(tokens, durationTokensAt(knobs.tempo));
- for (const [name, value] of Object.entries(durationDefaults)) {
+ for (const [name, value] of TIMED_DURATION_DEFAULTS) {
tokens[preTempoName(name)] = value;
}
// The press release fade, a fixed clock in the stylesheet, scales by the
diff --git a/packages/core/src/theme/tokens.stylex.ts b/packages/core/src/theme/tokens.stylex.ts
index 99ddf29a61..80acab6307 100644
--- a/packages/core/src/theme/tokens.stylex.ts
+++ b/packages/core/src/theme/tokens.stylex.ts
@@ -307,6 +307,8 @@ export const durationDefaults = {
'--duration-slow-min': '730ms',
'--duration-slow': '975ms',
'--duration-slow-max': '1300ms',
+ /** The standard dismissal duration. */
+ '--duration-exit': 'var(--duration-fast)',
} as const;
export const durationVars = stylex.defineVars(durationDefaults);