-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21371 from storybookjs/tom/21366-fix-unattached-a…
…rgsTable Docs: Allow ArgTable usage unattached
- Loading branch information
Showing
6 changed files
with
123 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,24 @@ | ||
import type { | ||
DocsContextProps, | ||
ModuleExport, | ||
NormalizedProjectAnnotations, | ||
PreparedMeta, | ||
Renderer, | ||
ResolvedModuleExportFromType, | ||
ResolvedModuleExportType, | ||
ResolvedModuleExportFromType, | ||
} from '@storybook/types'; | ||
import { prepareMeta } from '@storybook/preview-api'; | ||
import { useContext } from 'react'; | ||
import { DocsContext } from './DocsContext'; | ||
|
||
export type Of = Parameters<DocsContextProps['resolveOf']>[0]; | ||
|
||
export type EnhancedResolvedModuleExportType< | ||
TType extends ResolvedModuleExportType, | ||
TRenderer extends Renderer = Renderer | ||
> = TType extends 'component' | ||
? ResolvedModuleExportFromType<TType, TRenderer> & { | ||
projectAnnotations: NormalizedProjectAnnotations<Renderer>; | ||
} | ||
: TType extends 'meta' | ||
? ResolvedModuleExportFromType<TType, TRenderer> & { preparedMeta: PreparedMeta } | ||
: ResolvedModuleExportFromType<TType, TRenderer>; | ||
|
||
/** | ||
* A hook to resolve the `of` prop passed to a block. | ||
* will return the resolved module | ||
* if the resolved module is a meta it will include a preparedMeta property similar to a preparedStory | ||
* if the resolved module is a component it will include the project annotations | ||
*/ | ||
export const useOf = < | ||
TType extends ResolvedModuleExportType, | ||
TRenderer extends Renderer = Renderer | ||
>( | ||
export const useOf = <TType extends ResolvedModuleExportType>( | ||
moduleExportOrType: ModuleExport | TType, | ||
validTypes?: TType[] | ||
): EnhancedResolvedModuleExportType<TType, TRenderer> => { | ||
): ResolvedModuleExportFromType<TType> => { | ||
const context = useContext(DocsContext); | ||
const resolved = context.resolveOf(moduleExportOrType, validTypes); | ||
|
||
switch (resolved.type) { | ||
case 'component': { | ||
return { | ||
...resolved, | ||
projectAnnotations: context.projectAnnotations, | ||
} as EnhancedResolvedModuleExportType<TType, TRenderer>; | ||
} | ||
case 'meta': { | ||
return { | ||
...resolved, | ||
preparedMeta: prepareMeta( | ||
resolved.csfFile.meta, | ||
context.projectAnnotations, | ||
resolved.csfFile.moduleExports.default | ||
), | ||
} as EnhancedResolvedModuleExportType<TType, TRenderer>; | ||
} | ||
case 'story': | ||
default: { | ||
return resolved as EnhancedResolvedModuleExportType<TType, TRenderer>; | ||
} | ||
} | ||
return context.resolveOf(moduleExportOrType, validTypes); | ||
}; |