@muban/storybook
Version:
Storybook for Muban: View and Test Muban components.
37 lines (36 loc) • 1.4 kB
JavaScript
/* eslint-disable no-underscore-dangle */
import { SNIPPET_RENDERED, SourceType } from '@storybook/docs-tools';
import { addons, useEffect } from '@storybook/preview-api';
function skipSourceRender(context) {
const sourceParams = context.parameters.docs.source;
const isArgsStory = context.parameters['__isArgsStory'];
// always render if the user forces it
if (sourceParams.type === SourceType.DYNAMIC) {
return false;
}
// never render if the user is forcing the block to render code, or
// if the user provides code, or if it's not an args story.
return !isArgsStory || Boolean(sourceParams.code) || sourceParams.type === SourceType.CODE;
}
export function sourceDecorator(storyFn, context) {
const story = storyFn();
const renderedForSource = context.parameters.docs.source.excludeDecorators
? context.originalStoryFn(context.args, context)
: story;
let source;
if (!skipSourceRender(context)) {
if (typeof renderedForSource === 'string') {
source = renderedForSource;
}
else if (renderedForSource instanceof Element) {
source = renderedForSource.outerHTML;
}
}
useEffect(() => {
const { id, args } = context;
if (source) {
addons.getChannel().emit(SNIPPET_RENDERED, { id, args, source });
}
});
return story;
}