-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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 #25086 from storybookjs/shilman/enable-embedded-st…
…orybook Core: Maintenance changes for NextJS embedding
- Loading branch information
Showing
13 changed files
with
119 additions
and
88 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
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,2 +1,3 @@ | ||
export const parameters: {} = { renderer: 'react' }; | ||
export { render, renderToCanvas } from './render'; | ||
export { render } from './render'; | ||
export { renderToCanvas } from './renderToCanvas'; |
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,80 @@ | ||
import { global } from '@storybook/global'; | ||
import type { FC } from 'react'; | ||
import React, { Component as ReactComponent, StrictMode, Fragment } from 'react'; | ||
import { renderElement, unmountElement } from '@storybook/react-dom-shim'; | ||
|
||
import type { RenderContext } from '@storybook/types'; | ||
|
||
import type { ReactRenderer, StoryContext } from './types'; | ||
|
||
const { FRAMEWORK_OPTIONS } = global; | ||
|
||
class ErrorBoundary extends ReactComponent<{ | ||
showException: (err: Error) => void; | ||
showMain: () => void; | ||
children?: React.ReactNode; | ||
}> { | ||
state = { hasError: false }; | ||
|
||
static getDerivedStateFromError() { | ||
return { hasError: true }; | ||
} | ||
|
||
componentDidMount() { | ||
const { hasError } = this.state; | ||
const { showMain } = this.props; | ||
if (!hasError) { | ||
showMain(); | ||
} | ||
} | ||
|
||
componentDidCatch(err: Error) { | ||
const { showException } = this.props; | ||
// message partially duplicates stack, strip it | ||
showException(err); | ||
} | ||
|
||
render() { | ||
const { hasError } = this.state; | ||
const { children } = this.props; | ||
|
||
return hasError ? null : children; | ||
} | ||
} | ||
|
||
const Wrapper = FRAMEWORK_OPTIONS?.strictMode ? StrictMode : Fragment; | ||
|
||
export async function renderToCanvas( | ||
{ | ||
storyContext, | ||
unboundStoryFn, | ||
showMain, | ||
showException, | ||
forceRemount, | ||
}: RenderContext<ReactRenderer>, | ||
canvasElement: ReactRenderer['canvasElement'] | ||
) { | ||
const Story = unboundStoryFn as FC<StoryContext<ReactRenderer>>; | ||
|
||
const content = ( | ||
<ErrorBoundary showMain={showMain} showException={showException}> | ||
<Story {...storyContext} /> | ||
</ErrorBoundary> | ||
); | ||
|
||
// For React 15, StrictMode & Fragment doesn't exists. | ||
const element = Wrapper ? <Wrapper>{content}</Wrapper> : content; | ||
|
||
// In most cases, we need to unmount the existing set of components in the DOM node. | ||
// Otherwise, React may not recreate instances for every story run. | ||
// This could leads to issues like below: | ||
// https://github.com/storybookjs/react-storybook/issues/81 | ||
// (This is not the case when we change args or globals to the story however) | ||
if (forceRemount) { | ||
unmountElement(canvasElement); | ||
} | ||
|
||
await renderElement(element, canvasElement); | ||
|
||
return () => unmountElement(canvasElement); | ||
} |