# 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](#flux-layout) is the package `@vuesho/flux`.

**Layouts**

| Prop      | Type                                                                |
| --------- | ------------------------------------------------------------------- |
| `layout?` | `"docs" \| "notebook" \| "glass" \| "spacious" \| "flux" \| "home"` |

## Built-in Layouts [#built-in-layouts]

Set your layouts in the [theme file](/theme), `src/theme.ts`. Each built-in layout is a component from `vuesho/layouts/<name>`, and the one under `default` renders every content page:

```ts title="src/theme.ts"
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 }, // [!code highlight]
});
```

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:

```ts title="src/theme.ts"
import { baseTheme, defineTheme } from "vuesho/theme";
import NotebookLayout from "vuesho/layouts/notebook";

export default defineTheme({
  extends: baseTheme, // [!code highlight]
  layouts: {
    default: NotebookLayout,
    // Pages with `layout: home`, loaded only where they are.
    home: () => import("vuesho/layouts/home"),
  },
});
```

### Flux Layout [#flux-layout]

The flux layout is a package of its own. Install it:

```npm
npm i @vuesho/flux
```

`fluxPlugin()` adds the layout's classes to the site's stylesheet:

```ts title="vuesho.config.ts"
import { fluxPlugin } from "@vuesho/flux";

export default createSite(docs.toSource()).plugins(
  recommendedPreset(),
  fluxPlugin(), // [!code ++]
);
```

Then set it in the theme like the other layouts, from `@vuesho/flux/layout`:

```ts title="src/theme.ts"
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 [#choosing-a-layout]

### Per Page [#per-page]

To render a page with another layout from the theme, set `layout` in its frontmatter:

```md tab="content/credits.md"
---
title: Credits
layout: landing # [!code highlight]
---
```

```ts tab="src/theme.ts"
export default defineTheme({
  extends: baseTheme,
  layouts: {
    default: NotebookLayout,
    landing: () => import("./Landing.vue"), // [!code ++]
  },
});
```

```vue tab="src/Landing.vue"
<script setup lang="ts">
import type { ContentPage } from "vuesho/client";
// The site navbar, without a sidebar, around a centered column
import { HomeShell } from "vuesho/theme";

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

<template>
  <HomeShell>
    <h1>{{ page.pageData.title }}</h1>
    <component :is="page.Content" />
  </HomeShell>
</template>
```

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](/markdown/collections#frontmatter-schema) with `layout: z.enum(["landing"])`.

### Per Source [#per-source]

With [multiple collections](/markdown/collections#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:

```ts title="src/theme.ts"
import DocsLayout from "vuesho/layouts/docs"; // [!code ++]

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

## Layout Props [#layout-props]

A built-in layout takes two sets of props:

- `layoutProps`, for the navbar and the sidebar
- `pageProps`, 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:

```vue title="src/DocsPage.vue"
<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>
```

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

