From 47ab7141eb963542c973abaf9c18ab9e0fb947db Mon Sep 17 00:00:00 2001 From: Joshua Higgins Date: Sat, 17 Oct 2026 16:24:44 -0400 Subject: [PATCH] feat(mobile): add native iOS swipe-to-remove rows (#31) --- .../app/(app)/(tabs)/home/playlist/[id].tsx | 34 +- .../components/ios-swipe-to-remove-row.tsx | 359 ++++++++++++++++++ 2 files changed, 390 insertions(+), 3 deletions(-) create mode 100644 apps/mobile/components/ios-swipe-to-remove-row.tsx diff --git a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx index 8aef17c..dc5aeb8 100644 --- a/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx +++ b/apps/mobile/app/(app)/(tabs)/home/playlist/[id].tsx @@ -4,6 +4,7 @@ import { Stack, useIsFocused, useLocalSearchParams, useRouter } from "expo-route import { StatusBar } from "expo-status-bar" import { FlatList, + Platform, Pressable, StyleSheet, Text, @@ -14,6 +15,11 @@ import ReanimatedSwipeable from "react-native-gesture-handler/ReanimatedSwipeabl import { Ionicons } from "@expo/vector-icons" import { CoverImage } from "@/components/cover-image" +import { + SwipeToRemoveRow, + useSwipeRowGroup, + type SwipeRowGroup, +} from "@/components/ios-swipe-to-remove-row" import { MediaActionMenu, @@ -176,6 +182,7 @@ function PlaylistContent({ const canEdit = playlist.isOwner && playlistId !== null const loadedSongIds = useMemo(() => songs.map((song) => song.id), [songs]) const songIds = allSongIds ?? loadedSongIds + const swipeGroup = useSwipeRowGroup() const handlePlaySong = useCallback( (song: BrowseSong) => void playFromSong(song.id, songIds), @@ -190,16 +197,18 @@ function PlaylistContent({ const handleRemoveSong = useCallback( async (songId: Id<"songs">) => { - if (!playlistId) return + if (!playlistId) return false try { await removeSongs({ playlistId, songIds: [songId] }) showSuccess("Removed from playlist") + return true } catch (error) { showError( error instanceof Error ? error.message : "Failed to remove from playlist" ) + return false } }, [playlistId, removeSongs] @@ -295,6 +304,7 @@ function PlaylistContent({ favorited={isSongFavorite(song.id)} removable={canEdit} palette={palette} + swipeGroup={swipeGroup} onPlay={handlePlaySong} onAction={handleSongMenuAction} onRemove={handleRemoveSong} @@ -307,6 +317,7 @@ function PlaylistContent({ handleSongMenuAction, isSongFavorite, palette, + swipeGroup, ] ) @@ -327,6 +338,7 @@ function PlaylistContent({ contentInsetAdjustmentBehavior="automatic" onEndReached={onEndReached} onEndReachedThreshold={0.5} + onScrollBeginDrag={swipeGroup.closeOpenRow} ListHeaderComponent={ void onAction: (song: BrowseSong, action: MediaAction) => void - onRemove: (songId: Id<"songs">) => void + onRemove: (songId: Id<"songs">) => Promise }) { const colors = useThemeColors() + const handleRemove = useCallback(() => onRemove(song.id), [onRemove, song.id]) const row = ( + {row} + + ) + } + return ( { swipeable.close() - onRemove(song.id) + void handleRemove() }} style={[styles.removeAction, { backgroundColor: colors.destructive }]} > diff --git a/apps/mobile/components/ios-swipe-to-remove-row.tsx b/apps/mobile/components/ios-swipe-to-remove-row.tsx new file mode 100644 index 0000000..b32ada8 --- /dev/null +++ b/apps/mobile/components/ios-swipe-to-remove-row.tsx @@ -0,0 +1,359 @@ +import * as Haptics from "expo-haptics" +import { SymbolView } from "expo-symbols" +import { useCallback, useMemo, useState, type ReactNode } from "react" +import { + PlatformColor, + Pressable, + StyleSheet, + type LayoutChangeEvent, +} from "react-native" +import { Gesture, GestureDetector } from "react-native-gesture-handler" +import Animated, { + cancelAnimation, + Extrapolation, + interpolate, + useAnimatedStyle, + useSharedValue, + withSpring, + withTiming, +} from "react-native-reanimated" +import { scheduleOnRN } from "react-native-worklets" + +const ACTION_WIDTH = 82 +const CARD_ROUNDING_REVEAL = 40 +const PILL_MIN_MARGIN = 10 +const PILL_COMMITTED_MARGIN = 6 +const PILL_WIDTH_INPUT = [24, ACTION_WIDTH, 220] +const PILL_WIDTH_OUTPUT = [0, 62, 200] +const PILL_HEIGHT_INPUT = [0, 24, 62, 140, 200] +const PILL_HEIGHT_RATIO = [0, 0.3, 0.77, 0.82, 0.86] +const ICON_FULL_SIZE_REVEAL = 64 +const FULL_SWIPE_RATIO = 0.55 +const FLING_VELOCITY = 400 +const SETTLE_SPRING = { duration: 450, dampingRatio: 1 } +const LABEL_SPRING = { duration: 300, dampingRatio: 0.85 } +const SLIDE_OUT = { duration: 200 } +const COLLAPSE = { duration: 250 } + +/** + * Keeps at most one row open, like a `UITableView`: opening a row closes the + * previous one, and `closeOpenRow` closes whichever is open (call it when the + * list starts scrolling). + */ +export type SwipeRowGroup = { + setOpen: (close: () => void) => void + clear: (close: () => void) => void + closeOpenRow: () => void +} + +export function useSwipeRowGroup(): SwipeRowGroup { + const [group] = useState(() => { + let openRowClose: (() => void) | null = null + return { + setOpen(close) { + if (openRowClose && openRowClose !== close) openRowClose() + openRowClose = close + }, + clear(close) { + if (openRowClose === close) openRowClose = null + }, + closeOpenRow() { + openRowClose?.() + openRowClose = null + }, + } + }) + return group +} + +type SwipeToRemoveRowProps = { + group: SwipeRowGroup + backgroundColor: string + actionAccessibilityLabel: string + /** Resolves `false` if the removal failed, which restores the row. */ + onRemove: () => Promise + children: ReactNode +} + +/** + * The iOS 26 trailing swipe-to-delete row: the row lifts into a rounded card + * and a red pill grows in the revealed space. Release past half the pill to + * leave it open, or drag past the full-swipe point (haptic, the pill fills the + * space) to remove on release. + */ +export function SwipeToRemoveRow(props: SwipeToRemoveRowProps) { + const [generation, setGeneration] = useState(0) + const restore = useCallback(() => setGeneration((value) => value + 1), []) + + return +} + +function SwipeRowBody({ + group, + backgroundColor, + actionAccessibilityLabel, + onRemove, + onRestore, + children, +}: SwipeToRemoveRowProps & { onRestore: () => void }) { + const width = useSharedValue(0) + const height = useSharedValue(0) + const translateX = useSharedValue(0) + const startX = useSharedValue(0) + const committed = useSharedValue(false) + const commitProgress = useSharedValue(0) + const collapse = useSharedValue(0) + const removing = useSharedValue(false) + const [isOpen, setIsOpen] = useState(false) + + const close = useCallback(() => { + if (removing.value) return + translateX.value = withSpring(0, SETTLE_SPRING) + setIsOpen(false) + }, [removing, translateX]) + + const handleSwipeStart = useCallback(() => group.setOpen(close), [close, group]) + + const handleSettle = useCallback( + (open: boolean) => { + setIsOpen(open) + if (!open) group.clear(close) + }, + [close, group] + ) + + const handleRemove = useCallback(async () => { + group.clear(close) + if (!(await onRemove())) onRestore() + }, [close, group, onRemove, onRestore]) + + const animateRemoval = useCallback(() => { + "worklet" + removing.value = true + commitProgress.value = withSpring(1, LABEL_SPRING) + translateX.value = withTiming(-width.value, SLIDE_OUT, (slid) => { + if (!slid) return + collapse.value = withTiming(1, COLLAPSE, (collapsed) => { + if (collapsed) scheduleOnRN(handleRemove) + }) + }) + }, [collapse, commitProgress, handleRemove, removing, translateX, width]) + + const pan = useMemo(() => { + const gesture = Gesture.Pan() + .failOffsetY([-10, 10]) + .onStart(() => { + if (removing.value) return + cancelAnimation(translateX) + startX.value = translateX.value + scheduleOnRN(handleSwipeStart) + }) + .onUpdate((event) => { + if (removing.value) return + const rowWidth = width.value + const x = Math.max( + -rowWidth, + Math.min(0, startX.value + event.translationX) + ) + translateX.value = x + + const shouldCommit = -x > rowWidth * FULL_SWIPE_RATIO + if (shouldCommit !== committed.value) { + committed.value = shouldCommit + commitProgress.value = withSpring(shouldCommit ? 1 : 0, LABEL_SPRING) + scheduleOnRN(impact) + } + }) + .onEnd((event) => { + if (removing.value) return + if (committed.value) { + animateRemoval() + return + } + + const velocity = event.velocityX + const open = + velocity < -FLING_VELOCITY || + (velocity <= FLING_VELOCITY && translateX.value < -ACTION_WIDTH / 2) + translateX.value = withSpring(open ? -ACTION_WIDTH : 0, { + ...SETTLE_SPRING, + velocity, + }) + scheduleOnRN(handleSettle, open) + }) + + return isOpen + ? gesture.activeOffsetX([-10, 10]) + : gesture.activeOffsetX(-10).failOffsetX(10) + }, [ + animateRemoval, + commitProgress, + committed, + handleSettle, + handleSwipeStart, + isOpen, + removing, + startX, + translateX, + width, + ]) + + const handleLayout = useCallback( + (event: LayoutChangeEvent) => { + if (removing.value) return + width.value = event.nativeEvent.layout.width + height.value = event.nativeEvent.layout.height + }, + [height, removing, width] + ) + + const containerStyle = useAnimatedStyle(() => + collapse.value > 0 ? { height: height.value * (1 - collapse.value) } : {} + ) + + const contentStyle = useAnimatedStyle(() => ({ + borderRadius: interpolate( + -translateX.value, + [0, CARD_ROUNDING_REVEAL], + [0, height.value / 2], + Extrapolation.CLAMP + ), + transform: [{ translateX: translateX.value }], + })) + + const cardFillStyle = useAnimatedStyle(() => ({ + opacity: interpolate( + -translateX.value, + [0, CARD_ROUNDING_REVEAL], + [0, 1], + Extrapolation.CLAMP + ), + })) + + const actionStyle = useAnimatedStyle(() => ({ + width: -translateX.value, + })) + + // The pill grows out of a dot centered in the revealed space; past the + // full-swipe point it stretches to fill that space. + const pillStyle = useAnimatedStyle(() => { + const revealed = -translateX.value + const rowHeight = height.value + const resting = Math.min( + revealed - PILL_MIN_MARGIN * 2, + interpolate( + revealed, + PILL_WIDTH_INPUT, + PILL_WIDTH_OUTPUT, + Extrapolation.EXTEND + ) + ) + const pillWidth = Math.max( + 0, + resting + + (revealed - PILL_COMMITTED_MARGIN * 2 - resting) * + commitProgress.value + ) + const restingHeight = interpolate( + pillWidth, + PILL_HEIGHT_INPUT, + PILL_HEIGHT_RATIO.map((ratio) => ratio * rowHeight), + Extrapolation.CLAMP + ) + const pillHeight = + restingHeight + + (rowHeight - PILL_COMMITTED_MARGIN * 2 - restingHeight) * + commitProgress.value + return { + width: pillWidth, + height: pillHeight, + borderRadius: pillHeight / 2, + opacity: 1 - collapse.value, + } + }) + + const iconStyle = useAnimatedStyle(() => ({ + transform: [ + { + scale: interpolate( + -translateX.value, + [PILL_WIDTH_INPUT[0], ICON_FULL_SIZE_REVEAL], + [0, 1], + Extrapolation.CLAMP + ), + }, + ], + })) + + return ( + + + animateRemoval()} + style={styles.actionButton} + > + + + + + + + + + + + {children} + {isOpen ? ( + + ) : null} + + + + ) +} + +function impact() { + void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Medium) +} + +const styles = StyleSheet.create({ + container: { overflow: "hidden" }, + card: { overflow: "hidden" }, + cardFill: { + ...StyleSheet.absoluteFill, + backgroundColor: PlatformColor("tertiarySystemFill"), + }, + action: { + position: "absolute", + top: 0, + right: 0, + bottom: 0, + }, + actionButton: { + flex: 1, + alignItems: "center", + justifyContent: "center", + }, + pill: { + alignItems: "center", + justifyContent: "center", + overflow: "hidden", + backgroundColor: PlatformColor("systemRed"), + }, +})