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

SliderRow.android.tsx

82 lines3.1 KB

1import { Slider } from "@expo/ui/jetpack-compose";
2import { fillMaxWidth } from "@expo/ui/jetpack-compose/modifiers";
3import * as Haptics from "expo-haptics";
4import { useEffect, useRef } from "react";
5import { View } from "react-native";
6​
7import { AppText as Text } from "./AppText";
8import { SymbolView } from "./AppSymbol";
9import { NativeThemeHost } from "./NativeThemeHost.android";
10import { useControlTheme } from "./useControlTheme";
11import type { SliderRowProps } from "./SliderRow.types";
12​
13export function SliderRow(props: SliderRowProps) {
14 const { colors } = useControlTheme();
15 const draft = useRef(props.value);
16 const dragging = useRef(false);
17 useEffect(() => {
18 if (!dragging.current) draft.current = props.value;
19 }, [props.value]);
20 const commit = (value: number) => {
21 if (props.disabled) return;
22 const next = Math.min(
23 props.max,
24 Math.max(props.min, props.min + Math.round((value - props.min) / props.step) * props.step),
25 );
26 if (next === props.value) return;
27 void Haptics.selectionAsync().catch(() => undefined);
28 props.onChange(next);
29 };
30 return (
31 <View className={props.disabled ? "gap-2 p-4 opacity-45" : "gap-2 p-4"}>
32 <View className="flex-row items-center gap-4">
33 <SymbolView name={props.icon} size={24} tintColorClassName="accent-icon" />
34 <Text className="min-w-0 flex-1 text-base">{props.label}</Text>
35 <Text className="text-sm text-foreground-muted">{props.valueLabel}</Text>
36 </View>
37 <View
38 accessible
39 accessibilityLabel={props.label}
40 accessibilityRole="adjustable"
41 accessibilityState={{ disabled: Boolean(props.disabled) }}
42 accessibilityValue={{
43 min: props.min,
44 max: props.max,
45 now: props.value,
46 text: props.valueLabel,
47 }}
48 accessibilityActions={[{ name: "increment" }, { name: "decrement" }]}
49 onAccessibilityAction={({ nativeEvent }) => {
50 if (nativeEvent.actionName === "increment") commit(props.value + props.step);
51 else if (nativeEvent.actionName === "decrement") commit(props.value - props.step);
52 }}
53 >
54 <View importantForAccessibility="no-hide-descendants">
55 <NativeThemeHost style={{ height: 48, width: "100%" }}>
56 <Slider
57 min={props.min}
58 max={props.max}
59 value={props.value}
60 steps={Math.max(0, Math.round((props.max - props.min) / props.step) - 1)}
61 enabled={!props.disabled}
62 modifiers={[fillMaxWidth()]}
63 colors={{
64 thumbColor: colors["--color-primary"],
65 activeTrackColor: colors["--color-primary"],
66 inactiveTrackColor: colors["--color-secondary"],
67 }}
68 onValueChange={(value) => {
69 dragging.current = true;
70 draft.current = value;
71 }}
72 onValueChangeFinished={() => {
73 dragging.current = false;
74 commit(draft.current);
75 }}
76 />
77 </NativeThemeHost>
78 </View>
79 </View>
80 </View>
81 );