| 22 | const upFlick = -0.5; |
| 23 | |
| 24 | export const attachSheetDrag = ( |
| 25 | panel: HTMLElement, |
| 26 | onDismiss: (event: TouchEvent) => void, |
| 27 | { scroller = () => panel }: SheetDragOptions = {}, |
| 28 | ): (() => void) => { |
| 29 | const { style } = panel; |
| 30 | let startX = 0; |
| 31 | let startY = 0; |
| 32 | let startTime = 0; |
| 33 | let lastY = 0; |
| 34 | let lastTime = 0; |
| 35 | let velocity = 0; |
| 36 | let dy = 0; |
| 37 | let tracking = false; |
| 38 | let dragging = false; |
| 39 | let startedExpanded = false; |
| 40 | let restHeight = 0; |
| 41 | let fullHeight = 0; |
| 42 | |
| 43 | const isExpanded = () => panel.getAttribute('data-expanded') === 'true'; |
| 44 | // The height of a sheet at the top: its overlay's, or the viewport's |
| 45 | // for a sheet that is not inside one (the menu sheet). |
| 46 | const measureFull = () => |
| 47 | panel.closest<HTMLElement>('.shell-sheet-overlay')?.clientHeight ?? |
| 48 | window.visualViewport?.height ?? |
| 49 | window.innerHeight; |
| 50 | |
| 51 | // The settle that is still running, so a new one or a new drag ends it. |
| 52 | let endSettle: () => void = () => undefined; |
| 53 | |
| 54 | const settle = () => { |
| 55 | endSettle(); |
| 56 | panel.removeAttribute('data-dragging'); |
| 57 | style.transform = ''; |
| 58 | style.height = ''; |
| 59 | style.maxHeight = ''; |
| 60 | style.transition = ''; |
| 61 | style.removeProperty('--sheet-gap'); |
| 62 | }; |
| 63 | |
| 64 | const settleTo = (expanded: boolean) => { |
| 65 | endSettle(); |
| 66 | panel.removeAttribute('data-dragging'); |
| 67 | style.transition = `height ${motion.snap}ms ${motion.interaction}, transform ${motion.snap}ms ${motion.interaction}, padding-top ${motion.snap}ms ${motion.interaction}`; |
| 68 | style.transform = ''; |
| 69 | // The distance left to the top; a sheet that covers the status bar pads |
| 70 | // its content by how far it has entered it. |
| 71 | style.removeProperty('--sheet-gap'); |
| 72 | if (expanded) { |
| 73 | panel.setAttribute('data-expanded', 'true'); |
| 74 | // The attribute's rule carries the full height; the inline height |
| 75 | // only had to be a number for the transition to start from. |
| 76 | style.height = `${fullHeight}px`; |
| 77 | } else { |
| 78 | panel.removeAttribute('data-expanded'); |
| 79 | style.height = `${restHeight}px`; |
| 80 | } |
| 81 | // A child's transition ending (a row's press colour) is not the sheet |