-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Preview API: Add csf factory utilities #30388
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
/* eslint-disable no-underscore-dangle */ | ||
import type { | ||
Args, | ||
ComponentAnnotations, | ||
LegacyStoryAnnotationsOrFn, | ||
ModuleExports, | ||
ProjectAnnotations, | ||
Renderer, | ||
StoryAnnotations, | ||
} from '@storybook/types'; | ||
|
||
export function getCsfFactoryPreview(preview: ModuleExports): ProjectAnnotations<any> | null { | ||
return Object.values(preview).find(isCsfFactory) ?? null; | ||
} | ||
|
||
export function isCsfFactory(target: StoryAnnotations | ProjectAnnotations<any>) { | ||
return ( | ||
target != null && | ||
typeof target === 'object' && | ||
('isCSFFactory' in target || 'isCSFFactoryPreview' in target) | ||
); | ||
} | ||
|
||
export function getCsfFactoryAnnotations< | ||
TRenderer extends Renderer = Renderer, | ||
TArgs extends Args = Args, | ||
>( | ||
story: LegacyStoryAnnotationsOrFn<TRenderer>, | ||
meta?: ComponentAnnotations<TRenderer, TArgs>, | ||
projectAnnotations?: ProjectAnnotations<TRenderer> | ||
) { | ||
const _isCsfFactory = isCsfFactory(story); | ||
|
||
return { | ||
// TODO: @kasperpeulen will fix this once csf factory types are defined | ||
story: _isCsfFactory ? (story as any)?.input : story, | ||
meta: _isCsfFactory ? (story as any)?.meta?.input : meta, | ||
preview: _isCsfFactory ? (story as any)?.config?.input : projectAnnotations, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ export * from './getValuesFromArgTypes'; | |
export * from './composeConfigs'; | ||
export * from './stepRunners'; | ||
export * from './portable-stories'; | ||
export * from './csf-factory-utils'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: consider adding a newline at the end of file to match style of other exports |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import { dedent } from 'ts-dedent'; | |
import { HooksContext } from '../../../addons'; | ||
import { ReporterAPI } from '../reporter-api'; | ||
import { composeConfigs } from './composeConfigs'; | ||
import { getCsfFactoryAnnotations } from './csf-factory-utils'; | ||
import { getValuesFromArgTypes } from './getValuesFromArgTypes'; | ||
import { normalizeComponentAnnotations } from './normalizeComponentAnnotations'; | ||
import { normalizeProjectAnnotations } from './normalizeProjectAnnotations'; | ||
|
@@ -89,21 +90,6 @@ export function setProjectAnnotations<TRenderer extends Renderer = Renderer>( | |
|
||
const cleanups: CleanupCallback[] = []; | ||
|
||
export function getAnnotations<TRenderer extends Renderer = Renderer, TArgs extends Args = Args>( | ||
story: LegacyStoryAnnotationsOrFn<TRenderer>, | ||
meta?: ComponentAnnotations<TRenderer, TArgs>, | ||
projectAnnotations?: ProjectAnnotations<TRenderer> | ||
) { | ||
const isCsfFactory = | ||
(typeof story === 'function' || typeof story === 'object') && 'isCSFFactory' in story; | ||
|
||
return { | ||
storyAnnotations: isCsfFactory ? (story as any)?.input : story, | ||
componentAnnotations: isCsfFactory ? (story as any)?.meta?.input : meta, | ||
projectAnnotations: isCsfFactory ? (story as any)?.config?.input : projectAnnotations, | ||
}; | ||
} | ||
|
||
export function composeStory<TRenderer extends Renderer = Renderer, TArgs extends Args = Args>( | ||
storyAnnotations: LegacyStoryAnnotationsOrFn<TRenderer>, | ||
componentAnnotations: ComponentAnnotations<TRenderer, TArgs>, | ||
|
@@ -295,7 +281,8 @@ export function composeStories<TModule extends Store_CSFExports>( | |
|
||
const composedStories = Object.entries(stories).reduce( | ||
(storiesMap, [exportsName, story]: [string, any]) => { | ||
const { storyAnnotations, componentAnnotations } = getAnnotations(story); | ||
const { story: storyAnnotations, meta: componentAnnotations } = | ||
getCsfFactoryAnnotations(story); | ||
Comment on lines
+284
to
+285
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: Destructuring could fail if getCsfFactoryAnnotations returns undefined. Consider adding a null check or default values. |
||
if (!meta && componentAnnotations) { | ||
meta = componentAnnotations; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ export function processCSFFile<TRenderer extends Renderer>( | |
|
||
const firstStory: any = Object.values(namedExports)[0]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: casting to |
||
// CSF4 | ||
// TODO: @kasperpeulen will fix this once csf factory types are defined | ||
if (!defaultExport && 'isCSFFactory' in firstStory) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: check for |
||
const meta: NormalizedComponentAnnotations<TRenderer> = | ||
normalizeComponentAnnotations<TRenderer>(firstStory.meta.input, title, importPath); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: optional chaining on getInitialGlobals could be moved to line 24 where the function is destructured for better error handling