Swipe to dismiss
Also called swipe away or swipe to delete
A gesture where you drag an item sideways past a threshold to remove it, while a shorter swipe lets it spring back into place.
import { useRef } from 'react';
import { animate, motion, useMotionValue, useTransform } from 'motion/react';
const DISTANCE_THRESHOLD = 0.4; // share of the card's width
const VELOCITY_THRESHOLD = 500; // px per second, so a quick flick counts
type NotificationCardProps = {
title: string;
body: string;
onDismiss: () => void;
};
// Render inside a list: `layout` slides the cards below up when one leaves.
export function NotificationCard({
title,
body,
onDismiss
}: NotificationCardProps) {
const ref = useRef<HTMLLIElement>(null);
const x = useMotionValue(0);
const opacity = useTransform(x, [-200, 0, 200], [0, 1, 0]);
async function flyOut(direction: number) {
const width = ref.current!.offsetWidth;
await animate(x, direction * width, { duration: 0.25, ease: 'easeIn' });
onDismiss();
}
return (
<motion.li
ref={ref}
layout
drag='x' // horizontal only, so vertical scrolling still works
dragSnapToOrigin // a short swipe springs back into place
style={{ x, opacity }}
onDragEnd={(_, { offset, velocity }) => {
const width = ref.current!.offsetWidth;
const far = Math.abs(offset.x) > width * DISTANCE_THRESHOLD;
const fast = Math.abs(velocity.x) > VELOCITY_THRESHOLD;
if (far || fast) flyOut(Math.sign(fast ? velocity.x : offset.x));
}}
className='flex items-start gap-3 rounded-xl border bg-background p-3 shadow-sm'
>
<div className='flex-1'>
<p className='text-sm font-medium'>{title}</p>
<p className='text-sm text-muted-foreground'>{body}</p>
</div>
{/* Swiping is invisible, so mouse and keyboard users get a button. */}
<button
type='button'
onClick={() => flyOut(1)}
aria-label='Dismiss notification'
className='rounded-md px-1.5 text-muted-foreground hover:bg-muted'
>
✕
</button>
</motion.li>
);
}Make the notification cards swipe to dismiss with Motion: drag only on the x axis, fade the card as it moves, and dismiss it when released past 40% of its width or faster than 500px/s, animating it off screen over 250ms. Otherwise spring it back to its place. Slide the remaining cards up to close the gap, and keep a visible close button for mouse and keyboard users.
Paste into Layerwise or any AI coding tool.
What is Swipe to dismiss?
Swipe to dismiss lets people clear a notification, toast, or list item by flicking it away. The item follows the finger sideways and fades as it goes. Past a threshold, often 30 to 50% of its width, it flies off and the items below slide up to close the gap. A shorter drag springs it back.
Check speed as well as distance: a quick flick should dismiss even when the finger travels only a little. Lock the gesture to the horizontal axis so vertical scrolling still works.
Swiping is invisible until someone tries it, so also show a close button or menu action for mouse and keyboard users, and offer undo when dismissing deletes something.
Related terms
All termsDrag gesture
Moving an element by pressing on it and sliding the pointer or finger, usually within set bounds and with a glide after release.
Velocity
The speed and direction an element already has when an animation starts, which a spring carries forward so a flick flows into what follows.
Toast notification
A brief message that slides into a corner of the screen to confirm an action, then disappears on its own after a few seconds.
Undo
A way to reverse an action right after doing it, often a toast with an Undo button that appears after deleting or archiving something.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.