Skip to content
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

make typings on Docs component better #22050

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion code/addons/docs/src/DocsRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ export class DocsRenderer<TRenderer extends Renderer> {
...docsParameter?.components,
};

const TDocs = Docs as typeof Docs<TRenderer>;

return new Promise((resolve, reject) => {
import('@mdx-js/react')
.then(({ MDXProvider }) =>
// We use a `key={}` here to reset the `hasError` state each time we render ErrorBoundary
renderElement(
<ErrorBoundary showException={reject} key={Math.random()}>
<MDXProvider components={components}>
<Docs context={context} docsParameter={docsParameter} />
<TDocs context={context} docsParameter={docsParameter} />
</MDXProvider>
</ErrorBoundary>,
element
Expand Down
13 changes: 8 additions & 5 deletions code/ui/blocks/src/blocks/Docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ import type { DocsContextProps } from './DocsContext';
import { DocsContainer } from './DocsContainer';
import { DocsPage } from './DocsPage';

export type DocsProps<TFramework extends Renderer = Renderer> = {
export type DocsProps<TRenderer extends Renderer = Renderer> = {
docsParameter: Parameters;
context: DocsContextProps<TFramework>;
context: DocsContextProps<TRenderer>;
};

export const Docs = <T extends Renderer>({ docsParameter, context }: DocsProps<T>) => {
const Container: ComponentType<{ context: DocsContextProps; theme: Theme }> =
export function Docs<TRenderer extends Renderer = Renderer>({
context,
docsParameter,
}: DocsProps<TRenderer>) {
const Container: ComponentType<{ context: DocsContextProps<TRenderer>; theme: Theme }> =
docsParameter.container || DocsContainer;

const Page = docsParameter.page || DocsPage;
Expand All @@ -23,4 +26,4 @@ export const Docs = <T extends Renderer>({ docsParameter, context }: DocsProps<T
<Page />
</Container>
);
};
}
28 changes: 15 additions & 13 deletions code/ui/blocks/src/blocks/external/ExternalDocs.tsx
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()} />;
}