The best way to control Claude Code, Codex and more from any computer or phone
| 1 | const PAGE_SCROLL_ANIMATION_MS = 150; |
| 2 | const PAGE_SCROLL_ACCELERATION_MS = 400; |
| 3 | const PAGE_SCROLL_MAX_MULTIPLIER = 2; |
| 4 | |
| 5 | const PAGE_SCROLL_ALIGNMENT_OFFSET_PX = 36; |
| 6 | const PAGE_SCROLL_BOUNDARY_EPSILON_PX = 1; |
| 7 | const PAGE_SCROLL_HOLD_DELAY_MS = PAGE_SCROLL_ANIMATION_MS; |
| 8 | |
| 9 | export type PageScrollKey = "PageUp" | "PageDown"; |
| 10 | |
| 11 | type PageScrollMetrics = { |
| 12 | clientHeight: number; |
| 13 | scrollHeight: number; |
| 14 | scrollTop: number; |
| 15 | }; |
| 16 | |
| 17 | function canScrollInDirection( |
| 18 | { clientHeight, scrollHeight, scrollTop }: PageScrollMetrics, |
| 19 | key: PageScrollKey, |
| 20 | ): boolean { |
| 21 | const maxScrollTop = Math.max(0, scrollHeight - clientHeight); |
| 22 | const clampedScrollTop = Math.min(maxScrollTop, Math.max(0, scrollTop)); |
| 23 | return key === "PageUp" |
| 24 | ? clampedScrollTop > PAGE_SCROLL_BOUNDARY_EPSILON_PX |
| 25 | : clampedScrollTop < maxScrollTop - PAGE_SCROLL_BOUNDARY_EPSILON_PX; |
| 26 | } |
| 27 | |
| 28 | export function getTimelinePageScrollKey({ |
| 29 | altKey, |
| 30 | clientHeight, |
| 31 | ctrlKey, |
| 32 | defaultPrevented, |
| 33 | isComposing, |
| 34 | key, |
| 35 | keyCode, |
| 36 | metaKey, |
| 37 | scrollHeight, |
| 38 | scrollTop, |
| 39 | shiftKey, |
| 40 | }: { |
| 41 | altKey: boolean; |
| 42 | clientHeight: number; |
| 43 | ctrlKey: boolean; |
| 44 | defaultPrevented: boolean; |
| 45 | isComposing: boolean; |
| 46 | key: string; |
| 47 | keyCode: number; |
| 48 | metaKey: boolean; |
| 49 | scrollHeight: number; |
| 50 | scrollTop: number; |
| 51 | shiftKey: boolean; |
| 52 | }): PageScrollKey | null { |
| 53 | if (key !== "PageUp" && key !== "PageDown") { |
| 54 | return null; |
| 55 | } |
| 56 | if ( |
| 57 | defaultPrevented || |
| 58 | isComposing || |
| 59 | keyCode === 229 || |
| 60 | altKey || |
| 61 | ctrlKey || |
| 62 | metaKey || |
| 63 | shiftKey |
| 64 | ) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | const editorCanScroll = canScrollInDirection({ clientHeight, scrollHeight, scrollTop }, key); |
| 69 | return editorCanScroll ? null : key; |
| 70 | } |
| 71 | |
| 72 | type PageScrollContainer = PageScrollMetrics & { |
| 73 | getBoundingClientRect: () => { |
| 74 | height: number; |
| 75 | }; |
| 76 | }; |
| 77 | |
| 78 | type PageScrollEnv = { |
| 79 | now: () => number; |
| 80 | requestAnimationFrame: (callback: FrameRequestCallback) => number; |
| 81 | cancelAnimationFrame: (handle: number) => void; |
| 82 | setTimeout: (callback: () => void, delay: number) => number; |
| 83 | clearTimeout: (handle: number) => void; |
| 84 | prefersReducedMotion: () => boolean; |
| 85 | }; |
| 86 | |
| 87 | function getDefaultEnv(): PageScrollEnv { |
| 88 | return { |
| 89 | now: () => performance.now(), |
| 90 | requestAnimationFrame: (callback) => window.requestAnimationFrame(callback), |
| 91 | cancelAnimationFrame: (handle) => window.cancelAnimationFrame(handle), |
| 92 | setTimeout: (callback, delay) => window.setTimeout(callback, delay), |
| 93 | clearTimeout: (handle) => window.clearTimeout(handle), |
| 94 | prefersReducedMotion: () => window.matchMedia("(prefers-reduced-motion: reduce)").matches, |
| 95 | }; |
| 96 | } |
| 97 | |
| 98 | function getPageScrollMultiplier(holdElapsedMs: number): number { |
| 99 | const progress = Math.max(0, holdElapsedMs) / PAGE_SCROLL_ACCELERATION_MS; |
| 100 | return 1 + Math.min(progress, 1) * (PAGE_SCROLL_MAX_MULTIPLIER - 1); |
| 101 | } |
| 102 | |
| 103 | function getPageScrollVelocityPxPerMs({ |
| 104 | holdElapsedMs, |
| 105 | pageScrollDistancePx, |
| 106 | }: { |
| 107 | holdElapsedMs: number; |
| 108 | pageScrollDistancePx: number; |
| 109 | }): number { |
| 110 | return (pageScrollDistancePx * getPageScrollMultiplier(holdElapsedMs)) / PAGE_SCROLL_ANIMATION_MS; |
| 111 | } |
| 112 | |
| 113 | function getPageScrollDistancePx({ |
| 114 | containerHeightPx, |
| 115 | scrollPaddingBottomPx, |
| 116 | }: { |
| 117 | containerHeightPx: number; |
| 118 | scrollPaddingBottomPx: number; |
| 119 | }): number { |
| 120 | return Math.max(0, containerHeightPx - PAGE_SCROLL_ALIGNMENT_OFFSET_PX - scrollPaddingBottomPx); |
| 121 | } |
| 122 | |
| 123 | function getDirection(key: PageScrollKey): -1 | 1 { |
| 124 | return key === "PageUp" ? -1 : 1; |
| 125 | } |
| 126 | |
| 127 | function easeInOut(progress: number): number { |
| 128 | const ax = 3 * 0.42 - 3 * 0.58 + 1; |
| 129 | const bx = 3 * (0.58 - 2 * 0.42); |
| 130 | const cx = 3 * 0.42; |
| 131 | const ay = -2; |
| 132 | const by = 3; |
| 133 | const x = (value: number) => ((ax * value + bx) * value + cx) * value; |
| 134 | const y = (value: number) => (ay * value + by) * value * value; |
| 135 | |
| 136 | let value = progress; |
| 137 | for (let index = 0; index < 5; index += 1) { |
| 138 | const delta = x(value) - progress; |
| 139 | const derivative = (3 * ax * value + 2 * bx) * value + cx; |
| 140 | if (Math.abs(delta) < 1e-4 || derivative === 0) { |
| 141 | break; |
| 142 | } |
| 143 | value -= delta / derivative; |
| 144 | value = Math.min(1, Math.max(0, value)); |
| 145 | } |
| 146 | |
| 147 | return y(value); |
| 148 | } |
| 149 | |
| 150 | export function createPageScrollController({ |
| 151 | getContainer, |
| 152 | getScrollPaddingBottomPx, |
| 153 | onScrollStart, |
| 154 | env = getDefaultEnv(), |
| 155 | }: { |
| 156 | getContainer: () => PageScrollContainer | null; |
| 157 | getScrollPaddingBottomPx: () => number; |
| 158 | onScrollStart?: (key: PageScrollKey) => void; |
| 159 | env?: PageScrollEnv; |
| 160 | }) { |
| 161 | const state = { |
| 162 | activeKey: null as PageScrollKey | null, |
| 163 | discreteAnimationFrame: 0, |
| 164 | holdDelayTimeout: 0, |
| 165 | holdAnimationFrame: 0, |
| 166 | holdStartTime: 0, |
| 167 | lastFrameTime: 0, |
| 168 | holdActive: false, |
| 169 | }; |
| 170 | |
| 171 | const readPageScrollDistance = (container: PageScrollContainer) => |
| 172 | getPageScrollDistancePx({ |
| 173 | containerHeightPx: container.getBoundingClientRect().height, |
| 174 | scrollPaddingBottomPx: getScrollPaddingBottomPx(), |
| 175 | }); |
| 176 | |
| 177 | const cancelDiscreteAnimation = () => { |
| 178 | if (state.discreteAnimationFrame === 0) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | env.cancelAnimationFrame(state.discreteAnimationFrame); |
| 183 | state.discreteAnimationFrame = 0; |
| 184 | }; |
| 185 | |
| 186 | const stop = ({ |
| 187 | cancelDiscreteAnimation: shouldCancelDiscreteAnimation, |
| 188 | }: { |
| 189 | cancelDiscreteAnimation: boolean; |
| 190 | }) => { |
| 191 | if (state.holdDelayTimeout !== 0) { |
| 192 | env.clearTimeout(state.holdDelayTimeout); |
| 193 | state.holdDelayTimeout = 0; |
| 194 | } |
| 195 | |
| 196 | if (state.holdAnimationFrame !== 0) { |
| 197 | env.cancelAnimationFrame(state.holdAnimationFrame); |
| 198 | state.holdAnimationFrame = 0; |
| 199 | } |
| 200 | |
| 201 | if (shouldCancelDiscreteAnimation) { |
| 202 | cancelDiscreteAnimation(); |
| 203 | } |
| 204 | |
| 205 | state.activeKey = null; |
| 206 | state.holdStartTime = 0; |
| 207 | state.lastFrameTime = 0; |
| 208 | state.holdActive = false; |
| 209 | }; |
| 210 | |
| 211 | const scrollBy = (container: PageScrollContainer, deltaY: number) => { |
| 212 | cancelDiscreteAnimation(); |
| 213 | |
| 214 | if (env.prefersReducedMotion()) { |
| 215 | container.scrollTop += deltaY; |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | const startScrollTop = container.scrollTop; |
| 220 | const startTime = env.now(); |
| 221 | |
| 222 | const step = (now: number) => { |
| 223 | const progress = Math.min(1, (now - startTime) / PAGE_SCROLL_ANIMATION_MS); |
| 224 | container.scrollTop = startScrollTop + deltaY * easeInOut(progress); |
| 225 | |
| 226 | if (progress < 1) { |
| 227 | state.discreteAnimationFrame = env.requestAnimationFrame(step); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | state.discreteAnimationFrame = 0; |
| 232 | }; |
| 233 | |
| 234 | state.discreteAnimationFrame = env.requestAnimationFrame(step); |
| 235 | }; |
| 236 | |
| 237 | const startHoldScroll = (key: PageScrollKey, container: PageScrollContainer) => { |
| 238 | cancelDiscreteAnimation(); |
| 239 | state.holdActive = true; |
| 240 | state.holdStartTime = env.now(); |
| 241 | state.lastFrameTime = state.holdStartTime; |
| 242 | |
| 243 | const step = (now: number) => { |
| 244 | if (state.activeKey !== key) { |
| 245 | state.holdAnimationFrame = 0; |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | const deltaMs = now - state.lastFrameTime; |
| 250 | state.lastFrameTime = now; |
| 251 | |
| 252 | const velocityPxPerMs = getPageScrollVelocityPxPerMs({ |
| 253 | holdElapsedMs: now - state.holdStartTime, |
| 254 | pageScrollDistancePx: readPageScrollDistance(container), |
| 255 | }); |
| 256 | const previousScrollTop = container.scrollTop; |
| 257 | container.scrollTop = previousScrollTop + velocityPxPerMs * deltaMs * getDirection(key); |
| 258 | |
| 259 | if (deltaMs > 0 && container.scrollTop === previousScrollTop) { |
| 260 | stop({ cancelDiscreteAnimation: true }); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | state.holdAnimationFrame = env.requestAnimationFrame(step); |
| 265 | }; |
| 266 | |
| 267 | state.holdAnimationFrame = env.requestAnimationFrame(step); |
| 268 | }; |
| 269 | |
| 270 | return { |
| 271 | handleKeyDown(key: PageScrollKey) { |
| 272 | const container = getContainer(); |
| 273 | if (!container) { |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if (!canScrollInDirection(container, key)) { |
| 278 | return; |
| 279 | } |
| 280 | |
| 281 | if (state.activeKey === key) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | stop({ cancelDiscreteAnimation: true }); |
| 286 | state.activeKey = key; |
| 287 | onScrollStart?.(key); |
| 288 | |
| 289 | scrollBy(container, readPageScrollDistance(container) * getDirection(key)); |
| 290 | |
| 291 | state.holdDelayTimeout = env.setTimeout(() => { |
| 292 | state.holdDelayTimeout = 0; |
| 293 | if (state.activeKey !== key) { |
| 294 | return; |
| 295 | } |
| 296 | |
| 297 | startHoldScroll(key, container); |
| 298 | }, PAGE_SCROLL_HOLD_DELAY_MS); |
| 299 | }, |
| 300 | handleKeyUp(key: string) { |
| 301 | if (state.activeKey !== key) { |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | stop({ cancelDiscreteAnimation: state.holdActive }); |
| 306 | }, |
| 307 | releaseActiveKey() { |
| 308 | stop({ cancelDiscreteAnimation: state.holdActive }); |
| 309 | }, |
| 310 | dispose() { |
| 311 | stop({ cancelDiscreteAnimation: true }); |
| 312 | }, |
| 313 | }; |
| 314 | } |