Skip to content

Commit e58a485

Browse files
feat(mobile): use themed native iOS segmented controls (#20327)
1 parent cfafb4b commit e58a485

3 files changed

Lines changed: 124 additions & 0 deletions

File tree

‎apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ public final class T3NativeControlsModule: Module {
2020
}
2121
return ["supportsWorkspaceColumns": false]
2222
}
23+
View(T3SegmentedControlView.self) {
24+
ViewName("SegmentedControl")
25+
Events("onSelectionChange")
26+
Prop("labels") { (view: T3SegmentedControlView, value: [String]) in view.labels = value }
27+
Prop("selectedIndex") { (view: T3SegmentedControlView, value: Int) in view.selectedIndex = value }
28+
Prop("trackColor") { (view: T3SegmentedControlView, value: UIColor) in view.trackColor = value }
29+
Prop("selectedColor") { (view: T3SegmentedControlView, value: UIColor) in view.selectedColor = value }
30+
Prop("textColor") { (view: T3SegmentedControlView, value: UIColor) in view.textColor = value }
31+
Prop("mutedColor") { (view: T3SegmentedControlView, value: UIColor) in view.mutedColor = value }
32+
Prop("appearance") { (view: T3SegmentedControlView, value: String) in view.appearance = value }
33+
}
34+
2335
View(T3LayoutMetricsView.self) {
2436
ViewName("LayoutMetrics")
2537
Events("onMetricsChange")
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import ExpoModulesCore
2+
import UIKit
3+
4+
final class T3SegmentedControlView: ExpoView {
5+
let onSelectionChange = EventDispatcher()
6+
private let control = UISegmentedControl()
7+
var labels: [String] = [] {
8+
didSet {
9+
guard labels != oldValue else { return }
10+
control.removeAllSegments()
11+
for (index, label) in labels.enumerated() {
12+
control.insertSegment(withTitle: label, at: index, animated: false)
13+
}
14+
updateSelection()
15+
}
16+
}
17+
var selectedIndex = -1 { didSet { updateSelection() } }
18+
var trackColor: UIColor = .secondarySystemBackground { didSet { control.backgroundColor = trackColor } }
19+
var selectedColor: UIColor = .secondarySystemFill { didSet { control.selectedSegmentTintColor = selectedColor } }
20+
var textColor: UIColor = .label { didSet { updateColors() } }
21+
var mutedColor: UIColor = .secondaryLabel { didSet { updateColors() } }
22+
var appearance = "light" {
23+
didSet { overrideUserInterfaceStyle = appearance == "dark" ? .dark : .light }
24+
}
25+
26+
required init(appContext: AppContext? = nil) {
27+
super.init(appContext: appContext)
28+
addSubview(control)
29+
control.addTarget(self, action: #selector(selectionChanged), for: .valueChanged)
30+
}
31+
32+
override func layoutSubviews() {
33+
super.layoutSubviews()
34+
control.frame = bounds
35+
}
36+
37+
private func updateSelection() {
38+
control.selectedSegmentIndex = labels.indices.contains(selectedIndex) ? selectedIndex : UISegmentedControl.noSegment
39+
}
40+
41+
private func updateColors() {
42+
control.setTitleTextAttributes([.foregroundColor: mutedColor], for: .normal)
43+
control.setTitleTextAttributes([.foregroundColor: textColor], for: .selected)
44+
}
45+
46+
@objc private func selectionChanged() {
47+
onSelectionChange(["index": control.selectedSegmentIndex])
48+
}
49+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { requireNativeView } from "expo";
2+
import { View, type NativeSyntheticEvent, type ViewProps } from "react-native";
3+
4+
import {
5+
flattenThemeColor,
6+
themeColorWithAlpha,
7+
themeColorWithContrast,
8+
} from "../../lib/mobileTheme";
9+
import { useControlTheme } from "./useControlTheme";
10+
import type { SegmentedControlProps } from "./SegmentedControl.types";
11+
12+
export type { SegmentedControlProps } from "./SegmentedControl.types";
13+
14+
const NativeSegmentedControl = requireNativeView<
15+
ViewProps & {
16+
labels: string[];
17+
selectedIndex: number;
18+
trackColor: string;
19+
selectedColor: string;
20+
textColor: string;
21+
mutedColor: string;
22+
appearance: string;
23+
onSelectionChange: (event: NativeSyntheticEvent<{ index: number }>) => void;
24+
}
25+
>("T3NativeControls", "SegmentedControl");
26+
27+
export function SegmentedControl<Value extends string | number>(
28+
props: SegmentedControlProps<Value>,
29+
) {
30+
const { colors, colorScheme } = useControlTheme();
31+
const trackColor = flattenThemeColor(
32+
themeColorWithAlpha(colors["--color-primary"], colorScheme === "dark" ? 0.18 : 0.07),
33+
colors["--color-screen"],
34+
);
35+
const selectedColor =
36+
colorScheme === "dark" ? colors["--color-primary"] : colors["--color-secondary"];
37+
const selectedTextColor =
38+
colorScheme === "dark"
39+
? colors["--color-primary-foreground"]
40+
: colors["--color-secondary-foreground"];
41+
return (
42+
<View className={props.className}>
43+
<NativeSegmentedControl
44+
labels={props.options.map((option) => option.label)}
45+
selectedIndex={props.options.findIndex((option) => option.value === props.selected)}
46+
trackColor={trackColor}
47+
selectedColor={selectedColor}
48+
textColor={selectedTextColor}
49+
mutedColor={themeColorWithContrast(
50+
colors["--color-foreground-muted"],
51+
trackColor,
52+
colorScheme === "dark" ? 7 : 4.5,
53+
)}
54+
appearance={colorScheme}
55+
onSelectionChange={({ nativeEvent }) => {
56+
const option = props.options[nativeEvent.index];
57+
if (option) props.onSelect(option.value);
58+
}}
59+
style={{ height: props.size === "compact" ? 32 : 36 }}
60+
/>
61+
</View>
62+
);
63+
}

0 commit comments

Comments
 (0)