Skip to content

Commit f8d31ea

Browse files
committed
fix(mobile): give card controls a sunken theme variant
1 parent 658c93c commit f8d31ea

9 files changed

Lines changed: 170 additions & 114 deletions

‎apps/mobile/src/components/ui/SegmentedControl.ios.tsx‎

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { requireNativeView } from "expo";
22
import { View, type NativeSyntheticEvent, type ViewProps } from "react-native";
33

4-
import {
5-
flattenThemeColor,
6-
themeColorWithAlpha,
7-
themeColorWithContrast,
8-
} from "../../lib/mobileTheme";
4+
import { SegmentedControl as FlatSegmentedControl } from "./SegmentedControl.shared";
5+
import { segmentedControlColors } from "./segmentedControlColors";
96
import { useControlTheme } from "./useControlTheme";
107
import type { SegmentedControlProps } from "./SegmentedControl.types";
118

@@ -28,16 +25,14 @@ export function SegmentedControl<Value extends string | number>(
2825
props: SegmentedControlProps<Value>,
2926
) {
3027
const { colors, colorScheme } = useControlTheme();
31-
const trackColor = flattenThemeColor(
32-
themeColorWithAlpha(colors["--color-prominent-action"], colorScheme === "dark" ? 0.18 : 0.07),
33-
colors["--color-screen"],
28+
// UIKit adds a gray fill over the sunken card track.
29+
if (props.variant === "card") return <FlatSegmentedControl {...props} />;
30+
const { trackColor, selectedColor, selectedTextColor, mutedColor } = segmentedControlColors(
31+
colors,
32+
colorScheme,
33+
props.variant,
34+
"ios",
3435
);
35-
const selectedColor =
36-
colorScheme === "dark" ? colors["--color-prominent-action"] : colors["--color-secondary"];
37-
const selectedTextColor =
38-
colorScheme === "dark"
39-
? colors["--color-prominent-action-foreground"]
40-
: colors["--color-secondary-foreground"];
4136
return (
4237
<View className={props.className}>
4338
<NativeSegmentedControl
@@ -46,11 +41,7 @@ export function SegmentedControl<Value extends string | number>(
4641
trackColor={trackColor}
4742
selectedColor={selectedColor}
4843
textColor={selectedTextColor}
49-
mutedColor={themeColorWithContrast(
50-
colors["--color-foreground-muted"],
51-
trackColor,
52-
colorScheme === "dark" ? 7 : 4.5,
53-
)}
44+
mutedColor={mutedColor}
5445
appearance={colorScheme}
5546
onSelectionChange={({ nativeEvent }) => {
5647
const option = props.options[nativeEvent.index];
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { useState } from "react";
2+
import { I18nManager, Platform, Pressable, View } from "react-native";
3+
import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated";
4+
5+
import { segmentedControlColors } from "./segmentedControlColors";
6+
import { AppText } from "./AppText";
7+
import { useControlTheme } from "./useControlTheme";
8+
import type { SegmentedControlProps } from "./SegmentedControl.types";
9+
10+
export type { SegmentedControlProps } from "./SegmentedControl.types";
11+
12+
export function SegmentedControl<Value extends string | number>(
13+
props: SegmentedControlProps<Value>,
14+
) {
15+
const { colorScheme, colors } = useControlTheme();
16+
const [layout, setLayout] = useState({ width: 0, height: 0 });
17+
const { width } = layout;
18+
const reduceMotion = useReducedMotion();
19+
const selectedIndex = props.options.findIndex((option) => option.value === props.selected);
20+
const segmentWidth = props.options.length ? Math.max(0, width - 6) / props.options.length : 0;
21+
const selectionOffset = selectedIndex * segmentWidth * (I18nManager.isRTL ? -1 : 1);
22+
const selectionStyle = useAnimatedStyle(() => ({
23+
width: segmentWidth,
24+
transform: [{ translateX: withTiming(selectionOffset, { duration: reduceMotion ? 0 : 160 }) }],
25+
}));
26+
const { trackColor, selectedColor, selectedTextColor, mutedColor } = segmentedControlColors(
27+
colors,
28+
colorScheme,
29+
props.variant,
30+
Platform.OS,
31+
);
32+
const minHeight = props.size === "compact" ? 32 : Platform.OS === "android" ? 40 : 36;
33+
34+
return (
35+
<View collapsable={false} className={props.className}>
36+
<View
37+
onLayout={(event) => setLayout(event.nativeEvent.layout)}
38+
className="flex-row rounded-full p-[3px]"
39+
style={{
40+
minHeight,
41+
backgroundColor: trackColor,
42+
}}
43+
>
44+
{selectedIndex >= 0 && width > 0 ? (
45+
<Animated.View
46+
pointerEvents="none"
47+
className="absolute bottom-[3px] start-[3px] top-[3px] rounded-full"
48+
style={[
49+
{
50+
backgroundColor: selectedColor,
51+
},
52+
selectionStyle,
53+
]}
54+
/>
55+
) : null}
56+
{props.options.map((option) => {
57+
const selected = option.value === props.selected;
58+
return (
59+
<Pressable
60+
key={`${typeof option.value}:${option.value}`}
61+
accessibilityRole={props.role ?? "button"}
62+
accessibilityLabel={option.accessibilityLabel ?? option.label}
63+
accessibilityState={{ selected }}
64+
onPress={() => props.onSelect(option.value)}
65+
className="flex-1 items-center justify-center rounded-full px-2 py-1 active:opacity-70"
66+
style={{ minHeight: minHeight - 6 }}
67+
>
68+
<AppText
69+
textRole="footnote"
70+
className={selected ? "font-t3-bold" : "font-t3-medium"}
71+
style={{
72+
color: selected ? selectedTextColor : mutedColor,
73+
}}
74+
>
75+
{option.label}
76+
</AppText>
77+
</Pressable>
78+
);
79+
})}
80+
</View>
81+
</View>
82+
);
83+
}
Lines changed: 1 addition & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1 @@
1-
import { useState } from "react";
2-
import { I18nManager, Platform, Pressable, View } from "react-native";
3-
import Animated, { useAnimatedStyle, useReducedMotion, withTiming } from "react-native-reanimated";
4-
5-
import {
6-
flattenThemeColor,
7-
themeColorWithAlpha,
8-
themeColorWithContrast,
9-
} from "../../lib/mobileTheme";
10-
import { AppText } from "./AppText";
11-
import { useControlTheme } from "./useControlTheme";
12-
import type { SegmentedControlProps } from "./SegmentedControl.types";
13-
14-
export type { SegmentedControlProps } from "./SegmentedControl.types";
15-
16-
export function SegmentedControl<Value extends string | number>(
17-
props: SegmentedControlProps<Value>,
18-
) {
19-
const { colorScheme, colors } = useControlTheme();
20-
const [layout, setLayout] = useState({ width: 0, height: 0 });
21-
const { width } = layout;
22-
const reduceMotion = useReducedMotion();
23-
const selectedIndex = props.options.findIndex((option) => option.value === props.selected);
24-
const segmentWidth = props.options.length ? Math.max(0, width - 6) / props.options.length : 0;
25-
const selectionOffset = selectedIndex * segmentWidth * (I18nManager.isRTL ? -1 : 1);
26-
const selectionStyle = useAnimatedStyle(() => ({
27-
width: segmentWidth,
28-
transform: [{ translateX: withTiming(selectionOffset, { duration: reduceMotion ? 0 : 160 }) }],
29-
}));
30-
const dark = colorScheme === "dark";
31-
const trackColor = flattenThemeColor(
32-
themeColorWithAlpha(colors["--color-prominent-action"], dark ? 0.18 : 0.07),
33-
colors["--color-screen"],
34-
);
35-
const selectedColor = colors[dark ? "--color-prominent-action" : "--color-secondary"];
36-
const selectedTextColor =
37-
colors[dark ? "--color-prominent-action-foreground" : "--color-secondary-foreground"];
38-
const mutedColor = themeColorWithContrast(
39-
colors["--color-foreground-muted"],
40-
trackColor,
41-
dark ? 7 : 4.5,
42-
);
43-
const minHeight = props.size === "compact" ? 32 : Platform.OS === "android" ? 40 : 36;
44-
45-
return (
46-
<View collapsable={false} className={props.className}>
47-
<View
48-
onLayout={(event) => setLayout(event.nativeEvent.layout)}
49-
className="flex-row rounded-full p-[3px]"
50-
style={{
51-
minHeight,
52-
backgroundColor: trackColor,
53-
}}
54-
>
55-
{selectedIndex >= 0 && width > 0 ? (
56-
<Animated.View
57-
pointerEvents="none"
58-
className="absolute bottom-[3px] start-[3px] top-[3px] rounded-full"
59-
style={[
60-
{
61-
backgroundColor: selectedColor,
62-
},
63-
selectionStyle,
64-
]}
65-
/>
66-
) : null}
67-
{props.options.map((option) => {
68-
const selected = option.value === props.selected;
69-
return (
70-
<Pressable
71-
key={`${typeof option.value}:${option.value}`}
72-
accessibilityRole={props.role ?? "button"}
73-
accessibilityLabel={option.accessibilityLabel ?? option.label}
74-
accessibilityState={{ selected }}
75-
onPress={() => props.onSelect(option.value)}
76-
className="flex-1 items-center justify-center rounded-full px-2 py-1 active:opacity-70"
77-
style={{ minHeight: minHeight - 6 }}
78-
>
79-
<AppText
80-
textRole="footnote"
81-
className={selected ? "font-t3-bold" : "font-t3-medium"}
82-
style={{
83-
color: selected ? selectedTextColor : mutedColor,
84-
}}
85-
>
86-
{option.label}
87-
</AppText>
88-
</Pressable>
89-
);
90-
})}
91-
</View>
92-
</View>
93-
);
94-
}
1+
export { SegmentedControl, type SegmentedControlProps } from "./SegmentedControl.shared";

‎apps/mobile/src/components/ui/SegmentedControl.types.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface SegmentedControlProps<Value extends number | string> {
88
readonly onSelect: (value: Value) => void;
99
/** Compact uses a 32-point capsule; default uses 36 points on iOS and 40 on Android. */
1010
readonly size?: "default" | "compact";
11+
readonly variant?: "standalone" | "card";
1112
readonly role?: "tab" | "button";
1213
readonly className?: string;
1314
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {
2+
flattenThemeColor,
3+
themeColorContrastRatio,
4+
themeColorWithAlpha,
5+
themeColorWithContrast,
6+
type MobileThemeAppearance,
7+
type MobileThemeVariables,
8+
} from "../../lib/mobileTheme";
9+
import type { SegmentedControlProps } from "./SegmentedControl.types";
10+
11+
export function segmentedControlColors(
12+
colors: MobileThemeVariables,
13+
appearance: MobileThemeAppearance,
14+
variant: SegmentedControlProps<string>["variant"],
15+
platform: string,
16+
) {
17+
const dark = appearance === "dark";
18+
const card = variant === "card";
19+
const trackColor = card
20+
? colors[dark ? "--color-screen" : "--color-grouped-card"]
21+
: flattenThemeColor(
22+
themeColorWithAlpha(colors["--color-prominent-action"], dark ? 0.18 : 0.07),
23+
colors["--color-screen"],
24+
);
25+
let selectedColor = colors["--color-secondary"];
26+
if (card && !dark) selectedColor = colors["--color-screen"];
27+
if (!card && dark) selectedColor = colors["--color-prominent-action"];
28+
let selectedTextColor =
29+
colors[!card && dark ? "--color-prominent-action-foreground" : "--color-secondary-foreground"];
30+
if (!card && !dark && platform === "android") {
31+
const selection = colors["--color-thread-selected"];
32+
if (
33+
themeColorContrastRatio(selection, trackColor) >
34+
themeColorContrastRatio(selectedColor, trackColor)
35+
) {
36+
selectedColor = selection;
37+
selectedTextColor = colors["--color-thread-selected-foreground"];
38+
}
39+
}
40+
return {
41+
trackColor,
42+
selectedColor,
43+
selectedTextColor: themeColorWithContrast(selectedTextColor, selectedColor),
44+
mutedColor: themeColorWithContrast(
45+
colors["--color-foreground-muted"],
46+
trackColor,
47+
dark && !card ? 7 : 4.5,
48+
),
49+
};
50+
}

‎apps/mobile/src/features/development/ComponentCatalogScreen.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,16 @@ export function ComponentCatalogScreen() {
104104
<View className="gap-3">
105105
<AppText className="text-lg font-t3-semibold">Lists and selection</AppText>
106106
<ListSection>
107+
<View className="p-4">
108+
<SegmentedControl
109+
variant="card"
110+
options={CHOICES}
111+
selected={tab}
112+
onSelect={setTab}
113+
size="compact"
114+
role="tab"
115+
/>
116+
</View>
107117
<ListRow icon="folder" label="Projects" detail="4" onPress={() => setSheet(true)} />
108118
<ListToggleRow
109119
icon="bell.badge"

‎apps/mobile/src/features/settings/SettingsScheduledTasksRouteScreen.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,7 @@ function TaskForm({
951951
<SettingsSection title="Schedule">
952952
<View className="px-4 py-3">
953953
<SegmentedControl
954+
variant="card"
954955
options={[
955956
{ value: "fixed_time", label: "At a time" },
956957
{ value: "interval", label: "Interval" },

‎apps/mobile/src/features/threads/CustomSnoozeSheet.android.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export function CustomSnoozeSheet(props: Props) {
144144
<Text style={titleTypography}>Custom snooze</Text>
145145
<RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
146146
<SegmentedControl
147+
variant="card"
147148
options={modes}
148149
selected={mode}
149150
onSelect={(value) => {
@@ -155,6 +156,7 @@ export function CustomSnoozeSheet(props: Props) {
155156
{mode === "date" ? (
156157
<RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
157158
<SegmentedControl
159+
variant="card"
158160
options={[
159161
{
160162
value: "date",
@@ -230,7 +232,12 @@ export function CustomSnoozeSheet(props: Props) {
230232
</FilledTonalIconButton>
231233
</Row>
232234
<RNHostView matchContents={{ vertical: true }} modifiers={[fillMaxWidth()]}>
233-
<SegmentedControl options={units} selected={unit} onSelect={setUnit} />
235+
<SegmentedControl
236+
variant="card"
237+
options={units}
238+
selected={unit}
239+
onSelect={setUnit}
240+
/>
234241
</RNHostView>
235242
</Column>
236243
)}

‎apps/mobile/src/lib/mobileTheme.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,12 @@ function contrastRatio(
209209
);
210210
}
211211

212+
export function themeColorContrastRatio(first: string, second: string): number {
213+
const firstChannels = rgbChannels(first);
214+
const secondChannels = rgbChannels(second);
215+
return firstChannels && secondChannels ? contrastRatio(firstChannels, secondChannels) : 1;
216+
}
217+
212218
export function themeColorWithContrast(
213219
accent: string,
214220
surface: string | ReadonlyArray<string>,

0 commit comments

Comments
 (0)