Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

NativeSwitch.android.tsx

46 lines1.8 KB

1import { NativeThemeHost } from "./NativeThemeHost.android";
2import { Switch as ComposeSwitch } from "@expo/ui/jetpack-compose";
3import { View } from "react-native";
4import type { ThemedSwitchProps } from "./NativeSwitch.types";
5​
6import { useControlTheme } from "./useControlTheme";
7​
8/** Material's native switch, with the same palette and accessibility contract as our RN controls. */
9export function NativeSwitch(props: ThemedSwitchProps) {
10 const { colors } = useControlTheme();
11 const toggle = () => {
12 if (!props.disabled) props.onValueChange?.(!props.value);
13 };
14​
15 return (
16 <View
17 accessible
18 accessibilityLabel={props.accessibilityLabel}
19 accessibilityHint={props.accessibilityHint}
20 accessibilityRole="switch"
21 accessibilityState={{ checked: Boolean(props.value), disabled: Boolean(props.disabled) }}
22 accessibilityActions={[{ name: "activate", label: "Toggle" }]}
23 onAccessibilityAction={toggle}
24 style={props.style}
25 testID={props.testID}
26 >
27 <View importantForAccessibility="no-hide-descendants">
28 <NativeThemeHost ignoreSafeAreaKeyboardInsets style={{ width: 52, height: 48 }}>
29 <ComposeSwitch
30 value={Boolean(props.value)}
31 enabled={!props.disabled}
32 onCheckedChange={props.onValueChange ?? undefined}
33 colors={{
34 checkedTrackColor: colors["--color-switch-active-track"],
35 checkedThumbColor: colors["--color-switch-active-thumb"],
36 checkedBorderColor: colors["--color-switch-active-track"],
37 uncheckedTrackColor: colors["--color-switch-inactive-track"],
38 uncheckedThumbColor: colors["--color-switch-inactive-thumb"],
39 uncheckedBorderColor: colors["--color-switch-inactive-thumb"],
40 }}
41 />
42 </NativeThemeHost>
43 </View>
44 </View>
45 );