Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(motion): stop the upload pulse under reduced motion; CSS check covers every app stylesheet (MUL-9982) (#11322)

Jiayuan Zhang4ffc3bdd504f
diff --git a/packages/ui/docs/motion.md b/packages/ui/docs/motion.md
index 8eef51d7cc..07b5d2e0a6 100644
--- a/packages/ui/docs/motion.md
+++ b/packages/ui/docs/motion.md
@@ -99,7 +99,7 @@ The Appearance setting "Reduce motion" and the system's `prefers-reduced-motion`
- The Appearance setting zeroes CSS animation and transition durations for the whole document. The system setting reaches CSS only through media queries, so every CSS animation or transition also carries `motion-reduce:animate-none` or `motion-reduce:transition-none`.
- JavaScript motion (motion/react, WAAPI) reads `useReducedMotion()` from `@multica/core/appearance`, or `prefersReducedMotion()` outside render, which cover both settings. With either set, pass `duration: 0` or skip the animation. motion's own `useReducedMotion` and a bare `matchMedia` check see only the system setting. `MotionProvider` at the app root makes motion/react skip transform and layout animation under either setting, but opacity and color still animate, so the `duration: 0` above still applies.
- Test the reduced branch: with reduced motion, the end state is there in the same frame.
-- Two checks enforce this. The `NO_SYSTEM_ONLY_REDUCED_MOTION` lint rule rejects a `prefers-reduced-motion` query string in `packages/views`, `apps/web` and `apps/desktop`. `packages/ui/styles/reduced-motion.test.ts` fails when a class in `base.css` starts an animation and no `@media (prefers-reduced-motion: reduce)` rule stops it; the few exceptions are listed in that test, each with its reason.
+- Two checks enforce this. The `NO_SYSTEM_ONLY_REDUCED_MOTION` lint rule rejects a `prefers-reduced-motion` query string in `packages/views`, `apps/web` and `apps/desktop`. `packages/ui/styles/reduced-motion.test.ts` reads every stylesheet in `packages/ui`, `packages/views`, `apps/desktop` and `apps/web` (except the marketing site), and fails when a class starts an animation and no `@media (prefers-reduced-motion: reduce)` rule in the same file stops it; the few exceptions are listed in that test by file and class, each with its reason.
## Do not animate
diff --git a/packages/ui/docs/motion.zh.md b/packages/ui/docs/motion.zh.md
index 1b284fb518..09b5aaa0bf 100644
--- a/packages/ui/docs/motion.zh.md
+++ b/packages/ui/docs/motion.zh.md
@@ -99,7 +99,7 @@ Dialog、AlertDialog、Sheet、Popover、DropdownMenu、ContextMenu、Select、H
- 外观设置会把整个文档的 CSS 动画和过渡时长归零。系统设置只能通过媒体查询影响 CSS,所以每个 CSS 动画或过渡都要加 `motion-reduce:animate-none` 或 `motion-reduce:transition-none`。
- JavaScript 动效(motion/react、WAAPI)读取 `@multica/core/appearance` 的 `useReducedMotion()`,渲染之外用 `prefersReducedMotion()`,两者都覆盖这两个设置。任一设置开启时传 `duration: 0` 或跳过动画。motion 自带的 `useReducedMotion` 和单独的 `matchMedia` 判断只能看到系统设置。应用根部的 `MotionProvider` 会让 motion/react 在任一设置开启时跳过位移和布局动画,但透明度和颜色仍会过渡,所以上面的 `duration: 0` 仍然需要。
- 测试减少动态效果的分支:开启后,终点状态在同一帧出现。
-- 这两条由检查兜底。`NO_SYSTEM_ONLY_REDUCED_MOTION` lint 规则拦截 `packages/views`、`apps/web` 和 `apps/desktop` 里的 `prefers-reduced-motion` 查询字符串。`base.css` 里的类只要启动了动画,却没有 `@media (prefers-reduced-motion: reduce)` 规则把它停掉,`packages/ui/styles/reduced-motion.test.ts` 就会失败;少数例外列在这个测试里,每条都写明原因。
+- 这两条由检查兜底。`NO_SYSTEM_ONLY_REDUCED_MOTION` lint 规则拦截 `packages/views`、`apps/web` 和 `apps/desktop` 里的 `prefers-reduced-motion` 查询字符串。`packages/ui/styles/reduced-motion.test.ts` 读取 `packages/ui`、`packages/views`、`apps/desktop` 和 `apps/web`(营销站除外)里的全部样式文件,类只要启动了动画,同一文件里却没有 `@media (prefers-reduced-motion: reduce)` 规则把它停掉,测试就会失败;少数例外按「文件 + 类名」列在这个测试里,每条都写明原因。
## 不要加动效
diff --git a/packages/ui/styles/reduced-motion.test.ts b/packages/ui/styles/reduced-motion.test.ts
index 452ba282b2..98d54427a8 100644
--- a/packages/ui/styles/reduced-motion.test.ts
+++ b/packages/ui/styles/reduced-motion.test.ts
@@ -1,18 +1,53 @@
// @vitest-environment node
-import { readFileSync } from "node:fs";
+import { readdirSync, readFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
-const css = readFileSync(join(__dirname, "base.css"), "utf8");
+const ROOT = join(__dirname, "../../..");
+
+/**
+ * Where the product apps' stylesheets live. The marketing site, docs and
+ * storybook don't mount the Appearance settings, matching the scope of the
+ * `NO_SYSTEM_ONLY_REDUCED_MOTION` lint rule.
+ */
+const SCANNED = ["packages/ui", "packages/views", "apps/desktop/src", "apps/web"];
+const SKIPPED = ["apps/web/app/landing.css", "apps/web/app/(landing)", "apps/web/features/landing"];
+const BUILD_DIRS = new Set(["node_modules", "dist", "out", "build"]);
+
+function stylesheets(): string[] {
+ const found: string[] = [];
+ const walk = (dir: string) => {
+ for (const entry of readdirSync(join(ROOT, dir), { withFileTypes: true })) {
+ const path = `${dir}/${entry.name}`;
+ if (SKIPPED.includes(path)) continue;
+ if (entry.isDirectory()) {
+ if (!entry.name.startsWith(".") && !BUILD_DIRS.has(entry.name)) walk(path);
+ } else if (entry.name.endsWith(".css")) {
+ found.push(path);
+ }
+ }
+ };
+ SCANNED.forEach(walk);
+ return found.sort();
+}
+
+const sheets = Object.fromEntries(
+ stylesheets().map((path) => [path, readFileSync(join(ROOT, path), "utf8")]),
+);
+const css = sheets["packages/ui/styles/base.css"]!;
+const attachmentCss = sheets["packages/views/editor/styles/attachment.css"]!;
/**
* Animated classes that keep something under the system reduced-motion
- * setting. Each still needs a rule in `@media (prefers-reduced-motion: reduce)`;
- * the reason says why that rule is not `animation: none`.
+ * setting, by stylesheet. Each still needs a rule in
+ * `@media (prefers-reduced-motion: reduce)` in the same file; the reason says
+ * why that rule is not `animation: none`.
*/
-const EXCEPTIONS: Record<string, string> = {
- ".animate-skeleton":
- "Keeps only skeleton-hold, a 200ms visibility step that moves nothing, so data that lands fast still skips the placeholder. The pulse stops.",
+const EXCEPTIONS: Record<string, Record<string, string>> = {
+ "packages/ui/styles/base.css": {
+ ".animate-skeleton":
+ "Keeps only skeleton-hold, a 200ms visibility step that moves nothing, so data that lands fast still skips the placeholder. The pulse stops.",
+ },
};
const REDUCED_MEDIA = /@media\s*\(prefers-reduced-motion:\s*reduce\)\s*\{/g;
@@ -88,8 +123,83 @@ function stillMoving(source: string, exceptions: Record<string, string> = {}): s
return animated.filter((name) => !stopped.has(name) && !(name in exceptions));
}
+/**
+ * `file class` for each animated class that still moves. Each stylesheet is
+ * audited on its own: a reduced rule in one file doesn't count for another.
+ */
+function stillMovingIn(
+ files: Record<string, string>,
+ exceptions: Record<string, Record<string, string>> = EXCEPTIONS,
+): string[] {
+ return Object.entries(files).flatMap(([path, source]) =>
+ stillMoving(source, exceptions[path]).map((name) => `${path} ${name}`),
+ );
+}
+
+describe("stylesheets the apps load, under the system reduced-motion setting", () => {
+ it("scans the shared, product and desktop stylesheets, not the marketing site", () => {
+ const paths = Object.keys(sheets);
+ expect(paths).toEqual(
+ expect.arrayContaining([
+ "apps/desktop/src/renderer/src/globals.css",
+ "apps/web/app/globals.css",
+ "packages/ui/styles/base.css",
+ "packages/views/editor/styles/attachment.css",
+ ]),
+ );
+ expect(paths).not.toContain("apps/web/app/landing.css");
+ });
+
+ it("stops every animated class, not even leaving a fade", () => {
+ expect(stillMovingIn(sheets)).toEqual([]);
+ });
+
+ it.each(
+ Object.entries(EXCEPTIONS).flatMap(([path, names]) => Object.keys(names).map((name) => [path, name])),
+ )("%s %s is a live exception with its own reduced rule", (path, name) => {
+ expect(sheets[path], `${path} is not a scanned stylesheet`).toBeDefined();
+ const { animated, restyled } = audit(sheets[path]!);
+ expect(animated).toContain(name);
+ expect(restyled.has(name), `${name} needs a rule in @media (prefers-reduced-motion: reduce)`).toBe(true);
+ });
+
+ it("flags a new animated class in a stylesheet other than base.css", () => {
+ const path = "packages/views/editor/styles/code.css";
+ const files = { ...sheets, [path]: `${sheets[path]}\n.code-flash { animation: flash 1s infinite; }` };
+ expect(stillMovingIn(files)).toEqual([`${path} .code-flash`]);
+ });
+
+ it("doesn't let a reduced rule in another file stop a class", () => {
+ const files = {
+ "a.css": ".animate-a { animation: a 1s infinite; }",
+ "b.css": "@media (prefers-reduced-motion: reduce) { .animate-a { animation: none; } }",
+ };
+ expect(stillMovingIn(files)).toEqual(["a.css .animate-a"]);
+ });
+
+ it("holds an exception to the file it is listed for", () => {
+ const files = { ...sheets, "other.css": ".animate-skeleton { animation: pulse 2s infinite; }" };
+ expect(stillMovingIn(files)).toEqual(["other.css .animate-skeleton"]);
+ });
+});
+
+describe("attachment.css under the system reduced-motion setting", () => {
+ it("stops the upload pulse and keeps the image dimmed", () => {
+ const { animated, stopped } = audit(attachmentCss);
+ expect(animated).toContain(".image-uploading");
+ expect(stopped.has(".image-uploading")).toBe(true);
+ expect(attachmentCss).toMatch(/\.image-uploading\s*\{\s*opacity:\s*0\.5;/);
+ });
+
+ it("flags the upload pulse without its reduced rule", () => {
+ const withoutReduced = attachmentCss.replace(/@media\s*\(prefers-reduced-motion:\s*reduce\)\s*\{[^}]*\}\s*\}/, "");
+ expect(withoutReduced).not.toContain("prefers-reduced-motion");
+ expect(stillMoving(withoutReduced)).toEqual([".image-uploading"]);
+ });
+});
+
describe("base.css under the system reduced-motion setting", () => {
- const { animated, restyled } = audit(css);
+ const { animated } = audit(css);
it("finds the animated classes", () => {
expect(animated).toEqual(
@@ -110,30 +220,22 @@ describe("base.css under the system reduced-motion setting", () => {
]),
);
});
-
- it("stops every animated class, not even leaving a fade", () => {
- expect(stillMoving(css, EXCEPTIONS)).toEqual([]);
- });
-
- it.each(Object.keys(EXCEPTIONS))("%s is a live exception with its own reduced rule", (name) => {
- expect(animated).toContain(name);
- expect(restyled.has(name), `${name} needs a rule in @media (prefers-reduced-motion: reduce)`).toBe(true);
- });
});
describe("stillMoving", () => {
const reduced = (body: string) => `@media (prefers-reduced-motion: reduce) {\n${body}\n}`;
+ const baseExceptions = EXCEPTIONS["packages/ui/styles/base.css"];
it("flags a new .animate-* class with no reduced rule", () => {
const source = `${css}\n.animate-new-thing { animation: new-thing 150ms ease-out both; }`;
- expect(stillMoving(source, EXCEPTIONS)).toEqual([".animate-new-thing"]);
+ expect(stillMoving(source, baseExceptions)).toEqual([".animate-new-thing"]);
});
it("flags a class whose reduced rule was deleted", () => {
const covered = ".animate-chat-enter-prev,\n .animate-chat-enter-fade {";
expect(css).toContain(covered);
const source = css.replace(covered, ".animate-chat-enter-prev {");
- expect(stillMoving(source, EXCEPTIONS)).toEqual([".animate-chat-enter-fade"]);
+ expect(stillMoving(source, baseExceptions)).toEqual([".animate-chat-enter-fade"]);
});
it("flags a class whose reduced rule swaps in a shorter fade", () => {
diff --git a/packages/ui/turbo.json b/packages/ui/turbo.json
new file mode 100644
index 0000000000..07b0d63083
--- /dev/null
+++ b/packages/ui/turbo.json
@@ -0,0 +1,15 @@
+{
+ "extends": ["//"],
+ "tasks": {
+ "test": {
+ // styles/reduced-motion.test.ts reads the stylesheets of the packages
+ // and apps that load this one.
+ "inputs": [
+ "$TURBO_DEFAULT$",
+ "$TURBO_ROOT$/packages/views/**/*.css",
+ "$TURBO_ROOT$/apps/desktop/src/**/*.css",
+ "$TURBO_ROOT$/apps/web/**/*.css"
+ ]
+ }
+ }
+}
diff --git a/packages/views/editor/styles/attachment.css b/packages/views/editor/styles/attachment.css
index d4193b68de..588e897439 100644
--- a/packages/views/editor/styles/attachment.css
+++ b/packages/views/editor/styles/attachment.css
@@ -91,6 +91,13 @@
50% { opacity: 0.3; }
}
+/* The pulse stops; the dimmed image still reads as uploading. */
+@media (prefers-reduced-motion: reduce) {
+ .image-uploading {
+ animation: none;
+ }
+}
+
.image-toolbar {
/* Keep the resting controls near their image so tabbing to them scrolls it into view. */
position: absolute;