/
render.ts
56 lines (46 loc) · 1.39 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
import dedent from 'ts-dedent';
import { createApp, h, shallowRef, ComponentPublicInstance } from 'vue';
import { RenderContext, StoryFnVueReturnType } from './types';
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 default function render({
storyFn,
kind,
name,
args,
showMain,
showError,
showException,
forceRender,
}: RenderContext) {
storybookApp.config.errorHandler = showException;
const element: StoryFnVueReturnType = storyFn();
if (!element) {
showError({
title: `Expecting a Vue component from the story: "${name}" of "${kind}".`,
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('#root');
}
}