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

SettingsDragHandle.tsx

57 lines2.2 KB

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 const onStart = useEffectEvent(() => props.onStart());
17 const onMove = useEffectEvent((translation: number) => props.onMove(translation));
18 const onEnd = useEffectEvent((translation: number, cancelled: boolean) =>
19 props.onEnd(translation, cancelled),
20 );
21 // The worklets plugin copies the values these callbacks close over when the gesture is built,
22 // so the drop reads its translation from the final event rather than a tracked variable.
23 const [gesture] = useState(() =>
24 Gesture.Pan()
25 .minDistance(0)
26 .shouldCancelWhenOutside(false)
27 .runOnJS(true)
28 .onStart(() => onStart())
29 .onUpdate((event) => onMove(event.translationY))
30 .onFinalize((event, success) => onEnd(event.translationY, !success)),
31 );
32 return (
33 <GestureDetector gesture={gesture}>
34 <View
35 collapsable={false}
36 accessible
37 accessibilityRole="adjustable"
38 accessibilityLabel={`Reorder ${props.title}`}
39 accessibilityActions={[
40 ...(props.canMoveUp ? [{ name: "decrement", label: "Move up" }] : []),
41 ...(props.canMoveDown ? [{ name: "increment", label: "Move down" }] : []),
42 ]}
43 onAccessibilityAction={({ nativeEvent }) => {
44 if (nativeEvent.actionName === "decrement" && props.canMoveUp) props.onStep("up");
45 if (nativeEvent.actionName === "increment" && props.canMoveDown) props.onStep("down");
46 }}
47 style={{ width: 48, alignSelf: "stretch", alignItems: "center", justifyContent: "center" }}
48 >
49 <SymbolView
50 name="line.3.horizontal"
51 size={20}
52 tintColorClassName="accent-foreground-muted"
53 />
54 </View>
55 </GestureDetector>
56 );