Motion+

Swipe actions

iOS-style swipe actions where UI elements and animations respond dynamically to swipe progress. Actions trigger via taps or full swipes (for primary actions). A modern, space-efficient alternative to dropdown menus that keeps actions hidden behind the main item until revealed by a swipe gesture.

React
Tutorial time
5 min
Difficulty

Tutorial

Introduction

The Swipe actions example shows how to implement an iOS-style swipe gesture interface where swiping an item left or right reveals contextual actions. This pattern is commonly used in mobile UIs for actions like archive, delete, or flag, providing a space-efficient alternative to having these actions permanently visible.

This tutorial explores several advanced Motion for React APIs:

Get started

Let's start with the basic structure of our component:

"use client"

import { useCallback, useEffect, useRef, useState } from "react"

import { Archive, Clock9, Flag, MailOpen } from "lucide-react"
import type { LucideIcon } from "lucide-react"

const ITEM_HEIGHT = 80
const ITEM_MAX_WIDTH = 384

const SPRING_OPTIONS = {
    stiffness: 900,
    damping: 80,
}

export default function SwipeActions() {
    const [isSwiping, setIsSwiping] = useState(false)

    const swipeItemRef = useRef(null)
    const swipeItemWidth = useRef(0)
    const swipeStartX = useRef(0)
    const swipeStartOffset = useRef(0)
    const fullSwipeSnapPosition = useRef(null)
    const swipeContainerRef = useRef(null)

    return (
        <div style={styles.outerContainer}>
            <motion.div ref={swipeContainerRef} style={styles.swipeContainer}>
                <div style={styles.swipeItem}>
                    <SwipeItemContent />
                </div>
            </motion.div>
        </div>
    )
}

// We'll define these components later
const SwipeItemContent = () => {
    return (
        <div style={styles.swipeItemContent}>
            <div style={styles.messageContent}>
                <div style={styles.messageHeader}>
                    <div style={styles.messageTitle}>Welcome to Inbox</div>
                    <div style={styles.messageTime}>9:41 AM</div>
                </div>
                <div style={styles.messageText}>
                    Swipe left or right to see available actions
                </div>
            </div>
        </div>
    )
}

/**
 * ==============   Styles   ================
 */
/** Copy styles from example source code */

Related examples

Latest in React

Motion+

Unlock all 400+ examples

  • Source code for every Plus example.
  • Provide examples direct to your agent via Motion's MCP.
  • Lifetime access to new examples and APIs.