-
Notifications
You must be signed in to change notification settings - Fork 321
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(withLiveEdit): apply decorators from within self CSF module of a…
… story (#2077)
- Loading branch information
1 parent
fb54b9a
commit 6b1e520
Showing
5 changed files
with
52 additions
and
18 deletions.
There are no files selected for viewing
File renamed without changes.
21 changes: 21 additions & 0 deletions
21
packages/core/src/storybook/decorators/withLiveEdit/LiveContent/LiveContent.tsx
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,21 @@ | ||
import React from "react"; | ||
import { LiveProvider } from "react-live"; | ||
import LivePreview from "../../../components/live-preview/LivePreview"; | ||
import useApplyDecorators from "../hooks/useApplyDecorators"; | ||
import { LiveContentProps } from "./LiveContent.types"; | ||
import styles from "./LiveContent.module.scss"; | ||
|
||
const LiveContent = ({ code, scope, decorators, context }: LiveContentProps) => { | ||
const content: React.JSX.Element = ( | ||
<> | ||
<div className={styles.modifiedVersionIndicator}>Modified Version</div> | ||
<LiveProvider code={code} scope={scope} enableTypeScript> | ||
<LivePreview /> | ||
</LiveProvider> | ||
</> | ||
); | ||
|
||
return <>{useApplyDecorators(decorators || [], content, context)}</>; | ||
}; | ||
|
||
export default LiveContent; |
8 changes: 8 additions & 0 deletions
8
packages/core/src/storybook/decorators/withLiveEdit/LiveContent/LiveContent.types.ts
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,8 @@ | ||
import { Decorator, StoryContext } from "@storybook/react"; | ||
|
||
export interface LiveContentProps { | ||
code: string; | ||
scope: Record<string, unknown>; | ||
decorators: Decorator[]; | ||
context: StoryContext; | ||
} |
18 changes: 18 additions & 0 deletions
18
packages/core/src/storybook/decorators/withLiveEdit/hooks/useApplyDecorators.ts
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,18 @@ | ||
import React, { useMemo } from "react"; | ||
import { StoryContext, Decorator } from "@storybook/react"; | ||
|
||
const useApplyDecorators = (decorators: Decorator[], component: React.ReactElement, context: StoryContext) => { | ||
return useMemo(() => { | ||
let decoratedComponent = () => component; | ||
|
||
// recursively apply decorators to the component | ||
decorators.forEach(decorator => { | ||
const currentComponent = decoratedComponent; | ||
decoratedComponent = () => decorator(currentComponent, context); | ||
}); | ||
|
||
return decoratedComponent(); | ||
}, [decorators, component, context]); | ||
}; | ||
|
||
export default useApplyDecorators; |
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