Skip to content

Commit f9d61c8

Browse files
committed
fix(mobile): move Android undo toasts above bottom navigation
1 parent 18e2742 commit f9d61c8

3 files changed

Lines changed: 119 additions & 2 deletions

File tree

‎apps/mobile/src/features/home/AndroidHomeFab.android.tsx‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ import { useWorkspaceState } from "../../state/workspace";
1515
import { FabScrollContext } from "./FabScrollContext";
1616
import { updateFabScroll } from "./fab-scroll";
1717
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
18+
import { ThreadUndoNoticeHeightContext } from "./thread-undo-notice-layout";
1819

1920
export function AndroidHomeFabLayout(props: ComponentProps<typeof SharedAndroidHomeFabLayout>) {
2021
const insets = useSafeAreaInsets();
2122
const { appearance } = useAppearancePreferences();
2223
const { fontScale } = useWindowDimensions();
2324
const [layoutWidth, setLayoutWidth] = useState<number | null>(null);
25+
const [undoNoticeHeight, setUndoNoticeHeight] = useState(0);
2426
const { state } = useWorkspaceState();
2527
const [expanded, setExpanded] = useState(true);
2628
const scrollState = useRef({ anchor: 0, expanded: true });
@@ -40,15 +42,20 @@ export function AndroidHomeFabLayout(props: ComponentProps<typeof SharedAndroidH
4042
// keeps Material's 16dp corner margin; the bottom bar already consumes the gesture inset.
4143
return (
4244
<View className="flex-1" onLayout={(event) => setLayoutWidth(event.nativeEvent.layout.width)}>
43-
<FabScrollContext value={onScroll}>{props.children}</FabScrollContext>
45+
<ThreadUndoNoticeHeightContext value={setUndoNoticeHeight}>
46+
<FabScrollContext value={onScroll}>{props.children}</FabScrollContext>
47+
</ThreadUndoNoticeHeightContext>
4448
{!props.disabled && !props.sidebar && state.hasConnections && layoutWidth !== null ? (
4549
<NewThreadButton
4650
key={`${appearance.baseFontSize}:${fontScale}:${layoutWidth}`}
4751
extended
4852
expanded={expanded}
4953
onPress={props.onStartNewTask}
5054
className="absolute"
51-
style={androidHomeFabPosition(insets.bottom, props.sidebar)}
55+
style={[
56+
androidHomeFabPosition(insets.bottom, props.sidebar),
57+
undoNoticeHeight > 0 ? { bottom: insets.bottom + undoNoticeHeight + 24 } : null,
58+
]}
5259
/>
5360
) : null}
5461
</View>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { use, useLayoutEffect, useState } from "react";
2+
import { View } from "react-native";
3+
import { GestureDetector, usePanGesture } from "react-native-gesture-handler";
4+
import Animated, {
5+
cancelAnimation,
6+
ReduceMotion,
7+
runOnJS,
8+
useAnimatedStyle,
9+
useSharedValue,
10+
withTiming,
11+
} from "react-native-reanimated";
12+
import { useSafeAreaInsets } from "react-native-safe-area-context";
13+
import { Toast } from "../../components/ui/Toast";
14+
import {
15+
dismissThreadUndoNotice,
16+
readThreadUndoNotice,
17+
useThreadUndoNotice,
18+
} from "./thread-undo-notice";
19+
import { ThreadUndoNoticeHeightContext } from "./thread-undo-notice-layout";
20+
21+
const SWIPE_TIMING = { duration: 160, reduceMotion: ReduceMotion.System };
22+
23+
export function ThreadUndoNotice(props: { readonly visible: boolean }) {
24+
const notice = useThreadUndoNotice();
25+
// Replacing a notice must also cancel the native gesture tracking the old one.
26+
const [instance, setInstance] = useState({ notice, key: 0 });
27+
if (instance.notice !== notice) setInstance({ notice, key: instance.key + 1 });
28+
if (!props.visible || notice === null) return null;
29+
return <SwipeableUndoNotice key={instance.key} notice={notice} />;
30+
}
31+
32+
function SwipeableUndoNotice(props: {
33+
readonly notice: NonNullable<ReturnType<typeof useThreadUndoNotice>>;
34+
}) {
35+
const { notice } = props;
36+
const insets = useSafeAreaInsets();
37+
const setHeight = use(ThreadUndoNoticeHeightContext);
38+
const translation = useSharedValue(0);
39+
const width = useSharedValue(0);
40+
41+
useLayoutEffect(
42+
() => () => {
43+
cancelAnimation(translation);
44+
setHeight?.(0);
45+
},
46+
[setHeight, translation],
47+
);
48+
49+
const dismiss = () => {
50+
// A newer action can replace the notice while the swipe is animating out.
51+
if (readThreadUndoNotice() === notice) dismissThreadUndoNotice();
52+
};
53+
const gesture = usePanGesture({
54+
activeOffsetX: [-10, 10],
55+
failOffsetY: [-12, 12],
56+
onUpdate: (event) => {
57+
translation.set(event.translationX);
58+
},
59+
onDeactivate: (event) => {
60+
if (
61+
!event.canceled &&
62+
(Math.abs(event.translationX) > width.value * 0.25 ||
63+
(Math.abs(event.velocityX) > 800 && event.translationX * event.velocityX > 0))
64+
) {
65+
const direction = Math.sign(event.translationX || event.velocityX);
66+
translation.set(
67+
withTiming(direction * (width.value + 24), SWIPE_TIMING, (finished) => {
68+
if (finished) runOnJS(dismiss)();
69+
}),
70+
);
71+
} else {
72+
translation.set(withTiming(0, SWIPE_TIMING));
73+
}
74+
},
75+
});
76+
const animatedStyle = useAnimatedStyle(() => ({
77+
transform: [{ translateX: translation.value }],
78+
}));
79+
80+
return (
81+
<View
82+
className="absolute inset-x-3 z-[100]"
83+
style={{ bottom: insets.bottom + 12 }}
84+
pointerEvents="box-none"
85+
onLayout={(event) => {
86+
width.set(event.nativeEvent.layout.width);
87+
setHeight?.(event.nativeEvent.layout.height);
88+
}}
89+
>
90+
<GestureDetector gesture={gesture}>
91+
<Animated.View
92+
collapsable={false}
93+
style={animatedStyle}
94+
accessibilityActions={[{ name: "dismiss", label: "Dismiss message" }]}
95+
onAccessibilityAction={({ nativeEvent }) => {
96+
if (nativeEvent.actionName === "dismiss") dismiss();
97+
}}
98+
>
99+
<Toast
100+
message={`${notice.action} thread`}
101+
action={{ label: "Undo", onPress: () => void notice.undo() }}
102+
/>
103+
</Animated.View>
104+
</GestureDetector>
105+
</View>
106+
);
107+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createContext } from "react";
2+
3+
export const ThreadUndoNoticeHeightContext = createContext<((height: number) => void) | null>(null);

0 commit comments

Comments
 (0)