Stories
Live previews of Vue components.
@vuesho/story adds a live preview of a Vue component to a page, with controls generated from its prop and slot types. Change a control below:
Callout
This is a Callout
Setup
Install the Packages
The plugin needs TypeScript 6's JavaScript API, which TypeScript 7 doesn't have:
npm i @vuesho/story
npm i -D typescript@6Add the Plugin
import { createSite, recommendedPreset } from "vuesho/config";
import { defineDocs } from "vuesho/content";
import { storyPlugin } from "@vuesho/story";
const docs = defineDocs();
export default createSite(docs.toSource()).plugins(
recommendedPreset(),
storyPlugin(),
);Writing Stories
<auto-story> previews the component at path:
<auto-story path="../src/components/Button.vue" display-name="Button" />Prop
Type
The panel has a control for each prop and each slot, where a slot's control has a # prefix, like #default. A prop whose type has no control, like a function, keeps its default.
Write path and name as plain attributes, since a bound one, like :path, makes the page fail to compile.
Arguments
:args sets the initial values of the props and slots:
<auto-story
path="../src/components/Button.vue"
:args="{ initial: { size: 'lg', disabled: true, '#default': 'Save' } }"
/>Prop
Type
A key that isn't a prop or a slot, or a value of the wrong type, makes the page fail to render, and the error lists the props and slots. :args also accepts class, style and attributes for the component's root element, like id.
Variants
A list in :args sets variants for the reader to pick from. The props in fixed keep their value, and their controls are hidden:
<auto-story
path="../src/components/Button.vue"
:args="[
{ variant: 'Primary', initial: { '#default': 'Save' } },
{
variant: 'Danger',
description: 'For destructive actions',
initial: { '#default': 'Delete' },
fixed: { tone: 'danger' },
},
]"
/>Controls
controls.transform changes the generated controls, and controls.node replaces them. To show no controls, set controls.node to { type: 'object', properties: [] }. @vuesho/story/type-tree exports the control types:
<script setup lang="ts">
import type { ObjectNode } from "@vuesho/story/type-tree";
// Remove the control of the `data` prop
const transform = (node: ObjectNode) => ({
...node,
properties: node.properties.filter((property) => property.name !== "data"),
});
</script>
<auto-story path="../src/components/Graph.vue" :args="{ controls: { transform } }" />Rendering in a Frame
:frame renders a full-screen component, like a layout, in a scaled iframe. The iframe loads once the page is idle, and has a button to show it full screen:
<script setup lang="ts">
import Window from "../src/components/Window.vue";
</script>
<auto-story
path="../src/components/AppShell.lazy.ts"
:frame="{ src: '/story/app-shell', width: 1280, height: 800, Container: Window }"
/>Prop
Type
src is the path of a custom page, without the base path. The page renders the component in StoryFrame:
<script setup lang="ts">
import { StoryFrame } from "@vuesho/story/ui/client/frame";
import AppShell from "../../components/AppShell.vue";
</script>
<template>
<StoryFrame :Component="AppShell" />
</template>Point path to an async component, so the docs page doesn't bundle it and only the iframe loads it. The arguments reach the iframe as JSON, so their values must be serializable:
import { defineAsyncComponent } from "vue";
export default defineAsyncComponent(() => import("./AppShell.vue"));Reusing Stories
To use a story on several pages, put it in an included file. Its path is relative to that file:
<auto-story path="../src/components/Button.vue" :args="{ initial: { '#default': 'Save' } }" /><include>./_button-story.md</include>In a custom page, or anywhere outside Markdown, defineStory() creates the story as a component. It takes the component and the options of <auto-story>:
import { defineStory } from "@vuesho/story/client";
// `storyPlugin()` reads the props from the imported component's types.
import Button from "./Button.vue";
export const ButtonStory = defineStory(Button, {
displayName: "Button",
args: { initial: { "#default": "Save" } },
});Customizing the Panel
Copy the panel's source into your project with vuesho add:
npx vuesho add client/with-control --from @vuesho/storyThen write a component that renders Story with your copy of the panel in its default slot, and place it as Story.ts in src/components/content/:
import { Story } from "@vuesho/story/client";
import type { WithControlProps } from "@vuesho/story/ui/provider";
import { h, type FunctionalComponent } from "vue";
import { WithControl } from "../../ui/@vuesho/story/client/with-control";
type StoryProps = InstanceType<typeof Story>["$props"];
const MyStory: FunctionalComponent<StoryProps> = (props) =>
h(Story, props, { default: (panel: WithControlProps) => h(WithControl, panel) });
export default MyStory;In your panel, read the story with the composables from @vuesho/story/ui/provider:
useStoryArgs()returns the current values of the props and slots.useStory()returns the variants and the selected one.
Options
Prop
Type
In a project created by create-vue, tsconfig.json only references other configs, so point tsconfigPath to the one for your components. When a component's imports don't resolve, the build fails and names the file:
storyPlugin({ tsconfigPath: "tsconfig.app.json" });Translations
The panel's labels come in English, Japanese, Simplified Chinese and Traditional Chinese, and follow the page's language. To replace a label or add a language, use ui.translations:
createSite(docs.toSource()).config({
ui: {
translations: { "Props(story controls)": "Properties" },
},
});