Open History to see the last commit
1import { NativeThemeHost } from "./NativeThemeHost.android";
2import { Switch as ComposeSwitch } from "@expo/ui/jetpack-compose";
3import { Pressable, View } from "react-native";
4import type { ThemedSwitchProps } from "./NativeSwitch.types";
5
6import { useControlTheme } from "./useControlTheme";
7
8/**
9 * Material's native switch, with the same palette and accessibility contract as our RN controls.
10 * Compose only draws it. The RN Pressable owns the touch so it wins over a pressable parent row;
11 * with Compose handling touches, a tap on the switch ran the row's onPress instead.
12 */
13export function NativeSwitch(props: ThemedSwitchProps) {
14 const { colors } = useControlTheme();
15
16 return (
17 <Pressable
18 disabled={props.disabled}
19 onPress={() => props.onValueChange?.(!props.value)}
20 accessibilityLabel={props.accessibilityLabel}
21 accessibilityHint={props.accessibilityHint}
22 accessibilityRole="switch"
23 accessibilityState={{ checked: Boolean(props.value), disabled: Boolean(props.disabled) }}
24 style={props.style}
25 testID={props.testID}
26 >
27 <View importantForAccessibility="no-hide-descendants" pointerEvents="none">
28 <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ width: 52, height: 48 }}>
29 <ComposeSwitch
30 value={Boolean(props.value)}
31 enabled={!props.disabled}
32 colors={{
33 checkedTrackColor: colors["--color-switch-active-track"],
34 checkedThumbColor: colors["--color-switch-active-thumb"],
35 checkedBorderColor: colors["--color-switch-active-track"],
36 uncheckedTrackColor: colors["--color-switch-inactive-track"],
37 uncheckedThumbColor: colors["--color-switch-inactive-thumb"],
38 uncheckedBorderColor: colors["--color-switch-inactive-thumb"],
39 }}
40 />
41 </NativeThemeHost>
42 </View>
43 </Pressable>
44 );
45}