sailfork / t3code-github / apps/ mobile/ src/ features/ settings/ components/ SettingsDragHandle.tsx

import { useEffectEvent, useState } from "react";
import { View } from "react-native";
import { Gesture, GestureDetector } from "react-native-gesture-handler";

import { SymbolView } from "../../../components/ui/AppSymbol";

export function SettingsDragHandle(props: {
  readonly title: string;
  readonly canMoveUp: boolean;
  readonly canMoveDown: boolean;
  readonly onStart: () => void;
  readonly onMove: (translation: number) => void;
  readonly onEnd: (translation: number, cancelled: boolean) => void;
  readonly onStep: (direction: "up" | "down") => void;
}) {
  const onStart = useEffectEvent(() => props.onStart());
  const onMove = useEffectEvent((translation: number) => props.onMove(translation));
  const onEnd = useEffectEvent((translation: number, cancelled: boolean) =>
    props.onEnd(translation, cancelled),
  );
  // The worklets plugin copies the values these callbacks close over when the gesture is built,
  // so the drop reads its translation from the final event rather than a tracked variable.
  const [gesture] = useState(() =>
    Gesture.Pan()
      .minDistance(0)
      .shouldCancelWhenOutside(false)
      .runOnJS(true)
      .onStart(() => onStart())
      .onUpdate((event) => onMove(event.translationY))
      .onFinalize((event, success) => onEnd(event.translationY, !success)),
  );
  return (
    <GestureDetector gesture={gesture}>
      <View
        collapsable={false}
        accessible
        accessibilityRole="adjustable"
        accessibilityLabel={`Reorder ${props.title}`}
        accessibilityActions={[
          ...(props.canMoveUp ? [{ name: "decrement", label: "Move up" }] : []),
          ...(props.canMoveDown ? [{ name: "increment", label: "Move down" }] : []),
        ]}
        onAccessibilityAction={({ nativeEvent }) => {
          if (nativeEvent.actionName === "decrement" && props.canMoveUp) props.onStep("up");
          if (nativeEvent.actionName === "increment" && props.canMoveDown) props.onStep("down");
        }}
        style={{ width: 48, alignSelf: "stretch", alignItems: "center", justifyContent: "center" }}
      >
        <SymbolView
          name="line.3.horizontal"
          size={20}
          tintColorClassName="accent-foreground-muted"
        />
      </View>
    </GestureDetector>
  );
}