-
-
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 #18708 from storybookjs/tom/sb-453-dependencies-en…
…sure-react-dom-mdx Addon-docs: Move DocsRenderer back to addon-docs
- Loading branch information
Showing
18 changed files
with
175 additions
and
162 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { AnyFramework, Parameters } from '@storybook/csf'; | ||
import { DocsContextProps, DocsRenderFunction } from '@storybook/preview-web'; | ||
import { components as htmlComponents } from '@storybook/components'; | ||
import { Docs, CodeOrSourceMdx, AnchorMdx, HeadersMdx } from '@storybook/blocks'; | ||
import { MDXProvider } from '@mdx-js/react'; | ||
|
||
// TS doesn't like that we export a component with types that it doesn't know about (TS4203) | ||
export const defaultComponents: Record<string, any> = { | ||
...htmlComponents, | ||
code: CodeOrSourceMdx, | ||
a: AnchorMdx, | ||
...HeadersMdx, | ||
}; | ||
|
||
export class DocsRenderer<TFramework extends AnyFramework> { | ||
public render: DocsRenderFunction<TFramework>; | ||
|
||
public unmount: (element: HTMLElement) => void; | ||
|
||
constructor() { | ||
this.render = ( | ||
context: DocsContextProps<TFramework>, | ||
docsParameter: Parameters, | ||
element: HTMLElement, | ||
callback: () => void | ||
): void => { | ||
// Use a random key to force the container to re-render each time we call `renderDocs` | ||
// TODO: do we still need this? It was needed for angular (legacy) inline rendering: | ||
// https://github.com/storybookjs/storybook/pull/16149 | ||
ReactDOM.render( | ||
<MDXProvider components={defaultComponents}> | ||
<Docs key={Math.random()} context={context} docsParameter={docsParameter} /> | ||
</MDXProvider>, | ||
element, | ||
callback | ||
); | ||
}; | ||
|
||
this.unmount = (element: HTMLElement) => { | ||
ReactDOM.unmountComponentAtNode(element); | ||
}; | ||
} | ||
} |
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 +1,2 @@ | ||
export * from './blocks'; | ||
export { DocsRenderer } from './DocsRenderer'; |
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,19 +1,15 @@ | ||
/* eslint-disable react/prop-types */ | ||
import React from 'react'; | ||
import 'nextra-theme-docs/style.css'; | ||
import { ExternalDocsContainer } from '@storybook/addon-docs'; | ||
import { ExternalDocs } from '@storybook/addon-docs'; | ||
|
||
import * as reactAnnotations from '@storybook/react/preview'; | ||
import * as previewAnnotations from '../.storybook/preview'; | ||
|
||
const projectAnnotations = { | ||
...reactAnnotations, | ||
...previewAnnotations, | ||
}; | ||
|
||
export default function Nextra({ Component, pageProps }) { | ||
return ( | ||
<ExternalDocsContainer projectAnnotations={projectAnnotations}> | ||
<ExternalDocs projectAnnotationsList={[reactAnnotations, previewAnnotations]}> | ||
<Component {...pageProps} /> | ||
</ExternalDocsContainer> | ||
</ExternalDocs> | ||
); | ||
} |
47 changes: 25 additions & 22 deletions
47
examples/official-storybook/stories/addon-docs/mdx.stories.js
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,45 +1,48 @@ | ||
import React from 'react'; | ||
import { DocsContainer } from '@storybook/addon-docs'; | ||
import { themes } from '@storybook/theming'; | ||
import { MDXProvider } from '@mdx-js/react'; | ||
|
||
import markdown from './markdown.stories.mdx'; | ||
import { defaultComponents } from '../../../../addons/docs/src/DocsRenderer'; | ||
|
||
export default { | ||
title: 'Addons/Docs/mdx-in-story', | ||
decorators: [ | ||
(storyFn) => ( | ||
<DocsContainer | ||
context={{ componentStories: () => [], storyById: () => ({ parameters: {} }) }} | ||
> | ||
{storyFn()} | ||
</DocsContainer> | ||
), | ||
], | ||
parameters: { | ||
layout: 'fullscreen', | ||
}, | ||
parameters: { layout: 'fullscreen' }, | ||
}; | ||
|
||
// This renders the contents of the docs panel into story content | ||
// The purpose of these stories are to document that MDX renders properly in docs itself | ||
// As tools like Chromatic cannot capture docs entries, we need to create a story that | ||
// actually renders it's own docs, much like the DocsRenderer might. | ||
export const Typography = () => { | ||
const Docs = markdown.parameters.docs.page; | ||
return <Docs />; | ||
}; | ||
|
||
Typography.decorators = [ | ||
(storyFn) => ( | ||
<MDXProvider components={defaultComponents}> | ||
<DocsContainer context={{ componentStories: () => [], storyById: () => ({}) }}> | ||
{storyFn()} | ||
</DocsContainer> | ||
</MDXProvider> | ||
), | ||
]; | ||
|
||
export const DarkModeDocs = () => { | ||
const Docs = markdown.parameters.docs.page; | ||
return <Docs />; | ||
}; | ||
|
||
DarkModeDocs.decorators = [ | ||
(storyFn) => ( | ||
<DocsContainer | ||
context={{ | ||
type: 'legacy', | ||
componentStories: () => [], | ||
storyById: () => ({ parameters: { docs: { theme: themes.dark } } }), | ||
}} | ||
> | ||
{storyFn()} | ||
</DocsContainer> | ||
<MDXProvider components={defaultComponents}> | ||
<DocsContainer | ||
context={{ componentStories: () => [], storyById: () => ({}) }} | ||
theme={themes.dark} | ||
> | ||
{storyFn()} | ||
</DocsContainer> | ||
</MDXProvider> | ||
), | ||
]; |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import type { FunctionComponent, ComponentType } from 'react'; | ||
import type { AnyFramework, Parameters } from '@storybook/csf'; | ||
import type { Theme } from '@storybook/theming'; | ||
|
||
import type { DocsContextProps } from './DocsContext'; | ||
import { DocsContainer } from './DocsContainer'; | ||
import { DocsPage } from './DocsPage'; | ||
|
||
export type DocsProps<TFramework extends AnyFramework = AnyFramework> = { | ||
docsParameter: Parameters; | ||
context: DocsContextProps<TFramework>; | ||
}; | ||
|
||
export const Docs: FunctionComponent<DocsProps> = ({ docsParameter, context }) => { | ||
const Container: ComponentType<{ context: DocsContextProps; theme: Theme }> = | ||
docsParameter.container || DocsContainer; | ||
|
||
const Page = docsParameter.page || DocsPage; | ||
|
||
return ( | ||
<Container context={context} theme={docsParameter.theme}> | ||
<Page /> | ||
</Container> | ||
); | ||
}; |
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 was deleted.
Oops, something went wrong.
Oops, something went wrong.