# Theme

Customize the layouts and the Vue app.

The theme is the module that sets a site's layouts, the components in them, and the Vue app setup. Customize it in `src/theme.ts`.

## Theme File [#theme-file]

Extend the default theme, and override what you want to change:

```ts title="src/theme.ts"
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import type app from "../vuesho.config";

export default defineTheme<typeof app>({
  // The docs and home layouts, and the root component around every page
  extends: defaultTheme,
  // Your components in the layouts
  slots: {},
  // Tags in the <head> of every page
  head: [],
  // Vue plugins and router hooks
  enhanceApp({ app, router }) {},
});
```

`defineTheme<typeof app>()` checks the names of layouts, views and bodies against your config. Import the config with `import type`, so its code stays out of the browser.

## Slots [#slots]

Slots put your components into the layouts, like this banner above every page and comments under the content:

```vue tab="src/Announcement.vue"
<script setup lang="ts">
import { Banner } from "vuesho/ui/components/banner";
</script>

<template>
  <Banner id="v2-release" variant="rainbow">Acme 2 is out.</Banner>
</template>
```

```ts tab="src/theme.ts"
import { defineAsyncComponent } from "vue";
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import Announcement from "./Announcement.vue";

export default defineTheme({
  extends: defaultTheme,
  // [!code highlight:4]
  slots: {
    "layout-top": Announcement,
    "page-bottom": defineAsyncComponent(() => import("./Comments.vue")),
  },
});
```

A slot component can call [`usePage()`](/client-api) to read the current page, for example to render only on some pages. Wrap it in `defineAsyncComponent()` so its code loads only where it renders.

### Adding to the Layout [#adding-to-the-layout]

Each slot below adds components to a place in the docs layout. It takes a component or a list, rendered after the components that plugins add:

- `layout-top`: Above every page
- `sidebar-banner`: At the top of the sidebar
- `sidebar-footer`: At the bottom of the sidebar
- `page-actions`: Under the description, after the Markdown buttons
- `page-content`: Around the content of the page
- `page-bottom`: Under the content, above the previous and next links
- `toc-header`: Above the table of contents
- `toc-footer`: Below the table of contents
- `layout-bottom`: Below every page

`layout-top` and `layout-bottom` are on every page, [custom pages](/custom-pages) included. The other slots depend on the layout:

