Open History to see the last commit
1import { useLayoutEffect, useState } from "react";
2import { StyleSheet, View } from "react-native";
3import { GestureDetector, usePanGesture } from "react-native-gesture-handler";
4import Animated, {
5 cancelAnimation,
6 Easing,
7 FadeOut,
8 ReduceMotion,
9 runOnJS,
10 useAnimatedStyle,
11 useSharedValue,
12 withTiming,
13 type EntryAnimationsValues,
14} from "react-native-reanimated";
15import { useSafeAreaInsets } from "react-native-safe-area-context";
16import { Toast } from "../../components/ui/Toast";
17import { MOBILE_MOTION } from "../../lib/designTokens";
18import {
19 dismissThreadUndoNotice,
20 readThreadUndoNotice,
21 useThreadUndoNotice,
22} from "./thread-undo-notice";
23
24const SWIPE_TIMING = { duration: 160, reduceMotion: ReduceMotion.System };
25const ENTER_TIMING = {
26 duration: MOBILE_MOTION.enter,
27 easing: Easing.out(Easing.cubic),
28 reduceMotion: ReduceMotion.System,
29};
30const NOTICE_EXITING = FadeOut.duration(MOBILE_MOTION.exit).reduceMotion(ReduceMotion.System);
31const noticeEntering = (values: EntryAnimationsValues) => {
32 "worklet";
33 return {
34 initialValues: { originY: values.targetOriginY + values.targetHeight + 12 },
35 animations: { originY: withTiming(values.targetOriginY, ENTER_TIMING) },
36 };
37};
38
39export function ThreadUndoNotice(props: { readonly visible: boolean }) {
40 const notice = useThreadUndoNotice();
41 // Replacing a notice must also cancel the native gesture tracking the old one.
42 const [instance, setInstance] = useState({ notice, key: 0 });
43 if (instance.notice !== notice) setInstance({ notice, key: instance.key + 1 });
44 if (!props.visible || notice === null) return null;
45 return <SwipeableUndoNotice key={instance.key} notice={notice} />;
46}
47
48function SwipeableUndoNotice(props: {
49 readonly notice: NonNullable<ReturnType<typeof useThreadUndoNotice>>;
50}) {
51 const { notice } = props;
52 const insets = useSafeAreaInsets();
53 const translation = useSharedValue(0);
54 const width = useSharedValue(0);
55
56 useLayoutEffect(
57 () => () => {
58 cancelAnimation(translation);
59 },
60 [translation],
61 );
62
63 const dismiss = () => {
64 // A newer action can replace the notice while the swipe is animating out.
65 if (readThreadUndoNotice() === notice) dismissThreadUndoNotice();
66 };
67 const gesture = usePanGesture({
68 activeOffsetX: [-10, 10],
69 failOffsetY: [-12, 12],
70 onUpdate: (event) => {
71 translation.set(event.translationX);
72 },
73 onDeactivate: (event) => {
74 if (
75 !event.canceled &&
76 (Math.abs(event.translationX) > width.value * 0.25 ||
77 (Math.abs(event.velocityX) > 800 && event.translationX * event.velocityX > 0))
78 ) {
79 const direction = Math.sign(event.translationX || event.velocityX);
80 translation.set(
81 withTiming(direction * (width.value + 24), SWIPE_TIMING, (finished) => {
82 if (finished) runOnJS(dismiss)();
83 }),
84 );
85 } else {
86 translation.set(withTiming(0, SWIPE_TIMING));
87 }
88 },
89 });
90 const animatedStyle = useAnimatedStyle(() => ({
91 transform: [{ translateX: translation.value }],
92 }));
93
94 // Clip the entrance above navigation; the notice intentionally covers the anchored FAB.
95 return (
96 <Animated.View
97 className="absolute inset-x-3 z-[100] overflow-hidden"
98 style={{ bottom: insets.bottom + 12 }}
99 pointerEvents="box-none"
100 exiting={NOTICE_EXITING}
101 onLayout={(event) => {
102 width.set(event.nativeEvent.layout.width);
103 }}
104 >
105 <GestureDetector gesture={gesture}>
106 <Animated.View collapsable={false} entering={noticeEntering} style={animatedStyle}>
107 {/* Keep the dismiss target separate so TalkBack can also focus Undo. */}
108 <View
109 accessible
110 accessibilityRole="alert"
111 accessibilityLiveRegion="assertive"
112 accessibilityLabel={`${notice.action} thread`}
113 accessibilityActions={[{ name: "dismiss", label: "Dismiss message" }]}
114 onAccessibilityAction={({ nativeEvent }) => {
115 if (nativeEvent.actionName === "dismiss") dismiss();
116 }}
117 pointerEvents="none"
118 style={StyleSheet.absoluteFill}
119 />
120 <Toast
121 message={`${notice.action} thread`}
122 action={{ label: "Undo", onPress: () => void notice.undo() }}
123 />
124 </Animated.View>
125 </GestureDetector>
126 </Animated.View>
127 );
128}