-
-
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 #18099 from storybookjs/future/docs2/preview
Docs2: Handle new docs entries in the preview
- Loading branch information
Showing
30 changed files
with
862 additions
and
484 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,58 @@ | ||
import React, { ComponentType } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { AnyFramework, Parameters } from '@storybook/csf'; | ||
import { DocsRenderFunction } from '@storybook/preview-web'; | ||
|
||
import { DocsContainer } from './DocsContainer'; | ||
import { DocsPage } from './DocsPage'; | ||
import { DocsContext, DocsContextProps } from './DocsContext'; | ||
|
||
export class DocsRenderer<TFramework extends AnyFramework> { | ||
public render: DocsRenderFunction<TFramework>; | ||
|
||
public unmount: (element: HTMLElement) => void; | ||
|
||
constructor() { | ||
this.render = ( | ||
docsContext: DocsContextProps<TFramework>, | ||
docsParameters: Parameters, | ||
element: HTMLElement, | ||
callback: () => void | ||
): void => { | ||
renderDocsAsync(docsContext, docsParameters, element).then(callback); | ||
}; | ||
|
||
this.unmount = (element: HTMLElement) => { | ||
ReactDOM.unmountComponentAtNode(element); | ||
}; | ||
} | ||
} | ||
|
||
async function renderDocsAsync<TFramework extends AnyFramework>( | ||
docsContext: DocsContextProps<TFramework>, | ||
docsParameters: Parameters, | ||
element: HTMLElement | ||
) { | ||
// FIXME -- use DocsContainer, make it work for modern | ||
const SimpleContainer = ({ children }: any) => ( | ||
<DocsContext.Provider value={docsContext}>{children} </DocsContext.Provider> | ||
); | ||
|
||
const Container: ComponentType<{ context: DocsContextProps<TFramework> }> = | ||
docsParameters.container || | ||
(await docsParameters.getContainer?.()) || | ||
(docsContext.legacy ? DocsContainer : SimpleContainer); | ||
|
||
const Page: ComponentType = docsParameters.page || (await docsParameters.getPage?.()) || DocsPage; | ||
|
||
// Use `title` as a key so that we force a re-render every time we switch components | ||
const docsElement = ( | ||
<Container key={docsContext.title} context={docsContext}> | ||
<Page /> | ||
</Container> | ||
); | ||
|
||
await new Promise<void>((resolve) => { | ||
ReactDOM.render(docsElement, element, resolve); | ||
}); | ||
} |
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,6 +1,8 @@ | ||
export const parameters = { | ||
docs: { | ||
getContainer: async () => (await import('./blocks')).DocsContainer, | ||
getPage: async () => (await import('./blocks')).DocsPage, | ||
renderer: async () => { | ||
const { DocsRenderer } = await import('./blocks/DocsRenderer'); | ||
return new 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
import meta from '../button.stories'; | ||
import { Meta, Story } from '@storybook/addon-docs'; | ||
import meta, { Basic } from '../button.stories'; | ||
|
||
<Meta of={meta} /> | ||
|
||
# Docs with of | ||
|
||
hello docs | ||
|
||
<Story of={Basic} /> |
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
Oops, something went wrong.