-
-
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 #22050 from storybookjs/norbert/type-improvement-docs
make typings on Docs component better
- Loading branch information
Showing
3 changed files
with
26 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
import type { FunctionComponent } from 'react'; | ||
import type { PropsWithChildren } from 'react'; | ||
import React, { useRef } from 'react'; | ||
import type { Renderer, ProjectAnnotations } from '@storybook/types'; | ||
import { composeConfigs } from '@storybook/preview-api'; | ||
|
||
import { Docs } from '../Docs'; | ||
import { ExternalPreview } from './ExternalPreview'; | ||
|
||
export type ExternalDocsProps<TFramework extends Renderer = Renderer> = { | ||
projectAnnotationsList: ProjectAnnotations<TFramework>[]; | ||
export type ExternalDocsProps<TRenderer extends Renderer = Renderer> = { | ||
projectAnnotationsList: ProjectAnnotations<TRenderer>[]; | ||
}; | ||
|
||
function usePreview<TFramework extends Renderer = Renderer>( | ||
projectAnnotations: ProjectAnnotations<TFramework> | ||
function usePreview<TRenderer extends Renderer = Renderer>( | ||
projectAnnotations: ProjectAnnotations<TRenderer> | ||
) { | ||
const previewRef = useRef<ExternalPreview>(); | ||
if (!previewRef.current) previewRef.current = new ExternalPreview(projectAnnotations); | ||
const previewRef = useRef<ExternalPreview<TRenderer>>(); | ||
if (!previewRef.current) previewRef.current = new ExternalPreview<TRenderer>(projectAnnotations); | ||
return previewRef.current; | ||
} | ||
|
||
export const ExternalDocs: FunctionComponent<ExternalDocsProps> = ({ | ||
export function ExternalDocs<TRenderer extends Renderer = Renderer>({ | ||
projectAnnotationsList, | ||
children, | ||
}) => { | ||
const projectAnnotations = composeConfigs(projectAnnotationsList); | ||
const preview = usePreview(projectAnnotations); | ||
}: PropsWithChildren<ExternalDocsProps<TRenderer>>) { | ||
const projectAnnotations = composeConfigs<TRenderer>(projectAnnotationsList); | ||
const preview = usePreview<TRenderer>(projectAnnotations); | ||
const docsParameter = { | ||
...projectAnnotations.parameters?.docs, | ||
page: () => children, | ||
}; | ||
|
||
return <Docs docsParameter={docsParameter} context={preview.docsContext()} />; | ||
}; | ||
const TDocs = Docs as typeof Docs<TRenderer>; | ||
|
||
return <TDocs docsParameter={docsParameter} context={preview.docsContext()} />; | ||
} |