From d841bb417d7e5f8d4cd0e31786f40f5339f14912 Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Tue, 25 Jun 2024 16:28:33 +0200 Subject: [PATCH] Consolidate loader, play and render context and add a self referencing context property --- src/story.ts | 52 +++++++++++++++++++++++++++------------------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/src/story.ts b/src/story.ts index df41a93..1b0c5b9 100644 --- a/src/story.ts +++ b/src/story.ts @@ -200,7 +200,7 @@ export interface StrictGlobalTypes { [name: string]: StrictInputType; } -export type Renderer = { +export interface Renderer { /** What is the type of the `component` annotation in this renderer? */ component: unknown; @@ -215,7 +215,7 @@ export type Renderer = { // This generic type will eventually be filled in with TArgs // Credits to Michael Arnaldi. T?: unknown; -}; +} /** @deprecated - use `Renderer` */ export type AnyFramework = Renderer; @@ -247,13 +247,6 @@ export interface StoryContextUpdate { } export type ViewMode = 'story' | 'docs'; -export interface StoryContextForLoaders - extends StoryContextForEnhancers, - Required> { - hooks: unknown; - viewMode: ViewMode; - originalStoryFn: StoryFn; -} export type LoaderFunction = ( context: StoryContextForLoaders @@ -267,12 +260,30 @@ export type BeforeEach = ( ) => Awaitable; export interface StoryContext - extends StoryContextForLoaders { + extends StoryContextForEnhancers, + Required> { loaded: Record; abortSignal: AbortSignal; canvasElement: TRenderer['canvasElement']; + hooks: unknown; + originalStoryFn: StoryFn; + viewMode: ViewMode; + step: StepFunction; + context: this; } +/** @deprecated Use {@link StoryContext} instead. */ +export type StoryContextForLoaders< + TRenderer extends Renderer = Renderer, + TArgs = Args +> = StoryContext; + +/** @deprecated Use {@link StoryContext} instead. */ +export type PlayFunctionContext = StoryContext< + TRenderer, + TArgs +>; + export type StepLabel = string; export type StepFunction = ( @@ -280,13 +291,6 @@ export type StepFunction = play: PlayFunction ) => Promise | void; -export type PlayFunctionContext = StoryContext< - TRenderer, - TArgs -> & { - step: StepFunction; -}; - export type PlayFunction = ( context: PlayFunctionContext ) => Promise | void; @@ -325,10 +329,10 @@ export type DecoratorApplicator = ( label: StepLabel, play: PlayFunction, - context: PlayFunctionContext + context: StoryContext ) => Promise; -export type BaseAnnotations = { +export interface BaseAnnotations { /** * Wrapper components or Storybook decorators that wrap a story. * @@ -382,12 +386,10 @@ export type BaseAnnotations * Named tags for a story, used to filter stories in different contexts. */ tags?: Tag[]; -}; +} -export type ProjectAnnotations< - TRenderer extends Renderer = Renderer, - TArgs = Args -> = BaseAnnotations & { +export interface ProjectAnnotations + extends BaseAnnotations { argsEnhancers?: ArgsEnhancer[]; argTypesEnhancers?: ArgTypesEnhancer[]; /** @@ -398,7 +400,7 @@ export type ProjectAnnotations< globalTypes?: GlobalTypes; applyDecorators?: DecoratorApplicator; runStep?: StepRunner; -}; +} type StoryDescriptor = string[] | RegExp; export interface ComponentAnnotations