Skip to content
TypeScript

fix(mobile): keep segmented controls readable on cards and screens (#25729)

Alexe689dad42c84
diff --git a/apps/mobile/src/components/ui/SegmentedControl.ios.tsx b/apps/mobile/src/components/ui/SegmentedControl.ios.tsx
index 4862e99cdab..15883d300da 100644
--- a/apps/mobile/src/components/ui/SegmentedControl.ios.tsx
+++ b/apps/mobile/src/components/ui/SegmentedControl.ios.tsx
@@ -1,11 +1,8 @@
import { requireNativeView } from "expo";
import { View, type NativeSyntheticEvent, type ViewProps } from "react-native";
-import {
- flattenThemeColor,
- themeColorWithAlpha,
- themeColorWithContrast,
-} from "../../lib/mobileTheme";
+import { SegmentedControl as FlatSegmentedControl } from "./SegmentedControl.shared";
+import { segmentedControlColors } from "./segmentedControlColors";
import { useControlTheme } from "./useControlTheme";
import type { SegmentedControlProps } from "./SegmentedControl.types";
@@ -27,17 +24,16 @@ const NativeSegmentedControl = requireNativeView<
export function SegmentedControl<Value extends string | number>(
props: SegmentedControlProps<Value>,
) {
- const { colors, colorScheme } = useControlTheme();
- const trackColor = flattenThemeColor(
- themeColorWithAlpha(colors["--color-prominent-action"], colorScheme === "dark" ? 0.18 : 0.07),
- colors["--color-screen"],
+ const { colors, colorScheme, themeId } = useControlTheme();
+ // UIKit adds a gray fill over the sunken card track.
+ if (props.variant === "card") return <FlatSegmentedControl {...props} />;
+ const { trackColor, selectedColor, selectedTextColor, mutedColor } = segmentedControlColors(
+ colors,
+ colorScheme,
+ props.variant,
+ "ios",
+ themeId,
);
- const selectedColor =
- colorScheme === "dark" ? colors["--color-prominent-action"] : colors["--color-secondary"];
- const selectedTextColor =
- colorScheme === "dark"
- ? colors["--color-prominent-action-foreground"]
- : colors["--color-secondary-foreground"];
return (
<View className={props.className}>
<NativeSegmentedControl
@@ -46,11 +42,7 @@ export function SegmentedControl<Value extends string | number>(
trackColor={trackColor}
selectedColor={selectedColor}
textColor={selectedTextColor}
- mutedColor={themeColorWithContrast(
- colors["--color-foreground-muted"],
- trackColor,
- colorScheme === "dark" ? 7 : 4.5,
- )}
+ mutedColor={mutedColor}
appearance={colorScheme}
onSelectionChange={({ nativeEvent }) => {
const option = props.options[nativeEvent.index];
diff --git a/apps/mobile/src/components/ui/SegmentedControl.shared.tsx b/apps/mobile/src/components/ui/SegmentedControl.shared.tsx
new file mode 100644
index 00000000000..9eac685a8ca
--- /dev/null
+++ b/apps/mobile/src/components/ui/SegmentedControl.shared.tsx
@@ -0,0 +1,84 @@
+import { useState } from "react";
+import { I18nManager, Platform, Pressable, View } from "react-native";
+import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated";
+
+import { segmentedControlColors } from "./segmentedControlColors";
+import { AppText } from "./AppText";
+import { useControlTheme } from "./useControlTheme";
+import type { SegmentedControlProps } from "./SegmentedControl.types";
+
+export type { SegmentedControlProps } from "./SegmentedControl.types";
+
+export function SegmentedControl<Value extends string | number>(
+ props: SegmentedControlProps<Value>,
+) {
+ const { colorScheme, colors, themeId } = useControlTheme();
+ const [layout, setLayout] = useState({ width: 0, height: 0 });
+ const { width } = layout;
+ const reduceMotion = useReducedMotion();
+ const selectedIndex = props.options.findIndex((option) => option.value === props.selected);
+ const segmentWidth = props.options.length ? Math.max(0, width - 6) / props.options.length : 0;
+ const selectionOffset = selectedIndex * segmentWidth * (I18nManager.isRTL ? -1 : 1);
+ const selectionStyle = useAnimatedStyle(() => ({
+ width: segmentWidth,
+ transform: [{ translateX: withTiming(selectionOffset, { duration: reduceMotion ? 0 : 160 }) }],
+ }));
+ const { trackColor, selectedColor, selectedTextColor, mutedColor } = segmentedControlColors(
+ colors,
+ colorScheme,
+ props.variant,
+ Platform.OS,
+ themeId,
+ );
+ const minHeight = props.size === "compact" ? 32 : Platform.OS === "android" ? 40 : 36;
+
+ return (
+ <View collapsable={false} className={props.className}>
+ <View
+ onLayout={(event) => setLayout(event.nativeEvent.layout)}
+ className="flex-row rounded-full p-[3px]"
+ style={{
+ minHeight,
+ backgroundColor: trackColor,
+ }}
+ >
+ {selectedIndex >= 0 && width > 0 ? (
+ <Animated.View
+ pointerEvents="none"
+ className="absolute bottom-[3px] start-[3px] top-[3px] rounded-full"
+ style={[
+ {
+ backgroundColor: selectedColor,
+ },
+ selectionStyle,
+ ]}
+ />
+ ) : null}
+ {props.options.map((option) => {
+ const selected = option.value === props.selected;
+ return (
+ <Pressable
+ key={`${typeof option.value}:${option.value}`}
+ accessibilityRole={props.role ?? "button"}
+ accessibilityLabel={option.accessibilityLabel ?? option.label}
+ accessibilityState={{ selected }}
+ onPress={() => props.onSelect(option.value)}
+ className="flex-1 items-center justify-center rounded-full px-2 py-1 active:opacity-70"
+ style={{ minHeight: minHeight - 6 }}
+ >
+ <AppText
+ textRole="footnote"
+ className={selected ? "font-t3-bold" : "font-t3-medium"}
+ style={{
+ color: selected ? selectedTextColor : mutedColor,
+ }}
+ >
+ {option.label}
+ </AppText>
+ </Pressable>
+ );
+ })}
+ </View>
+ </View>
+ );
+}
diff --git a/apps/mobile/src/components/ui/SegmentedControl.tsx b/apps/mobile/src/components/ui/SegmentedControl.tsx
index 0fdc46c0f27..bbcbc4a5f45 100644
--- a/apps/mobile/src/components/ui/SegmentedControl.tsx
+++ b/apps/mobile/src/components/ui/SegmentedControl.tsx
@@ -1,78 +1 @@
-import { useState } from "react";
-import { I18nManager, Platform, Pressable, View } from "react-native";
-import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated";
-
-import { AppText } from "./AppText";
-import { useControlTheme } from "./useControlTheme";
-import type { SegmentedControlProps } from "./SegmentedControl.types";
-
-export type { SegmentedControlProps } from "./SegmentedControl.types";
-
-export function SegmentedControl<Value extends string | number>(
- props: SegmentedControlProps<Value>,
-) {
- const { colorScheme, colors } = useControlTheme();
- const [layout, setLayout] = useState({ width: 0, height: 0 });
- const { width } = layout;
- const reduceMotion = useReducedMotion();
- const selectedIndex = props.options.findIndex((option) => option.value === props.selected);
- const segmentWidth = props.options.length ? Math.max(0, width - 6) / props.options.length : 0;
- const selectionOffset = selectedIndex * segmentWidth * (I18nManager.isRTL ? -1 : 1);
- const selectionStyle = useAnimatedStyle(() => ({
- width: segmentWidth,
- transform: [{ translateX: withTiming(selectionOffset, { duration: reduceMotion ? 0 : 160 }) }],
- }));
- const dark = colorScheme === "dark";
- const minHeight = props.size === "compact" ? 32 : Platform.OS === "android" ? 40 : 36;
-
- return (
- <View collapsable={false} className={props.className}>
- <View
- onLayout={(event) => setLayout(event.nativeEvent.layout)}
- className="flex-row rounded-full p-[3px]"
- style={{
- minHeight,
- backgroundColor: colors[dark ? "--color-screen" : "--color-grouped-card"],
- }}
- >
- {selectedIndex >= 0 && width > 0 ? (
- <Animated.View
- pointerEvents="none"
- className="absolute bottom-[3px] start-[3px] top-[3px] rounded-full"
- style={[
- {
- backgroundColor: colors[dark ? "--color-secondary" : "--color-screen"],
- },
- selectionStyle,
- ]}
- />
- ) : null}
- {props.options.map((option) => {
- const selected = option.value === props.selected;
- return (
- <Pressable
- key={`${typeof option.value}:${option.value}`}
- accessibilityRole={props.role ?? "button"}
- accessibilityLabel={option.accessibilityLabel ?? option.label}
- accessibilityState={{ selected }}
- onPress={() => props.onSelect(option.value)}
- className="flex-1 items-center justify-center rounded-full px-2 py-1 active:opacity-70"
- style={{ minHeight: minHeight - 6 }}
- >
- <AppText
- textRole="footnote"
- className={selected ? "font-t3-bold" : "font-t3-medium"}
- style={{
- color:
- colors[selected ? "--color-secondary-foreground" : "--color-foreground-muted"],
- }}
- >
- {option.label}
- </AppText>
- </Pressable>
- );
- })}
- </View>
- </View>
- );
-}
+export { SegmentedControl, type SegmentedControlProps } from "./SegmentedControl.shared";
diff --git a/apps/mobile/src/components/ui/SegmentedControl.types.ts b/apps/mobile/src/components/ui/SegmentedControl.types.ts
index 779b49fe1a4..472ed46b0e2 100644
--- a/apps/mobile/src/components/ui/SegmentedControl.types.ts
+++ b/apps/mobile/src/components/ui/SegmentedControl.types.ts
@@ -8,6 +8,7 @@ export interface SegmentedControlProps<Value extends number | string> {
readonly onSelect: (value: Value) => void;
/** Compact uses a 32-point capsule; default uses 36 points on iOS and 40 on Android. */
readonly size?: "default" | "compact";
+ readonly variant?: "standalone" | "card";
readonly role?: "tab" | "button";
readonly className?: string;
}
diff --git a/apps/mobile/src/components/ui/segmentedControlColors.ts b/apps/mobile/src/components/ui/segmentedControlColors.ts
new file mode 100644
index 00000000000..6b58675726e
--- /dev/null
+++ b/apps/mobile/src/components/ui/segmentedControlColors.ts
@@ -0,0 +1,58 @@
+import {
+ flattenThemeColor,
+ themeColorContrastRatio,
+ themeColorWithAlpha,
+ themeColorWithContrast,
+ type MobileThemeAppearance,
+ type MobileThemeId,
+ type MobileThemeVariables,
+} from "../../lib/mobileTheme";
+import type { SegmentedControlProps } from "./SegmentedControl.types";
+
+export function segmentedControlColors(
+ colors: MobileThemeVariables,
+ appearance: MobileThemeAppearance,
+ variant: SegmentedControlProps<string>["variant"],
+ platform: string,
+ themeId: MobileThemeId,
+) {
+ const dark = appearance === "dark";
+ const card = variant === "card";
+ const accent =
+ colors[themeId === "material-you" ? "--color-primary" : "--color-prominent-action"];
+ const trackColor = card
+ ? dark
+ ? colors["--color-screen"]
+ : flattenThemeColor(
+ themeColorWithAlpha(colors["--color-foreground"], 0.07),
+ colors["--color-grouped-card"],
+ )
+ : flattenThemeColor(themeColorWithAlpha(accent, dark ? 0.1 : 0.07), colors["--color-screen"]);
+ let selectedColor = colors["--color-secondary"];
+ if (card && !dark) selectedColor = colors["--color-screen"];
+ if (dark && (!card || themeId === "t3-code")) {
+ selectedColor = flattenThemeColor(themeColorWithAlpha(accent, 0.32), colors["--color-screen"]);
+ }
+ let selectedTextColor =
+ colors[!card && dark ? "--color-foreground" : "--color-secondary-foreground"];
+ if (!card && !dark && platform === "android") {
+ const selection = colors["--color-thread-selected"];
+ if (
+ themeColorContrastRatio(selection, trackColor) >
+ themeColorContrastRatio(selectedColor, trackColor)
+ ) {
+ selectedColor = selection;
+ selectedTextColor = colors["--color-thread-selected-foreground"];
+ }
+ }
+ return {
+ trackColor,
+ selectedColor,
+ selectedTextColor: themeColorWithContrast(selectedTextColor, selectedColor),
+ mutedColor: themeColorWithContrast(
+ colors["--color-foreground-muted"],
+ trackColor,
+ dark && !card ? 7 : 4.5,
+ ),
+ };
+}
diff --git a/apps/mobile/src/components/ui/useControlTheme.ts b/apps/mobile/src/components/ui/useControlTheme.ts
index d2181ba8954..cb7225eddb4 100644
--- a/apps/mobile/src/components/ui/useControlTheme.ts
+++ b/apps/mobile/src/components/ui/useControlTheme.ts
@@ -1,9 +1,10 @@
import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
export function useControlTheme() {
- const { themeAppearance, themeVariables, systemColorsActive, appearance } =
+ const { themeId, themeAppearance, themeVariables, systemColorsActive, appearance } =
useAppearancePreferences();
return {
+ themeId,
colorScheme: themeAppearance,
colors: themeVariables,
systemColorsActive,
diff --git a/apps/mobile/src/features/development/ComponentCatalogScreen.tsx b/apps/mobile/src/features/development/ComponentCatalogScreen.tsx
index 9a3d894d384..d692e3e129b 100644
--- a/apps/mobile/src/features/development/ComponentCatalogScreen.tsx
+++ b/apps/mobile/src/features/development/ComponentCatalogScreen.tsx
@@ -104,6 +104,16 @@ export function ComponentCatalogScreen() {
<View className="gap-3">
<AppText className="text-lg font-t3-semibold">Lists and selection</AppText>
<ListSection>
+ <View className="p-4">
+ <SegmentedControl
+ variant="card"
+ options={CHOICES}
+ selected={tab}
+ onSelect={setTab}
+ size="compact"
+ role="tab"
+ />
+ </View>
<ListRow icon="folder" label="Projects" detail="4" onPress={() => setSheet(true)} />
<ListToggleRow
icon="bell.badge"
diff --git a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx
index d16e08035ca..583ab61cf7d 100644
--- a/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx
+++ b/apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx
@@ -951,6 +951,7 @@ function TaskForm({
<SettingsSection title="Schedule">
<View className="px-4 py-3">
<SegmentedControl
+ variant="card"
options={[
{ value: "fixed_time", label: "At a time" },
{ value: "interval", label: "Interval" },
diff --git a/apps/mobile/src/features/threads/CustomSnoozeSheet.android.tsx b/apps/mobile/src/features/threads/CustomSnoozeSheet.android.tsx
index dc65543c37b..fd5a08d678a 100644
--- a/apps/mobile/src/features/threads/CustomSnoozeSheet.android.tsx
+++ b/apps/mobile/src/features/threads/CustomSnoozeSheet.android.tsx
@@ -144,6 +144,7 @@ export function CustomSnoozeSheet(props: Props) {
<Text style={titleTypography}>Custom snooze</Text>
<RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
<SegmentedControl
+ variant="card"
options={modes}
selected={mode}
onSelect={(value) => {
@@ -155,6 +156,7 @@ export function CustomSnoozeSheet(props: Props) {
{mode === "date" ? (
<RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
<SegmentedControl
+ variant="card"
options={[
{
value: "date",
@@ -230,7 +232,12 @@ export function CustomSnoozeSheet(props: Props) {
</FilledTonalIconButton>
</Row>
<RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
- <SegmentedControl options={units} selected={unit} onSelect={setUnit} />
+ <SegmentedControl
+ variant="card"
+ options={units}
+ selected={unit}
+ onSelect={setUnit}
+ />
</RNHostView>
</Column>
)}
diff --git a/apps/mobile/src/lib/mobileTheme.ts b/apps/mobile/src/lib/mobileTheme.ts
index ed45f051884..afe26b7ddd2 100644
--- a/apps/mobile/src/lib/mobileTheme.ts
+++ b/apps/mobile/src/lib/mobileTheme.ts
@@ -209,6 +209,12 @@ function contrastRatio(
);
}
+export function themeColorContrastRatio(first: string, second: string): number {
+ const firstChannels = rgbChannels(first);
+ const secondChannels = rgbChannels(second);
+ return firstChannels && secondChannels ? contrastRatio(firstChannels, secondChannels) : 1;
+}
+
export function themeColorWithContrast(
accent: string,
surface: string | ReadonlyArray<string>,