Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(code)!: exclude global decorators by default #53

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ export default {

The addon can be configured via the `playroom` [parameter](https://storybook.js.org/docs/react/writing-stories/parameters). The following options are available:

| Option | Type | Description | Default |
| :------------------------------- | :-------- | :--------------------------------------- | :---------------------- |
| `url` | `string` | the Playroom URL | `http://localhost:9000` |
| `code` | `string` | code to be used instead of story source | |
| `disable` | `boolean` | whether to disable the addon | `false` |
| `reactElementToJSXStringOptions` | `object` | [react-element-to-jsx-string options][1] | `{ sortProps: false }` |
| Option | Type | Description | Default |
| :------------------------------- | :-------- | :--------------------------------------------------- | :---------------------- |
| `url` | `string` | the Playroom URL | `http://localhost:9000` |
| `code` | `string` | code to be used instead of story source | |
| `disable` | `boolean` | whether to disable the addon | `false` |
| `includeDecorators` | `boolean` | whether to include global decorators in stories code | `false` |
| `reactElementToJSXStringOptions` | `object` | [react-element-to-jsx-string options][1] | `{ sortProps: false }` |

To configure for all stories, set the `playroom` parameter in [`.storybook/preview.js`](https://storybook.js.org/docs/react/configure/overview#configure-story-rendering):

Expand Down
3 changes: 3 additions & 0 deletions src/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ type Options = {
url?: string
code?: string
disable?: boolean
includeDecorators?: boolean
reactElementToJSXStringOptions?: ReactElementToJSXStringOptions
}

export const getOptions = ({
url = 'http://localhost:9000',
code = '',
disable = false,
includeDecorators = false,
reactElementToJSXStringOptions = { sortProps: false },
}: Options = {}): Required<Options> => ({
url,
code,
disable,
includeDecorators,
reactElementToJSXStringOptions,
})
10 changes: 7 additions & 3 deletions src/withGlobals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ export const withGlobals = (
StoryFn: StoryFunction<Renderer>,
context: StoryContext<Renderer>,
) => {
const { parameters } = context
const { parameters, undecoratedStoryFn } = context
const playroomConfig = parameters[PARAM_KEY]
const { url, code, reactElementToJSXStringOptions } =
const { url, code, includeDecorators, reactElementToJSXStringOptions } =
getOptions(playroomConfig)
const story = StoryFn() as ReactElement
const storyCode = includeDecorators
? story
: (undecoratedStoryFn(context) as ReactElement)

const jsxString =
code || reactElementToJSXString(story, reactElementToJSXStringOptions)
code || reactElementToJSXString(storyCode, reactElementToJSXStringOptions)
const codeUrl = url && createUrl({ baseUrl: url, code: jsxString })

const emit = useChannel({})
Expand Down