# Docs with Fumadocs

Goal: a docs site with [Fumadocs](https://fumadocs.dev) 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](/api-reference/fumadocs)) 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](/content)). Reach for Fumadocs when you want its sidebar, table of contents, search dialog and MDX components.

1. Install the packages. Tailwind v4 goes through the PostCSS pass of `@hamur/css` ([Styling](/styling#tailwind-v4)); Fumadocs' styles use its utilities.

```sh
bun add @hamur/fumadocs @hamur/css fumadocs-core fumadocs-mdx fumadocs-ui
bun add -d tailwindcss @tailwindcss/postcss postcss @types/mdx
```

2. Configure the build. `fumadocs-mdx`'s Bun plugin turns `.mdx` files into modules, and `cssCompilers.postcss` runs Tailwind. Modules and plugins are imported, never named as strings:

````ts [examples/fumadocs/hamur.config.ts]
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](https://github.com/fuma-nama/fumadocs/pull/3921) is released. Until then apply that change to the package (`bun patch fumadocs-mdx`); the example's README says how.

3. Import the Fumadocs styles after Tailwind, in a stylesheet the app root imports:

````css [examples/fumadocs/app/app.css]
@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;
}
````

4. Wrap Fumadocs' root provider in `HamurProvider` in the app root, which imports `app.css`, so its styles are linked on every page. `HamurProvider` gives Fumadocs the path, params, router and `Link` of 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. `RootProvider` adds the search dialog and the dark mode switch:

````tsx [examples/fumadocs/app/app.tsx]
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>
  )
}
````

5. Declare the docs and the loader. `defineDocs` reads `content/docs/`; `loader` turns it into a page tree and URLs under `/docs`:

````ts [examples/fumadocs/app/lib/source.ts]
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",
})
````

6. Serve the page tree and search from handlers. `getPage` is called from the page's `load`, so the tree is in the page payload; `searchDocs` is a raw handler at `/api/search`, where Fumadocs' search dialog asks:

````ts [examples/fumadocs/server/docs.ts]
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),
})
````

7. Render the page. The `load` fetches the tree for the slug; the page renders `DocsLayout` and `DocsPage` with the compiled MDX body:

````ts [examples/fumadocs/app/pages/docs/[[...slug]].load.ts]
import { getPage } from "#server"
import type { Route } from "./+types/[[...slug]]"

export const load = (({ params }) => getPage({ slug: params.slug ?? [] })) satisfies Route.Load
````

````tsx [examples/fumadocs/app/pages/docs/[[...slug]].tsx]
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](/styling), [Routing](/routing), [Handlers](/handlers), [`@hamur/fumadocs`](/api-reference/fumadocs).
