Client API
Composables and components from vuesho/client.
vuesho/client exports the composables and components that your components, layouts and custom pages use to read the current page, the site, the router and the color mode. Call a composable in <script setup>. Most composables return refs:
<script setup lang="ts">
import { usePage, useSite } from "vuesho/client";
const page = usePage();
const { name } = useSite();
</script>
<template>
<p>{{ page.pageData.title }}, from {{ name }}.</p>
</template>md writes a component's Markdown for LLMs. getHighlighter() loads your site's highlighter with its code themes, for code that a component highlights in the browser. It also exports the types ContentPage, PageOf and Theme.
Page and Site Data
usePage()
A ref of the current page. Every page has url and pageData, the data a layout renders, like title. kind is one of these:
kind | Page | Fields |
|---|---|---|
content | A content source's page | type, its source's key, and data |
custom | A custom page | props and contentOf, from definePage() |
view | A page that a plugin adds | type, the view's name, props and contentOf |
not-found | The not found page | None |
error | A page that failed to render on the server | None |
usePage<typeof app>() types the page from your config. Once kind is narrowed to content, page.data has the frontmatter fields, and with multiple sources, narrowing type narrows page.data to one source's fields:
<script setup lang="ts">
import { usePage } from "vuesho/client";
import type app from "../../vuesho.config";
const page = usePage<typeof app>();
</script>
<template>
<time v-if="page.kind === 'content' && page.data.date">{{ page.data.date }}</time>
</template>useSite()
The site in the current page's language, as refs. localizePath() returns a path in another language, for translated links:
Prop
Type
usePageTree()
A ref of the page tree in the reader's language, which the sidebar renders. vuesho/page-tree has functions to search it, like findSiblings():
<script setup lang="ts">
import { computed } from "vue";
import { usePage, usePageTree } from "vuesho/client";
import { findSiblings } from "vuesho/page-tree";
import { Card, Cards } from "vuesho/ui/components/card";
const page = usePage();
const tree = usePageTree();
const peers = computed(
() => findSiblings(tree.value, page.value.url).filter((node) => node.type === "page"),
);
</script>
<template>
<Cards>
<Card v-for="peer in peers" :key="peer.url" :href="peer.url" :description="peer.description">
<template #title><component :is="peer.name" /></template>
</Card>
</Cards>
</template>A node's name and icon are components, rendered with <component :is>. The whole app shares the tree, so run a function that changes nodes, like visit(), on a copy.
Prop
Type
useDataRoute()
Takes a data route's path, as a string, a ref or a getter, and returns a ref of its value. The server sends the value with the page, so the page has it from its first render. For a path the page didn't receive, the value is undefined until it loads:
<script setup lang="ts">
import { useDataRoute } from "vuesho/client";
const stars = useDataRoute<{ count: number }>("/api/stars.json");
</script>
<template>
<span v-if="stars">{{ stars.count }} stars</span>
</template>Routes and Links
Link
A link on your site. A link to a page navigates without reloading the document, and loads the page when the reader hovers over it. A URL on another site opens in a new tab:
<script setup lang="ts">
import { Link } from "vuesho/client";
</script>
<template>
<Link href="/install">Get started</Link>
<Link href="https://github.com/acme/docs">GitHub</Link>
</template>A path starts from your site's root, and Link adds the base path:
Prop
Type
addBasePath()
Adds the base path to a site path, for a URL that no Link renders, like a file in public/:
<script setup lang="ts">
import { addBasePath } from "vuesho/client";
</script>
<template>
<img :src="addBasePath('/logo.png')" alt="Acme" />
</template>useRoute()
The current page's route, as a reactive object. Its path has no base path, and href is the URL in the address bar:
Prop
Type
A page built to HTML has no query, so render a component that reads query only after the app mounts.
useRouter()
The router, which navigates from code. push() and replace() take a site path, and pending is true while a page loads:
<script setup lang="ts">
import { useRouter } from "vuesho/client";
const router = useRouter();
const go = (event: Event) => router.push((event.target as HTMLSelectElement).value);
</script>
<template>
<select aria-label="Version" @change="go">
<option value="/docs">Latest</option>
<option value="/v1/docs">v1</option>
</select>
<p v-if="router.pending.value" role="status">Loading…</p>
</template>{ scroll: false } keeps the scroll position. refresh() reloads the current page from the server and keeps the position, for a CMS's live preview. beforeEach() and afterEach() add router hooks.
Prop
Type
Layouts and Color Mode
useColorMode()
Returns the color mode as two refs. preference is the reader's choice, "light", "dark" or "system", and a component can set it. isDark is whether the page is dark:
<script setup lang="ts">
import { useColorMode } from "vuesho/client";
const { preference, isDark } = useColorMode();
</script>
<template>
<button type="button" @click="preference = isDark ? 'light' : 'dark'">
Switch the color mode
</button>
</template>isDark is undefined on the server and until the app is mounted, and so is preference unless the color mode is fixed. Style a page for dark mode with .dark in CSS, and use isDark for what a script draws, like a chart.
Fixed Color Modes
When the color mode is fixed, or the page sets colorMode, setting preference keeps the reader's choice, which shows once the color mode isn't fixed.
useLayoutProps()
A computed ref of the layout props for a layout component from vuesho/ui, like DocsLayout. It merges the props from the config, the themes and the plugins, and keeps only the props that the component declares. The second argument's props win, nested objects merge key by key, and a list like links replaces the shared one:
import { useLayoutProps } from "vuesho/client";
import { DocsLayout, type DocsLayoutProps } from "vuesho/ui/layouts/docs";
const layoutProps = useLayoutProps<DocsLayoutProps>(DocsLayout, {
sidebar: { collapsible: false },
});A layout built from UI parts binds them with v-bind, as Building From UI Parts shows.
Slot Composables
A custom layout renders the theme's slots with these composables, one for each kind of slot:
import { useAddingSlot, useReplacingSlot, useWrappingSlot } from "vuesho/client";
// A component that renders every component in the slot, or `undefined` without any.
const PageBottom = useAddingSlot("page-bottom");
// The last component in the slot, or `undefined` without any.
const NavTitle = useReplacingSlot("nav-title");
// A component that renders its default slot inside every component in the slot.
const PageContent = useWrappingSlot("page-content");Components
Image
An image, optimized and lazily loaded like Markdown images. src takes the URL that the browser requests: a Vite import, or a path in public/ with the base path:
<script setup lang="ts">
import { Image, addBasePath } from "vuesho/client";
</script>
<template>
<Image :src="addBasePath('/hero.png')" alt="The editor" :width="1200" :height="630" preload />
</template>Pages
definePage()
Declares a custom page's data and props. definePage() is a macro, so call it once, at the top level of the script, with an object literal that refers only to imports:
<script setup lang="ts">
import { definePage } from "vuesho/client";
definePage({ data: { title: "About", description: "Who writes these docs." } });
</script>useBuildError()
Returns a function that fails vuesho build with an error from the current page, for a mistake that would otherwise show only on the deployed site. On a server, the page fails too, unless it renders on request. In the browser, the function does nothing:
<script setup lang="ts">
import { useBuildError } from "vuesho/client";
const props = defineProps<{ id?: string }>();
const fail = useBuildError();
if (!props.id) fail("<Video> needs the id of a video.");
</script>
<template>
<iframe v-if="id" :src="`https://www.youtube-nocookie.com/embed/${id}`" title="Video" />
</template>