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:
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-topAbove every page
Sidebar
sidebar-bannerPage links
sidebar-footerPage
Title and description
Markdown buttons
page-actionspage-contentContent
page-bottomPrevious and next links
On This Page
toc-headerHeading links
toc-footerlayout-bottomBelow every page
layout-top and layout-bottom are on every page, custom pages included. The other slots depend on the layout:
notebookandfluxhave the same slots asdocs.glassandspacioushave nosidebar-bannerorsidebar-footer.homehas onlypage-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:
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-containersidebarnav-titlesearch-trigger-fullPage links
language-selectlanguage-select-textcolor-mode-switchpage-containerbreadcrumbTitle and content
footertocHeading links
header: all layouts except fluxlayout-container: docs, notebook, flux and homesidebar: all layouts except homesidebar-triggerandpage-container: docs, notebook and fluxbreadcrumb,footerandtoc: docs, notebook, spacious and fluxtoc-popover: docs, notebook and spacioussidebar-collapse-trigger: notebooksidebar-drawer: glass and spaciousactionsandtoc-dropdown: spaciousnavigation-panelandtab-dropdown: flux
A part that holds other parts receives them as props:
layout-container,page-containerandlanguage-selectreceive their content aschildren.toc,toc-popoverandtoc-dropdownreceiveheaderandfooter.sidebarreceivesbannerandfooter, in the docs, notebook and flux layouts.
Render these props in your component, so the components that other slots add still appear:
<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:
<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.
<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 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
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:
Copy the part with
vuesho add:npx vuesho add layouts/docs/slots/sidebarThe files go to
src/ui/layouts/docs/slots/sidebar, and the modules they import that the package doesn't export are copied too, undersrc/ui.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 }, });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/docsThe 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:
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():
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:
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:
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:
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:
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:
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:
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:
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.