GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(web): highlight visible settings sections · pingdotgg/t3code@c952f74

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • apps/web/src
    • components/settings
      • SettingsSidebarNav.tsx
      • settingsSectionVisibility.test.ts
      • settingsSectionVisibility.ts
    • routes
      • settings.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎apps/web/src/components/settings/SettingsSidebarNav.tsx‎

Copy file name to clipboardExpand all lines: apps/web/src/components/settings/SettingsSidebarNav.tsx
+37-1Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
Settings2Icon,
2323
XIcon,
2424
} from "lucide-react";
25-
import { useLocation, useNavigate } from "@tanstack/react-router";
25+
import { useLocation, useNavigate, useRouterState } from "@tanstack/react-router";
2626

2727
import { Button } from "../ui/button";
2828
import { Input } from "../ui/input";
@@ -42,6 +42,7 @@ import {
4242
} from "../ui/sidebar";
4343
import { SidebarUtilityMenu } from "../sidebar/SidebarChrome";
4444
import { scrollToSettingsTarget } from "./settingsLayout";
45+
import { observeSettingsSectionVisibility } from "./settingsSectionVisibility";
4546
import { useSettingsSidebarListAnimation } from "./settingsSidebarListAnimation";
4647
import {
4748
searchSettings,
@@ -100,6 +101,7 @@ const SETTINGS_PAGE_SECTIONS: Partial<
100101
"/settings/appearance": [
101102
{ label: "Colors & themes", targetId: "appearance" },
102103
{ label: "Interface", targetId: "appearance-interface" },
104+
{ label: "Motion", targetId: "motion" },
103105
{ label: "Typography", targetId: "typography" },
104106
],
105107
"/settings/source-control": [
@@ -112,6 +114,8 @@ const SETTINGS_PAGE_SECTIONS: Partial<
112114
],
113115
};
114116

