Pull to refresh
Also called pull down to refresh or swipe to refresh
A gesture where you drag a list down from the top to reveal a spinner and load fresh content, common in mobile feeds and inboxes.
import { useRef, useState, type ReactNode, type TouchEvent } from 'react';
import { animate, motion, useMotionValue, useTransform } from 'motion/react';
const RESISTANCE = 0.5; // the list moves half as far as the finger
const THRESHOLD = 60; // px of list travel needed to refresh
const HOLD_AT = 48; // where the list waits while new items load
const SETTLE = { type: 'spring', bounce: 0 } as const;
// Desktop has no pull gesture, so keep a Refresh button there too.
type Props = { onRefresh: () => Promise<void>; children: ReactNode };
export function PullToRefresh({ onRefresh, children }: Props) {
const y = useMotionValue(0);
const rotate = useTransform(y, [0, THRESHOLD], [0, 360]);
const startY = useRef<number | null>(null);
const [refreshing, setRefreshing] = useState(false);
async function refresh() {
setRefreshing(true);
animate(y, HOLD_AT, SETTLE);
await onRefresh();
setRefreshing(false);
animate(y, 0, SETTLE);
}
function onTouchMove(event: TouchEvent<HTMLDivElement>) {
if (startY.current === null || event.currentTarget.scrollTop > 0) return;
const pulled = event.touches[0]!.clientY - startY.current;
y.set(Math.max(0, pulled) * RESISTANCE);
}
function onTouchEnd() {
if (startY.current === null) return;
startY.current = null;
if (y.get() > THRESHOLD) refresh();
else animate(y, 0, SETTLE);
}
return (
<div className='relative h-[32rem] overflow-hidden rounded-2xl border'>
{/* Hidden behind the list until it moves; turns with the pull. */}
<motion.span
style={{ rotate }}
className={`absolute top-3 left-1/2 -ml-3 size-6 rounded-full border-2 border-muted border-t-foreground ${refreshing ? 'animate-spin' : ''}`}
/>
<motion.div
style={{ y }}
onTouchStart={(event) => {
const atTop = event.currentTarget.scrollTop === 0 && !refreshing;
startY.current = atTop ? event.touches[0]!.clientY : null;
}}
onTouchMove={onTouchMove}
onTouchEnd={onTouchEnd}
// contain: the browser's own pull-to-refresh doesn't fight this one.
className='relative h-full overflow-y-auto overscroll-y-contain bg-background'
>
{children}
</motion.div>
</div>
);
}Add pull to refresh to the inbox list on mobile: when the list is scrolled to the top and the user drags down, move the list with increasing resistance (about half the finger distance), and show a spinner that rotates with the pull. If released past 60px, hold the list 48px down with the spinner spinning until new messages load, then spring back. Set overscroll-behavior-y: contain on the page, and keep a refresh button for desktop.
Paste into Layerwise or any AI coding tool.
What is Pull to refresh?
Pull to refresh starts when a list is already scrolled to the top and you keep dragging down. The content follows with growing resistance, and an indicator appears in the gap, often spinning as you pull. Release past a threshold and the spinner keeps going while new content loads, then the list springs back with the new items on top.
The resistance is what makes it feel right: the list moves less than the finger, so the pull feels elastic, and a short pull that does not reach the threshold simply springs back. It suits feeds, inboxes, and lists that change often on phones. On desktop, people expect a refresh button, so keep one available.
Phone browsers have their own pull-to-refresh that reloads the whole page. In Chrome on Android, setting overscroll-behavior-y: contain on the page turns it off, so it does not fight with yours.
Often confused with
- Infinite scroll
- Infinite scroll loads more items when you reach the bottom of a list. Pull to refresh loads newer items when you pull down from the top.
Related terms
All termsRubber-banding
An edge effect where content dragged past its limit moves less than your finger, then springs back on release, like a stretched rubber band.
Spinner
A small looping animation, usually a rotating ring, that shows the app is busy with a short task of unknown length.
Loading state
The view a screen shows while its data is on the way, designed together with the outcomes that follow: loaded, empty, or error.
Drag gesture
Moving an element by pressing on it and sliding the pointer or finger, usually within set bounds and with a glide after release.
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.