-
-
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.
Move
DocsRenderer
back to addon-docs
- Loading branch information
Showing
14 changed files
with
128 additions
and
85 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,33 @@ | ||
import React, { ComponentType } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { AnyFramework, Parameters } from '@storybook/csf'; | ||
import { DocsContextProps, DocsRenderFunction } from '@storybook/preview-web'; | ||
import { Docs, DocsPage } from '@storybook/blocks'; | ||
|
||
export class DocsRenderer<TFramework extends AnyFramework> { | ||
public render: DocsRenderFunction<TFramework>; | ||
|
||
public unmount: (element: HTMLElement) => void; | ||
|
||
constructor() { | ||
this.render = ( | ||
context: DocsContextProps<TFramework>, | ||
parameters: 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( | ||
<Docs key={Math.random()} context={context} parameters={parameters} />, | ||
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
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, ExternalDocsContainer } 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> | ||
); | ||
} |
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,23 @@ | ||
import React, { FunctionComponent, ComponentType } from 'react'; | ||
import { AnyFramework, Parameters } from '@storybook/csf'; | ||
import { DocsContextProps } from './DocsContext'; | ||
import { DocsContainer } from './DocsContainer'; | ||
import { DocsPage } from './DocsPage'; | ||
|
||
export type DocsProps<TFramework extends AnyFramework = AnyFramework> = { | ||
parameters: Parameters; | ||
context: DocsContextProps<TFramework>; | ||
}; | ||
|
||
export const Docs: FunctionComponent<DocsProps> = ({ parameters, context }) => { | ||
const Container: ComponentType<{ context: DocsContextProps }> = | ||
parameters.container || DocsContainer; | ||
|
||
const Page = parameters.page || DocsPage; | ||
|
||
return ( | ||
<Container context={context}> | ||
<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.
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,32 @@ | ||
import React, { FunctionComponent, ComponentType, useRef } from 'react'; | ||
import { AnyFramework, ProjectAnnotations } from '@storybook/csf'; | ||
import { composeConfigs } from '@storybook/store'; | ||
|
||
import { Docs } from '../Docs'; | ||
import { ExternalPreview } from './ExternalPreview'; | ||
|
||
export type ExternalDocsProps<TFramework extends AnyFramework = AnyFramework> = { | ||
projectAnnotationsList: ProjectAnnotations<TFramework>[]; | ||
}; | ||
|
||
function usePreview<TFramework extends AnyFramework = AnyFramework>( | ||
projectAnnotations: ProjectAnnotations<TFramework> | ||
) { | ||
const previewRef = useRef<ExternalPreview>(); | ||
if (!previewRef.current) previewRef.current = new ExternalPreview(projectAnnotations); | ||
return previewRef.current; | ||
} | ||
|
||
export const ExternalDocs: FunctionComponent<ExternalDocsProps> = ({ | ||
projectAnnotationsList, | ||
children, | ||
}) => { | ||
const projectAnnotations = composeConfigs(projectAnnotationsList); | ||
const preview = usePreview(projectAnnotations); | ||
const parameters = { | ||
...projectAnnotations.parameters?.docs, | ||
page: () => children, | ||
}; | ||
|
||
return <Docs parameters={parameters} context={preview.docsContext()} />; | ||
}; |
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