diff --git a/packages/frontend/src/components/settings/SettingsShell.tsx b/packages/frontend/src/components/settings/SettingsShell.tsx index 16e9e35..68bd7cb 100644 --- a/packages/frontend/src/components/settings/SettingsShell.tsx +++ b/packages/frontend/src/components/settings/SettingsShell.tsx @@ -4,7 +4,7 @@ import type { LucideIcon } from "lucide-react"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectSeparator, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Separator } from "@/components/ui/separator"; import { cn } from "@/lib/utils"; -import { useScrollSpy } from "./useScrollSpy"; +import { useScrollSpy, useTrailingSpacer } from "./useScrollSpy"; export interface SettingsSectionDef { /** Matches the `id` on the section's content wrapper in the page JSX. */ @@ -277,6 +277,8 @@ export function SettingsShell({ title, description, groups, sections, children } const orderedIds = resolved.flatMap(group => group.sections.map(s => s.id)); const idsKey = orderedIds.join("|"); const { active, scrollTo } = useScrollSpy(orderedIds); + const spacerRef = useRef(null); + const spacerHeight = useTrailingSpacer(orderedIds[orderedIds.length - 1] ?? null, spacerRef); // Deep-link support (e.g. /settings#billing, /settings#sessions): on hash // change, scroll to the section once it has rendered. scrollTo also sets the @@ -362,15 +364,15 @@ export function SettingsShell({ title, description, groups, sections, children } {/* Content column - one measure for header, divider and sections. - The bottom padding is most of a viewport tall so every section, - even a short trailing one (Sessions, Danger zone), can scroll up to - the scroll-spy reading line and a jump lands it at the top. - Below sm, text inputs, select triggers and text buttons grow to a + It ends in a measured spacer just tall enough that the last + section, even a short one (Sessions, Danger zone), can scroll up + to the scroll-spy reading line - but no further, so the page can't + be scrolled until it's nearly off screen. Below sm, text inputs, select triggers and text buttons grow to a 44px touch target (icon buttons carry an aria-label and keep their own square sizing; switches/radios/OTP slots are excluded). */}
{children} +
diff --git a/packages/frontend/src/components/settings/useScrollSpy.test.ts b/packages/frontend/src/components/settings/useScrollSpy.test.ts index a52eee8..f46f017 100644 --- a/packages/frontend/src/components/settings/useScrollSpy.test.ts +++ b/packages/frontend/src/components/settings/useScrollSpy.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { pickActiveSection, readingLine } from "./useScrollSpy"; +import { pickActiveSection, readingLine, trailingSpacerHeight } from "./useScrollSpy"; import { resolveOutline, type SettingsGroupDef, type SettingsSectionDef } from "./SettingsShell"; describe("pickActiveSection", () => { @@ -57,6 +57,29 @@ describe("pickActiveSection", () => { }); }); +describe("trailingSpacerHeight", () => { + // 1000px viewport -> reading line at 160px. + it("adds just enough tail for a short last section to reach the reading line", () => { + // Danger zone starts at 3000 and the content (incl. its own bottom padding) + // ends at 3120: 120px of natural tail. + const spacer = trailingSpacerHeight({ viewportHeight: 1000, naturalScrollHeight: 3120, lastTop: 3000 }); + expect(spacer).toBe(1000 - 160 - 120 + 1); + // At max scroll, the last section's top sits at (or a hair above) the line. + const maxScroll = 3120 + spacer - 1000; + const topInViewport = 3000 - maxScroll; + expect(topInViewport).toBeLessThanOrEqual(readingLine(1000)); + expect(topInViewport).toBeGreaterThan(readingLine(1000) - 2); + }); + + it("adds nothing when the last section is already tall enough", () => { + expect(trailingSpacerHeight({ viewportHeight: 1000, naturalScrollHeight: 4000, lastTop: 3000 })).toBe(0); + }); + + it("adds nothing when the page fits without scrolling", () => { + expect(trailingSpacerHeight({ viewportHeight: 1000, naturalScrollHeight: 900, lastTop: 700 })).toBe(0); + }); +}); + describe("resolveOutline", () => { const groups: SettingsGroupDef[] = [ { id: "site", label: "Site" }, diff --git a/packages/frontend/src/components/settings/useScrollSpy.ts b/packages/frontend/src/components/settings/useScrollSpy.ts index 6baa25a..ff110fa 100644 --- a/packages/frontend/src/components/settings/useScrollSpy.ts +++ b/packages/frontend/src/components/settings/useScrollSpy.ts @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useRef, useState } from "react"; +import { useCallback, useEffect, useLayoutEffect, useRef, useState, type RefObject } from "react"; /** * Fraction of the scroll viewport (from its top) that acts as the "reading @@ -36,13 +36,13 @@ export function readingLine(viewportHeight: number): number { * (in the supplied order) whose top has crossed the reading line, or the first * present section when none has. * - * SettingsShell pads the end of its content column by most of a viewport, so - * every section - including short trailing ones like Sessions or Danger zone - - * can scroll all the way up to the line; no "sweep" is needed. The only - * bottom-of-page special case left is narrow: when the container is clamped at - * the bottom AND the last section's top is already in the upper half of the - * viewport (e.g. a shorter-than-expected viewport ate the padding), the last - * section wins. A last section merely peeking in at the bottom never steals + * SettingsShell ends its content column with a measured spacer (see + * trailingSpacerHeight) just tall enough for the last section - even a short + * one like Sessions or Danger zone - to scroll up to the line, and no further; + * no "sweep" is needed. The only bottom-of-page special case left is narrow: + * when the container is clamped at the bottom AND the last section's top is + * already in the upper half of the viewport (a safety net if the spacer is + * momentarily stale), the last section wins. A last section merely peeking in at the bottom never steals * the highlight from the section being read. */ export function pickActiveSection(orderedIds: string[], geometry: SpyGeometry): string | null { @@ -206,3 +206,76 @@ export function useScrollSpy(orderedIds: string[]): ScrollSpy { return { active, scrollTo }; } + +/** + * Height for the spacer that ends a settings content column: exactly enough + * extra scroll for the last section's top to reach the reading line at max + * scroll (so scroll-spy can select it), and never more - so the page can't be + * scrolled until the last section is nearly off screen. 0 when the content + * already has that much tail below the last section, or fits without + * scrolling. All px, in scroll-content coordinates; `naturalScrollHeight` is + * the scroll height without the spacer. + */ +export function trailingSpacerHeight({ + viewportHeight, + naturalScrollHeight, + lastTop, +}: { + viewportHeight: number; + naturalScrollHeight: number; + lastTop: number; +}): number { + if (naturalScrollHeight <= viewportHeight) return 0; + const naturalTail = naturalScrollHeight - lastTop; + // +1px so sub-pixel layout can't leave the top a hair below the line. + return Math.max(0, Math.ceil(viewportHeight - readingLine(viewportHeight) - naturalTail + 1)); +} + +/** + * Measures the trailing spacer (see trailingSpacerHeight) for section + * `lastId`. `spacerRef` is the spacer element; its parent (the content column) + * and the scroll container are observed so the height tracks async-loaded + * content and viewport resizes. A layout effect, so a hash deep-link's scroll + * already sees the final height. + */ +export function useTrailingSpacer(lastId: string | null, spacerRef: RefObject): number { + const [height, setHeight] = useState(0); + + useLayoutEffect(() => { + const spacer = spacerRef.current; + if (!lastId || !spacer) { + setHeight(0); + return; + } + const root = scrollContainerOf(spacer); + + const measure = () => { + const last = document.getElementById(lastId); + if (!last) return; + const scroller = root ?? document.scrollingElement ?? document.documentElement; + const viewportHeight = root ? root.clientHeight : window.innerHeight; + const rootTop = root ? root.getBoundingClientRect().top : 0; + setHeight( + trailingSpacerHeight({ + viewportHeight, + naturalScrollHeight: scroller.scrollHeight - spacer.offsetHeight, + lastTop: last.getBoundingClientRect().top - rootTop + scroller.scrollTop, + }), + ); + }; + + measure(); + // The result excludes the spacer's own height, so the re-measure its + // resize triggers settles immediately. + const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(measure); + ro?.observe(spacer.parentElement ?? spacer); + if (root) ro?.observe(root); + window.addEventListener("resize", measure); + return () => { + ro?.disconnect(); + window.removeEventListener("resize", measure); + }; + }, [lastId, spacerRef]); + + return height; +}