Skip to content

Commit 90cfa08

Browse files
authored
Fix the reference sidebar and scrolling, table line breaks and the mobile menu (#242)
* Mark the current page in the reference sidebar trailingSlash: true gives usePathname() a trailing slash that the sidebar's hrefs lack, so no entry has matched since, and the sidebar never scrolled to the current one. Signed-off-by: Guanzhou Song <26541184+GuanzhouSong@users.noreply.github.com> * Scroll reference pages with the window, with a sticky sidebar The 100vh pane started below the banner and navbar, so its last ~100px sat below the fold, the window and the article scrolled separately, and the navbar covered the pane's top once the window moved. Signed-off-by: Guanzhou Song <26541184+GuanzhouSong@users.noreply.github.com> * Render <br> in Markdown as a line break react-markdown prints raw HTML as text, so the $densify Limitations table showed literal <br>. Only <br> is converted; other HTML stays escaped. Signed-off-by: Guanzhou Song <26541184+GuanzhouSong@users.noreply.github.com> * Fit the mobile menu to the screen and close it on navigation The open menu had no height limit inside the sticky nav, so on short screens its last links were out of reach. It also stayed open after Escape, the logo, or Back. Signed-off-by: Guanzhou Song <26541184+GuanzhouSong@users.noreply.github.com> --------- Signed-off-by: Guanzhou Song <26541184+GuanzhouSong@users.noreply.github.com> Co-authored-by: Guanzhou Song <26541184+GuanzhouSong@users.noreply.github.com>
1 parent 7f67326 commit 90cfa08

6 files changed

Lines changed: 97 additions & 11 deletions

File tree

‎app/components/Index.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ export default function Index({
4040
}: {
4141
groupedReferences: Record<string, Record<string, ReferencePage[]>>;
4242
}) {
43-
const pathname = usePathname();
43+
// trailingSlash: true makes this "/x/", but the hrefs compared below have no slash
44+
const pathname = usePathname().replace(/\/+$/, '');
4445
const activeItemRef = useRef<HTMLAnchorElement>(null);
4546
const containerRef = useRef<HTMLElement>(null);
4647

@@ -114,6 +115,7 @@ export default function Index({
114115
<Link
115116
href={`/docs/reference/${typeSection.type}`}
116117
ref={isTypeActive ? activeItemRef : null}
118+
aria-current={isTypeActive ? 'page' : undefined}
117119
className={`flex-1 px-3 py-3 rounded-lg text-sm font-semibold uppercase transition-all duration-200 ${
118120
isTypeActive
119121
? "bg-blue-500/20 text-blue-300 border border-blue-500/30"
@@ -147,6 +149,7 @@ export default function Index({
147149
<Link
148150
href={`/docs/reference/${typeSection.type}/${categorySection.category}`}
149151
ref={isCategoryActive ? activeItemRef : null}
152+
aria-current={isCategoryActive ? 'page' : undefined}
150153
className={`flex-1 px-3 py-3 rounded-lg text-sm transition-all duration-200 ${
151154
isCategoryActive
152155
? "bg-blue-500/20 text-blue-300 border border-blue-500/30"
@@ -170,6 +173,7 @@ export default function Index({
170173
<Link
171174
href={itemPath}
172175
ref={isItemActive ? activeItemRef : null}
176+
aria-current={isItemActive ? 'page' : undefined}
173177
className={`flex-1 px-3 py-3 rounded-lg text-sm transition-all duration-200 ${
174178
isItemActive
175179
? "bg-blue-500/20 text-blue-300 border border-blue-500/30"

‎app/components/Markdown.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ interface MarkdownProps {
1515
sourcePath: string;
1616
}
1717

18+
// GFM table cells can't hold newlines, so docs break lines with <br>; render that one tag, not raw HTML.
19+
function remarkBrTags() {
20+
return (tree: any) => {
21+
const visit = (node: any) => node.children?.forEach((child: any, i: number) => {
22+
if (child.type === 'html' && /^<br\s*\/?>$/i.test(child.value)) node.children[i] = { type: 'break' };
23+
else visit(child);
24+
});
25+
visit(tree);
26+
};
27+
}
28+
29+
const remarkPlugins = [remarkGfm, remarkBrTags];
30+
1831
export default function Markdown({ content, sourcePath }: MarkdownProps) {
1932
const processedContent = useMemo(() => {
2033
// Split content by H2 headings to group sections
@@ -29,7 +42,7 @@ export default function Markdown({ content, sourcePath }: MarkdownProps) {
2942
elements.push(
3043
<div key={`intro-${index}`} className="mb-8">
3144
<ReactMarkdown
32-
remarkPlugins={[remarkGfm]}
45+
remarkPlugins={remarkPlugins}
3346
components={getMarkdownComponents(sourcePath)}
3447
>
3548
{section}
@@ -49,7 +62,7 @@ export default function Markdown({ content, sourcePath }: MarkdownProps) {
4962
{title}
5063
</h2>
5164
<ReactMarkdown
52-
remarkPlugins={[remarkGfm]}
65+
remarkPlugins={remarkPlugins}
5366
components={getMarkdownComponents(sourcePath)}
5467
>
5568
{sectionContent}

‎app/components/Navbar.tsx‎

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import { useEffect, useRef, useState, type ReactNode } from "react";
44
import Image from "next/image";
55
import Link from "next/link";
6+
import { usePathname } from "next/navigation";
67
// The Jekyll layout renders the same file, so the blog and announcement pages show this nav too
78
import nav from "../../blogs/_data/nav.json";
89
import { withBasePath } from "../services/sitePath";
@@ -96,14 +97,45 @@ function ExternalIcon() {
9697
);
9798
}
9899

100+
// The banner above the sticky nav varies in height, so fit the open menu to the screen space actually left
101+
function fitToViewport(menu: HTMLDivElement | null) {
102+
if (!menu) return;
103+
const fit = () => {
104+
menu.style.maxHeight = `${window.innerHeight - menu.getBoundingClientRect().top}px`;
105+
};
106+
fit();
107+
window.addEventListener("resize", fit);
108+
return () => window.removeEventListener("resize", fit);
109+
}
110+
99111
export default function Navbar() {
112+
const pathname = usePathname();
100113
const [isMenuOpen, setIsMenuOpen] = useState(false);
114+
const [menuPathname, setMenuPathname] = useState(pathname);
101115
const [openGroup, setOpenGroup] = useState<string | null>(null);
102116
const desktopNav = useRef<HTMLDivElement>(null);
117+
const menuButton = useRef<HTMLButtonElement>(null);
103118

104119
const closeMenu = () => setIsMenuOpen(false);
105120
const closeGroup = () => setOpenGroup(null);
106121

122+
// Close on any navigation, back/forward included; resetting during render avoids a frame of open menu on the new page
123+
if (menuPathname !== pathname) {
124+
setMenuPathname(pathname);
125+
setIsMenuOpen(false);
126+
}
127+
128+
useEffect(() => {
129+
if (!isMenuOpen) return;
130+
const closeOnEscape = (event: KeyboardEvent) => {
131+
if (event.key !== "Escape") return;
132+
setIsMenuOpen(false);
133+
menuButton.current?.focus();
134+
};
135+
document.addEventListener("keydown", closeOnEscape);
136+
return () => document.removeEventListener("keydown", closeOnEscape);
137+
}, [isMenuOpen]);
138+
107139
useEffect(() => {
108140
if (!openGroup) return;
109141
// Safari never focuses a clicked button, so blur alone cannot close the panel
@@ -127,7 +159,7 @@ export default function Navbar() {
127159
<nav className="sticky top-0 z-50 border-b border-neutral-700 bg-neutral-900/95 shadow-sm backdrop-blur">
128160
<div className="mx-auto max-w-7xl px-4 sm:px-6 lg:px-8">
129161
<div className="flex items-center justify-between gap-4 py-3 md:h-16 md:py-0">
130-
<Link href="/" className="flex shrink-0 items-center gap-2 text-xl font-bold text-white sm:text-2xl">
162+
<Link href="/" onClick={closeMenu} className="flex shrink-0 items-center gap-2 text-xl font-bold text-white sm:text-2xl">
131163
<Image
132164
src={withBasePath("/images/DocumentDB Logo - background removed.png")}
133165
alt="DocumentDB Logo"
@@ -215,6 +247,7 @@ export default function Navbar() {
215247
</div>
216248

217249
<button
250+
ref={menuButton}
218251
type="button"
219252
className="inline-flex h-10 w-10 items-center justify-center rounded-md border border-neutral-700 text-gray-200 transition-colors hover:border-blue-400 hover:text-white focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-blue-400 lg:hidden"
220253
aria-expanded={isMenuOpen}
@@ -235,7 +268,7 @@ export default function Navbar() {
235268
</div>
236269

237270
{isMenuOpen ? (
238-
<div id="mobile-navigation" className="border-t border-neutral-800 py-3 lg:hidden">
271+
<div id="mobile-navigation" ref={fitToViewport} className="overflow-y-auto border-t border-neutral-800 py-3 lg:hidden">
239272
<div className="flex flex-col gap-1">
240273
{links.map((item) =>
241274
"items" in item ? (

‎app/docs/reference/layout.tsx‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ export default function ReferenceLayout({
1717
}>) {
1818
const groupedReferences = getReferencesGroupedByTypeAndCategory();
1919

20-
// Phones get no sidebar, so the page scrolls rather than a 100vh pane half under the navbar
20+
// overflow-clip, not hidden: hidden makes a scroll container and the sidebar would stop sticking
2121
return (
22-
<div className="min-h-screen bg-neutral-900 relative overflow-hidden md:max-h-screen">
22+
<div className="min-h-screen bg-neutral-900 relative overflow-clip">
2323
{/* Background elements */}
2424
<div className="absolute inset-0 bg-gradient-to-br from-neutral-900 via-neutral-800 to-black"></div>
2525
<div className="absolute inset-0 opacity-5">
@@ -29,9 +29,9 @@ export default function ReferenceLayout({
2929
style={{ animationDelay: "1.5s" }}
3030
></div>
3131
</div>
32-
<div className="relative flex md:h-screen">
33-
{/* Sidebar (desktop only; mobile gets the disclosure below) */}
34-
<div className="hidden w-80 bg-neutral-800/50 backdrop-blur-sm border-r border-neutral-700/50 md:flex flex-col h-full">
32+
<div className="relative flex">
33+
{/* Sidebar (desktop only; mobile gets the disclosure below), held under the 4rem navbar */}
34+
<div className="hidden w-80 bg-neutral-800/50 backdrop-blur-sm border-r border-neutral-700/50 md:flex flex-col md:sticky md:top-16 md:h-[calc(100dvh-4rem)]">
3535
<div className="p-6 border-b border-neutral-700/50 flex-shrink-0">
3636
<Link href="/docs" className="text-blue-400 hover:text-blue-300 text-sm mb-4 flex items-center transition-colors">
3737
<svg
@@ -53,7 +53,7 @@ export default function ReferenceLayout({
5353
</div>
5454
<Index groupedReferences={groupedReferences} />
5555
</div>
56-
<article className="min-w-0 flex-1 p-4 sm:p-8 md:h-full md:overflow-y-auto">
56+
<article className="min-w-0 flex-1 p-4 sm:p-8">
5757
<div className="max-w-4xl">
5858
{/* Mobile reference navigation */}
5959
<details className="mb-6 rounded-lg border border-neutral-700/50 bg-neutral-800/50 md:hidden">

‎tests/markdownBasePath.test.tsx‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,12 @@ describe('Markdown heading anchors', () => {
4141
}
4242
});
4343
});
44+
45+
describe('Markdown table cells', () => {
46+
it('turns <br> into a line break but keeps other HTML escaped', async () => {
47+
const html = await render('| a | b |\n| --- | --- |\n| x <br> y <br/> z | <b>bold</b> |');
48+
expect(html.match(/<br\/>/g)).toHaveLength(2);
49+
expect(html).not.toContain('&lt;br');
50+
expect(html).toContain('&lt;b&gt;bold&lt;/b&gt;');
51+
});
52+
});

‎tests/referenceIndex.test.tsx‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
import { renderToStaticMarkup } from 'react-dom/server';
3+
import { createElement } from 'react';
4+
5+
const pathname = vi.hoisted(() => ({ value: '' }));
6+
vi.mock('next/navigation', () => ({ usePathname: () => pathname.value }));
7+
8+
import Index from '../app/components/Index';
9+
10+
const item = (name: string) => ({ name, description: '', reference: `operators/accumulators/${name}`, type: 'operators', category: 'accumulators' });
11+
const groupedReferences = { operators: { accumulators: [item('$avg'), item('$sum')] } };
12+
13+
function currentLinks(path: string) {
14+
pathname.value = path;
15+
const html = renderToStaticMarkup(createElement(Index, { groupedReferences }));
16+
return [...html.matchAll(/<a[^>]*aria-current="page"[^>]*>([^<]*)<\/a>/g)].map((match) => match[1]);
17+
}
18+
19+
describe('Reference sidebar current page', () => {
20+
// trailingSlash: true serves every page at "/x/"; the static render sees "%24", the browser "$"
21+
it('marks exactly the current entry', () => {
22+
expect(currentLinks('/docs/reference/operators/accumulators/%24avg/')).toEqual(['$avg']);
23+
expect(currentLinks('/docs/reference/operators/accumulators/$avg/')).toEqual(['$avg']);
24+
expect(currentLinks('/docs/reference/operators/accumulators/')).toEqual(['Accumulators']);
25+
expect(currentLinks('/docs/reference/operators/')).toEqual(['Operators']);
26+
});
27+
});

0 commit comments

Comments
 (0)