How to Build a Drag Gesture People Can Interrupt
Why some bottom sheets feel alive and others ignore you, with a live demo to compare, the full JavaScript in four steps and four tests for a real phone
Throw a sheet closed on your phone, change your mind, and catch it halfway. On a good one, it stops under your finger as if it had weight. On a bad one, it ignores you until its animation ends, and for half a second the phone is in charge. No screenshot will ever show you that difference, and it is most of what people mean when they say an app feels right.
Try both here. Drag the sheet, let go while it is moving, and grab it again before it lands.
This is a live example: a bottom sheet with two behaviors. In the first you can catch it while it moves. In the second it ignores every touch until its animation has finished.
Why the second one feels broken
The locked version plays a fixed animation from A to B and refuses every touch until it ends. Plenty of sheets work this way because their code only knows the start and the finish. The free version keeps two numbers at all times, position and velocity. Anything can take over from those numbers at any moment: a finger, a spring, or another finger.
That is the whole idea. Store where the thing is and how fast it is moving, and let every input start from there.
Build it in four steps
This is the drag and the spring in plain JavaScript. It needs one element with the class sheet, fixed to the bottom of the screen.
1. Keep the position yourself
const sheet = document.querySelector('.sheet')
const CLOSED = sheet.offsetHeight - 44 // leaves the handle visible
let y = CLOSED, v = 0, frame = 0, grab = null, finger = 0, lastY = 0, lastT = 0
const draw = () => { sheet.style.transform = `translateY(${y}px)` }
draw()
2. Follow the finger from where the sheet is
The important line is cancelAnimationFrame. Touching the sheet stops whatever it was doing. The line after it saves the distance between the finger and the sheet, so it does not jump to meet you.
sheet.addEventListener('pointerdown', (e) => {
if (grab !== null) return // a second finger changes nothing
finger = e.pointerId
cancelAnimationFrame(frame)
grab = e.clientY - y
v = 0
lastY = e.clientY
lastT = e.timeStamp
sheet.setPointerCapture(e.pointerId)
})
sheet.addEventListener('pointermove', (e) => {
if (grab === null || e.pointerId !== finger) return
y = Math.max(0, e.clientY - grab)
const dt = e.timeStamp - lastT
if (dt > 0) v = ((e.clientY - lastY) / dt) * 1000
lastY = e.clientY
lastT = e.timeStamp
draw()
})
3. Let the throw decide where it lands
A short, fast flick should close the sheet even if the finger barely moved. So ask where the sheet would end up at this speed, and compare that point with the middle.
const release = (e) => {
if (grab === null || e.pointerId !== finger) return
grab = null
if (e.timeStamp - lastT > 80) v = 0 // the finger rested first
v = Math.max(-2000, Math.min(2000, v))
springTo(y + v * 0.2 > CLOSED / 2 ? CLOSED : 0)
}
sheet.addEventListener('pointerup', release)
sheet.addEventListener('pointercancel', release)
4. Finish with a spring that inherits the speed
function springTo(target) {
let last = performance.now()
const step = (now) => {
const dt = Math.min(0.032, Math.max(0, (now - last) / 1000))
last = now
v += (-170 * (y - target) - 22 * v) * dt
y += v * dt
const done = Math.abs(v) < 4 && Math.abs(y - target) < 0.4
if (done) { y = target; v = 0 }
draw()
if (!done) frame = requestAnimationFrame(step)
}
frame = requestAnimationFrame(step)
}
Raise 0.2 if short flicks do not carry far enough, 170 for a tighter spring and 22 for less bounce. In the CSS, give the sheet touch-action: none so the browser does not scroll the page at the same time, and box-shadow: 0 80px 0 in its own background color so a small bounce never shows a gap underneath.
What to leave out
- Motion on things people do all day. A menu opened from the keyboard fifty times a day should appear at once. Save the spring for things that are dragged.
- Deleting during the drag. Revealing a panel halfway through a swipe is fine. Deleting a message should wait until the finger lifts, so there is time to change your mind.
- Gestures as the only way in. Every sheet needs a button that does the same thing, reachable by keyboard.
- The spring itself, for people who asked for less motion. With
prefers-reduced-motion, setyto the target and draw once.
The test that decides if it is done
Open it on a real phone and do these four things:
- Throw the sheet closed, catch it halfway, and pull it back open. It should not jump.
- Reverse direction three times without letting go. The sheet should stay under the same spot of your finger.
- Flick it quickly over a very short distance. It should follow the flick.
- Drag it slowly past the middle, stop, and let go. It should settle on the nearest side.
If all four pass, ship it. If one fails, the cause is almost always a place where the code forgot the position or the speed.
The topic came from Rauno Freiberg. His free essay Invisible Details of Interaction Design is the best next read, and two chapters of his paid manual Devouring Details are free. The paid chapters were not used here.