diff --git a/src/__tests__/native/media-query.test.tsx b/src/__tests__/native/media-query.test.tsx index 4f6d7660..ab9501f2 100644 --- a/src/__tests__/native/media-query.test.tsx +++ b/src/__tests__/native/media-query.test.tsx @@ -5,7 +5,7 @@ import { View } from "react-native-css/components/View"; import { registerCSS, testID } from "react-native-css/jest"; import { colorScheme } from "react-native-css/runtime"; -import { dimensions } from "../../native/reactivity"; +import { dimensions, reducedMotion } from "../../native/reactivity"; jest.mock("react-native", () => { const RN = jest.requireActual("react-native"); @@ -317,3 +317,202 @@ describe("max-resolution", () => { expect(component.props.style).toStrictEqual(undefined); }); }); + +describe("a reduced-motion preference AccessibilityInfo has not answered", () => { + let answered: ReturnType; + + beforeEach(() => { + answered = reducedMotion.get(); + reducedMotion.set(undefined); + }); + + afterEach(() => { + act(() => { + reducedMotion.set(answered); + }); + }); + + test("a mounted view takes the device's answer when it arrives", () => { + registerCSS(` + .my-class { color: #000000; } + @media (prefers-reduced-motion: reduce) { .my-class { color: #020202; } }`); + + render(); + const component = screen.getByTestId(testID); + expect(component.props.style).toStrictEqual({ color: "#000" }); + + act(() => { + reducedMotion.set("reduce"); + }); + expect(component.props.style).toStrictEqual({ color: "#020202" }); + }); + + test.each([ + "(prefers-reduced-motion: reduce)", + "(prefers-reduced-motion: no-preference)", + "(prefers-reduced-motion)", + ])("leaves @media %s unmatched", (query) => { + registerCSS( + `.my-class { color: #000000; } @media ${query} { .my-class { color: #010101; } }`, + ); + + render(); + + expect(screen.getByTestId(testID).props.style).toStrictEqual({ + color: "#000", + }); + }); + + test("a mounted view follows the reduced-motion preference as it changes", () => { + registerCSS(` + .my-class { color: #000000; } + @media (prefers-reduced-motion: no-preference) { .my-class { color: #010101; } } + @media (prefers-reduced-motion: reduce) { .my-class { color: #020202; } }`); + + render(); + const component = screen.getByTestId(testID); + expect(component.props.style).toStrictEqual({ color: "#000" }); + + act(() => { + reducedMotion.set("no-preference"); + }); + expect(component.props.style).toStrictEqual({ color: "#010101" }); + + act(() => { + reducedMotion.set("reduce"); + }); + expect(component.props.style).toStrictEqual({ color: "#020202" }); + + act(() => { + reducedMotion.set("no-preference"); + }); + expect(component.props.style).toStrictEqual({ color: "#010101" }); + }); +}); + +// Chromium 156, Firefox 157 and WebKit 27.2 agree on every row: [query, reduce/light, reduce/dark, no-preference/light, no-preference/dark] +const BROWSER_MEDIA_ROWS = [ + ["(prefers-reduced-motion: reduce)", true, true, false, false], + ["(prefers-reduced-motion: no-preference)", false, false, true, true], + ["(prefers-reduced-motion)", true, true, false, false], + ["not (prefers-reduced-motion)", false, false, true, true], + ["not (prefers-reduced-motion: reduce)", false, false, true, true], + ["not (prefers-reduced-motion: no-preference)", true, true, false, false], + ["not all and (prefers-reduced-motion: reduce)", false, false, true, true], + ["(prefers-reduced-motion: bogus)", false, false, false, false], + ["(prefers-reduced-motion: initial)", false, false, false, false], + ["(PREFERS-REDUCED-MOTION: reduce)", true, true, false, false], + ["( prefers-reduced-motion : reduce )", true, true, false, false], + ["(prefers-reduced-motion:/* a comment */reduce)", true, true, false, false], + ["((prefers-reduced-motion: reduce))", true, true, false, false], + ["screen and (prefers-reduced-motion: reduce)", true, true, false, false], + [ + "only screen and (prefers-reduced-motion: reduce)", + true, + true, + false, + false, + ], + ["all and (prefers-reduced-motion: reduce)", true, true, false, false], + ["print and (prefers-reduced-motion: reduce)", false, false, false, false], + ["not screen and (prefers-reduced-motion: reduce)", false, false, true, true], + ["not print and (prefers-reduced-motion: reduce)", true, true, true, true], + ["(prefers-reduced-motion: reduce), print", true, true, false, false], + [ + "(prefers-reduced-motion: reduce) and (min-width: 1px)", + true, + true, + false, + false, + ], + [ + "(prefers-reduced-motion: reduce) and (prefers-color-scheme: dark)", + false, + true, + false, + false, + ], + [ + "(prefers-reduced-motion: reduce), (prefers-color-scheme: dark)", + true, + true, + false, + true, + ], + [ + "(prefers-reduced-motion: reduce) or (prefers-color-scheme: dark)", + true, + true, + false, + true, + ], + [ + "not ((prefers-reduced-motion: reduce) and (prefers-color-scheme: dark))", + true, + false, + true, + true, + ], + [ + "(prefers-reduced-motion: reduce) and (not (prefers-color-scheme: dark))", + true, + false, + false, + false, + ], + [ + "(prefers-reduced-motion: no-preference) and (prefers-reduced-motion: reduce)", + false, + false, + false, + false, + ], + [ + "(prefers-reduced-motion: reduce) and (unknown-feature)", + false, + false, + false, + false, + ], + [ + "(prefers-reduced-motion: reduce) or (unknown-feature)", + true, + true, + false, + false, + ], + ["(prefers-color-scheme: dark)", false, true, false, true], + ["(unknown-feature)", false, false, false, false], +] as const; + +const MEDIA_STATES = [ + ["reduce", "light"], + ["reduce", "dark"], + ["no-preference", "light"], + ["no-preference", "dark"], +] as const; + +describe.each(BROWSER_MEDIA_ROWS)( + "@media %s matches as browsers match it", + (query, ...matches) => { + test.each( + MEDIA_STATES.map( + ([motion, scheme], index) => [motion, scheme, matches[index]] as const, + ), + )("under %s motion in the %s scheme: %s", (motion, scheme, expected) => { + act(() => { + reducedMotion.set(motion); + colorScheme.set(scheme); + }); + registerCSS( + `.my-class { color: #000000; } @media ${query} { .my-class { color: #010101; } }`, + ); + + render(); + + expect(screen.getByTestId(testID).props.style).toStrictEqual({ + color: expected ? "#010101" : "#000", + }); + }); + }, +); diff --git a/src/__tests__/native/reduce-motion-init.test.ts b/src/__tests__/native/reduce-motion-init.test.ts new file mode 100644 index 00000000..867f1b0d --- /dev/null +++ b/src/__tests__/native/reduce-motion-init.test.ts @@ -0,0 +1,105 @@ +type ReduceMotionListener = (isReduceMotionEnabled: boolean) => void; + +interface AccessibilityInfoDouble { + readonly isReduceMotionEnabled?: () => Promise; + readonly addEventListener?: ( + event: string, + listener: ReduceMotionListener, + ) => { readonly remove: () => void }; +} + +function importReactivityWith( + accessibilityInfo: AccessibilityInfoDouble | undefined, +) { + jest.resetModules(); + // The module pins its state on globalThis, which outlives the registry reset, so a re-import builds a fresh one only once it is gone + globalThis.__react_native_css_reactivity = undefined; + // A Proxy over requireActual rather than a spread: spreading react-native eagerly triggers its lazy DevMenu getter and throws + jest.doMock("react-native", () => { + const actual = jest.requireActual>("react-native"); + + return new Proxy(actual, { + get: (target, property): unknown => + property === "AccessibilityInfo" + ? accessibilityInfo + : Reflect.get(target, property), + }); + }); + + return jest.requireActual( + "../../native/reactivity", + ); +} + +// Every promise the module chains settles before a macrotask runs +function settle() { + return new Promise((resolve) => { + setImmediate(resolve); + }); +} + +function controlledAccessibilityInfo() { + const listeners: ReduceMotionListener[] = []; + let answer: (isReduceMotionEnabled: boolean) => void = () => undefined; + const answered = new Promise((resolve) => { + answer = resolve; + }); + + return { + accessibilityInfo: { + isReduceMotionEnabled: () => answered, + addEventListener: (event: string, listener: ReduceMotionListener) => { + if (event === "reduceMotionChanged") listeners.push(listener); + return { remove: () => undefined }; + }, + }, + answer, + change: (isReduceMotionEnabled: boolean) => { + for (const listener of listeners) listener(isReduceMotionEnabled); + }, + }; +} + +describe("the reduced-motion preference", () => { + test("is unknown until AccessibilityInfo answers", () => { + const { accessibilityInfo } = controlledAccessibilityInfo(); + + const { reducedMotion } = importReactivityWith(accessibilityInfo); + + expect(reducedMotion.get()).toBeUndefined(); + }); + + test("takes AccessibilityInfo's answer, then follows its change event", async () => { + const { accessibilityInfo, answer, change } = controlledAccessibilityInfo(); + const { reducedMotion } = importReactivityWith(accessibilityInfo); + + answer(true); + await settle(); + expect(reducedMotion.get()).toBe("reduce"); + + change(false); + expect(reducedMotion.get()).toBe("no-preference"); + }); + + test("keeps a change event over an answer that resolves after it", async () => { + const { accessibilityInfo, answer, change } = controlledAccessibilityInfo(); + const { reducedMotion } = importReactivityWith(accessibilityInfo); + + change(true); + answer(false); + await settle(); + + expect(reducedMotion.get()).toBe("reduce"); + }); + + test("is no-preference where AccessibilityInfo rejects, without an unhandled rejection", async () => { + const { reducedMotion } = importReactivityWith({ + isReduceMotionEnabled: () => + Promise.reject(new Error("NativeAccessibilityManager unavailable")), + addEventListener: () => ({ remove: () => undefined }), + }); + await settle(); + + expect(reducedMotion.get()).toBe("no-preference"); + }); +}); diff --git a/src/native/conditions/media-query.ts b/src/native/conditions/media-query.ts index afd51250..bab0e350 100644 --- a/src/native/conditions/media-query.ts +++ b/src/native/conditions/media-query.ts @@ -1,19 +1,23 @@ -/* eslint-disable */ import { I18nManager, PixelRatio, Platform } from "react-native"; import type { MediaCondition } from "react-native-css/compiler"; -import { colorScheme, vh, vw, type Getter } from "../reactivity"; +import { colorScheme, reducedMotion, vh, vw, type Getter } from "../reactivity"; export function testMediaQuery(mediaQueries: MediaCondition[], get: Getter) { return mediaQueries.every((query) => test(query, get)); } -function test(mediaQuery: MediaCondition, get: Getter): Boolean { +function test(mediaQuery: MediaCondition, get: Getter): boolean { switch (mediaQuery[0]) { case "[]": - case "!!": return false; + case "!!": + // (prefers-reduced-motion) alone matches reduce, its one value besides no-preference (MQ5 ยง2.4.2), and nothing before the device answers + return ( + mediaQuery[1] === "prefers-reduced-motion" && + get(reducedMotion) === "reduce" + ); case "!": return !test(mediaQuery[1], get); case "&": @@ -34,7 +38,7 @@ function test(mediaQuery: MediaCondition, get: Getter): Boolean { } } -function testComparison(mediaQuery: MediaCondition, get: Getter): Boolean { +function testComparison(mediaQuery: MediaCondition, get: Getter): boolean { const value = mediaQuery[2]; switch (mediaQuery[1]) { @@ -47,6 +51,9 @@ function testComparison(mediaQuery: MediaCondition, get: Getter): Boolean { case "prefers-color-scheme": { return value === get(colorScheme); } + case "prefers-reduced-motion": { + return value === get(reducedMotion); + } case "display-mode": return value === "native" || Platform.OS === value; case "min-width": diff --git a/src/native/reactivity.ts b/src/native/reactivity.ts index 42d654d3..06170a1a 100644 --- a/src/native/reactivity.ts +++ b/src/native/reactivity.ts @@ -1,6 +1,7 @@ /* eslint-disable */ import { createContext } from "react"; import { + AccessibilityInfo, Appearance, Dimensions, type ColorSchemeName, @@ -9,6 +10,8 @@ import { import type { StyleDescriptor } from "react-native-css/compiler"; +import type { ReducedMotionPreference } from "../runtime.types"; + export type Effect = { observers: Set>; run(): void; @@ -252,6 +255,14 @@ export function toVariableRecord( return record; } +/** Reduced Motion ************************************************************/ + +function toReducedMotionPreference( + isReduceMotionEnabled: boolean, +): ReducedMotionPreference { + return isReduceMotionEnabled ? "reduce" : "no-preference"; +} + /******************************* Process globals ******************************/ export type ContainerContextValue = Record< @@ -286,6 +297,28 @@ function createReactivityState() { ); Appearance.addChangeListener((event) => colorScheme.set(event.colorScheme)); + // React Native reads the setting only through a callback, so the preference is unknown until AccessibilityInfo answers + const reducedMotion = observable( + undefined, + ); + + AccessibilityInfo.addEventListener( + "reduceMotionChanged", + (isReduceMotionEnabled: boolean) => { + reducedMotion.set(toReducedMotionPreference(isReduceMotionEnabled)); + }, + ); + + // React Native rejects where the platform has no accessibility module, and the user has stated no preference to it + void AccessibilityInfo.isReduceMotionEnabled() + .then(toReducedMotionPreference, () => "no-preference" as const) + .then((answer) => { + // A change event that arrived first is newer than the answer + if (reducedMotion.get() === undefined) { + reducedMotion.set(answer); + } + }); + const containerLayoutFamily = weakFamily(() => { return observable({ x: 0, @@ -306,6 +339,7 @@ function createReactivityState() { vw, vh, colorScheme, + reducedMotion, ContainerContext: createContext({}), containerLayoutFamily, @@ -339,6 +373,7 @@ export const { vw, vh, colorScheme, + reducedMotion, ContainerContext, containerLayoutFamily, containerWidthFamily, diff --git a/src/runtime.types.ts b/src/runtime.types.ts index 649ac2c7..2e75f20e 100644 --- a/src/runtime.types.ts +++ b/src/runtime.types.ts @@ -157,3 +157,5 @@ export interface ColorScheme { get: () => ColorSchemeName | null | undefined; set: (value: ColorSchemeName | null | undefined) => void; } + +export type ReducedMotionPreference = "no-preference" | "reduce";