Skip to content

Commit

Permalink
Allow originalStoryFn to be set lazily
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpeulen committed Jun 13, 2024
1 parent 4e21251 commit ef0285f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions code/lib/preview-api/src/modules/store/csf/prepareStory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { combineParameters } from '../parameters';
import { defaultDecorateStory } from '../decorators';
import { groupArgsByTarget, UNTARGETED } from '../args';
import { normalizeArrays } from './normalizeArrays';
import { getUsedProps } from '../../../modules/preview-web/render/mount-utils';

// Combine all the metadata about a story (both direct and inherited from the component/global scope)
// into a "renderable" story function, with all decorators applied, parameters passed as context etc
Expand Down Expand Up @@ -79,7 +80,7 @@ export function prepareStory<TRenderer extends Renderer>(
};

const undecoratedStoryFn = (context: StoryContext<TRenderer>) =>
(render as ArgsStoryFn<TRenderer>)(context.args, context);
(context.originalStoryFn as ArgsStoryFn<TRenderer>)(context.args, context);

// Currently it is only possible to set these globally
const { applyDecorators = defaultDecorateStory, runStep } = projectAnnotations;
Expand All @@ -97,20 +98,24 @@ export function prepareStory<TRenderer extends Renderer>(
storyAnnotations?.render ||
componentAnnotations.render ||
projectAnnotations.render;
if (!render) throw new Error(`No render function available for storyId '${id}'`);

const decoratedStoryFn = applyHooks<TRenderer>(applyDecorators)(undecoratedStoryFn, decorators);
const unboundStoryFn = (context: StoryContext<TRenderer>) => decoratedStoryFn(context);

const playFunction = storyAnnotations?.play || componentAnnotations.play;

const mountUsed = playFunction && getUsedProps(playFunction).includes('mount');
if (!render && !mountUsed) {
throw new Error(`No render function available for storyId '${id}'`);
}

return {
...partialAnnotations,
moduleExport,
id,
name,
story: name,
originalStoryFn: render,
originalStoryFn: render!,
undecoratedStoryFn,
unboundStoryFn,
applyLoaders,
Expand Down

0 comments on commit ef0285f

Please sign in to comment.