diff --git a/src/__tests__/native/media-query.test.tsx b/src/__tests__/native/media-query.test.tsx index 020b4aad..4f6d7660 100644 --- a/src/__tests__/native/media-query.test.tsx +++ b/src/__tests__/native/media-query.test.tsx @@ -1,4 +1,4 @@ -import { PixelRatio } from "react-native"; +import { Appearance, PixelRatio } from "react-native"; import { act, render, screen } from "@testing-library/react-native"; import { View } from "react-native-css/components/View"; @@ -62,6 +62,40 @@ test("color scheme", () => { }); }); +// "unspecified" (react-native 0.82-0.86) and "auto" (0.87+) are not in every +// version's ColorSchemeName, but both mean "follow the system" at runtime. +test.each([null, undefined, "unspecified", "auto"])( + "color scheme %p follows the system", + (value) => { + registerCSS(` +.my-class { color: blue; } + +@media (prefers-color-scheme: dark) { + .my-class { color: red; } +}`); + + render(); + const component = screen.getByTestId(testID); + + act(() => { + colorScheme.set("dark"); + }); + + expect(component.props.style).toStrictEqual({ + color: "#f00", + }); + + act(() => { + colorScheme.set(value as Parameters[0]); + }); + + expect(component.props.style).toStrictEqual({ + color: "#00f", + }); + expect(colorScheme.get()).toBe(Appearance.getColorScheme() ?? "light"); + }, +); + test("width (plain)", () => { registerCSS(` .my-class { color: blue; } diff --git a/src/jest/index.ts b/src/jest/index.ts index f581d44f..d16a492a 100644 --- a/src/jest/index.ts +++ b/src/jest/index.ts @@ -23,7 +23,9 @@ beforeEach(() => { StyleCollection.styles.clear(); resetVariableRegistries(); dimensions.set(Dimensions.get("window")); - Appearance.setColorScheme("unspecified"); + Appearance.setColorScheme( + "unspecified" as Parameters[0], + ); colorScheme.set(null); }); diff --git a/src/native/api.tsx b/src/native/api.tsx index c6c5effb..5a8d1fe0 100644 --- a/src/native/api.tsx +++ b/src/native/api.tsx @@ -74,7 +74,9 @@ export const colorScheme: ColorScheme = { return colorSchemeObs.get() ?? Appearance.getColorScheme() ?? "light"; }, set(value) { - return colorSchemeObs.set(value === "unspecified" ? null : value); + return colorSchemeObs.set( + value === "light" || value === "dark" ? value : null, + ); }, }; diff --git a/src/web/api.tsx b/src/web/api.tsx index 28e09ab8..b38eec10 100644 --- a/src/web/api.tsx +++ b/src/web/api.tsx @@ -68,7 +68,11 @@ export const colorScheme: ColorScheme = { return Appearance.getColorScheme(); }, set(name) { - Appearance.setColorScheme(name ?? "unspecified"); + Appearance.setColorScheme( + name === "light" || name === "dark" + ? name + : ("unspecified" as Parameters[0]), + ); }, };