Skip to Content
Vuesho

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

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

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 put your components into the layouts, like this banner above every page and comments under the content:

<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>

A slot component can call usePage() 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

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

sidebar-banner

Page links

sidebar-footer

Page

Title and description

Markdown buttons

page-actions
page-content

Content

page-bottom

Previous and next links

On This Page

toc-header

Heading links

toc-footer
layout-bottom

Below every page

layout-top and layout-bottom are on every page, custom pages included. The other slots depend on the layout:

  • notebook and flux have the same slots as docs.
  • glass and spacious have no sidebar-banner or sidebar-footer.
  • home has only page-content.

Wrapping the Content

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

<template>
  <div class="content-frame">
    <slot />
  </div>
</template>

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

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:

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

defineProps<{ class?: string }>(); 
</script>

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

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
sidebarnav-titlesearch-trigger-full

Page links

language-selectlanguage-select-text
color-mode-switch
page-containerbreadcrumb

Title and content

footer
toc

Heading links

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:

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.

Multiple Plugins

When two plugins replace the same part, the later plugin's component renders. To choose another, fill the slot in your theme.

Using Layout Wrappers

A layout wrapper 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:

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

The UI is Fumadocs 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 to change it.

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 package.

Differences From Fumadocs UI

The Fumadocs UI docs apply, with these differences:

Fumadocs UIVuesho
A prop that takes a node, like titleA 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 DocsPageNamed slots in kebab case, like #nav-title
A node in an option, like the text of a linkText, or a component like () => h("span", "Docs"), never a VNode

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

Reka UI Primitives

The UI is built on Reka UI. Install reka-ui and import its primitives from it. vuesho add installs it for copied files.

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

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

  1. Copy the part with vuesho add:

    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:

    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>
    src/theme.ts
    import { defineTheme } from "vuesho/theme";
    import defaultTheme from "vuesho/theme/default";
    import DocsLayout from "./DocsLayout.vue"; 
    
    export default defineTheme({
      extends: defaultTheme,
      layouts: { default: DocsLayout }, 
    });
  3. Edit the files under src/ui, with any Tailwind CSS class.

Copying the Theme

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

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

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:

<script setup lang="ts">
import { Root } from "vuesho/theme";
</script>

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

For a banner or a widget, use a slot instead.

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:

src/theme.ts
import icon from "./icon.svg?url"; 

export default defineTheme({
  extends: defaultTheme,
  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 shows.

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():

src/theme.ts
import { createPinia } from "pinia"; 

export default defineTheme({
  extends: defaultTheme,
  enhanceApp({ app }) {
    app.use(createPinia());
  },
});

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

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

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:

src/theme.ts
export default defineTheme({
  extends: defaultTheme,
  enhanceApp({ router }) {
    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:

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

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

Layout Props

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

src/theme.ts
import StatusIndicator from "./StatusIndicator.vue"; 

export default defineTheme({
  extends: defaultTheme,
  layoutProps: () => ({
    links: [{ type: "custom", children: StatusIndicator }],
  }),
});

The function runs once, after enhanceApp, and returns the 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

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 index:

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

A body is content that a plugin returns as data, like the blocks of a Notion page. bodies replaces the component that renders it:

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

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

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:

Prop

Type

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:

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.