export default defineTheme({
  extends: baseTheme,
  layouts: {
    default: NotebookLayout, // [!code --]
    default: DocsPage, // [!code ++]
  },
});
```

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](/config) and from the [theme's `layoutProps`](/theme#layout-props), and a list like `links` replaces the shared one. To keep the shared links and add more, read them with [`useLayoutProps()`](/client-api#uselayoutprops).

The tables show the `docs` layout's props. The other layouts have a few more, like `tabMode: "navbar"` in `notebook`.

### `DocsLayoutProps` [#docslayoutprops]

### DocsLayoutProps

| Prop               | Type                                                        | Description                       |
| ------------------ | ----------------------------------------------------------- | --------------------------------- |
| `tree`             | `PageTree.Root`                                             |                                   |
| `sidebar?`         | `SidebarOptions \| undefined`                               |                                   |
| `tabMode?`         | `"top" \| "auto" \| undefined`                              |                                   |
| `tabs?`            | `false \| LayoutTab[] \| GetLayoutTabsOptions \| undefined` |                                   |
| `containerProps?`  | `HTMLAttributes \| undefined`                               |                                   |
| `aiChat?`          | `AIChatOptions \| undefined`                                |                                   |
| `githubUrl?`       | `string \| undefined`                                       | The URL of the GitHub repository. |
| `links?`           | `LinkItemType[] \| undefined`                               |                                   |
| `nav?`             | `NavOptions \| undefined`                                   | Options of the navigation.        |
| `searchToggle?`    | `SearchToggleOptions \| undefined`                          |                                   |
| `colorModeSwitch?` | `ThemeSwitchOptions \| undefined`                           |                                   |

### `DocsSidebarProps` [#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](/theme#slots) instead, which need no wrapper.

### DocsSidebarProps

| Prop                | Type                                              | Description                                                                                                    |
| ------------------- | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
| `enabled?`          | `boolean \| undefined`                            |                                                                                                                |
| `banner?`           | `string \| Component \| undefined`                |                                                                                                                |
| `footer?`           | `string \| Component \| undefined`                |                                                                                                                |
| `components?`       | `Partial<SidebarPageTreeComponents> \| undefined` |                                                                                                                |
| `collapsible?`      | `boolean \| undefined`                            | Lets the reader collapse the sidebar on desktop. Default: `true`                                               |
| `defaultOpenLevel?` | `number \| undefined`                             | Opens the folders whose level is lower than or equal to this one by default. Levels start from 1. Default: `0` |
| `prefetch?`         | `boolean \| undefined`                            | Prefetches the links. Without a value, the links keep their default.                                           |

### `DocsPageProps` [#docspageprops]

`pageProps` override the page data, so `pageProps.full` replaces `full` in the frontmatter:

### DocsPageProps

| Prop                     | Type                                        | Description                                                        |
| ------------------------ | ------------------------------------------- | ------------------------------------------------------------------ |
| `full?`                  | `boolean \| undefined`                      | Extends the page to fill all the available space. Default: `false` |
| `footer?`                | `FooterOptions \| undefined`                |                                                                    |
| `breadcrumb?`            | `BreadcrumbOptions \| undefined`            |                                                                    |
| `tableOfContent?`        | `TableOfContentOptions \| undefined`        |                                                                    |
| `tableOfContentPopover?` | `TableOfContentPopoverOptions \| undefined` |                                                                    |

### Showing One Folder [#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:

```vue title="src/DocsPage.vue"
<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")); // [!code highlight]
</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](/navigation#root-folders).

## Landing Page [#landing-page]

Without a `page` prop, `HomeLayout` renders its default slot under the site navbar, with no sidebar. Use it in a [custom page](/custom-pages) at `src/pages/index.vue` for a landing page:

```vue title="src/pages/index.vue"
<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`:

```ts title="vuesho.config.ts"
// Adds the URL prefix `/docs`. Unlike `toSource({ baseDir: "docs" })`, the sidebar keeps its shape.
export default createSite(docs.toSource(), { baseSlugs: ["docs"] }); // [!code highlight]
```

## Custom Layout [#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:

```vue tab="src/Article.vue"
<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>
```

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

A page has two sets of data:

- `page.data` is the file's data as written, like a Markdown page's frontmatter.
- `page.pageData` is 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](/plugins/feedback).

### ContentPage

| Prop       | Type                     | Description                                                    |
| ---------- | ------------------------ | -------------------------------------------------------------- |
| `url`      | `string`                 | The page's URL path, without the base path.                    |
| `kind`     | `"content"`              |                                                                |
| `type?`    | `string \| undefined`    | The source key, or none with one source.                       |
| `data`     | `object`                 | The page file's data as JSON, with `icon` as HTML.             |
| `pageData` | `PageData`               | The page's resolved metadata, after the `transformPage` hooks. |
| `params`   | `Record<string, string>` | The route params matched for a dynamic content file.           |
| `toc`      | `TOCEntry[]`             | The page's headings for the table of contents.                 |
| `Content`  | `Component`              | Renders the content, or nothing when the source gives none.    |

### Typed Pages [#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](/markdown/collections#frontmatter-schema):

```vue title="src/Article.vue"
<script setup lang="ts">
import type { PageOf } from "vuesho/client";
import type app from "../vuesho.config";

defineProps<{ page: PageOf<typeof app, "docs"> }>(); // [!code highlight]
</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 [#layout-slots]

Declare a custom layout's slots in `ThemeSlots`, each as `"add"`, `"replace"` or `"wrap"`, and render each slot with its composable:

```vue title="src/Article.vue"
<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 [#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](/plugins/feedback), still shows:

```vue title="src/DocsPage.vue"
<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"); // [!code highlight:3]
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 [#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:

```ts title="src/layouts/Wide.ts"
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:

```ts
import type { ThemeSlotsOf } from "vuesho/theme";

declare module "vuesho/client" {
  interface ThemeSlots extends ThemeSlotsOf<LayoutSlots, Parts> {}
}
```
