Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Make docs nav work without JS

Blue Mouse141902643229
diff --git a/docs/app/[[...slug]]/page.tsx b/docs/app/[[...slug]]/page.tsx
index 0c9f98b50..6ae292f20 100644
--- a/docs/app/[[...slug]]/page.tsx
+++ b/docs/app/[[...slug]]/page.tsx
@@ -1,5 +1,7 @@
import { source } from "@/lib/source";
import { DocsPage, DocsBody, DocsTitle, DocsDescription } from "fumadocs-ui/page";
+import { TOC, TOCProvider } from "fumadocs-ui/layouts/docs/page/slots/toc";
+import { MobileContents } from "@/components/MobileContents";
import { notFound } from "next/navigation";
import { FeedbackWidget } from "@/components/FeedbackWidget";
import { IntegrationsGrid } from "@/components/IntegrationsGrid";
@@ -22,6 +24,7 @@ export default async function Page(props: { params: Promise<{ slug?: string[] }>
return (
<DocsPage
toc={page.data.toc}
+ slots={{ toc: { provider: TOCProvider, main: TOC, popover: MobileContents } }}
editOnGithub={{
repo: GITHUB_REPO,
owner: GITHUB_OWNER,
diff --git a/docs/app/api/search/route.ts b/docs/app/api/search/route.ts
index 41fe0f779..2df45ac04 100644
--- a/docs/app/api/search/route.ts
+++ b/docs/app/api/search/route.ts
@@ -1,6 +1,5 @@
-import { source } from "@/lib/source";
-import { createFromSource } from "fumadocs-core/search/server";
+import { docsSearch } from "@/lib/search";
export const revalidate = false;
-export const { GET } = createFromSource(source);
+export const { GET } = docsSearch;
diff --git a/docs/app/browse/page.tsx b/docs/app/browse/page.tsx
new file mode 100644
index 000000000..7fdf45e8f
--- /dev/null
+++ b/docs/app/browse/page.tsx
@@ -0,0 +1,53 @@
+import { DocsPage, DocsTitle } from "fumadocs-ui/page";
+import Link from "next/link";
+import { docsSearch } from "@/lib/search";
+import { source } from "@/lib/source";
+
+export const metadata = { title: "Browse documentation" };
+
+export default async function BrowsePage({
+ searchParams,
+}: {
+ searchParams: Promise<{ q?: string | string[] }>;
+}) {
+ const { q } = await searchParams;
+ const query = typeof q === "string" ? q.trim() : "";
+ const pages = query
+ ? (await docsSearch.search(query))
+ .filter((result) => result.type === "page")
+ .map((result) => ({ url: result.url, title: result.content }))
+ : source
+ .getPages()
+ .map((page) => ({ url: page.url, title: page.data.title }))
+ .sort((a, b) => a.title.localeCompare(b.title, "en"));
+
+ return (
+ <DocsPage full>
+ <DocsTitle>Browse documentation</DocsTitle>
+ <form action="/docs/browse" role="search" className="my-4 flex gap-2">
+ <input
+ type="search"
+ id="search"
+ name="q"
+ aria-label="Search documentation"
+ placeholder="Search documentation"
+ defaultValue={query}
+ className="min-w-0 flex-1 rounded-md border px-3 py-2"
+ />
+ <button type="submit" className="rounded-md border px-4 py-2">
+ Search
+ </button>
+ </form>
+ {pages.length === 0 && <p>No matching pages.</p>}
+ <ul>
+ {pages.map((page) => (
+ <li key={page.url}>
+ <Link href={page.url} prefetch={false} className="block py-2 underline">
+ {page.title}
+ </Link>
+ </li>
+ ))}
+ </ul>
+ </DocsPage>
+ );
+}
diff --git a/docs/app/layout.tsx b/docs/app/layout.tsx
index 52c78e3ca..12c9f57ec 100644
--- a/docs/app/layout.tsx
+++ b/docs/app/layout.tsx
@@ -4,6 +4,7 @@ import { GithubInfo } from "fumadocs-ui/components/github-info";
import type { ReactNode } from "react";
import { addIntegrationLogos } from "@/components/IntegrationLogo";
import { source } from "@/lib/source";
+import { MobileHeader } from "@/components/MobileHeader";
import Footer from "./components/Footer";
import Analytics from "./components/Analytics";
import "./global.css";
@@ -38,6 +39,7 @@ export default function RootLayout({ children }: { children: ReactNode }) {
<body>
<RootProvider search={{ options: { api: "/docs/api/search" } }}>
<DocsLayout
+ slots={{ header: MobileHeader }}
tree={addIntegrationLogos(source.getPageTree())}
nav={{
title: (
diff --git a/docs/components/MobileContents.tsx b/docs/components/MobileContents.tsx
new file mode 100644
index 000000000..c8513ff7d
--- /dev/null
+++ b/docs/components/MobileContents.tsx
@@ -0,0 +1,36 @@
+"use client";
+
+import { useEffect, useRef, useState } from "react";
+import { useTOCItems } from "fumadocs-ui/components/toc";
+import { TOCPopover } from "fumadocs-ui/layouts/docs/page/slots/toc";
+
+export function MobileContents() {
+ const headings = useTOCItems();
+ const details = useRef<HTMLDetailsElement>(null);
+ const [ready, setReady] = useState(false);
+
+ useEffect(() => {
+ if (!details.current?.open) setReady(true);
+ }, []);
+
+ if (ready) return <TOCPopover />;
+
+ return (
+ <details
+ ref={details}
+ className="border-b bg-fd-background text-sm [grid-area:toc-popover] xl:hidden"
+ onToggle={(event) => {
+ if (!event.currentTarget.open) setReady(true);
+ }}
+ >
+ <summary className="cursor-pointer px-4 py-3">On this page</summary>
+ <nav aria-label="On this page" className="px-4 pb-2">
+ {headings.map(({ url, title }) => (
+ <a key={url} href={url} className="block py-2">
+ {title}
+ </a>
+ ))}
+ </nav>
+ </details>
+ );
+}
diff --git a/docs/components/MobileHeader.tsx b/docs/components/MobileHeader.tsx
new file mode 100644
index 000000000..768bc192a
--- /dev/null
+++ b/docs/components/MobileHeader.tsx
@@ -0,0 +1,36 @@
+"use client";
+
+import { ListIcon, MagnifyingGlassIcon } from "@phosphor-icons/react";
+import { useDocsLayout } from "fumadocs-ui/layouts/docs";
+import { Header } from "fumadocs-ui/layouts/docs/slots/header";
+import { useSidebar } from "fumadocs-ui/layouts/docs/slots/sidebar";
+
+export function MobileHeader() {
+ const { mode } = useSidebar();
+ const { slots } = useDocsLayout();
+
+ if (mode === "drawer") return <Header className="[&_button]:size-11" />;
+
+ return (
+ <header
+ id="nd-subnav"
+ className="sticky top-0 z-30 flex h-14 items-center gap-1 border-b bg-fd-background ps-4 pe-2 [grid-area:header] md:hidden max-md:layout:[--fd-header-height:--spacing(14)]"
+ >
+ <slots.navTitle className="me-auto inline-flex items-center" />
+ <a
+ href="/docs/browse#search"
+ aria-label="Search documentation"
+ className="grid size-11 place-items-center"
+ >
+ <MagnifyingGlassIcon size={20} />
+ </a>
+ <a
+ href="/docs/browse"
+ aria-label="Browse documentation"
+ className="grid size-11 place-items-center"
+ >
+ <ListIcon size={20} />
+ </a>
+ </header>
+ );
+}
diff --git a/docs/lib/search.ts b/docs/lib/search.ts
new file mode 100644
index 000000000..9beb9533b
--- /dev/null
+++ b/docs/lib/search.ts
@@ -0,0 +1,4 @@
+import { createFromSource } from "fumadocs-core/search/server";
+import { source } from "./source";
+
+export const docsSearch = createFromSource(source);