77 use ,
88 useCallback ,
99 useEffect ,
10+ useLayoutEffect ,
1011 useRef ,
1112 useState ,
1213 type ComponentProps ,
@@ -19,17 +20,23 @@ import type {
1920 StyleProp ,
2021 ViewStyle ,
2122} from "react-native" ;
22- import { Pressable , View } from "react-native" ;
23+ import { Alert , Pressable , View } from "react-native" ;
2324import ReanimatedSwipeable , {
2425 type SwipeableMethods ,
2526} from "react-native-gesture-handler/ReanimatedSwipeable" ;
2627import Animated , {
28+ cancelAnimation ,
29+ Easing ,
2730 Extrapolation ,
31+ ReduceMotion ,
2832 interpolate ,
2933 runOnJS ,
34+ runOnUI ,
3035 type SharedValue ,
3136 useAnimatedReaction ,
3237 useAnimatedStyle ,
38+ useSharedValue ,
39+ withTiming ,
3340} from "react-native-reanimated" ;
3441
3542import { AppText as Text } from "../../components/AppText" ;
@@ -61,6 +68,13 @@ interface ThreadSwipeAction {
6168 readonly onPress : ( ) => void ;
6269}
6370
71+ /** Dismiss before committing; false restores the row, success changes its resetKey or removes it. */
72+ type ThreadSwipePrimaryAction = Omit < ThreadSwipeAction , "onPress" > &
73+ (
74+ | { readonly dismissOnPress : true ; readonly onPress : ( ) => Promise < boolean > }
75+ | { readonly dismissOnPress ?: false ; readonly onPress : ( ) => void }
76+ ) ;
77+
6478interface ThreadSwipeSecondaryAction extends ThreadSwipeAction {
6579 readonly tone : "primary" | "secondary" | "danger" ;
6680}
@@ -218,7 +232,7 @@ export function useSwipeableScrollGate(options?: {
218232 } ;
219233}
220234
221- export function ThreadSwipeable ( props : {
235+ interface ThreadSwipeableProps {
222236 readonly backgroundColor : ColorValue ;
223237 readonly children : ( close : ( ) => void ) => ReactNode ;
224238 /** Uses action visuals that fit inside compact 44pt rows. The press target
@@ -238,7 +252,7 @@ export function ThreadSwipeable(props: {
238252 readonly onDelete : ( ) => void ;
239253 readonly onSwipeableClose ?: ( methods : SwipeableMethods ) => void ;
240254 readonly onSwipeableWillOpen ?: ( methods : SwipeableMethods ) => void ;
241- readonly primaryAction : ThreadSwipeAction ;
255+ readonly primaryAction : ThreadSwipePrimaryAction ;
242256 /**
243257 * Omitted keeps the v1 destructive Delete action. Explicit null opts out of
244258 * a secondary action entirely so a gated Snooze can never fall back to an
@@ -255,7 +269,15 @@ export function ThreadSwipeable(props: {
255269 typeof ReanimatedSwipeable
256270 > [ "simultaneousWithExternalGesture" ] ;
257271 readonly threadTitle : string ;
258- } ) {
272+ }
273+
274+ export function ThreadSwipeable ( props : ThreadSwipeableProps ) {
275+ // Recycled content gets fresh native and animation state. Late callbacks
276+ // from the previous row retain its action, never the replacement's action.
277+ return < ThreadSwipeableRow key = { props . resetKey } { ...props } /> ;
278+ }
279+
280+ function ThreadSwipeableRow ( props : ThreadSwipeableProps ) {
259281 const swipeableRef = useRef < SwipeableMethods | null > ( null ) ;
260282 const fullSwipeArmedRef = useRef ( false ) ;
261283 const hasSecondaryAction = props . secondaryAction !== null ;
@@ -265,14 +287,119 @@ export function ThreadSwipeable(props: {
265287 props . fullSwipeAction ?? ( props . secondaryAction === undefined ? "delete" : "primary" ) ;
266288 const close = useCallback ( ( ) => swipeableRef . current ?. close ( ) , [ ] ) ;
267289 const gateEnabled = use ( SwipeableScrollGateContext ) ;
268- const resetKey = props . resetKey ;
269- useEffect ( ( ) => {
270- if ( resetKey === undefined ) {
271- return ;
290+ const mountedRef = useRef ( true ) ;
291+ const pendingDismissRef = useRef < ( ( ) => Promise < boolean > ) | null > ( null ) ;
292+ const activeTranslationRef = useRef < SharedValue < number > | null > ( null ) ;
293+ const [ isDismissing , setIsDismissing ] = useState ( false ) ;
294+ const dismissing = useSharedValue ( false ) ;
295+ const rowHeight = useSharedValue ( 0 ) ;
296+ const rowWidth = useSharedValue ( props . fullSwipeWidth ) ;
297+ const collapse = useSharedValue ( 0 ) ;
298+ const actionOpacity = useSharedValue ( 1 ) ;
299+ const primaryAction = props . primaryAction ;
300+ const onSwipeableClose = props . onSwipeableClose ;
301+
302+ const restoreRow = useCallback ( ( ) => {
303+ swipeableRef . current ?. close ( ) ;
304+ collapse . set ( 0 ) ;
305+ actionOpacity . set ( 1 ) ;
306+ dismissing . set ( false ) ;
307+ setIsDismissing ( false ) ;
308+ } , [ actionOpacity , collapse , dismissing ] ) ;
309+
310+ const finishDismiss = useCallback ( async ( ) => {
311+ const action = pendingDismissRef . current ;
312+ if ( ! action ) return ;
313+ pendingDismissRef . current = null ;
314+ try {
315+ const succeeded = await action ( ) ;
316+ if ( ! succeeded && mountedRef . current ) restoreRow ( ) ;
317+ } catch ( error ) {
318+ if ( mountedRef . current ) restoreRow ( ) ;
319+ Alert . alert (
320+ "Could not settle thread" ,
321+ error instanceof Error ? error . message : "The thread could not be settled." ,
322+ ) ;
272323 }
273- fullSwipeArmedRef . current = false ;
274- swipeableRef . current ?. reset ( ) ;
275- } , [ resetKey ] ) ;
324+ } , [ restoreRow ] ) ;
325+
326+ useLayoutEffect ( ( ) => {
327+ mountedRef . current = true ;
328+ return ( ) => {
329+ mountedRef . current = false ;
330+ cancelAnimation ( collapse ) ;
331+ cancelAnimation ( actionOpacity ) ;
332+ if ( activeTranslationRef . current ) cancelAnimation ( activeTranslationRef . current ) ;
333+ // Scrolling a committed row out of the recycled list must still settle it.
334+ void finishDismiss ( ) ;
335+ } ;
336+ } , [ actionOpacity , collapse , finishDismiss ] ) ;
337+
338+ const beginDismiss = useCallback (
339+ ( translation : SharedValue < number > ) => {
340+ if ( ! primaryAction . dismissOnPress ) return ;
341+ pendingDismissRef . current = primaryAction . onPress ;
342+ activeTranslationRef . current = translation ;
343+ fullSwipeArmedRef . current = false ;
344+ if ( ! mountedRef . current ) {
345+ void finishDismiss ( ) ;
346+ return ;
347+ }
348+ setIsDismissing ( true ) ;
349+ if ( swipeableRef . current ) onSwipeableClose ?.( swipeableRef . current ) ;
350+ } ,
351+ [ finishDismiss , primaryAction , onSwipeableClose ] ,
352+ ) ;
353+
354+ const dismiss = useCallback (
355+ ( translation : SharedValue < number > ) => {
356+ "worklet" ;
357+ if ( dismissing . value ) return ;
358+ dismissing . set ( true ) ;
359+ runOnJS ( beginDismiss ) ( translation ) ;
360+ const timing = {
361+ duration : 220 ,
362+ easing : Easing . out ( Easing . cubic ) ,
363+ reduceMotion : ReduceMotion . System ,
364+ } ;
365+ actionOpacity . set ( withTiming ( 0 , timing ) ) ;
366+ // Never reverse a swipe that already carried the row beyond its width.
367+ translation . set (
368+ withTiming ( Math . min ( translation . value , - rowWidth . value ) , timing , ( finished ) => {
369+ if ( ! finished ) return ;
370+ collapse . set (
371+ withTiming ( 1 , { ...timing , duration : 180 } , ( collapsed ) => {
372+ if ( collapsed ) runOnJS ( finishDismiss ) ( ) ;
373+ } ) ,
374+ ) ;
375+ } ) ,
376+ ) ;
377+ } ,
378+ [ actionOpacity , beginDismiss , collapse , dismissing , finishDismiss , rowWidth ] ,
379+ ) ;
380+ const dismissStyle = useAnimatedStyle ( ( ) => ( {
381+ height : dismissing . value ? rowHeight . value * ( 1 - collapse . value ) : undefined ,
382+ pointerEvents : dismissing . value ? "none" : "auto" ,
383+ overflow : "hidden" ,
384+ } ) ) ;
385+ const actionStyle = useAnimatedStyle ( ( ) => ( { opacity : actionOpacity . value , height : "100%" } ) ) ;
386+ const dismissOnPress = primaryAction . dismissOnPress === true ;
387+ const handleRelease = useCallback (
388+ ( translation : SharedValue < number > ) => {
389+ "worklet" ;
390+ if ( dismissing . value ) return true ;
391+ if (
392+ dismissOnPress &&
393+ fullSwipeAction === "primary" &&
394+ - translation . value >= fullSwipeThreshold
395+ ) {
396+ dismiss ( translation ) ;
397+ return true ;
398+ }
399+ return false ;
400+ } ,
401+ [ dismiss , dismissing , dismissOnPress , fullSwipeAction , fullSwipeThreshold ] ,
402+ ) ;
276403 const handleFullSwipeArmedChange = useCallback ( ( armed : boolean ) => {
277404 if ( armed && ! fullSwipeArmedRef . current ) {
278405 void Haptics . impactAsync ( Haptics . ImpactFeedbackStyle . Medium ) ;
@@ -281,78 +408,94 @@ export function ThreadSwipeable(props: {
281408 } , [ ] ) ;
282409
283410 return (
284- < ReanimatedSwipeable
285- ref = { swipeableRef }
286- animationOptions = { THREAD_SWIPE_SPRING }
287- childrenContainerStyle = { { backgroundColor : props . backgroundColor } }
288- containerStyle = { [ { backgroundColor : props . backgroundColor } , props . containerStyle ] }
289- dragOffsetFromRightEdge = { 8 }
290- enabled = { props . enabled !== false && gateEnabled }
291- enableTrackpadTwoFingerGesture = { props . enableTrackpadSwipe ?? true }
292- // Fail the swipe once the pan is vertically dominant (patched-in RNGH
293- // prop) — otherwise trackpad scrolls with ~8px of horizontal drift
294- // start opening rows because the swipe pan runs simultaneously with
295- // the list scroll gesture and never gets disqualified by Y movement.
296- failOffsetY = { [ - 10 , 10 ] }
297- friction = { 1 }
298- onSwipeableClose = { ( ) => {
299- fullSwipeArmedRef . current = false ;
300- if ( swipeableRef . current ) {
301- props . onSwipeableClose ?.( swipeableRef . current ) ;
302- }
303- } }
304- onSwipeableOpenStartDrag = { ( ) => {
305- if ( swipeableRef . current ) {
306- props . onSwipeableWillOpen ?.( swipeableRef . current ) ;
307- }
308- } }
309- onSwipeableWillOpen = { ( ) => {
310- const methods = swipeableRef . current ;
311- if ( ! methods ) {
312- return ;
313- }
411+ < Animated . View style = { dismissStyle } >
412+ < View
413+ onLayout = { ( { nativeEvent : { layout } } ) => {
414+ rowHeight . set ( layout . height ) ;
415+ rowWidth . set ( layout . width ) ;
416+ } }
417+ >
418+ < ReanimatedSwipeable
419+ ref = { swipeableRef }
420+ animationOptions = { THREAD_SWIPE_SPRING }
421+ childrenContainerStyle = { { backgroundColor : props . backgroundColor } }
422+ containerStyle = { [ { backgroundColor : props . backgroundColor } , props . containerStyle ] }
423+ dragOffsetFromRightEdge = { 8 }
424+ enabled = { ! isDismissing && props . enabled !== false && gateEnabled }
425+ enableTrackpadTwoFingerGesture = { props . enableTrackpadSwipe ?? true }
426+ // Fail the swipe once the pan is vertically dominant (patched-in RNGH
427+ // prop) — otherwise trackpad scrolls with ~8px of horizontal drift
428+ // start opening rows because the swipe pan runs simultaneously with
429+ // the list scroll gesture and never gets disqualified by Y movement.
430+ failOffsetY = { [ - 10 , 10 ] }
431+ friction = { 1 }
432+ onSwipeableClose = { ( ) => {
433+ fullSwipeArmedRef . current = false ;
434+ if ( swipeableRef . current ) {
435+ props . onSwipeableClose ?.( swipeableRef . current ) ;
436+ }
437+ } }
438+ onSwipeableRelease = { handleRelease }
439+ onSwipeableOpenStartDrag = { ( ) => {
440+ if ( swipeableRef . current ) {
441+ props . onSwipeableWillOpen ?.( swipeableRef . current ) ;
442+ }
443+ } }
444+ onSwipeableWillOpen = { ( ) => {
445+ const methods = swipeableRef . current ;
446+ if ( ! methods ) {
447+ return ;
448+ }
314449
315- props . onSwipeableWillOpen ?.( methods ) ;
316- if ( fullSwipeArmedRef . current ) {
317- fullSwipeArmedRef . current = false ;
318- methods . close ( ) ;
319- if ( fullSwipeAction === "primary" ) {
320- props . primaryAction . onPress ( ) ;
321- } else {
322- props . onDelete ( ) ;
323- }
324- }
325- } }
326- overshootFriction = { 1 }
327- overshootRight
328- renderRightActions = { ( _progress , translation , methods ) => (
329- < ThreadSwipeActions
330- backgroundColor = { props . backgroundColor }
331- compact = { props . compactActions === true }
332- fullSwipeAction = { fullSwipeAction }
333- fullSwipeThreshold = { fullSwipeThreshold }
334- onFullSwipeArmedChange = { handleFullSwipeArmedChange }
335- primaryAction = { {
336- ...props . primaryAction ,
337- onPress : ( ) => {
450+ props . onSwipeableWillOpen ?.( methods ) ;
451+ if ( fullSwipeArmedRef . current && ! ( dismissOnPress && fullSwipeAction === "primary" ) ) {
452+ fullSwipeArmedRef . current = false ;
338453 methods . close ( ) ;
339- props . primaryAction . onPress ( ) ;
340- } ,
454+ if ( fullSwipeAction === "primary" ) {
455+ props . primaryAction . onPress ( ) ;
456+ } else {
457+ props . onDelete ( ) ;
458+ }
459+ }
341460 } }
342- secondaryAction = { resolveSecondaryAction ( {
343- close : ( ) => methods . close ( ) ,
344- onDelete : props . onDelete ,
345- secondaryAction : props . secondaryAction ,
346- threadTitle : props . threadTitle ,
347- } ) }
348- translation = { translation }
349- />
350- ) }
351- rightThreshold = { actionsWidth * 0.42 }
352- simultaneousWithExternalGesture = { props . simultaneousWithExternalGesture }
353- >
354- { props . children ( close ) }
355- </ ReanimatedSwipeable >
461+ overshootFriction = { 1 }
462+ overshootRight
463+ renderRightActions = { ( _progress , translation , methods ) => (
464+ < Animated . View style = { actionStyle } >
465+ < ThreadSwipeActions
466+ backgroundColor = { props . backgroundColor }
467+ compact = { props . compactActions === true }
468+ fullSwipeAction = { fullSwipeAction }
469+ fullSwipeThreshold = { fullSwipeThreshold }
470+ onFullSwipeArmedChange = { handleFullSwipeArmedChange }
471+ primaryAction = { {
472+ ...primaryAction ,
473+ onPress : ( ) => {
474+ if ( primaryAction . dismissOnPress ) {
475+ runOnUI ( dismiss ) ( translation ) ;
476+ } else {
477+ methods . close ( ) ;
478+ primaryAction . onPress ( ) ;
479+ }
480+ } ,
481+ } }
482+ secondaryAction = { resolveSecondaryAction ( {
483+ close : ( ) => methods . close ( ) ,
484+ onDelete : props . onDelete ,
485+ secondaryAction : props . secondaryAction ,
486+ threadTitle : props . threadTitle ,
487+ } ) }
488+ translation = { translation }
489+ />
490+ </ Animated . View >
491+ ) }
492+ rightThreshold = { actionsWidth * 0.42 }
493+ simultaneousWithExternalGesture = { props . simultaneousWithExternalGesture }
494+ >
495+ { props . children ( close ) }
496+ </ ReanimatedSwipeable >
497+ </ View >
498+ </ Animated . View >
356499 ) ;
357500}
358501
0 commit comments