Docs with Fumadocs
Goal: a docs site with Fumadocs on a Hamur React app: its layout and sidebar, MDX pages, search at /api/search and dark mode. The runnable sample is examples/fumadocs/; @hamur/fumadocs (API) connects Fumadocs to Hamur's router, and fumadocs-ui and fumadocs-mdx are dependencies of the app.
When a plain Markdown site is enough: @hamur/content renders content/ pages with no UI kit (Content). Reach for Fumadocs when you want its sidebar, table of contents, search dialog and MDX components.
- Install the packages. Tailwind v4 goes through the PostCSS pass of
@hamur/css(Styling); Fumadocs' styles use its utilities.
bun add @hamur/fumadocs @hamur/css fumadocs-core fumadocs-mdx fumadocs-ui
bun add -d tailwindcss @tailwindcss/postcss postcss @types/mdx
- Configure the build.
fumadocs-mdx's Bun plugin turns.mdxfiles into modules, andcssCompilers.postcssruns Tailwind. Modules and plugins are imported, never named as strings:
import css from "@hamur/css"
import tailwindcss from "@tailwindcss/postcss"
import { createMdxPlugin } from "fumadocs-mdx/bun"
import { defineConfig } from "hamur"
export default defineConfig({
framework: "react",
modules: [css],
cssCompilers: {
postcss: { plugins: [tailwindcss({ base: import.meta.dir })] },
},
build: {
plugins: [createMdxPlugin()],
},
app: {
head: {
htmlAttrs: { lang: "en" },
bodyAttrs: { class: "flex flex-col min-h-screen" },
},
},
})
The plugin of fumadocs-mdx 15.6.4 fails under hamur build until fuma-nama/fumadocs#3921 is released. Until then apply that change to the package (bun patch fumadocs-mdx); the example's README says how.
- Import the Fumadocs styles after Tailwind, in a stylesheet the app root imports:
@import "tailwindcss";
@import "fumadocs-ui/css/neutral.css";
@import "fumadocs-ui/css/preset.css";
html {
scrollbar-gutter: stable;
}
html > body[data-scroll-locked] {
margin-right: 0px !important;
--removed-body-scroll-bar-size: 0px !important;
}
- Wrap Fumadocs' root provider in
HamurProviderin the app root, which importsapp.css, so its styles are linked on every page.HamurProvidergives Fumadocs the path, params, router andLinkof the page on screen, so the sidebar marks the current page and a click on a sidebar item is a client navigation, not a document request.RootProvideradds the search dialog and the dark mode switch:
import { Outlet } from "@hamur/adapter-react"
import { HamurProvider } from "@hamur/fumadocs"
import { RootProvider } from "fumadocs-ui/provider/base"
import "./app.css"
export default function App() {
return (
<HamurProvider>
<RootProvider>
<Outlet />
</RootProvider>
</HamurProvider>
)
}
- Declare the docs and the loader.
defineDocsreadscontent/docs/;loaderturns it into a page tree and URLs under/docs:
import { loader } from "fumadocs-core/source"
import { defineDocs } from "fumadocs-mdx/macro"
export const docs = defineDocs({
dir: "content/docs",
docs: {
async: true,
},
})
export const source = loader({
source: docs.toFumadocsSource(),
baseUrl: "/docs",
})
- Serve the page tree and search from handlers.
getPageis called from the page'sload, so the tree is in the page payload;searchDocsis a raw handler at/api/search, where Fumadocs' search dialog asks:
import { createFromSource } from "fumadocs-core/search/server"
import { createError, defineHandler } from "hamur/server"
import { source } from "#app/lib/source.ts"
export const getPage = defineHandler({
method: "GET",
async handler({ slug }: { slug: string[] }) {
const page = source.getPage(slug)
if (!page) throw createError({ status: 404 })
return {
path: page.path,
pageTree: await source.serializePageTree(source.getPageTree()),
}
},
})
const search = createFromSource(source)
export const searchDocs = defineHandler({
route: "/api/search",
method: "GET",
raw: true,
handler: (_, { event }) => search.GET(event.request),
})
- Render the page. The
loadfetches the tree for the slug; the page rendersDocsLayoutandDocsPagewith the compiled MDX body:
import { getPage } from "#server"
import type { Route } from "./+types/[[...slug]]"
export const load = (({ params }) => getPage({ slug: params.slug ?? [] })) satisfies Route.Load
import { useData, useSeoMeta } from "@hamur/adapter-react"
import { useFumadocsLoader } from "fumadocs-core/source/client"
import { DocsLayout } from "fumadocs-ui/layouts/docs"
import { DocsBody, DocsDescription, DocsPage, DocsTitle } from "fumadocs-ui/layouts/docs/page"
import { use } from "react"
import { useMDXComponents } from "#app/components/mdx.tsx"
import { baseOptions } from "#app/lib/layout.shared.tsx"
import { docs } from "#app/lib/source.ts"
import type { Route } from "./+types/[[...slug]]"
function Content({ path }: { path: string }) {
const page = docs.getPage(path)
if (!page) throw new Error(`unknown page: ${path}`)
const { toc } = use(page.load())
const Mdx = page.body
useSeoMeta({ title: page.title, description: page.description })
return (
<DocsPage toc={toc}>
<DocsTitle>{page.title}</DocsTitle>
<DocsDescription>{page.description}</DocsDescription>
<DocsBody>
<Mdx components={useMDXComponents()} />
</DocsBody>
</DocsPage>
)
}
export default function Page() {
const { path, pageTree } = useFumadocsLoader(useData<Route.Name>())
return (
<DocsLayout {...baseOptions()} tree={pageTree}>
<Content path={path} />
</DocsLayout>
)
}
A missing slug throws a 404 from the handler. Dark mode needs nothing more: RootProvider sets it up and the layout shows the switch.
The test tests/browser/fumadocs.test.ts renders a docs page with the sidebar and checks that a click on another item moves the active mark without a document request.
See also: Styling, Routing, Handlers, @hamur/fumadocs.