From 02198857038ce98c6fc8e249055de1c256945101 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 23 Nov 2026 17:50:06 -0500 Subject: [PATCH] fix: type-check color scheme writes on every react-native in the peer range react-native's ColorSchemeName changes inside the >=0.81 peer range: 0.81 declares 'light' | 'dark' | null | undefined, 0.82-0.86 'light' | 'dark' | 'unspecified', and 0.87+ narrows it to 'light' | 'dark' and adds 'auto' to setColorScheme. Comparing or passing the "unspecified" literal therefore failed tsc on 0.81 (three places) and on 0.87+ (native colorScheme.set). Keep react-native's own type and narrow at runtime instead: a value that is not 'light' or 'dark' means "follow the system". The one place that must hand that to Appearance passes "unspecified", the only reset value every version's native module accepts, asserted to the installed setColorScheme parameter type. Runtime behaviour is unchanged for every value main accepted; "auto" now also clears the override instead of being stored as the scheme. Co-authored-by: Yevhenii Co-Authored-By: Claude Opus 5.5 --- src/__tests__/native/media-query.test.tsx | 36 ++++++++++++++++++++++- src/jest/index.ts | 4 ++- src/native/api.tsx | 4 ++- src/web/api.tsx | 6 +++- 4 files changed, 46 insertions(+), 4 deletions(-) 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]), + ); }, };