Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/frontend/src/components/settings/SettingsShell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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<HTMLDivElement>(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
Expand Down Expand Up @@ -362,15 +364,15 @@ export function SettingsShell({ title, description, groups, sections, children }
</aside>

{/* 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). */}
<div
className={cn(
"min-w-0 max-w-[44rem] flex-1 pb-[max(8rem,calc(100dvh-12rem))]",
"min-w-0 max-w-[44rem] flex-1",
"max-sm:[&_input:not([type=checkbox],[type=radio],[type=file],[type=color],[data-input-otp])]:min-h-11",
"max-sm:[&_button:not([aria-label],[role=switch],[role=radio],[role=checkbox])]:min-h-11",
"max-sm:[&_button[role=combobox]]:min-h-11",
Expand Down Expand Up @@ -425,6 +427,7 @@ export function SettingsShell({ title, description, groups, sections, children }
)}
<Separator className="mb-6 mt-5 md:mb-10 md:mt-7" />
{children}
<div ref={spacerRef} aria-hidden style={{ height: spacerHeight }} />
</div>
</div>
</div>
Expand Down
25 changes: 24 additions & 1 deletion packages/frontend/src/components/settings/useScrollSpy.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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" },
Expand Down
89 changes: 81 additions & 8 deletions packages/frontend/src/components/settings/useScrollSpy.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<HTMLElement | null>): 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;
}
Loading