-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
/
render.ts
39 lines (36 loc) · 1.18 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
/* eslint-disable no-param-reassign */
import { document, Node } from 'global';
import dedent from 'ts-dedent';
import { simulatePageLoad, simulateDOMContentLoaded } from '@storybook/client-api';
import { RenderContext } from './types';
const rootElement = document.getElementById('root');
export default function renderMain({
storyFn,
kind,
name,
showError,
forceRender,
targetDOMNode = rootElement,
}: RenderContext) {
const element = storyFn();
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.
`,
});
}
}