- `notebook` and [`flux`](/layouts#flux-layout) have the same slots as `docs`.
- `glass` and `spacious` have no `sidebar-banner` or `sidebar-footer`.
- `home` has only `page-content`.

### Wrapping the Content [#wrapping-the-content]

The `page-content` slot wraps the content of every page. Render the content with `<slot />` in your component:

```vue tab="src/ContentFrame.vue"
<template>
  <div class="content-frame">
    <slot />
  </div>
</template>
```

```ts tab="src/theme.ts"
import ContentFrame from "./ContentFrame.vue"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  slots: { "page-content": ContentFrame }, // [!code ++]
});
```

When plugins wrap the content too, your component is the outermost.

### Replacing a Part [#replacing-a-part]

A slot can also replace a part of the layout, in every layout that has the part. Your component receives the props of the built-in part:

```vue tab="src/NavTitle.vue"
<script setup lang="ts">
import { Link } from "vuesho/client";
import logo from "./logo.svg";

defineProps<{ class?: string }>(); // [!code word:class]
</script>

<template>
  <Link href="/" :class="$props.class">
    <img :src="logo" alt="Acme" height="24" />
  </Link>
</template>
```

```ts tab="src/theme.ts"
import NavTitle from "./NavTitle.vue"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  slots: { "nav-title": NavTitle }, // [!code ++]
});
```

The docs layout has the parts below. Three more parts are on every page: `search-dialog` for the search dialog, `search-footer` for the footer of the search dialog, and `image` for the images in the UI:

- `layout-container`: The grid of the layout
- `sidebar`: The sidebar
- `nav-title`: The site name
- `search-trigger-full`: The search button
- `language-select`: The language menu
- `language-select-text`: The name of the current language, in the language menu
- `color-mode-switch`: The color mode button
- `page-container`: The page, inside the layout
- `breadcrumb`: The breadcrumb
- `footer`: The previous and next links
- `toc`: The table of contents
- `header`: The navbar on small screens
- `search-trigger-sm`: The search button on small screens
- `sidebar-trigger`: The button that opens the sidebar
- `toc-popover`: The table of contents on small screens

<Accordions>
<Accordion title="Parts of the other layouts">
- `header`: all layouts except flux
- `layout-container`: docs, notebook, flux and home
- `sidebar`: all layouts except home
- `sidebar-trigger` and `page-container`: docs, notebook and flux
- `breadcrumb`, `footer` and `toc`: docs, notebook, spacious and flux
- `toc-popover`: docs, notebook and spacious
- `sidebar-collapse-trigger`: notebook
- `sidebar-drawer`: glass and spacious
- `actions` and `toc-dropdown`: spacious
- `navigation-panel` and `tab-dropdown`: flux
</Accordion>
</Accordions>

A part that holds other parts receives them as props:

- `layout-container`, `page-container` and `language-select` receive their content as `children`.
- `toc`, `toc-popover` and `toc-dropdown` receive `header` and `footer`.
- `sidebar` receives `banner` and `footer`, in the docs, notebook and flux layouts.

Render these props in your component, so the components that other slots add still appear:

```vue title="src/PageContainer.vue"
<script setup lang="ts">
import type { VNode } from "vue";

defineProps<{ children: VNode }>();
</script>

<template>
  <article class="my-page">
    <component :is="children" />
  </article>
</template>
```

To start from the built-in part, import it from `vuesho/ui/layouts/<name>/slots/<part>`, like `Sidebar` of `vuesho/ui/layouts/docs/slots/sidebar`, or [copy it](#copying-the-ui).

<Callout title="Multiple Plugins">
When two plugins replace the same part, the later plugin's component renders. To choose another, fill the slot in your theme.
</Callout>

### Using Layout Wrappers [#using-layout-wrappers]

A [layout wrapper](/layouts#layout-props) can also fill slots in its template. A template slot that replaces a part takes priority over the theme, and one that adds to a place renders after the components of the theme:

```vue title="src/DocsPage.vue"
<script setup lang="ts">
import type { ContentPage } from "vuesho/client";
import DocsLayout from "vuesho/layouts/docs";

defineProps<{ page: ContentPage }>();
</script>

<template>
  <DocsLayout :page="page">
    <template #page-actions>
      <a :href="`https://github.com/acme/docs/issues/new?title=${page.pageData.title}`">
        Report an issue
      </a>
    </template>
  </DocsLayout>
</template>
```

## UI Components [#ui-components]

The UI is [Fumadocs UI](https://fumadocs.dev/docs/ui) ported to Vue. Import its components from `vuesho/ui/*`, at the same paths as in Fumadocs UI. An internal part isn't exported, so [copy it](#copying-the-ui) to change it.

```vue title="src/components/NextSteps.vue"
<script setup lang="ts">
import { Card, Cards } from "vuesho/ui/components/card";
</script>

<template>
  <Cards>
    <Card title="Getting Started" href="/">Create your first site.</Card>
    <Card title="Theme" href="/theme">Customize the UI.</Card>
  </Cards>
</template>
```

A site reaches most often for `components/banner`, `components/github-info` and `components/dynamic-codeblock`, a code block highlighted in the browser. To merge class names as the UI does, use `cn()` from the [`cn`](https://www.npmjs.com/package/cn) package.

### Differences From Fumadocs UI [#differences-from-fumadocs-ui]

The [Fumadocs UI docs](https://fumadocs.dev/docs/ui) apply, with these differences:

| Fumadocs UI                                    | Vuesho                                                             |
| ---------------------------------------------- | ------------------------------------------------------------------ |
| A prop that takes a node, like `title`         | A prop for text, and a slot of the same name for markup            |
| A hook, like `useSidebar()`                    | A composable of the same name                                      |
| The `slots` prop of a layout or of `DocsPage`  | Named slots in kebab case, like `#nav-title`                       |
| A node in an option, like the `text` of a link | Text, or a component like `() => h("span", "Docs")`, never a VNode |

Render such a value with `RenderNode` from `vuesho/ui/layouts/shared`.

### Reka UI Primitives [#reka-ui-primitives]

The UI is built on [Reka UI](https://reka-ui.com). Install `reka-ui` and import its primitives from it. [`vuesho add`](/cli#vuesho-add) installs it for copied files.

## Copying the UI [#copying-the-ui]

When slots and props aren't enough, copy a part of the UI into your project and edit it.

### Copying a Part [#copying-a-part]

Copy the smallest part you need, so the rest of the UI keeps getting updates:

1. Copy the part with `vuesho add`:

   ```npm
   npx vuesho add layouts/docs/slots/sidebar
   ```

   The files go to `src/ui/layouts/docs/slots/sidebar`, and the modules they import that the package doesn't export are copied too, under `src/ui`.

2. The command prints a wrapper of the layout that fills the slot with your copy. Write the wrapper and set it as the layout in your theme:

   ```vue title="src/DocsLayout.vue"
   <script setup lang="ts">
   import DocsLayout from "vuesho/layouts/docs";
   import { Sidebar, SidebarTrigger } from "./ui/layouts/docs/slots/sidebar";
   </script>

   <template>
     <DocsLayout>
       <template #sidebar><Sidebar /></template>
       <template #sidebar-trigger><SidebarTrigger /></template>
     </DocsLayout>
   </template>
   ```

   ```ts title="src/theme.ts"
   import { defineTheme } from "vuesho/theme";
   import defaultTheme from "vuesho/theme/default";
   import DocsLayout from "./DocsLayout.vue"; // [!code ++]

   export default defineTheme({
     extends: defaultTheme,
     layouts: { default: DocsLayout }, // [!code ++]
   });
   ```

3. Edit the files under `src/ui`, with any [Tailwind CSS](/styling#tailwind-css) class.

### Copying the Theme [#copying-the-theme]

To change a layout itself, copy the whole default theme, then the layout:

```npm
npx vuesho add theme
npx vuesho add layouts/docs
```

The command copies the whole default theme to `<dir>/theme/` (`src/ui/theme/` by default) and rewrites your project's imports to the copy.

`npx vuesho add` lists the other parts you can copy.

## App Setup [#app-setup]

### Root Component [#root-component]

`Root` wraps every page, custom pages included. To put a component around every page, like one that calls `provide()`, wrap the built-in `Root`:

```vue tab="src/Root.vue"
<script setup lang="ts">
import { Root } from "vuesho/theme";
</script>

<template>
  <Root>
    <slot />
  </Root>
</template>
```

```ts tab="src/theme.ts"
import Root from "./Root.vue"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  Root, // [!code ++]
});
```

For a banner or a widget, use a [slot](#slots) instead.

### Head Tags [#head-tags]

`head` adds tags to the `<head>` of every page, like an analytics script or a favicon. For a file in `public/`, prefix its URL with the base path:

```ts title="src/theme.ts"
import icon from "./icon.svg?url"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  // [!code ++:4]
  head: [
    { tag: "script", attrs: { src: `${import.meta.env.BASE_URL}analytics.js`, defer: true } },
    { tag: "link", attrs: { rel: "icon", href: icon, type: "image/svg+xml" } },
  ],
});
```

For tags that depend on the page, use `head` in the config, as [SEO](/seo) shows.

### Vue Plugins [#vue-plugins]

`enhanceApp` runs on the server and in the browser, before the first page renders. It receives the Vue app, so it can install Vue plugins with `app.use()`:

```ts title="src/theme.ts"
import { createPinia } from "pinia"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  // [!code ++:3]
  enhanceApp({ app }) {
    app.use(createPinia());
  },
});
```

For a plugin that only works in the browser, return early on the server and import the plugin dynamically:

```ts title="src/theme.ts"
export default defineTheme({
  extends: defaultTheme,
  // [!code word:async]
  async enhanceApp({ app }) {
    if (import.meta.env.SSR) return;
    const { default: plugin } = await import("./browser-plugin");
    app.use(plugin);
  },
});
```

### Router Hooks [#router-hooks]

`router.beforeEach()` runs before each navigation, on the server and in the browser. Return a path to redirect, or `false` to cancel the navigation. On the server, the redirect is an HTTP redirect:

```ts title="src/theme.ts"
export default defineTheme({
  extends: defaultTheme,
  enhanceApp({ router }) {
    // [!code highlight:3]
    router.beforeEach((to) => {
      if (to.path.startsWith("/v1/")) return to.path.replace("/v1/", "/v2/");
    });
  },
});
```

`router.afterEach()` runs in the browser once the page is shown:

```ts title="src/theme.ts"
import { trackPageView } from "./analytics";

export default defineTheme({
  extends: defaultTheme,
  enhanceApp({ router }) {
    // [!code highlight:3]
    router.afterEach((to, from) => {
      // `from` is undefined on the first page
      if (from) trackPageView(to.href);
    });
  },
});
```

### Layout Props [#layout-props]

`layoutProps` sets layout props from the app, where they can use browser state, like a component in the navbar:

```ts title="src/theme.ts"
import StatusIndicator from "./StatusIndicator.vue"; // [!code ++]

export default defineTheme({
  extends: defaultTheme,
  // [!code ++:3]
  layoutProps: () => ({
    links: [{ type: "custom", children: StatusIndicator }],
  }),
});
```

The function runs once, after `enhanceApp`, and returns the [layout props](/layouts#layout-props), a ref or a getter. A theme's props override `ui.layoutProps` in `vuesho.config.ts`, and its `links` are added after the config's.

## Views and Page Bodies [#views-and-page-bodies]

Plugins render their pages with theme components, which you can replace. `views` replaces the component of a page that a plugin adds, like the [blog](/plugins/blog) index:

```ts title="src/theme.ts"
export default defineTheme<typeof app>({
  extends: defaultTheme,
  views: { "blog.index": () => import("./BlogIndex.vue") }, // [!code ++]
});
```

A body is content that a plugin returns as data, like the blocks of a [Notion](/content-sources/notion) page. `bodies` replaces the component that renders it:

```ts title="src/theme.ts"
export default defineTheme<typeof app>({
  extends: defaultTheme,
  bodies: { notion: () => import("./NotionBody.vue") }, // [!code ++]
});
```

A view component receives its page as the `page` prop, typed with `PageOf<typeof app, "blog.index">`. A body component receives the data of the body as props.

## Theme Inheritance [#theme-inheritance]

`extends` takes a theme or a list of themes, where a later theme overrides an earlier one. Plugin themes come next, and your theme comes last. The table shows how each option merges:

### Theme

| Prop           | Type                                                                                      | Description                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| -------------- | ----------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `extends?`     | `Theme \| readonly Theme[] \| undefined`                                                  | The themes this one builds on, like the default theme. `layouts`, `views` and `bodies` merge with this theme's entries winning, `head` lists are joined, every `enhanceApp` runs in order, this theme's `layoutProps` win, and `Root`, `NotFound` and `ServerError` are replaced. In a list, a later theme wins over an earlier one. The site's theme also builds on its plugins' themes, after those in `extends`.                             |
| `Root?`        | `Component \| undefined`                                                                  | The root component, around every page in the app, custom pages included. Its default slot is the page.                                                                                                                                                                                                                                                                                                                                          |
| `NotFound?`    | `Component \| (() => Promise<Component \| { default: Component; }>) \| undefined`         | Renders a URL that has no page. It can be a function that imports it, like `() => import("./NotFound.vue")`, so only a URL without a page loads its code.                                                                                                                                                                                                                                                                                       |
| `ServerError?` | `Component \| (() => Promise<Component \| { default: Component; }>) \| undefined`         | Renders a page that failed to render on a server, which answers with status 500, in place of the page and with the not found page's page data. It can be a function that imports it, like `NotFound`.                                                                                                                                                                                                                                           |
| `layouts?`     | `Record<string, PageComponent<never>> \| undefined`                                       | Layouts for content pages, by name. A content page renders with the layout its `layout` names, or else its source key's, or else `default`.                                                                                                                                                                                                                                                                                                     |
| `views?`       | `Record<string, PageComponent<never>> \| undefined`                                       | The component for each plugin view, by view name.                                                                                                                                                                                                                                                                                                                                                                                               |
| `bodies?`      | `Record<string, BodyComponent<never>> \| undefined`                                       | The component for each plugin body, by type. A body is data a plugin gives for the theme to render: a page's content, like a Notion page's blocks, which the layout renders as `page.Content`, or a component the server renders alone to HTML with the context's `getBodyHtml()`, like a page's Open Graph image. A function that imports it keeps it out of the chunks of pages that don't render it.                                         |
| `slots?`       | `SlotComponents<ThemeSlots> \| undefined`                                                 | Components for the slots of the root component and the layouts, by slot name. A slot that adds to a place takes one or a list, rendered in order. A slot that replaces a part takes one, renders the last theme's, and receives the part's props. A slot that wraps a part takes one or a list, each receiving the part as its default slot, with the last one outermost. Each is an ordinary component, which reads the page with `usePage()`. |
| `head?`        | `HeadTag[] \| undefined`                                                                  | Head tags for every page, like a script that runs before the first paint. A URL is the one the browser requests, like an imported file's URL, so a file in `public/` is `import.meta.env.BASE_URL` followed by its path.                                                                                                                                                                                                                        |
| `enhanceApp?`  | `((ctx: { app: App; router: Router; }) => Awaitable<void>) \| undefined`                  | Registers Vue plugins and router hooks before the app loads its first page, on the server and in the browser. The app awaits it, so it can import a module first.                                                                                                                                                                                                                                                                               |
| `layoutProps?` | `((ctx: { app: App; router: Router; }) => MaybeRefOrGetter<AppLayoutProps>) \| undefined` | Gives the layouts props from the app, for state that lives in the browser, like whether a chat is open. It runs once per app, after `enhanceApp` and in the app's context, so it can `inject()` what `enhanceApp` provided. It returns the props, or a ref or getter for them, which the layouts follow. They apply over the server's layout props.                                                                                             |

### Starting From Scratch [#starting-from-scratch]

A theme without `extends` starts empty, so the site doesn't load the built-in layouts. Give it layouts and a `NotFound` page:

```ts title="src/theme.ts"
import { defineTheme } from "vuesho/theme";
import type app from "../vuesho.config";
import Page from "./Page.vue";

export default defineTheme<typeof app>({
  NotFound: () => import("./NotFound.vue"),
  layouts: { default: Page },
});
```

Set `ServerError` to render pages that fail on the server, with the status 500. Without it, the server responds with plain HTML.

The default stylesheet still loads until you add `src/theme.css`.
