Repository navigation
feat(mobile): map native hosts to the app theme #18567
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
juliusmarminge
merged 4 commits into
t3/mobile-ds-catalog
from
t3/mobile-native-theme-host
Oct 15, 2026
+335
−17
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6fbb287
feat(mobile): map native hosts to the app theme
juliusmarminge bf6dce9
fix(mobile): compile the patched Compose host from source
juliusmarminge a77faa7
test(mobile): check native palette contrast with table cases
juliusmarminge 90c094c
fix(mobile): pin the patched Expo UI version
juliusmarminge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { Host } from "@expo/ui/jetpack-compose"; | ||
| import type { ComponentProps } from "react"; | ||
| import { createNativeThemeColors } from "../lib/nativeTheme"; | ||
| import { useControlTheme } from "./useControlTheme"; | ||
|
|
||
| /** One native island, using T3's resolved colors or the user's Material You palette. */ | ||
| export function NativeThemeHost( | ||
| props: Omit<ComponentProps<typeof Host>, "colorScheme" | "seedColor" | "colorOverrides">, | ||
| ) { | ||
| const { colors, colorScheme, systemColorsActive } = useControlTheme(); | ||
| return ( | ||
| <Host | ||
| {...props} | ||
| colorScheme={colorScheme} | ||
| colorOverrides={systemColorsActive ? undefined : createNativeThemeColors(colors)} | ||
| /> | ||
| ); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { Host } from "@expo/ui/swift-ui"; | ||
| import type { ComponentProps } from "react"; | ||
| import { useControlTheme } from "./useControlTheme"; | ||
|
|
||
| /** SwiftUI shares the app's appearance and action tint. Row fills stay explicit. */ | ||
| export function NativeThemeHost( | ||
| props: Omit<ComponentProps<typeof Host>, "colorScheme" | "seedColor">, | ||
| ) { | ||
| const { colors, colorScheme } = useControlTheme(); | ||
| return <Host {...props} colorScheme={colorScheme} seedColor={colors["--color-primary"]} />; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import { describe, expect, it } from "vite-plus/test"; | ||
| import { getMobileThemeVariables, MOBILE_THEME_OPTIONS } from "./mobileTheme"; | ||
| import { createNativeThemeColors } from "./nativeTheme"; | ||
|
|
||
| function luminance(hex: string) { | ||
| const channels = [1, 3, 5].map((offset) => { | ||
| const value = Number.parseInt(hex.slice(offset, offset + 2), 16) / 255; | ||
| return value <= 0.04045 ? value / 12.92 : ((value + 0.055) / 1.055) ** 2.4; | ||
| }); | ||
| return channels[0]! * 0.2126 + channels[1]! * 0.7152 + channels[2]! * 0.0722; | ||
| } | ||
|
|
||
| function contrast(a: string, b: string) { | ||
| const values = [luminance(a), luminance(b)].sort((x, y) => y - x); | ||
| return (values[0]! + 0.05) / (values[1]! + 0.05); | ||
| } | ||
|
|
||
| const cases = MOBILE_THEME_OPTIONS.flatMap((theme) => { | ||
| if (theme.id === "material-you") return []; | ||
| const themeId = theme.id; | ||
| return (["light", "dark"] as const).map((appearance) => ({ | ||
| label: theme.label, | ||
| themeId, | ||
| appearance, | ||
| })); | ||
| }); | ||
|
|
||
| describe("native theme colors", () => { | ||
| it.each(cases)( | ||
| "$label $appearance keeps native labels legible on every container", | ||
| ({ themeId, appearance }) => { | ||
| const colors = createNativeThemeColors(getMobileThemeVariables(themeId, appearance)); | ||
| for (const surface of [ | ||
| colors.surface, | ||
| colors.surfaceContainer, | ||
| colors.surfaceContainerHigh, | ||
| colors.surfaceContainerHighest, | ||
| ]) { | ||
| expect(contrast(colors.onSurface, surface)).toBeGreaterThanOrEqual(4.5); | ||
| } | ||
| expect(contrast(colors.onPrimary, colors.primary)).toBeGreaterThanOrEqual(4.5); | ||
| for (const color of Object.values(colors)) expect(color).toMatch(/^#[0-9a-f]{6}$/i); | ||
| }, | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import type { MaterialColors } from "@expo/ui/jetpack-compose"; | ||
| import { flattenThemeColor, type MobileThemeVariables } from "./mobileTheme"; | ||
|
|
||
| /** Native controls use the same resolved palette as RN, without generating new hues. */ | ||
| export function createNativeThemeColors(colors: MobileThemeVariables): MaterialColors { | ||
| const roles = { | ||
| primary: colors["--color-primary"], | ||
| onPrimary: colors["--color-primary-foreground"], | ||
| primaryContainer: colors["--color-thread-selected"], | ||
| onPrimaryContainer: colors["--color-thread-selected-foreground"], | ||
| inversePrimary: colors["--color-primary-foreground"], | ||
| secondary: colors["--color-secondary"], | ||
| onSecondary: colors["--color-secondary-foreground"], | ||
| secondaryContainer: colors["--color-subtle-strong"], | ||
| onSecondaryContainer: colors["--color-secondary-foreground"], | ||
| tertiary: colors["--color-primary"], | ||
| onTertiary: colors["--color-primary-foreground"], | ||
| tertiaryContainer: colors["--color-thread-selected"], | ||
| onTertiaryContainer: colors["--color-thread-selected-foreground"], | ||
| background: colors["--color-screen"], | ||
| onBackground: colors["--color-foreground"], | ||
| surface: colors["--color-screen"], | ||
| onSurface: colors["--color-foreground"], | ||
| surfaceVariant: colors["--color-card-alt"], | ||
| onSurfaceVariant: colors["--color-foreground-muted"], | ||
| surfaceTint: colors["--color-primary"], | ||
| inverseSurface: colors["--color-foreground"], | ||
| inverseOnSurface: colors["--color-screen"], | ||
| error: colors["--color-danger-foreground"], | ||
| onError: colors["--color-danger"], | ||
| errorContainer: colors["--color-danger"], | ||
| onErrorContainer: colors["--color-danger-foreground"], | ||
| outline: colors["--color-input-border"], | ||
| outlineVariant: colors["--color-separator"], | ||
| scrim: "#000000", | ||
| surfaceBright: colors["--color-card-alt"], | ||
| surfaceDim: colors["--color-input"], | ||
| surfaceContainer: colors["--color-grouped-card"], | ||
| surfaceContainerHigh: colors["--color-card-alt"], | ||
| surfaceContainerHighest: colors["--color-subtle-strong"], | ||
| surfaceContainerLow: colors["--color-sheet-solid"], | ||
| surfaceContainerLowest: colors["--color-input"], | ||
| primaryFixed: colors["--color-primary"], | ||
| primaryFixedDim: colors["--color-primary"], | ||
| onPrimaryFixed: colors["--color-primary-foreground"], | ||
| onPrimaryFixedVariant: colors["--color-primary-foreground"], | ||
| secondaryFixed: colors["--color-secondary"], | ||
| secondaryFixedDim: colors["--color-secondary"], | ||
| onSecondaryFixed: colors["--color-secondary-foreground"], | ||
| onSecondaryFixedVariant: colors["--color-secondary-foreground"], | ||
| tertiaryFixed: colors["--color-primary"], | ||
| tertiaryFixedDim: colors["--color-primary"], | ||
| onTertiaryFixed: colors["--color-primary-foreground"], | ||
| onTertiaryFixedVariant: colors["--color-primary-foreground"], | ||
| } satisfies Record<keyof MaterialColors, string>; | ||
| // Material scheme roles are opaque. Composite translucent RN fills onto the canvas. | ||
| return Object.fromEntries( | ||
| Object.entries(roles).map(([role, color]) => [ | ||
| role, | ||
| flattenThemeColor(color, colors["--color-screen"]), | ||
| ]), | ||
| ) as MaterialColors; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.