/
render.ts
66 lines (53 loc) · 1.89 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import dedent from 'ts-dedent';
import { createApp, h, shallowRef, ComponentPublicInstance } from 'vue';
import { RenderContext } from '@storybook/store';
import { ArgsStoryFn } from '@storybook/csf';
import { StoryFnVueReturnType } from './types';
import { VueFramework } from './types-6-0';
export const render: ArgsStoryFn<VueFramework> = (props, context) => {
const { id, component: Component } = context;
if (!Component) {
throw new Error(
`Unable to render story ${id} as the component annotation is missing from the default export`
);
}
// TODO remove this hack
return h(Component as Parameters<typeof h>[0], props);
};
export const activeStoryComponent = shallowRef<StoryFnVueReturnType | null>(null);
let root: ComponentPublicInstance | null = null;
export const storybookApp = createApp({
// If an end-user calls `unmount` on the app, we need to clear our root variable
unmounted() {
root = null;
},
setup() {
return () => {
if (!activeStoryComponent.value)
throw new Error('No Vue 3 Story available. Was it set correctly?');
return h(activeStoryComponent.value);
};
},
});
export function renderToDOM(
{ title, name, storyFn, showMain, showError, showException }: RenderContext<VueFramework>,
domElement: HTMLElement
) {
storybookApp.config.errorHandler = showException;
const element: StoryFnVueReturnType = storyFn();
if (!element) {
showError({
title: `Expecting a Vue component from the story: "${name}" of "${title}".`,
description: dedent`
Did you forget to return the Vue component from the story?
Use "() => ({ template: '<my-comp></my-comp>' })" or "() => ({ components: MyComp, template: '<my-comp></my-comp>' })" when defining the story.
`,
});
return;
}
showMain();
activeStoryComponent.value = element;
if (!root) {
root = storybookApp.mount(domElement);
}
}