Open History to see the last commit
1/**
2 * Swipe-to-close maths for the bottom sheet, kept free of the DOM so it can be
3 * tested on its own.
4 */
5
6/** Movement (px) before a press on the header counts as a drag rather than a tap. */
7export const DRAG_START_THRESHOLD = 6;
8
9/** Dragged further than this share of the sheet's height: close on release. */
10const CLOSE_DISTANCE_RATIO = 0.25;
11/** A downward flick faster than this (px/ms) closes, however short. */
12const CLOSE_VELOCITY = 0.5;
13/** …as long as it moved at least this far, so a jittery tap doesn't close. */
14const MIN_FLICK_DISTANCE = 24;
15
16/**
17 * How far the sheet follows the finger. Down: one to one. Up: the sheet is
18 * already fully open, so it gives a little and resists, like iOS sheets do.
19 */
20export function sheetOffset(dragDistance: number): number {
21 if (dragDistance >= 0) return dragDistance;
22 return -12 * (1 - Math.exp(dragDistance / 60));
23}
24
25export function shouldCloseOnRelease({
26 offset,
27 velocity,
28 sheetHeight,
29}: {
30 /** Current downward offset in px. */
31 offset: number;
32 /** Downward speed at release in px/ms (negative when moving up). */
33 velocity: number;
34 sheetHeight: number;
35}): boolean {
36 if (offset >= sheetHeight * CLOSE_DISTANCE_RATIO) return true;
37 return velocity >= CLOSE_VELOCITY && offset >= MIN_FLICK_DISTANCE;
38}