Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

scroll-anchor.ts

92 lines3.1 KB

1/**
2 * Scroll positions anchored to elements instead of raw pixels.
3 *
4 * When the app sheet closes, the guide must be exactly where the reader left
5 * it, even if something above that point changed height in the meantime (a
6 * section gained its "You did it" check, a font finished loading…). So instead
7 * of `scrollTop` alone we remember which section was at the top of the view
8 * and how far it was from the top edge, and put it back there.
9 */
10​
11/** An anchor element's position relative to the top edge of the scroll viewport. */
12export interface AnchorBox {
13 id: string;
14 top: number;
15 bottom: number;
17​
18export interface ScrollAnchor {
19 scrollTop: number;
20 /** The anchor at the top edge of the view, if any. */
21 anchorId: string | null;
22 /** Where that anchor's top was, relative to the top edge (≤ 0 once scrolled into it). */
23 anchorTop: number;
25​
26/**
27 * The anchor that contains the line `offset` px below the top of the view. In
28 * a gap between anchors, the last one above the line wins; above the first
29 * anchor there is none.
30 */
31export function anchorAt(boxes: readonly AnchorBox[], offset: number): AnchorBox | undefined {
32 let match: AnchorBox | undefined;
33 for (const box of boxes) {
34 if (box.top > offset) break;
35 match = box;
36 }
37 return match;
39​
40/** The `scrollTop` that puts the anchor back where it was, clamped to the scroll range. */
41export function scrollTopForAnchor({
42 saved,
43 currentScrollTop,
44 currentAnchorTop,
45 maxScrollTop,
46}: {
47 saved: ScrollAnchor;
48 currentScrollTop: number;
49 /** Where the anchor is now, or `undefined` if it's gone. */
50 currentAnchorTop: number | undefined;
51 maxScrollTop: number;
52}): number {
53 const target =
54 currentAnchorTop === undefined
55 ? saved.scrollTop
56 : currentScrollTop + (currentAnchorTop - saved.anchorTop);
57 return Math.min(Math.max(target, 0), Math.max(maxScrollTop, 0));
59​
60/* DOM side ----------------------------------------------------------------- */
61​
62const ANCHOR_SELECTOR = "[data-scroll-anchor]";
63​
64/** Measures every `[data-scroll-anchor]` element inside `container`, in document order. */
65export function measureAnchors(container: HTMLElement): AnchorBox[] {
66 const viewportTop = container.getBoundingClientRect().top;
67 return Array.from(container.querySelectorAll<HTMLElement>(ANCHOR_SELECTOR), (element) => {
68 const rect = element.getBoundingClientRect();
69 return { id: element.id, top: rect.top - viewportTop, bottom: rect.bottom - viewportTop };
70 });
72​
73export function captureScrollAnchor(container: HTMLElement): ScrollAnchor {
74 const anchor = anchorAt(measureAnchors(container), 0);
75 return {
76 scrollTop: container.scrollTop,
77 anchorId: anchor?.id ?? null,
78 anchorTop: anchor?.top ?? 0,
79 };
81​
82export function restoreScrollAnchor(container: HTMLElement, saved: ScrollAnchor) {
83 const current = saved.anchorId
84 ? measureAnchors(container).find((box) => box.id === saved.anchorId)
85 : undefined;
86 container.scrollTop = scrollTopForAnchor({
87 saved,
88 currentScrollTop: container.scrollTop,
89 currentAnchorTop: current?.top,
90 maxScrollTop: container.scrollHeight - container.clientHeight,
91 });