From d841bb417d7e5f8d4cd0e31786f40f5339f14912 Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Tue, 25 Jun 2024 16:28:33 +0200 Subject: [PATCH 1/4] 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 From b13ced295ff8aaa75fe00fec3ef9b4d48d252426 Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Wed, 26 Jun 2024 17:14:21 +0200 Subject: [PATCH 2/4] Fix types to interfaces --- src/story.ts | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/story.ts b/src/story.ts index 1b0c5b9..2eaf9a5 100644 --- a/src/story.ts +++ b/src/story.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-empty-interface */ import type { RemoveIndexSignature, Simplify, UnionToIntersection } from 'type-fest'; import type { SBScalarType, SBType } from './SBType'; @@ -273,16 +274,12 @@ export interface StoryContext = StoryContext; +export interface StoryContextForLoaders + extends StoryContext {} /** @deprecated Use {@link StoryContext} instead. */ -export type PlayFunctionContext = StoryContext< - TRenderer, - TArgs ->; +export interface PlayFunctionContext + extends StoryContext {} export type StepLabel = string; From d6ab1d5045cc37876c327139be79c93444653b77 Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Wed, 26 Jun 2024 17:16:20 +0200 Subject: [PATCH 3/4] Add canvas property to context that can be implemented by addons such as @storybook/test --- src/story.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/story.ts b/src/story.ts index 2eaf9a5..f98d2e4 100644 --- a/src/story.ts +++ b/src/story.ts @@ -260,6 +260,8 @@ export type BeforeEach = ( context: StoryContext ) => Awaitable; +export interface Canvas {} + export interface StoryContext extends StoryContextForEnhancers, Required> { @@ -271,6 +273,7 @@ export interface StoryContext; context: this; + canvas: Canvas; } /** @deprecated Use {@link StoryContext} instead. */ From ba767850ebb52f8a3bf2bdafca6bc9c521dc750f Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Thu, 27 Jun 2024 11:12:54 +0200 Subject: [PATCH 4/4] Disable unnecessary eslint rule --- package.json | 1 + src/story.ts | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index b38ce45..ba64d01 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,7 @@ "@storybook/eslint-config-storybook" ], "rules": { + "@typescript-eslint/no-empty-interface": "off", "import/no-unresolved": "error", "jest/expect-expect": [ "warn", diff --git a/src/story.ts b/src/story.ts index f98d2e4..424edf1 100644 --- a/src/story.ts +++ b/src/story.ts @@ -1,4 +1,3 @@ -/* eslint-disable @typescript-eslint/no-empty-interface */ import type { RemoveIndexSignature, Simplify, UnionToIntersection } from 'type-fest'; import type { SBScalarType, SBType } from './SBType';