-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
render.ts
94 lines (83 loc) · 2.93 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* eslint-disable no-param-reassign */
import { document, fetch, Node } from 'global';
import dedent from 'ts-dedent';
import { Args, ArgTypes } from '@storybook/api';
import { simulatePageLoad, simulateDOMContentLoaded } from '@storybook/client-api';
import { RenderContext, FetchStoryHtmlType } from './types';
const rootElement = document.getElementById('root');
const defaultFetchStoryHtml: FetchStoryHtmlType = async (url, path, params, storyContext) => {
const fetchUrl = new URL(`${url}/${path}`);
fetchUrl.search = new URLSearchParams({ ...storyContext.globals, ...params }).toString();
const response = await fetch(fetchUrl);
return response.text();
};
const buildStoryArgs = (args: Args, argTypes: ArgTypes) => {
const storyArgs = { ...args };
Object.keys(argTypes).forEach((key: string) => {
const argType = argTypes[key];
const { control } = argType;
const controlType = control && control.type.toLowerCase();
const argValue = storyArgs[key];
switch (controlType) {
case 'date':
// For cross framework & language support we pick a consistent representation of Dates as strings
storyArgs[key] = new Date(argValue).toISOString();
break;
case 'array': {
// use the supplied separator when serializing an array as a string
const separator = control.separator || ',';
storyArgs[key] = argValue.join(separator);
break;
}
case 'object':
// send objects as JSON strings
storyArgs[key] = JSON.stringify(argValue);
break;
default:
}
});
return storyArgs;
};
export async function renderMain({
id,
kind,
name,
showError,
forceRender,
parameters,
storyContext,
storyFn,
args,
argTypes,
targetDOMNode = rootElement,
}: RenderContext) {
// Some addons wrap the storyFn so we need to call it even though Server doesn't need the answer
storyFn();
const storyArgs = buildStoryArgs(args, argTypes);
const {
server: { url, id: storyId, fetchStoryHtml = defaultFetchStoryHtml, params },
} = parameters;
const fetchId = storyId || id;
const storyParams = { ...params, ...storyArgs };
const element = await fetchStoryHtml(url, fetchId, storyParams, storyContext);
if (typeof element === 'string') {
targetDOMNode.innerHTML = element;
simulatePageLoad(targetDOMNode);
} else if (element instanceof Node) {
// Don't re-mount the element if it didn't change and neither did the story
if (targetDOMNode.firstChild === element && forceRender === true) {
return;
}
targetDOMNode.innerHTML = '';
targetDOMNode.appendChild(element);
simulateDOMContentLoaded();
} else {
showError({
title: `Expecting an HTML snippet or DOM node from the story: "${name}" of "${kind}".`,
description: dedent`
Did you forget to return the HTML snippet from the story?
Use "() => <your snippet or node>" or when defining the story.
`,
});
}
}