Skip to content
TypeScript

feat(mobile): use themed native iOS segmented controls (#20327)

Julius Marmingee58a4857130a
diff --git a/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift b/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift
index 3289fe90363..57fa2a7c643 100644
--- a/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift
+++ b/apps/mobile/modules/t3-native-controls/ios/T3NativeControlsModule.swift
@@ -20,6 +20,18 @@ public final class T3NativeControlsModule: Module {
}
return ["supportsWorkspaceColumns": false]
}
+ View(T3SegmentedControlView.self) {
+ ViewName("SegmentedControl")
+ Events("onSelectionChange")
+ Prop("labels") { (view: T3SegmentedControlView, value: [String]) in view.labels = value }
+ Prop("selectedIndex") { (view: T3SegmentedControlView, value: Int) in view.selectedIndex = value }
+ Prop("trackColor") { (view: T3SegmentedControlView, value: UIColor) in view.trackColor = value }
+ Prop("selectedColor") { (view: T3SegmentedControlView, value: UIColor) in view.selectedColor = value }
+ Prop("textColor") { (view: T3SegmentedControlView, value: UIColor) in view.textColor = value }
+ Prop("mutedColor") { (view: T3SegmentedControlView, value: UIColor) in view.mutedColor = value }
+ Prop("appearance") { (view: T3SegmentedControlView, value: String) in view.appearance = value }
+ }
+
View(T3LayoutMetricsView.self) {
ViewName("LayoutMetrics")
Events("onMetricsChange")
diff --git a/apps/mobile/modules/t3-native-controls/ios/T3SegmentedControlView.swift b/apps/mobile/modules/t3-native-controls/ios/T3SegmentedControlView.swift
new file mode 100644
index 00000000000..9e60a85b7d2
--- /dev/null
+++ b/apps/mobile/modules/t3-native-controls/ios/T3SegmentedControlView.swift
@@ -0,0 +1,49 @@
+import ExpoModulesCore
+import UIKit
+
+final class T3SegmentedControlView: ExpoView {
+ let onSelectionChange = EventDispatcher()
+ private let control = UISegmentedControl()
+ var labels: [String] = [] {
+ didSet {
+ guard labels != oldValue else { return }
+ control.removeAllSegments()
+ for (index, label) in labels.enumerated() {
+ control.insertSegment(withTitle: label, at: index, animated: false)
+ }
+ updateSelection()
+ }
+ }
+ var selectedIndex = -1 { didSet { updateSelection() } }
+ var trackColor: UIColor = .secondarySystemBackground { didSet { control.backgroundColor = trackColor } }
+ var selectedColor: UIColor = .secondarySystemFill { didSet { control.selectedSegmentTintColor = selectedColor } }
+ var textColor: UIColor = .label { didSet { updateColors() } }
+ var mutedColor: UIColor = .secondaryLabel { didSet { updateColors() } }
+ var appearance = "light" {
+ didSet { overrideUserInterfaceStyle = appearance == "dark" ? .dark : .light }
+ }
+
+ required init(appContext: AppContext? = nil) {
+ super.init(appContext: appContext)
+ addSubview(control)
+ control.addTarget(self, action: #selector(selectionChanged), for: .valueChanged)
+ }
+
+ override func layoutSubviews() {
+ super.layoutSubviews()
+ control.frame = bounds
+ }
+
+ private func updateSelection() {
+ control.selectedSegmentIndex = labels.indices.contains(selectedIndex) ? selectedIndex : UISegmentedControl.noSegment
+ }
+
+ private func updateColors() {
+ control.setTitleTextAttributes([.foregroundColor: mutedColor], for: .normal)
+ control.setTitleTextAttributes([.foregroundColor: textColor], for: .selected)
+ }
+
+ @objc private func selectionChanged() {
+ onSelectionChange(["index": control.selectedSegmentIndex])
+ }
+}
diff --git a/apps/mobile/src/components/ui/SegmentedControl.ios.tsx b/apps/mobile/src/components/ui/SegmentedControl.ios.tsx
new file mode 100644
index 00000000000..d9a75166483
--- /dev/null
+++ b/apps/mobile/src/components/ui/SegmentedControl.ios.tsx
@@ -0,0 +1,63 @@
+import { requireNativeView } from "expo";
+import { View, type NativeSyntheticEvent, type ViewProps } from "react-native";
+
+import {
+ flattenThemeColor,
+ themeColorWithAlpha,
+ themeColorWithContrast,
+} from "../../lib/mobileTheme";
+import { useControlTheme } from "./useControlTheme";
+import type { SegmentedControlProps } from "./SegmentedControl.types";
+
+export type { SegmentedControlProps } from "./SegmentedControl.types";
+
+const NativeSegmentedControl = requireNativeView<
+ ViewProps & {
+ labels: string[];
+ selectedIndex: number;
+ trackColor: string;
+ selectedColor: string;
+ textColor: string;
+ mutedColor: string;
+ appearance: string;
+ onSelectionChange: (event: NativeSyntheticEvent<{ index: number }>) => void;
+ }
+>("T3NativeControls", "SegmentedControl");
+
+export function SegmentedControl<Value extends string | number>(
+ props: SegmentedControlProps<Value>,
+) {
+ const { colors, colorScheme } = useControlTheme();
+ const trackColor = flattenThemeColor(
+ themeColorWithAlpha(colors["--color-primary"], colorScheme === "dark" ? 0.18 : 0.07),
+ colors["--color-screen"],
+ );
+ const selectedColor =
+ colorScheme === "dark" ? colors["--color-primary"] : colors["--color-secondary"];
+ const selectedTextColor =
+ colorScheme === "dark"
+ ? colors["--color-primary-foreground"]
+ : colors["--color-secondary-foreground"];
+ return (
+ <View className={props.className}>
+ <NativeSegmentedControl
+ labels={props.options.map((option) => option.label)}
+ selectedIndex={props.options.findIndex((option) => option.value === props.selected)}
+ trackColor={trackColor}
+ selectedColor={selectedColor}
+ textColor={selectedTextColor}
+ mutedColor={themeColorWithContrast(
+ colors["--color-foreground-muted"],
+ trackColor,
+ colorScheme === "dark" ? 7 : 4.5,
+ )}
+ appearance={colorScheme}
+ onSelectionChange={({ nativeEvent }) => {
+ const option = props.options[nativeEvent.index];
+ if (option) props.onSelect(option.value);
+ }}
+ style={{ height: props.size === "compact" ? 32 : 36 }}
+ />
+ </View>
+ );
+}