Open History to see the last commit
1import { useEffectEvent, useState } 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 // Gesture callbacks fire outside render, so effect events give them the
17 // latest props while the gesture object itself stays stable.
18 const onStart = useEffectEvent(() => props.onStart());
19 const onMove = useEffectEvent((translation: number) => props.onMove(translation));
20 const onEnd = useEffectEvent((translation: number, cancelled: boolean) =>
21 props.onEnd(translation, cancelled),
22 );
23 const [gesture] = useState(() => {
24 let translation = 0;
25 return Gesture.Pan()
26 .minDistance(0)
27 .shouldCancelWhenOutside(false)
28 .runOnJS(true)
29 .onStart(() => {
30 translation = 0;
31 onStart();
32 })
33 .onUpdate((event) => {
34 translation = event.translationY;
35 onMove(event.translationY);
36 })
37 .onFinalize((_, success) => onEnd(translation, !success));
38 });
39 return (
40 <GestureDetector gesture={gesture}>
41 <View
42 collapsable={false}
43 accessible
44 accessibilityRole="adjustable"
45 accessibilityLabel={`Reorder ${props.title}`}
46 accessibilityActions={[
47 ...(props.canMoveUp ? [{ name: "decrement", label: "Move up" }] : []),
48 ...(props.canMoveDown ? [{ name: "increment", label: "Move down" }] : []),
49 ]}
50 onAccessibilityAction={({ nativeEvent }) => {
51 if (nativeEvent.actionName === "decrement" && props.canMoveUp) props.onStep("up");
52 if (nativeEvent.actionName === "increment" && props.canMoveDown) props.onStep("down");
53 }}
54 style={{ width: 48, alignSelf: "stretch", alignItems: "center", justifyContent: "center" }}
55 >
56 <SymbolView
57 name="line.3.horizontal"
58 size={20}
59 tintColorClassName="accent-foreground-muted"
60 />
61 </View>
62 </GestureDetector>
63 );
64}