Layouts
The page layouts and their options.
A layout renders a content page with the navbar, the sidebar and the table of contents around it. There are six layouts, and docs is the default. Five come with vuesho, and the flux layout is the package @vuesho/flux.
Layouts
Built-in Layouts
Set your layouts in the theme file, src/theme.ts. Each built-in layout is a component from vuesho/layouts/<name>, and the one under default renders every content page:
import { defineTheme } from "vuesho/theme";
import defaultTheme from "vuesho/theme/default";
import NotebookLayout from "vuesho/layouts/notebook";
export default defineTheme({
extends: defaultTheme,
layouts: { default: NotebookLayout },
});The default theme includes the docs layout, so a site on another layout still loads the docs layout's code. To avoid that, extend baseTheme, which has no layouts, and list each layout you use:
import { baseTheme, defineTheme } from "vuesho/theme";
import NotebookLayout from "vuesho/layouts/notebook";
export default defineTheme({
extends: baseTheme,
layouts: {
default: NotebookLayout,
// Pages with `layout: home`, loaded only where they are.
home: () => import("vuesho/layouts/home"),
},
});Flux Layout
The flux layout is a package of its own. Install it:
npm i @vuesho/fluxfluxPlugin() adds the layout's classes to the site's stylesheet:
import { fluxPlugin } from "@vuesho/flux";
export default createSite(docs.toSource()).plugins(
recommendedPreset(),
fluxPlugin(),
);Then set it in the theme like the other layouts, from @vuesho/flux/layout:
import { baseTheme, defineTheme } from "vuesho/theme";
import FluxLayout from "@vuesho/flux/layout";
export default defineTheme({
extends: baseTheme,
layouts: { default: FluxLayout },
});Its UI parts are under @vuesho/flux/ui/layouts/flux, like NavigationPanel.
Choosing a Layout
Per Page
To render a page with another layout from the theme, set layout in its frontmatter:
---
title: Credits
layout: landing
---The default theme includes home, so layout: home works without a theme file. A page whose layout isn't in the theme fails to render. To catch that as a type error, use a frontmatter schema with layout: z.enum(["landing"]).
Per Source
With multiple collections or sources, set a layout for each source by its key. A source without a key in layouts uses default, and without default, every source needs a key:
import DocsLayout from "vuesho/layouts/docs";
export default defineTheme({
extends: baseTheme,
layouts: {
default: NotebookLayout,
api: DocsLayout,
},
});Layout Props
A built-in layout takes two sets of props:
layoutProps, for the navbar and the sidebarpageProps, for the table of contents, the breadcrumb and the footer
To pass them, wrap the layout in a component, and set the wrapper as the layout:
<script setup lang="ts">
import type { ContentPage } from "vuesho/client";
import NotebookLayout from "vuesho/layouts/notebook";
import Logo from "./Logo.vue";
defineProps<{ page: ContentPage }>();
</script>
<template>
<NotebookLayout
:page="page"
:layout-props="{ tabMode: 'navbar', nav: { title: Logo } }"
:page-props="{ tableOfContent: { style: 'clerk' } }"
/>
</template>import DocsPage from "./DocsPage.vue";
export default defineTheme({
extends: baseTheme,
layouts: {
default: NotebookLayout,
default: DocsPage,
},
});These props can hold Vue components and functions, like Logo above. They win over the props that every layout shares, which come from ui.layoutProps in the config and from the theme's layoutProps, and a list like links replaces the shared one. To keep the shared links and add more, read them with useLayoutProps().
The tables show the docs layout's props. The other layouts have a few more, like tabMode: "navbar" in notebook.
DocsLayoutProps
Prop
Type
DocsSidebarProps
These props go in the sidebar field of layoutProps. To add a component to the sidebar on every page, use the sidebar-footer and sidebar-banner slots instead, which need no wrapper.
Prop
Type
DocsPageProps
pageProps override the page data, so pageProps.full replaces full in the frontmatter:
Prop
Type
Showing One Folder
To show only one folder in the sidebar, get its tree with selectTreeRoot() from vuesho/page-tree, and pass it to layoutProps.tree. When the folder doesn't exist, selectTreeRoot() throws an error that lists the folders:
<script setup lang="ts">
import { computed } from "vue";
import { usePageTree, type ContentPage } from "vuesho/client";
import { selectTreeRoot } from "vuesho/page-tree";
import DocsLayout from "vuesho/layouts/docs";
defineProps<{ page: ContentPage }>();
const pageTree = usePageTree();
const tree = computed(() => selectTreeRoot(pageTree.value, "guides"));
</script>
<template>
<DocsLayout :page="page" :layout-props="{ tree }" />
</template>usePageTree() returns the tree in the current page's language, so the sidebar shows the folder in that language. To switch between folders with tabs instead, make them root folders.
Landing Page
Without a page prop, HomeLayout renders its default slot under the site navbar, with no sidebar. Use it in a custom page at src/pages/index.vue for a landing page:
<script setup lang="ts">
import HomeLayout from "vuesho/layouts/home";
import { Card, Cards } from "vuesho/ui/components/card";
</script>
<template>
<HomeLayout>
<main class="mx-auto w-full max-w-(--fd-layout-width) px-4 py-12">
<h1 class="mb-6 text-2xl font-semibold">Acme Docs</h1>
<Cards>
<Card title="Quick Start" href="/docs">Create your first site.</Card>
<Card title="Theme" href="/docs/theme">Change the UI of your site.</Card>
</Cards>
</main>
</HomeLayout>
</template>The landing page uses /, which is also the URL of content/index.md, so serve the content pages under /docs with baseSlugs:
// Adds the URL prefix `/docs`. Unlike `toSource({ baseDir: "docs" })`, the sidebar keeps its shape.
export default createSite(docs.toSource(), { baseSlugs: ["docs"] }); Custom Layout
A layout is a Vue component that receives the page as its page prop, and renders the content with <component :is="page.Content" />. Register it with () => import() to load it only on the pages that use it:
<script setup lang="ts">
import { useWrappingSlot, type ContentPage } from "vuesho/client";
import HomeLayout from "vuesho/layouts/home";
defineProps<{ page: ContentPage }>();
const PageContent = useWrappingSlot("page-content");
</script>
<template>
<HomeLayout>
<article class="mx-auto max-w-3xl px-4 py-12">
<h1>{{ page.pageData.title }}</h1>
<p v-if="page.pageData.description">{{ page.pageData.description }}</p>
<PageContent>
<component :is="page.Content" />
</PageContent>
</article>
</HomeLayout>
</template>A page has two sets of data:
page.datais the file's data as written, like a Markdown page's frontmatter.page.pageDatais what a layout renders, like the title, the description and the last update date, filled in by Vuesho and the plugins.
Read page.pageData, unless you need a custom frontmatter field.
In the example, HomeLayout adds the site navbar, and the page-content slot renders what plugins add around the content, like feedback.
Prop
Type
Typed Pages
When createSite() takes sources by key, like createSite({ docs: docs.toSource() }), type the page with PageOf. page.data then has the fields of the source's frontmatter schema:
<script setup lang="ts">
import type { PageOf } from "vuesho/client";
import type app from "../vuesho.config";
defineProps<{ page: PageOf<typeof app, "docs"> }>();
</script>defineTheme<typeof app>() then shows a type error when a layout's page prop doesn't match the pages it renders. layouts.default renders every source's pages, so its page prop must accept all of them.
Layout Slots
Declare a custom layout's slots in ThemeSlots, each as "add", "replace" or "wrap", and render each slot with its composable:
<script lang="ts">
declare module "vuesho/client" {
interface ThemeSlots {
"article-aside": "add";
"article-header": "replace";
}
}
</script>
<script setup lang="ts">
import { useAddingSlot, useReplacingSlot, useWrappingSlot, type ContentPage } from "vuesho/client";
defineProps<{ page: ContentPage }>();
// All the components of the slot, or `undefined` when the slot is empty
const ArticleAside = useAddingSlot("article-aside");
// The last component of the slot, or `undefined` when the slot is empty
const ArticleHeader = useReplacingSlot("article-header");
// Renders its default slot inside every component of the slot
const PageContent = useWrappingSlot("page-content");
</script>
<template>
<article>
<ArticleHeader v-if="ArticleHeader" :title="page.pageData.title" />
<h1 v-else>{{ page.pageData.title }}</h1>
<PageContent>
<component :is="page.Content" />
</PageContent>
<aside v-if="ArticleAside"><ArticleAside /></aside>
</article>
</template>Your theme can then fill article-aside and article-header in slots, with type checks, like a built-in layout's slots.
Building From UI Parts
A custom layout can arrange a built-in layout's parts itself. useLayoutProps() returns the layout props from the config, the themes and the plugins. Render the page-actions, page-content and page-bottom slots where the built-in layout has them, so what plugins add, like feedback, still shows:
<script lang="ts">
declare module "vuesho/client" {
interface ThemeSlots {
"page-actions": "add";
"page-content": "wrap";
"page-bottom": "add";
}
}
</script>
<script setup lang="ts">
import {
useAddingSlot,
useLayoutProps,
usePageTree,
useWrappingSlot,
type ContentPage,
} from "vuesho/client";
import { toTocItems } from "vuesho/theme";
import { DocsLayout, type DocsLayoutProps } from "vuesho/ui/layouts/docs";
import {
DocsBody,
DocsDescription,
DocsPage,
DocsTitle,
MarkdownCopyButton,
PageLastUpdate,
ViewOptionsPopover,
} from "vuesho/ui/layouts/docs/page";
defineProps<{ page: ContentPage }>();
const tree = usePageTree();
const layoutProps = useLayoutProps<DocsLayoutProps>(DocsLayout);
const PageActions = useAddingSlot("page-actions");
const PageContent = useWrappingSlot("page-content");
const PageBottom = useAddingSlot("page-bottom");
</script>
<template>
<DocsLayout :tree="tree" v-bind="layoutProps">
<DocsPage :key="page.url" :toc="toTocItems(page.toc)" :full="page.pageData.full">
<DocsTitle>{{ page.pageData.title }}</DocsTitle>
<DocsDescription v-if="page.pageData.description" class="mb-0">
{{ page.pageData.description }}
</DocsDescription>
<div class="flex flex-row gap-2 items-center border-b pt-2 pb-6">
<MarkdownCopyButton
v-if="page.pageData.markdownUrl"
:markdown-url="page.pageData.markdownUrl"
/>
<ViewOptionsPopover
:markdown-url="page.pageData.markdownUrl"
:github-url="page.pageData.githubUrl"
/>
<PageActions v-if="PageActions" />
</div>
<DocsBody>
<PageContent><component :is="page.Content" /></PageContent>
</DocsBody>
<PageLastUpdate
v-if="page.pageData.lastModified"
:date="new Date(page.pageData.lastModified)"
/>
<PageBottom v-if="PageBottom" />
</DocsPage>
</DocsLayout>
</template>toTocItems() returns table of contents items for the page's headings.
Layout Packages
To publish a layout that renders pages like the built-in docs layouts, with the page actions, the theme's slots and the parts a site can replace, make it from your UI components with createDocsFamilyLayout(), as @vuesho/flux does:
import { createDocsFamilyLayout, type LayoutSlots, type PartSlots } from "vuesho/theme";
import { DocsLayout, type DocsLayoutProps, type DocsSlots } from "./ui";
import {
DocsBody,
DocsDescription,
DocsPage,
DocsTitle,
PageLastUpdate,
type DocsPageProps,
type DocsPageSlots,
} from "./ui/page";
type Parts = PartSlots<DocsSlots, "layout"> & PartSlots<DocsPageSlots, "page">;
export default createDocsFamilyLayout<DocsLayoutProps, DocsPageProps, LayoutSlots & Parts>(
{ DocsLayout, DocsPage, DocsTitle, DocsDescription, DocsBody, PageLastUpdate },
{
// The sidebar takes `banner` and `footer`, which the slots `sidebar-banner` and `sidebar-footer` fill.
sidebarSlots: true,
// A part that the built-in layouts don't have: its name for a site, and the slot of `DocsLayout` it fills.
parts: { "side-panel": ["layout", "side-panel"] },
},
);Declare the layout's slots with ThemeSlotsOf, so a theme fills them with type checks:
import type { ThemeSlotsOf } from "vuesho/theme";
declare module "vuesho/client" {
interface ThemeSlots extends ThemeSlotsOf<LayoutSlots, Parts> {}
}