Skip to content
TypeScript

refactor(mobile): share theme mapping across Android controls (#19217)

Julius Marmingee028a083b913
diff --git a/apps/mobile/src/components/AppActivityIndicator.android.tsx b/apps/mobile/src/components/AppActivityIndicator.android.tsx
index 0604db8e8a9..018a572f25a 100644
--- a/apps/mobile/src/components/AppActivityIndicator.android.tsx
+++ b/apps/mobile/src/components/AppActivityIndicator.android.tsx
@@ -1,4 +1,5 @@
-import { LoadingIndicator, Host } from "@expo/ui/jetpack-compose";
+import { NativeThemeHost } from "./NativeThemeHost.android";
+import { LoadingIndicator } from "@expo/ui/jetpack-compose";
import { size as composeSize } from "@expo/ui/jetpack-compose/modifiers";
import { StyleSheet, View, type ActivityIndicatorProps } from "react-native";
import { useResolveClassNames, withUniwind } from "uniwind";
@@ -30,12 +31,12 @@ function AndroidActivityIndicator({
style={{ width: diameter, height: diameter }}
>
{animating ? (
- <Host ignoreSafeAreaKeyboardInsets style={{ flex: 1 }}>
+ <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ flex: 1 }}>
<LoadingIndicator
color={color ?? primaryColor}
modifiers={[composeSize(diameter, diameter)]}
/>
- </Host>
+ </NativeThemeHost>
) : null}
</View>
</View>
diff --git a/apps/mobile/src/components/Button.android.tsx b/apps/mobile/src/components/Button.android.tsx
index 7c0fcf45bcb..1d1faa8bb08 100644
--- a/apps/mobile/src/components/Button.android.tsx
+++ b/apps/mobile/src/components/Button.android.tsx
@@ -1,10 +1,10 @@
import { SymbolView } from "./AppSymbol";
+import { NativeThemeHost } from "./NativeThemeHost.android";
import {
Box,
Button as ComposeButton,
LoadingIndicator,
FilledTonalButton,
- Host,
Row,
RNHostView,
Text,
@@ -19,7 +19,7 @@ import type { ButtonProps } from "./Button";
import { useAndroidControlSizing } from "./useAndroidControlSizing";
export function Button(props: ButtonProps) {
- const { colorScheme, colors } = useControlTheme();
+ const { colors } = useControlTheme();
const typography = useScaledTextRole("footnote");
const { scale, mediumIconSize } = useAndroidControlSizing();
const tone = props.tone ?? "secondary";
@@ -62,9 +62,8 @@ export function Button(props: ButtonProps) {
style={props.fullWidth ? { width: "100%" } : { alignSelf: "flex-start" }}
>
<View importantForAccessibility="no-hide-descendants">
- <Host
+ <NativeThemeHost
matchContents={props.fullWidth ? { vertical: true } : true}
- colorScheme={colorScheme}
ignoreSafeAreaKeyboardInsets
style={props.fullWidth ? { width: "100%" } : { alignSelf: "flex-start" }}
>
@@ -110,7 +109,7 @@ export function Button(props: ButtonProps) {
<Text style={{ ...typography, fontWeight: "500" }}>{props.label}</Text>
</Row>
</Component>
- </Host>
+ </NativeThemeHost>
</View>
</View>
);
diff --git a/apps/mobile/src/components/MaterialConfirmDialog.android.tsx b/apps/mobile/src/components/MaterialConfirmDialog.android.tsx
index 35057ce10f5..dbd47797d8a 100644
--- a/apps/mobile/src/components/MaterialConfirmDialog.android.tsx
+++ b/apps/mobile/src/components/MaterialConfirmDialog.android.tsx
@@ -1,6 +1,6 @@
+import { NativeThemeHost } from "./NativeThemeHost.android";
import {
AlertDialog,
- Host,
OutlinedTextField,
Text,
TextButton,
@@ -12,7 +12,7 @@ import { useScaledTextRole } from "../features/settings/appearance/useScaledText
import type { MaterialConfirmDialogProps } from "./MaterialConfirmDialog";
export function MaterialConfirmDialog(props: MaterialConfirmDialogProps) {
- const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
+ const { themeVariables: colors } = useAppearancePreferences();
const titleTypography = useScaledTextRole("title");
const bodyTypography = useScaledTextRole("footnote");
const inputTypography = useScaledTextRole("body");
@@ -25,11 +25,7 @@ export function MaterialConfirmDialog(props: MaterialConfirmDialogProps) {
props.onConfirm(value);
};
return (
- <Host
- colorScheme={themeAppearance}
- ignoreSafeAreaKeyboardInsets
- style={{ height: 0, width: 0 }}
- >
+ <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ height: 0, width: 0 }}>
<AlertDialog
onDismissRequest={props.onCancel}
tonalElevation={0}
@@ -96,6 +92,6 @@ export function MaterialConfirmDialog(props: MaterialConfirmDialogProps) {
</TextButton>
</AlertDialog.ConfirmButton>
</AlertDialog>
- </Host>
+ </NativeThemeHost>
);
}
diff --git a/apps/mobile/src/components/MaterialFloatingActionButton.android.tsx b/apps/mobile/src/components/MaterialFloatingActionButton.android.tsx
index 29f396fc9d6..5c59723502d 100644
--- a/apps/mobile/src/components/MaterialFloatingActionButton.android.tsx
+++ b/apps/mobile/src/components/MaterialFloatingActionButton.android.tsx
@@ -1,8 +1,8 @@
+import { NativeThemeHost } from "./NativeThemeHost.android";
import {
Box,
ExtendedFloatingActionButton,
FloatingActionButton,
- Host,
LargeFloatingActionButton,
Text,
} from "@expo/ui/jetpack-compose";
@@ -23,7 +23,7 @@ export function MaterialFloatingActionButton(props: {
readonly className?: string;
readonly style?: StyleProp<ViewStyle>;
}) {
- const { themeAppearance, themeVariables: colors } = useAppearancePreferences();
+ const { themeVariables: colors } = useAppearancePreferences();
const typography = useScaledTextRole("footnote");
const { scale, iconSize: standardIconSize, fabSize, largeFabSize } = useAndroidControlSizing();
const buttonSize = props.variant === "large" ? largeFabSize : fabSize;
@@ -49,7 +49,7 @@ export function MaterialFloatingActionButton(props: {
style={props.style}
>
<View importantForAccessibility="no-hide-descendants">
- <Host matchContents colorScheme={themeAppearance} ignoreSafeAreaKeyboardInsets>
+ <NativeThemeHost matchContents ignoreSafeAreaKeyboardInsets>
<Component
modifiers={[
defaultMinSize({ minWidth: buttonSize }),
@@ -71,7 +71,7 @@ export function MaterialFloatingActionButton(props: {
</ExtendedFloatingActionButton.Text>
) : null}
</Component>
- </Host>
+ </NativeThemeHost>
</View>
{/* The RN icon stays outside Compose so it cannot intercept native button taps. */}
<View
diff --git a/apps/mobile/src/components/MaterialIconButton.android.tsx b/apps/mobile/src/components/MaterialIconButton.android.tsx
index 0c7e6daa5e8..ed4b39840fa 100644
--- a/apps/mobile/src/components/MaterialIconButton.android.tsx
+++ b/apps/mobile/src/components/MaterialIconButton.android.tsx
@@ -1,9 +1,5 @@
-import {
- FilledIconButton,
- FilledTonalIconButton,
- Host,
- IconButton,
-} from "@expo/ui/jetpack-compose";
+import { NativeThemeHost } from "./NativeThemeHost.android";
+import { FilledIconButton, FilledTonalIconButton, IconButton } from "@expo/ui/jetpack-compose";
import { size } from "@expo/ui/jetpack-compose/modifiers";
import { View } from "react-native";
@@ -20,7 +16,7 @@ export function MaterialIconButton(props: {
readonly variant?: "standard" | "primary" | "tonal" | "danger";
readonly tintColorClassName?: string;
}) {
- const { colorScheme, colors } = useControlTheme();
+ const { colors } = useControlTheme();
const { iconSize, buttonSize } = useAndroidControlSizing();
const variant = props.variant ?? "standard";
const Component =
@@ -57,8 +53,7 @@ export function MaterialIconButton(props: {
style={{ width: buttonSize, height: buttonSize }}
>
<View importantForAccessibility="no-hide-descendants">
- <Host
- colorScheme={colorScheme}
+ <NativeThemeHost
ignoreSafeAreaKeyboardInsets
style={{ width: buttonSize, height: buttonSize }}
>
@@ -74,7 +69,7 @@ export function MaterialIconButton(props: {
>
{null}
</Component>
- </Host>
+ </NativeThemeHost>
</View>
{/* Keep RN SVG measurement outside Compose; the native button owns touch and ripple. */}
<View pointerEvents="none" className="absolute inset-0 items-center justify-center">
diff --git a/apps/mobile/src/components/MaterialMenuPopup.android.tsx b/apps/mobile/src/components/MaterialMenuPopup.android.tsx
index 4f9fe3d1ada..b0ccbdbe066 100644
--- a/apps/mobile/src/components/MaterialMenuPopup.android.tsx
+++ b/apps/mobile/src/components/MaterialMenuPopup.android.tsx
@@ -1,9 +1,9 @@
+import { NativeThemeHost } from "./NativeThemeHost.android";
import {
Box,
Column,
DropdownMenu,
DropdownMenuItem,
- Host,
RNHostView,
Text,
} from "@expo/ui/jetpack-compose";
@@ -47,7 +47,7 @@ function MenuIcon(props: {
/** Native popup positioned at the original trigger, outside virtualized rows. */
export function MaterialMenuPopup(props: MaterialMenuPopupProps) {
- const { appearance, themeAppearance, themeVariables: colors } = useAppearancePreferences();
+ const { appearance, themeVariables: colors } = useAppearancePreferences();
const { scale, menuItemHeight } = useAndroidControlSizing();
const body = resolveScaledTextRole("body", appearance.baseFontSize);
const caption = resolveScaledTextRole("caption", appearance.baseFontSize);
@@ -129,19 +129,17 @@ export function MaterialMenuPopup(props: MaterialMenuPopupProps) {
);
if (props.inline) {
return (
- <Host
- colorScheme={themeAppearance}
+ <NativeThemeHost
ignoreSafeAreaKeyboardInsets
matchContents
style={{ width: props.menuWidth }}
>
<Column>{items}</Column>
- </Host>
+ </NativeThemeHost>
);
}
return (
- <Host
- colorScheme={themeAppearance}
+ <NativeThemeHost
ignoreSafeAreaKeyboardInsets
style={{
position: "absolute",
@@ -157,6 +155,6 @@ export function MaterialMenuPopup(props: MaterialMenuPopupProps) {
</DropdownMenu.Trigger>
<DropdownMenu.Items>{items}</DropdownMenu.Items>
</DropdownMenu>
- </Host>
+ </NativeThemeHost>
);
}
diff --git a/apps/mobile/src/components/MaterialRadioIndicator.android.tsx b/apps/mobile/src/components/MaterialRadioIndicator.android.tsx
index 6573501ac29..21923c76809 100644
--- a/apps/mobile/src/components/MaterialRadioIndicator.android.tsx
+++ b/apps/mobile/src/components/MaterialRadioIndicator.android.tsx
@@ -1,56 +1,28 @@
-import { Host, RadioButton } from "@expo/ui/jetpack-compose";
+import { RadioButton } from "@expo/ui/jetpack-compose";
import { size } from "@expo/ui/jetpack-compose/modifiers";
import { View } from "react-native";
-
-import { useAppearancePreferences } from "../features/settings/appearance/AppearancePreferencesProvider";
+import { NativeThemeHost } from "./NativeThemeHost.android";
+import { useControlTheme } from "./useControlTheme";
/** The enclosing radio row owns selection, touch and accessibility. */
export function MaterialRadioIndicator({ selected }: { readonly selected: boolean }) {
- const { themeAppearance, themeVariables, systemColorsActive } = useAppearancePreferences();
- // Expo's radio button cannot override colors in the pinned SDK. Custom
- // themes need their exact accent rather than a generated Material palette.
- if (!systemColorsActive) {
- return (
- <View
- pointerEvents="none"
- importantForAccessibility="no-hide-descendants"
- style={{ width: 24, height: 24, alignItems: "center", justifyContent: "center" }}
- >
- <View
- style={{
- width: 20,
- height: 20,
- borderRadius: 10,
- borderWidth: 2,
- borderColor:
- themeVariables[selected ? "--color-primary-text" : "--color-foreground-muted"],
- alignItems: "center",
- justifyContent: "center",
- }}
- >
- {selected ? (
- <View
- style={{
- width: 10,
- height: 10,
- borderRadius: 5,
- backgroundColor: themeVariables["--color-primary-text"],
- }}
- />
- ) : null}
- </View>
- </View>
- );
- }
+ const { colors, systemColorsActive } = useControlTheme();
return (
<View pointerEvents="none" importantForAccessibility="no-hide-descendants">
- <Host
- colorScheme={themeAppearance}
- ignoreSafeAreaKeyboardInsets
- style={{ width: 24, height: 24 }}
- >
- <RadioButton selected={selected} modifiers={[size(24, 24)]} />
- </Host>
+ <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ width: 24, height: 24 }}>
+ <RadioButton
+ selected={selected}
+ modifiers={[size(24, 24)]}
+ colors={
+ systemColorsActive
+ ? {}
+ : {
+ selectedColor: colors["--color-primary-text"],
+ unselectedColor: colors["--color-foreground-muted"],
+ }
+ }
+ />
+ </NativeThemeHost>
</View>
);
}
diff --git a/apps/mobile/src/components/MaterialScrollComposeButton.android.tsx b/apps/mobile/src/components/MaterialScrollComposeButton.android.tsx
index 8c653e67ee9..ade5b8e226c 100644
--- a/apps/mobile/src/components/MaterialScrollComposeButton.android.tsx
+++ b/apps/mobile/src/components/MaterialScrollComposeButton.android.tsx
@@ -1,4 +1,5 @@
-import { Box, ExtendedFloatingActionButton, Host, Icon, Text } from "@expo/ui/jetpack-compose";
+import { NativeThemeHost } from "./NativeThemeHost.android";
+import { Box, ExtendedFloatingActionButton, Icon, Text } from "@expo/ui/jetpack-compose";
import {
defaultMinSize,
fillMaxWidth,
@@ -20,7 +21,7 @@ export function MaterialScrollComposeButton(props: {
readonly className?: string;
readonly style?: StyleProp<ViewStyle>;
}) {
- const { appearance, themeAppearance, themeVariables: colors } = useAppearancePreferences();
+ const { appearance, themeVariables: colors } = useAppearancePreferences();
const typography = resolveScaledTextRole("footnote", appearance.baseFontSize);
const { iconSize, fabSize } = useAndroidControlSizing();
// Scale the native 56dp minimum; keep text and icons at their requested sizes.
@@ -38,9 +39,8 @@ export function MaterialScrollComposeButton(props: {
style={[props.style, { left: 20, height: fabSize }]}
>
<View pointerEvents="none" importantForAccessibility="no-hide-descendants">
- <Host
+ <NativeThemeHost
matchContents={{ vertical: true }}
- colorScheme={themeAppearance}
ignoreSafeAreaKeyboardInsets
style={{ width: "100%" }}
>
@@ -83,7 +83,7 @@ export function MaterialScrollComposeButton(props: {
</ExtendedFloatingActionButton.Text>
</ExtendedFloatingActionButton>
</Box>
- </Host>
+ </NativeThemeHost>
</View>
{/* The visual host is wider than the button; only this target intercepts list touches. */}
<Pressable
diff --git a/apps/mobile/src/components/MaterialSwitch.android.tsx b/apps/mobile/src/components/MaterialSwitch.android.tsx
index 1c1478f0b50..93cee8ef077 100644
--- a/apps/mobile/src/components/MaterialSwitch.android.tsx
+++ b/apps/mobile/src/components/MaterialSwitch.android.tsx
@@ -1,4 +1,5 @@
-import { Host, Switch as ComposeSwitch } from "@expo/ui/jetpack-compose";
+import { NativeThemeHost } from "./NativeThemeHost.android";
+import { Switch as ComposeSwitch } from "@expo/ui/jetpack-compose";
import { View } from "react-native";
import type { ThemedSwitchProps } from "./MaterialSwitch.types";
@@ -6,7 +7,7 @@ import { useControlTheme } from "./useControlTheme";
/** Material's native switch, with the same palette and accessibility contract as our RN controls. */
export function MaterialSwitch(props: ThemedSwitchProps) {
- const { colorScheme, colors } = useControlTheme();
+ const { colors } = useControlTheme();
const toggle = () => {
if (!props.disabled) props.onValueChange?.(!props.value);
};
@@ -24,11 +25,7 @@ export function MaterialSwitch(props: ThemedSwitchProps) {
testID={props.testID}
>
<View importantForAccessibility="no-hide-descendants">
- <Host
- colorScheme={colorScheme}
- ignoreSafeAreaKeyboardInsets
- style={{ width: 52, height: 48 }}
- >
+ <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ width: 52, height: 48 }}>
<ComposeSwitch
value={Boolean(props.value)}
enabled={!props.disabled}
@@ -42,7 +39,7 @@ export function MaterialSwitch(props: ThemedSwitchProps) {
uncheckedBorderColor: colors["--color-switch-inactive-thumb"],
}}
/>
- </Host>
+ </NativeThemeHost>
</View>
</View>
);
diff --git a/apps/mobile/src/components/SliderRow.android.tsx b/apps/mobile/src/components/SliderRow.android.tsx
index 34008e566e9..c0d4228a65e 100644
--- a/apps/mobile/src/components/SliderRow.android.tsx
+++ b/apps/mobile/src/components/SliderRow.android.tsx
@@ -6,7 +6,7 @@ import { View } from "react-native";
import { AppText as Text } from "./AppText";
import { SymbolView } from "./AppSymbol";
-import { NativeThemeHost } from "./NativeThemeHost";
+import { NativeThemeHost } from "./NativeThemeHost.android";
import { useControlTheme } from "./useControlTheme";
import type { SliderRow as SharedSliderRow } from "./SliderRow";