Open History to see the last commit
1import { Picker } from "@expo/ui/swift-ui";
2import { View } from "react-native";
3
4import { useControlTheme } from "./useControlTheme";
5import { NativeThemeHost } from "./NativeThemeHost";
6import type { SegmentedControlProps } from "./SegmentedControl.types";
7
8export type { SegmentedControlProps } from "./SegmentedControl.types";
9
10/** Let UIKit own segment sizing, selection, and accessibility through SwiftUI. */
11export function SegmentedControl<Value extends string | number>(
12 props: SegmentedControlProps<Value>,
13) {
14 const { colors } = useControlTheme();
15 return (
16 <View className={props.className}>
17 <View accessibilityElementsHidden>
18 <NativeThemeHost matchContents={{ vertical: true }} style={{ width: "100%" }}>
19 <Picker
20 selection={props.selected}
21 onSelectionChange={props.onSelect}
22 segments={props.options.map((option) => ({
23 label: option.label,
24 value: option.value,
25 }))}
26 segmentColors={{
27 background: colors["--color-card"],
28 selectedBackground: colors["--color-secondary"],
29 text: colors["--color-foreground-muted"],
30 selectedText: colors["--color-secondary-foreground"],
31 }}
32 />
33 </NativeThemeHost>
34 </View>
35 <View pointerEvents="none" className="absolute inset-0 flex-row">
36 {props.options.map((option) => (
37 <View
38 key={String(option.value)}
39 accessible
40 accessibilityRole={props.role ?? "button"}
41 accessibilityLabel={option.accessibilityLabel ?? option.label}
42 accessibilityState={{ selected: option.value === props.selected }}
43 accessibilityActions={[{ name: "activate" }]}
44 onAccessibilityAction={() => props.onSelect(option.value)}
45 className="flex-1"
46 />
47 ))}
48 </View>
49 </View>
50 );
51}