-
-
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 #16412 from storybookjs/lazy-load-docs
Addon-docs: Lazy load docs to reduce bundle size
- Loading branch information
Showing
8 changed files
with
62 additions
and
33 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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React, { ComponentType } from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { AnyFramework } from '@storybook/csf'; | ||
import { Story } from '@storybook/store'; | ||
|
||
import { DocsContextProps } from './types'; | ||
import { NoDocs } from './NoDocs'; | ||
|
||
export function renderDocs<TFramework extends AnyFramework>( | ||
story: Story<TFramework>, | ||
docsContext: DocsContextProps<TFramework>, | ||
element: HTMLElement, | ||
callback: () => void | ||
) { | ||
return renderDocsAsync(story, docsContext, element).then(callback); | ||
} | ||
|
||
async function renderDocsAsync<TFramework extends AnyFramework>( | ||
story: Story<TFramework>, | ||
docsContext: DocsContextProps<TFramework>, | ||
element: HTMLElement | ||
) { | ||
const { docs } = story.parameters; | ||
if ((docs?.getPage || docs?.page) && !(docs?.getContainer || docs?.container)) { | ||
throw new Error('No `docs.container` set, did you run `addon-docs/preset`?'); | ||
} | ||
|
||
const DocsContainer: ComponentType<{ context: DocsContextProps<TFramework> }> = | ||
docs.container || | ||
(await docs.getContainer?.()) || | ||
(({ children }: { children: Element }) => <>{children}</>); | ||
|
||
const Page: ComponentType = docs.page || (await docs.getPage?.()) || NoDocs; | ||
|
||
// Use `componentId` as a key so that we force a re-render every time | ||
// we switch components | ||
const docsElement = ( | ||
<DocsContainer key={story.componentId} context={docsContext}> | ||
<Page /> | ||
</DocsContainer> | ||
); | ||
|
||
await new Promise<void>((resolve) => { | ||
ReactDOM.render(docsElement, element, resolve); | ||
}); | ||
} | ||
|
||
export function unmountDocs(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