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

SettingsDragHandle.tsx

63 lines2.1 KB

1import { useEffect, useMemo, useRef } from "react";
2import { View } from "react-native";
3import { Gesture, GestureDetector } from "react-native-gesture-handler";
4​
5import { SymbolView } from "../../../components/ui/AppSymbol";
6​
7export function SettingsDragHandle(props: {
8 readonly title: string;
9 readonly canMoveUp: boolean;
10 readonly canMoveDown: boolean;
11 readonly onStart: () => void;
12 readonly onMove: (translation: number) => void;
13 readonly onEnd: (translation: number, cancelled: boolean) => void;
14 readonly onStep: (direction: "up" | "down") => void;
15}) {
16 const latest = useRef(props);
17 useEffect(() => {
18 latest.current = props;
19 });
20 const translation = useRef(0);
21 const gesture = useMemo(
22 () =>
23 Gesture.Pan()
24 .minDistance(0)
25 .shouldCancelWhenOutside(false)
26 .runOnJS(true)
27 .onStart(() => {
28 translation.current = 0;
29 latest.current.onStart();
30 })
31 .onUpdate((event) => {
32 translation.current = event.translationY;
33 latest.current.onMove(event.translationY);
34 })
35 .onFinalize((_, success) => latest.current.onEnd(translation.current, !success)),
36 [],
37 );
38 return (
39 <GestureDetector gesture={gesture}>
40 <View
41 collapsable={false}
42 accessible
43 accessibilityRole="adjustable"
44 accessibilityLabel={`Reorder ${props.title}`}
45 accessibilityActions={[
46 ...(props.canMoveUp ? [{ name: "decrement", label: "Move up" }] : []),
47 ...(props.canMoveDown ? [{ name: "increment", label: "Move down" }] : []),
48 ]}
49 onAccessibilityAction={({ nativeEvent }) => {
50 if (nativeEvent.actionName === "decrement" && props.canMoveUp) props.onStep("up");
51 if (nativeEvent.actionName === "increment" && props.canMoveDown) props.onStep("down");
52 }}
53 style={{ width: 48, alignSelf: "stretch", alignItems: "center", justifyContent: "center" }}
54 >
55 <SymbolView
56 name="line.3.horizontal"
57 size={20}
58 tintColorClassName="accent-foreground-muted"
59 />
60 </View>
61 </GestureDetector>
62 );