117+
const EMPTY_VISIBLE_SETTINGS_SECTION_IDS: ReadonlySet<string> = new Set();
118+
115119
function SettingsSectionIcon({ to }: { to: SettingsPath }) {
116120
const Icon = SETTINGS_SECTION_ICONS[to];
117121
return <Icon className="mt-0.5 size-3.5 shrink-0 text-sidebar-muted-foreground/60" />;
@@ -151,11 +155,18 @@ function SettingsMenuRegion({
151155
export function SettingsSidebarNav({ pathname }: { pathname: string }) {
152156
const navigate = useNavigate();
153157
const currentHash = useLocation({ select: (location) => location.hash });
158+
const resolvedPathname = useRouterState({
159+
select: (state) => state.resolvedLocation?.pathname,
160+
});
154161
const { isMobile, setOpenMobile, open, setOpen } = useSidebar();
155162
const searchInputRef = useRef<HTMLInputElement>(null);
156163
const menuRef = useRef<HTMLUListElement>(null);
157164
const [query, setQuery] = useState("");
158165
const [activeResultIndex, setActiveResultIndex] = useState(0);
166+
const [sectionVisibility, setSectionVisibility] = useState<{
167+
readonly path: SettingsPath;
168+
readonly targetIds: ReadonlySet<string>;
169+
} | null>(null);
159170
const searchableItems = useAvailableSettingsSearchItems();
160171
const results = useMemo(() => searchSettings(query, searchableItems), [query, searchableItems]);
161172
const isSearching = query.trim().length > 0;
@@ -166,8 +177,32 @@ export function SettingsSidebarNav({ pathname }: { pathname: string }) {
166177
const activeSettingsPath = SETTINGS_NAV_ITEMS.find(
167178
(item) => pathname === item.to || pathname.startsWith(`${item.to}/`),
168179
)?.to;
180+
const observedSettingsPath = SETTINGS_NAV_ITEMS.find(
181+
(item) => resolvedPathname === item.to || resolvedPathname?.startsWith(`${item.to}/`) === true,
182+
)?.to;
183+
const observedPageSections = observedSettingsPath
184+
? SETTINGS_PAGE_SECTIONS[observedSettingsPath]
185+
: undefined;
186+
const visiblePageSectionIds =
187+
sectionVisibility && sectionVisibility.path === activeSettingsPath
188+
? sectionVisibility.targetIds
189+
: EMPTY_VISIBLE_SETTINGS_SECTION_IDS;
169190
const prepareMenuAnimation = useSettingsSidebarListAnimation(menuRef, menuItemOrderKey);
170191

192+
useEffect(() => {
193+
if (!observedSettingsPath || !observedPageSections) return;
194+
const container = document.querySelector<HTMLElement>("[data-settings-page-layout]");
195+
if (!container) return;
196+
197+
return observeSettingsSectionVisibility({
198+
container,
199+
targetIds: observedPageSections.map((section) => section.targetId),
200+
onChange(targetIds) {
201+
setSectionVisibility({ path: observedSettingsPath, targetIds: new Set(targetIds) });
202+
},
203+
});
204+
}, [observedPageSections, observedSettingsPath]);
205+
171206
useEffect(() => {
172207
setActiveResultIndex((index) => Math.min(index, Math.max(results.length - 1, 0)));
173208
}, [results.length]);
@@ -429,6 +464,7 @@ export function SettingsSidebarNav({ pathname }: { pathname: string }) {
429464
<SidebarMenuSubButton
430465
render={<button type="button" />}
431466
size="sm"
467+
isActive={visiblePageSectionIds.has(section.targetId)}
432468
className="w-full text-sidebar-muted-foreground/65"
433469
onClick={() => handlePageSectionClick(item.to, section.targetId)}
434470
>
Collapse file

‎apps/web/src/components/settings/settingsSectionVisibility.test.ts‎

Copy file name to clipboard
+212Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
import { describe, expect, it, vi } from "vite-plus/test";
2+
3+
import {
4+
observeSettingsSectionVisibility,
5+
type SettingsSectionVisibilityEnvironment,
6+
} from "./settingsSectionVisibility";
7+
8+
type VisibilityEntry = Pick<
9+
IntersectionObserverEntry,
10+
"intersectionRatio" | "isIntersecting" | "target"
11+
>;
12+
13+
function createHarness(
14+
targetIds: ReadonlyArray<string>,
15+
initialRoot: Element | null = { name: "initial-root" } as unknown as Element,
16+
) {
17+
const targets = new Map(
18+
targetIds.map((targetId) => [targetId, { targetId } as unknown as Element]),
19+
);
20+
const observed = new Set<Element>();
21+
const unobserve = vi.fn((target: Element) => observed.delete(target));
22+
const disconnectIntersections = vi.fn();
23+
const disconnectMutations = vi.fn();
24+
const intersectionCallbacks: Array<(entries: ReadonlyArray<VisibilityEntry>) => void> = [];
25+
const intersectionRoots: Element[] = [];
26+
let root = initialRoot;
27+
let onMutation = () => {};
28+
29+
const environment: SettingsSectionVisibilityEnvironment = {
30+
findRoot: () => root,
31+
findTarget: (_root, targetId) => targets.get(targetId) ?? null,
32+
createIntersectionObserver(callback, observedRoot) {
33+
intersectionCallbacks.push(callback);
34+
intersectionRoots.push(observedRoot);
35+
return {
36+
observe: (target) => observed.add(target),
37+
unobserve,
38+
disconnect: disconnectIntersections,
39+
};
40+
},
41+
createMutationObserver(callback) {
42+
onMutation = callback;
43+
return { disconnect: disconnectMutations };
44+
},
45+
};
46+
47+
return {
48+
environment,
49+
targets,
50+
observed,
51+
unobserve,
52+
disconnectIntersections,
53+
disconnectMutations,
54+
intersectionCallbacks,
55+
intersectionRoots,
56+
intersect(entries: ReadonlyArray<VisibilityEntry>) {
57+
intersectionCallbacks.at(-1)?.(entries);
58+
},
59+
mutate() {
60+
onMutation();
61+
},
62+
replaceRoot(nextRoot: Element | null) {
63+
root = nextRoot;
64+
onMutation();
65+
},
66+
};
67+
}
68+
69+
function visibleEntry(
70+
target: Element,
71+
{ isIntersecting = true, intersectionRatio = 1 } = {},
72+
): VisibilityEntry {
73+
return { target, isIntersecting, intersectionRatio };
74+
}
75+
76+
describe("settings section visibility", () => {
77+
it("accumulates visible sections and emits them in sidebar order", () => {
78+
const harness = createHarness(["one", "two", "three"]);
79+
const emissions: ReadonlyArray<string>[] = [];
80+
observeSettingsSectionVisibility({
81+
container: {} as Element,
82+
targetIds: ["one", "two", "three"],
83+
onChange: (visible) => emissions.push(visible),
84+
environment: harness.environment,
85+
});
86+
87+
harness.intersect([visibleEntry(harness.targets.get("two")!)]);
88+
harness.intersect([visibleEntry(harness.targets.get("one")!)]);
89+
90+
expect(emissions).toEqual([[], ["two"], ["one", "two"]]);
91+
});
92+
93+
it("treats zero-ratio and non-intersecting entries as hidden", () => {
94+
const harness = createHarness(["one", "two"]);
95+
const emissions: ReadonlyArray<string>[] = [];
96+
observeSettingsSectionVisibility({
97+
container: {} as Element,
98+
targetIds: ["one", "two"],
99+
onChange: (visible) => emissions.push(visible),
100+
environment: harness.environment,
101+
});
102+
103+
const one = harness.targets.get("one")!;
104+
const two = harness.targets.get("two")!;
105+
harness.intersect([visibleEntry(one), visibleEntry(two)]);
106+
harness.intersect([visibleEntry(one, { intersectionRatio: 0 })]);
107+
harness.intersect([visibleEntry(two, { isIntersecting: false })]);
108+
109+
expect(emissions).toEqual([[], ["one", "two"], ["two"], []]);
110+
});
111+
112+
it("resyncs replaced and removed targets without retaining stale visibility", () => {
113+
const harness = createHarness(["dynamic"]);
114+
const emissions: ReadonlyArray<string>[] = [];
115+
observeSettingsSectionVisibility({
116+
container: {} as Element,
117+
targetIds: ["dynamic"],
118+
onChange: (visible) => emissions.push(visible),
119+
environment: harness.environment,
120+
});
121+
122+
const firstTarget = harness.targets.get("dynamic")!;
123+
harness.intersect([visibleEntry(firstTarget)]);
124+
const replacementTarget = { targetId: "dynamic-replacement" } as unknown as Element;
125+
harness.targets.set("dynamic", replacementTarget);
126+
harness.mutate();
127+
128+
expect(harness.unobserve).toHaveBeenCalledWith(firstTarget);
129+
expect(harness.observed.has(replacementTarget)).toBe(true);
130+
expect(emissions.at(-1)).toEqual([]);
131+
132+
harness.intersect([visibleEntry(firstTarget)]);
133+
expect(emissions.at(-1)).toEqual([]);
134+
harness.intersect([visibleEntry(replacementTarget)]);
135+
expect(emissions.at(-1)).toEqual(["dynamic"]);
136+
137+
harness.targets.delete("dynamic");
138+
harness.mutate();
139+
expect(harness.unobserve).toHaveBeenCalledWith(replacementTarget);
140+
expect(emissions.at(-1)).toEqual([]);
141+
});
142+
143+
it("rebinds when navigation replaces the settings scroll root", () => {
144+
const harness = createHarness(["section"]);
145+
const emissions: ReadonlyArray<string>[] = [];
146+
observeSettingsSectionVisibility({
147+
container: {} as Element,
148+
targetIds: ["section"],
149+
onChange: (visible) => emissions.push(visible),
150+
environment: harness.environment,
151+
});
152+
153+
const firstTarget = harness.targets.get("section")!;
154+
harness.intersect([visibleEntry(firstTarget)]);
155+
const firstObserverCallback = harness.intersectionCallbacks[0]!;
156+
const nextRoot = { name: "next-root" } as unknown as Element;
157+
const nextTarget = { targetId: "next-section" } as unknown as Element;
158+
harness.targets.set("section", nextTarget);
159+
harness.replaceRoot(nextRoot);
160+
161+
expect(harness.disconnectIntersections).toHaveBeenCalledOnce();
162+
expect(harness.intersectionRoots.at(-1)).toBe(nextRoot);
163+
expect(harness.observed.has(nextTarget)).toBe(true);
164+
expect(emissions.at(-1)).toEqual([]);
165+
166+
firstObserverCallback([visibleEntry(firstTarget)]);
167+
expect(emissions.at(-1)).toEqual([]);
168+
harness.intersect([visibleEntry(nextTarget)]);
169+
expect(emissions.at(-1)).toEqual(["section"]);
170+
});
171+
172+
it("starts observing when the settings scroll root mounts later", () => {
173+
const harness = createHarness(["section"], null);
174+
const emissions: ReadonlyArray<string>[] = [];
175+
observeSettingsSectionVisibility({
176+
container: {} as Element,
177+
targetIds: ["section"],
178+
onChange: (visible) => emissions.push(visible),
179+
environment: harness.environment,
180+
});
181+
182+
expect(emissions).toEqual([[]]);
183+
expect(harness.intersectionRoots).toEqual([]);
184+
185+
const root = { name: "mounted-root" } as unknown as Element;
186+
harness.replaceRoot(root);
187+
harness.intersect([visibleEntry(harness.targets.get("section")!)]);
188+
189+
expect(harness.intersectionRoots).toEqual([root]);
190+
expect(emissions.at(-1)).toEqual(["section"]);
191+
});
192+
193+
it("disconnects both observers and ignores callbacks after cleanup", () => {
194+
const harness = createHarness(["one"]);
195+
const onChange = vi.fn();
196+
const cleanup = observeSettingsSectionVisibility({
197+
container: {} as Element,
198+
targetIds: ["one"],
199+
onChange,
200+
environment: harness.environment,
201+
});
202+
203+
cleanup();
204+
harness.intersect([visibleEntry(harness.targets.get("one")!)]);
205+
harness.mutate();
206+
207+
expect(harness.disconnectIntersections).toHaveBeenCalledOnce();
208+
expect(harness.disconnectMutations).toHaveBeenCalledOnce();
209+
expect(onChange).toHaveBeenCalledTimes(1);
210+
expect(onChange).toHaveBeenLastCalledWith([]);
211+
});
212+
});

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.