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

SettingsDragHandle.tsx

62 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 const [gesture] = useState(() => {
22 let translation = 0;
23 return Gesture.Pan()
24 .minDistance(0)
25 .shouldCancelWhenOutside(false)
26 .runOnJS(true)
27 .onStart(() => {
28 translation = 0;
29 onStart();
30 })
31 .onUpdate((event) => {
32 translation = event.translationY;
33 onMove(event.translationY);
34 })
35 .onFinalize((_, success) => onEnd(translation, !success));
36 });
37 return (
38 <GestureDetector gesture={gesture}>
39 <View
40 collapsable={false}
41 accessible
42 accessibilityRole="adjustable"
43 accessibilityLabel={`Reorder ${props.title}`}
44 accessibilityActions={[
45 ...(props.canMoveUp ? [{ name: "decrement", label: "Move up" }] : []),
46 ...(props.canMoveDown ? [{ name: "increment", label: "Move down" }] : []),
47 ]}
48 onAccessibilityAction={({ nativeEvent }) => {
49 if (nativeEvent.actionName === "decrement" && props.canMoveUp) props.onStep("up");
50 if (nativeEvent.actionName === "increment" && props.canMoveDown) props.onStep("down");
51 }}
52 style={{ width: 48, alignSelf: "stretch", alignItems: "center", justifyContent: "center" }}
53 >
54 <SymbolView
55 name="line.3.horizontal"
56 size={20}
57 tintColorClassName="accent-foreground-muted"
58 />
59 </View>
60 </GestureDetector>
61 );