diff --git a/src/__tests__/native/_reduced-motion-types.ts b/src/__tests__/native/_reduced-motion-types.ts new file mode 100644 index 00000000..424aab4a --- /dev/null +++ b/src/__tests__/native/_reduced-motion-types.ts @@ -0,0 +1,31 @@ +import { + reducedMotion as rootReducedMotion, + type ReducedMotionPreference, +} from "react-native-css"; +import { reducedMotion as nativeReducedMotion } from "react-native-css/native"; +import { reducedMotion as webReducedMotion } from "react-native-css/web"; + +// This module is checked by TypeScript and deliberately excluded from Jest. +export const preferences: readonly (ReducedMotionPreference | undefined)[] = [ + rootReducedMotion.get(), + nativeReducedMotion.get(), + webReducedMotion.get(), +]; + +// @ts-expect-error get() is undefined until the device answers. +export const answered: ReducedMotionPreference = rootReducedMotion.get(); + +rootReducedMotion.set("reduce"); +nativeReducedMotion.set("no-preference"); +webReducedMotion.set(null); + +// @ts-expect-error A preference is one of the keywords prefers-reduced-motion takes. +rootReducedMotion.set("bogus"); +// @ts-expect-error A preference is one of the keywords prefers-reduced-motion takes, as written. +nativeReducedMotion.set("REDUCE"); +// @ts-expect-error null, not an empty string, follows the device again. +webReducedMotion.set(""); +// @ts-expect-error null, not undefined, follows the device again. +nativeReducedMotion.set(undefined); +// @ts-expect-error The preference is a keyword, not a boolean. +webReducedMotion.set(true); diff --git a/src/__tests__/native/media-query.test.tsx b/src/__tests__/native/media-query.test.tsx index ab9501f2..1af52320 100644 --- a/src/__tests__/native/media-query.test.tsx +++ b/src/__tests__/native/media-query.test.tsx @@ -3,9 +3,9 @@ import { Appearance, PixelRatio } from "react-native"; import { act, render, screen } from "@testing-library/react-native"; import { View } from "react-native-css/components/View"; import { registerCSS, testID } from "react-native-css/jest"; -import { colorScheme } from "react-native-css/runtime"; +import { colorScheme, reducedMotion } from "react-native-css/runtime"; -import { dimensions, reducedMotion } from "../../native/reactivity"; +import { deviceReducedMotion, dimensions } from "../../native/reactivity"; jest.mock("react-native", () => { const RN = jest.requireActual("react-native"); @@ -319,16 +319,16 @@ describe("max-resolution", () => { }); describe("a reduced-motion preference AccessibilityInfo has not answered", () => { - let answered: ReturnType; + let answered: ReturnType; beforeEach(() => { - answered = reducedMotion.get(); - reducedMotion.set(undefined); + answered = deviceReducedMotion.get(); + deviceReducedMotion.set(undefined); }); afterEach(() => { act(() => { - reducedMotion.set(answered); + deviceReducedMotion.set(answered); }); }); @@ -342,7 +342,7 @@ describe("a reduced-motion preference AccessibilityInfo has not answered", () => expect(component.props.style).toStrictEqual({ color: "#000" }); act(() => { - reducedMotion.set("reduce"); + deviceReducedMotion.set("reduce"); }); expect(component.props.style).toStrictEqual({ color: "#020202" }); }); @@ -374,22 +374,74 @@ describe("a reduced-motion preference AccessibilityInfo has not answered", () => expect(component.props.style).toStrictEqual({ color: "#000" }); act(() => { - reducedMotion.set("no-preference"); + deviceReducedMotion.set("no-preference"); }); expect(component.props.style).toStrictEqual({ color: "#010101" }); act(() => { - reducedMotion.set("reduce"); + deviceReducedMotion.set("reduce"); }); expect(component.props.style).toStrictEqual({ color: "#020202" }); act(() => { - reducedMotion.set("no-preference"); + deviceReducedMotion.set("no-preference"); }); expect(component.props.style).toStrictEqual({ color: "#010101" }); }); }); +test("reducedMotion.set holds through a change of the device's setting, and set(null) follows the device again", () => { + registerCSS(` +.my-class { color: #000000; } +@media (prefers-reduced-motion: reduce) { .my-class { color: #020202; } }`); + const answered = deviceReducedMotion.get(); + + render(); + const component = screen.getByTestId(testID); + + try { + act(() => { + reducedMotion.set("no-preference"); + deviceReducedMotion.set("reduce"); + }); + expect(reducedMotion.get()).toBe("no-preference"); + expect(component.props.style).toStrictEqual({ color: "#000" }); + + act(() => { + reducedMotion.set(null); + }); + expect(reducedMotion.get()).toBe("reduce"); + expect(component.props.style).toStrictEqual({ color: "#020202" }); + } finally { + act(() => { + deviceReducedMotion.set(answered); + }); + } +}); + +test("reducedMotion.set overrides the device's preference, and set(null) follows the device again at once", () => { + registerCSS(` +.my-class { color: #000000; } +@media (prefers-reduced-motion: reduce) { .my-class { color: #020202; } }`); + + render(); + const component = screen.getByTestId(testID); + expect(reducedMotion.get()).toBe("no-preference"); + expect(component.props.style).toStrictEqual({ color: "#000" }); + + act(() => { + reducedMotion.set("reduce"); + }); + expect(reducedMotion.get()).toBe("reduce"); + expect(component.props.style).toStrictEqual({ color: "#020202" }); + + act(() => { + reducedMotion.set(null); + }); + expect(reducedMotion.get()).toBe("no-preference"); + expect(component.props.style).toStrictEqual({ color: "#000" }); +}); + // 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], diff --git a/src/__tests__/web/reduced-motion.test.ts b/src/__tests__/web/reduced-motion.test.ts new file mode 100644 index 00000000..ce4091c6 --- /dev/null +++ b/src/__tests__/web/reduced-motion.test.ts @@ -0,0 +1,77 @@ +import { reducedMotion } from "../../web/api"; + +function withMatchMedia(value: unknown, run: () => void) { + Object.defineProperty(window, "matchMedia", { configurable: true, value }); + try { + run(); + } finally { + Reflect.deleteProperty(window, "matchMedia"); + reducedMotion.set(null); + } +} + +test.each([ + [true, "reduce"], + [false, "no-preference"], +] as const)( + "reducedMotion reads %s from matchMedia as %s", + (matches, preference) => { + withMatchMedia( + (query: string) => ({ + matches: query === "(prefers-reduced-motion: reduce)" && matches, + }), + () => { + expect(reducedMotion.get()).toBe(preference); + }, + ); + }, +); + +test("an override changes what scripts read until set(null) reads matchMedia again", () => { + withMatchMedia( + () => ({ matches: true }), + () => { + reducedMotion.set("no-preference"); + expect(reducedMotion.get()).toBe("no-preference"); + + reducedMotion.set(null); + expect(reducedMotion.get()).toBe("reduce"); + }, + ); +}); + +test.each([ + ["no matchMedia", undefined], + ["a matchMedia that is no function", {}], + ["a matchMedia stub that returns nothing", () => undefined], +])("reducedMotion has no preference to read with %s", (_, value) => { + withMatchMedia(value, () => { + expect(reducedMotion.get()).toBeUndefined(); + }); +}); + +test("a server keeps no override, since it renders every visitor's page in one process", () => { + const browserWindow: unknown = globalThis.window; + Reflect.deleteProperty(globalThis, "window"); + + try { + reducedMotion.set("reduce"); + expect(reducedMotion.get()).toBeUndefined(); + } finally { + Reflect.set(globalThis, "window", browserWindow); + } + + expect(reducedMotion.get()).toBeUndefined(); +}); + +test("reducedMotion reads a matchMedia double's new answer", () => { + const matchMedia = jest.fn(() => ({ matches: false })); + + withMatchMedia(matchMedia, () => { + expect(reducedMotion.get()).toBe("no-preference"); + + matchMedia.mockImplementation(() => ({ matches: true })); + + expect(reducedMotion.get()).toBe("reduce"); + }); +}); diff --git a/src/jest/index.ts b/src/jest/index.ts index d16a492a..c0df9424 100644 --- a/src/jest/index.ts +++ b/src/jest/index.ts @@ -6,7 +6,7 @@ import { compile, type CompilerOptions } from "react-native-css/compiler"; import { StyleCollection } from "react-native-css/native"; import { resetVariableRegistries } from "react-native-css/native-internal"; -import { colorScheme, dimensions } from "../native/reactivity"; +import { colorScheme, dimensions, reducedMotion } from "../native/reactivity"; declare global { /* eslint-disable @typescript-eslint/no-namespace */ @@ -27,6 +27,7 @@ beforeEach(() => { "unspecified" as Parameters[0], ); colorScheme.set(null); + reducedMotion.set(undefined); }); const debugDefault = Boolean( diff --git a/src/native/api.tsx b/src/native/api.tsx index 5a8d1fe0..ae7d75cb 100644 --- a/src/native/api.tsx +++ b/src/native/api.tsx @@ -10,6 +10,7 @@ import type { ColorScheme, Props, ReactComponent, + ReducedMotion, Styled, StyledConfiguration, StyledOptions, @@ -19,6 +20,7 @@ import { usePassthrough } from "./react/usePassthrough"; import { cleanupEffect, colorScheme as colorSchemeObs, + reducedMotion as reducedMotionObs, toVariableRecord, VAR_SYMBOL, type Effect, @@ -80,6 +82,15 @@ export const colorScheme: ColorScheme = { }, }; +export const reducedMotion: ReducedMotion = { + get() { + return reducedMotionObs.get(); + }, + set(value) { + reducedMotionObs.set(value ?? undefined); + }, +}; + export const useUnstableNativeVariable = useNativeVariable; export const useCssElement = < diff --git a/src/native/reactivity.ts b/src/native/reactivity.ts index 06170a1a..e17017e4 100644 --- a/src/native/reactivity.ts +++ b/src/native/reactivity.ts @@ -297,15 +297,20 @@ 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( + // React Native reads the setting only through a callback, so the device's preference is unknown until AccessibilityInfo answers + const deviceReducedMotion = observable( undefined, ); + // The app's override holds through the device's changes until it is set to undefined + const reducedMotion = observable< + ReducedMotionPreference | undefined, + ReducedMotionPreference | undefined + >((read, override) => override ?? read(deviceReducedMotion)); AccessibilityInfo.addEventListener( "reduceMotionChanged", (isReduceMotionEnabled: boolean) => { - reducedMotion.set(toReducedMotionPreference(isReduceMotionEnabled)); + deviceReducedMotion.set(toReducedMotionPreference(isReduceMotionEnabled)); }, ); @@ -314,8 +319,8 @@ function createReactivityState() { .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); + if (deviceReducedMotion.get() === undefined) { + deviceReducedMotion.set(answer); } }); @@ -339,6 +344,7 @@ function createReactivityState() { vw, vh, colorScheme, + deviceReducedMotion, reducedMotion, ContainerContext: createContext({}), @@ -373,6 +379,7 @@ export const { vw, vh, colorScheme, + deviceReducedMotion, reducedMotion, ContainerContext, containerLayoutFamily, diff --git a/src/runtime.types.ts b/src/runtime.types.ts index 2e75f20e..ee67ad2f 100644 --- a/src/runtime.types.ts +++ b/src/runtime.types.ts @@ -159,3 +159,9 @@ export interface ColorScheme { } export type ReducedMotionPreference = "no-preference" | "reduce"; + +export interface ReducedMotion { + get: () => ReducedMotionPreference | undefined; + /** Overrides the device's setting until `null`, which follows the device again; on web it changes only what `get()` returns */ + set: (value: ReducedMotionPreference | null) => void; +} diff --git a/src/web/api.tsx b/src/web/api.tsx index b38eec10..38c50ca0 100644 --- a/src/web/api.tsx +++ b/src/web/api.tsx @@ -9,12 +9,18 @@ import { Appearance } from "react-native"; import type { ColorScheme, Props, + ReducedMotion, StyledConfiguration, StyledOptions, } from "react-native-css"; -import type { ReactComponent, Styled } from "../runtime.types"; +import type { + ReactComponent, + ReducedMotionPreference, + Styled, +} from "../runtime.types"; import { assignStyle } from "./assign-style"; +import { browserPreference } from "./browser-preference"; const defaultMapping: StyledConfiguration> = { className: "style", @@ -76,6 +82,29 @@ export const colorScheme: ColorScheme = { }, }; +export const reducedMotion: ReducedMotion = + browserPreference(() => { + // Server rendering has no window, and React Native's jest environment a window without matchMedia + if ( + typeof window === "undefined" || + typeof window.matchMedia !== "function" + ) { + return undefined; + } + + // Scripts read this, never a render, so each read asks matchMedia again, whose answer a test's double may change; a test's stub may return no list at all + const matchMedia: ( + this: Window, + query: string, + ) => MediaQueryList | undefined = window.matchMedia; + const query = matchMedia.call(window, "(prefers-reduced-motion: reduce)"); + if (query === undefined) { + return undefined; + } + + return query.matches ? "reduce" : "no-preference"; + }); + /** * @deprecated Use `` instead. */ diff --git a/src/web/browser-preference.ts b/src/web/browser-preference.ts new file mode 100644 index 00000000..ba2b458f --- /dev/null +++ b/src/web/browser-preference.ts @@ -0,0 +1,19 @@ +// The browser evaluates the stylesheet's @media rules and matchMedia() itself, so an override changes only what get() returns, where MQ5 ยง13's requestOverride() would reach both once a browser ships it +export function browserPreference( + readBrowser: () => Value | undefined, +): { + readonly get: () => Value | undefined; + readonly set: (value: Value | null) => void; +} { + let override: Value | undefined; + + return { + get: () => override ?? readBrowser(), + set: (value) => { + // A server renders every visitor's page in one process, so only a browser keeps an override + if (typeof window !== "undefined") { + override = value ?? undefined; + } + }, + }; +}