Open History to see the last commit
1import {
2 Host,
3 SegmentedButton,
4 SingleChoiceSegmentedButtonRow,
5 Text,
6} from "@expo/ui/jetpack-compose";
7import { defaultMinSize, fillMaxWidth } from "@expo/ui/jetpack-compose/modifiers";
8import { useAppearancePreferences } from "../../features/settings/appearance/AppearancePreferencesProvider";
9import { useScaledTextRole } from "../../features/settings/appearance/useScaledTextRole";
10import { View } from "react-native";
11
12import { useControlTheme } from "./useControlTheme";
13import type { SegmentedControlProps } from "./SegmentedControl.types";
14
15function HostedSegmentedControl<Value extends number | string>(
16 props: SegmentedControlProps<Value>,
17) {
18 const { colorScheme } = useControlTheme();
19 return (
20 <View className={props.className}>
21 <View importantForAccessibility="no-hide-descendants">
22 <Host
23 matchContents={{ vertical: true }}
24 colorScheme={colorScheme}
25 ignoreSafeAreaKeyboardInsets
26 style={{ width: "100%" }}
27 >
28 <SegmentedControlContent {...props} />
29 </Host>
30 </View>
31 <View pointerEvents="none" className="absolute inset-0 flex-row">
32 {props.options.map((option) => (
33 <View
34 key={String(option.value)}
35 accessible
36 accessibilityRole={props.role ?? "button"}
37 accessibilityLabel={option.accessibilityLabel ?? option.label}
38 accessibilityState={{ selected: option.value === props.selected }}
39 accessibilityActions={[{ name: "activate" }]}
40 onAccessibilityAction={() => props.onSelect(option.value)}
41 className="flex-1"
42 />
43 ))}
44 </View>
45 </View>
46 );
47}
48
49export type { SegmentedControlProps } from "./SegmentedControl.types";
50
51/** Compose content shared by screen controls and native dialogs, inside their existing Host. */
52function SegmentedControlContent<Value extends number | string>(
53 props: SegmentedControlProps<Value>,
54) {
55 const { themeVariables: colors } = useAppearancePreferences();
56 const typography = useScaledTextRole("footnote");
57 return (
58 <SingleChoiceSegmentedButtonRow modifiers={[fillMaxWidth()]}>
59 {props.options.map((option) => (
60 <SegmentedButton
61 key={String(option.value)}
62 selected={option.value === props.selected}
63 onClick={() => props.onSelect(option.value)}
64 modifiers={[defaultMinSize({ minHeight: 48 })]}
65 colors={{
66 activeContainerColor: colors["--color-secondary"],
67 activeContentColor: colors["--color-secondary-foreground"],
68 inactiveContainerColor: "transparent",
69 inactiveContentColor: colors["--color-foreground"],
70 activeBorderColor: colors["--color-border"],
71 inactiveBorderColor: colors["--color-border"],
72 }}
73 >
74 <SegmentedButton.Label>
75 <Text style={typography}>{option.label}</Text>
76 </SegmentedButton.Label>
77 </SegmentedButton>
78 ))}
79 </SingleChoiceSegmentedButtonRow>
80 );
81}
82
83/** Content is for callers already inside an Android native Host. */
84export const SegmentedControl = Object.assign(HostedSegmentedControl, {
85 Content: SegmentedControlContent,
86});