mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-05 16:11:33 +08:00
Web-components: Attempt default render function
This commit is contained in:
parent
62b77cc7d4
commit
c01bb7decd
@ -2,4 +2,4 @@ import { parameters as docsParams } from './docs/config';
|
|||||||
|
|
||||||
export const parameters = { framework: 'web-components' as const, ...docsParams };
|
export const parameters = { framework: 'web-components' as const, ...docsParams };
|
||||||
export { decorators, argTypesEnhancers } from './docs/config';
|
export { decorators, argTypesEnhancers } from './docs/config';
|
||||||
export { renderToDOM } from './render';
|
export { render, renderToDOM } from './render';
|
||||||
|
@ -3,16 +3,33 @@
|
|||||||
import global from 'global';
|
import global from 'global';
|
||||||
|
|
||||||
import { dedent } from 'ts-dedent';
|
import { dedent } from 'ts-dedent';
|
||||||
import { render } from 'lit-html';
|
import { render as litRender, html } from 'lit-html';
|
||||||
// Keep `.js` extension to avoid issue with Webpack (related to export map?)
|
// Keep `.js` extension to avoid issue with Webpack (related to export map?)
|
||||||
// eslint-disable-next-line import/extensions
|
// eslint-disable-next-line import/extensions
|
||||||
import { isTemplateResult } from 'lit-html/directive-helpers.js';
|
import { isTemplateResult } from 'lit-html/directive-helpers.js';
|
||||||
import { simulatePageLoad, simulateDOMContentLoaded } from '@storybook/preview-web';
|
import { simulatePageLoad, simulateDOMContentLoaded } from '@storybook/preview-web';
|
||||||
import type { RenderContext } from '@storybook/store';
|
import type { RenderContext } from '@storybook/store';
|
||||||
|
import { ArgsStoryFn } from '@storybook/csf';
|
||||||
import { WebComponentsFramework } from './types';
|
import { WebComponentsFramework } from './types';
|
||||||
|
|
||||||
const { Node } = global;
|
const { Node } = global;
|
||||||
|
|
||||||
|
export const render: ArgsStoryFn<WebComponentsFramework> = (args, context) => {
|
||||||
|
const { id, component } = context;
|
||||||
|
if (!component) {
|
||||||
|
throw new Error(
|
||||||
|
`Unable to render story ${id} as the component annotation is missing from the default export`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: replace by one of
|
||||||
|
// - https://github.com/lit/lit/pull/1960
|
||||||
|
// - https://github.com/open-wc/open-wc/pull/2398
|
||||||
|
const argProps = Object.entries(args).map(([key, val]) => `.${key}=${val}`);
|
||||||
|
|
||||||
|
return html`<${component} ${argProps.join(' ')}></${component}>`;
|
||||||
|
};
|
||||||
|
|
||||||
export function renderToDOM(
|
export function renderToDOM(
|
||||||
{ storyFn, kind, name, showMain, showError, forceRemount }: RenderContext<WebComponentsFramework>,
|
{ storyFn, kind, name, showMain, showError, forceRemount }: RenderContext<WebComponentsFramework>,
|
||||||
domElement: Element
|
domElement: Element
|
||||||
@ -29,7 +46,7 @@ export function renderToDOM(
|
|||||||
}
|
}
|
||||||
const renderTo = domElement.querySelector<HTMLElement>('[id="root-inner"]');
|
const renderTo = domElement.querySelector<HTMLElement>('[id="root-inner"]');
|
||||||
|
|
||||||
render(element, renderTo);
|
litRender(element, renderTo);
|
||||||
simulatePageLoad(domElement);
|
simulatePageLoad(domElement);
|
||||||
} else if (typeof element === 'string') {
|
} else if (typeof element === 'string') {
|
||||||
domElement.innerHTML = element;
|
domElement.innerHTML = element;
|
||||||
|
1
code/renderers/web-components/template/stories/README.md
Normal file
1
code/renderers/web-components/template/stories/README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
Placeholder until we write some render-specific stories
|
Loading…
x
Reference in New Issue
Block a user