Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Components: Add script tag support #12509

Merged
merged 1 commit into from Sep 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions app/web-components/package.json
Expand Up @@ -40,6 +40,7 @@
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/preset-env": "^7.11.5",
"@storybook/addons": "6.1.0-alpha.12",
"@storybook/client-api": "6.1.0-alpha.12",
"@storybook/core": "6.1.0-alpha.12",
"@types/webpack-env": "^1.15.2",
"babel-plugin-bundled-import-meta": "^0.3.1",
Expand Down
4 changes: 4 additions & 0 deletions app/web-components/src/client/preview/render.ts
@@ -1,6 +1,7 @@
import { document, Node } from 'global';
import dedent from 'ts-dedent';
import { render, TemplateResult } from 'lit-html';
import { simulatePageLoad, simulateDOMContentLoaded } from '@storybook/client-api';
import { RenderContext } from './types';

const rootElement = document.getElementById('root');
Expand All @@ -26,8 +27,10 @@ export default function renderMain({
const renderTo = rootElement.querySelector('[id="root-inner"]');

render(element, renderTo);
simulatePageLoad(rootElement);
} else if (typeof element === 'string') {
rootElement.innerHTML = element;
simulatePageLoad(rootElement);
} else if (element instanceof Node) {
// Don't re-mount the element if it didn't change and neither did the story
if (rootElement.firstChild === element && forceRender === true) {
Expand All @@ -36,6 +39,7 @@ export default function renderMain({

rootElement.innerHTML = '';
rootElement.appendChild(element);
simulateDOMContentLoaded();
} else {
showError({
title: `Expecting an HTML snippet or DOM node from the story: "${name}" of "${kind}".`,
Expand Down
13 changes: 13 additions & 0 deletions examples/web-components-kitchen-sink/stories/script.stories.js
@@ -0,0 +1,13 @@
import { html } from 'lit-html';

export default {
title: 'Script Tag',
};

export const inTemplate = () =>
html`<div>JS alert</div>
<script>
alert('hello');
</script>`;

export const inString = () => '<div>JS alert</div><script>alert("hello")</script>';