diff-pane.tsx
This file
Path
apps/web/src/features/code/diff-pane.tsx
92 lines · 3.1 KiB · TypeScript
Last changed by
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
History of this fileTake it
Merge build/1211-code-browseroscabrielbefore adoptiona6808d3
lines 1–92why, one step back1/**2 * Where a commit's diff scrolls: a box the height of the screen below the3 * bars, which sticks under the toolbar once the page head has scrolled4 * away. The diff scrolls inside it (Pierre's `CodeView` needs a scroller5 * of its own to draw only what is in view), and its file headers stick at6 * its top.7 *8 * Until the box has reached the toolbar, the box does not scroll: a wheel9 * or a swipe over the diff moves the page instead, so the head scrolls10 * away first and nothing traps the reader. Once stuck, it scrolls; at its11 * top a further scroll up moves the page back. Without script it always12 * scrolls.13 */14import { useEffect, useRef, useState } from "react";15import type { ReactNode } from "react";1617/** The box's place: under the toolbar, as tall as the screen below it (and above a phone's bottom bar). */18const PANE =19 "group/pane sticky top-[5.1875rem] h-[calc(100dvh-5.1875rem-3.5rem-env(safe-area-inset-bottom))] scroll-mt-[5.1875rem] tablet:top-[5.4375rem] tablet:h-[calc(100dvh-5.4375rem)] tablet:scroll-mt-[5.4375rem] shell:top-[5.1875rem] shell:h-[calc(100dvh-5.1875rem)] shell:scroll-mt-[5.1875rem] -mt-6 -mb-16 flex min-h-0 flex-col";2021/** What a scroller inside the box puts on itself: all the box's height, scrolling only once the box is stuck. */22export const PANE_SCROLLER =23 "min-h-0 flex-1 overflow-auto overscroll-y-auto group-data-[stuck=false]/pane:overflow-y-hidden";2425/** Pixels from a computed length (`83px`); nothing, none. */26const pixels = (length: string) => Number(/^-?[\d.]+/u.exec(length)?.[0] ?? 0);2728/**29 * Follow whether `box` has reached its sticky place, read on every scroll30 * (of main, or of the page below the shell's width), telling `stuck`; the31 * returned function stops.32 */33const followStuck = (box: HTMLElement, stuck: (now: boolean) => void) => {34 let frame = 0;3536 const read = () => {37 frame = 0;38 stuck(39 box.getBoundingClientRect().top <=40 pixels(globalThis.getComputedStyle(box).top) + 141 );42 };4344 const onScroll = () => {45 if (frame === 0) {46 frame = globalThis.requestAnimationFrame(read);47 }48 };4950 read();51 document.addEventListener("scroll", onScroll, {52 capture: true,53 passive: true,54 });55 globalThis.addEventListener("resize", onScroll, { passive: true });5657 return () => {58 globalThis.cancelAnimationFrame(frame);59 document.removeEventListener("scroll", onScroll, { capture: true });60 globalThis.removeEventListener("resize", onScroll);61 };62};6364/** Whether the box has reached its sticky place; none before the page runs. */65const useStuck = () => {66 const pane = useRef<HTMLDivElement>(null);67 const [stuck, setStuck] = useState<boolean>();6869 useEffect(() => {70 const box = pane.current;7172 return box === null ? undefined : followStuck(box, setStuck);73 }, []);7475 return { pane, stuck };76};7778export const DiffPane = ({ children }: { readonly children: ReactNode }) => {79 const { pane, stuck } = useStuck();8081 return (82 <div83 className={PANE}84 data-diff-pane=""85 data-files=""86 data-stuck={stuck}87 ref={pane}88 >89 {children}90 </div>91 );92};