Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/__tests__/native/_reduced-motion-types.ts
Original file line number Diff line number Diff line change
@@ -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);
72 changes: 62 additions & 10 deletions src/__tests__/native/media-query.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -319,16 +319,16 @@ describe("max-resolution", () => {
});

describe("a reduced-motion preference AccessibilityInfo has not answered", () => {
let answered: ReturnType<typeof reducedMotion.get>;
let answered: ReturnType<typeof deviceReducedMotion.get>;

beforeEach(() => {
answered = reducedMotion.get();
reducedMotion.set(undefined);
answered = deviceReducedMotion.get();
deviceReducedMotion.set(undefined);
});

afterEach(() => {
act(() => {
reducedMotion.set(answered);
deviceReducedMotion.set(answered);
});
});

Expand All @@ -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" });
});
Expand Down Expand Up @@ -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(<View testID={testID} className="my-class" />);
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(<View testID={testID} className="my-class" />);
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],
Expand Down
77 changes: 77 additions & 0 deletions src/__tests__/web/reduced-motion.test.ts
Original file line number Diff line number Diff line change
@@ -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");
});
});
3 changes: 2 additions & 1 deletion src/jest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -27,6 +27,7 @@ beforeEach(() => {
"unspecified" as Parameters<typeof Appearance.setColorScheme>[0],
);
colorScheme.set(null);
reducedMotion.set(undefined);
});

const debugDefault = Boolean(
Expand Down
11 changes: 11 additions & 0 deletions src/native/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
ColorScheme,
Props,
ReactComponent,
ReducedMotion,
Styled,
StyledConfiguration,
StyledOptions,
Expand All @@ -19,6 +20,7 @@ import { usePassthrough } from "./react/usePassthrough";
import {
cleanupEffect,
colorScheme as colorSchemeObs,
reducedMotion as reducedMotionObs,
toVariableRecord,
VAR_SYMBOL,
type Effect,
Expand Down Expand Up @@ -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 = <
Expand Down
17 changes: 12 additions & 5 deletions src/native/reactivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReducedMotionPreference | undefined>(
// React Native reads the setting only through a callback, so the device's preference is unknown until AccessibilityInfo answers
const deviceReducedMotion = observable<ReducedMotionPreference | undefined>(
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));
},
);

Expand All @@ -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);
}
});

Expand All @@ -339,6 +344,7 @@ function createReactivityState() {
vw,
vh,
colorScheme,
deviceReducedMotion,
reducedMotion,

ContainerContext: createContext<ContainerContextValue>({}),
Expand Down Expand Up @@ -373,6 +379,7 @@ export const {
vw,
vh,
colorScheme,
deviceReducedMotion,
reducedMotion,
ContainerContext,
containerLayoutFamily,
Expand Down
6 changes: 6 additions & 0 deletions src/runtime.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
31 changes: 30 additions & 1 deletion src/web/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<ComponentType<{ style: unknown }>> = {
className: "style",
Expand Down Expand Up @@ -76,6 +82,29 @@ export const colorScheme: ColorScheme = {
},
};

export const reducedMotion: ReducedMotion =
browserPreference<ReducedMotionPreference>(() => {
// 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 `<VariableContextProvider />` instead.
*/
Expand Down
Loading