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

Allows users to create a custom storybook preview config #2048

Closed
wants to merge 1 commit into from

Conversation

corbt
Copy link
Contributor

@corbt corbt commented Mar 20, 2021

This allows a user to create a custom Storybook preview file in web/config/storybook.preview.js and have it imported into the framework's version.

I haven't added any documentation yet for two reasons. First, I want some confirmation that this approach is reasonable. But also, this actually took me a lot longer than it should have because I didn't realize that we're still on Storybook 5.3 and had to use the old addDecorator syntax. I suspect this will trip up other users as well, so perhaps it would be worthwhile to update to the latest Storybook before publicizing this feature? Anyone who tries to reference the Storybook docs for the syntax of this file will likely also be confused.

Sample web/config/storybook.preview.js:

import React from 'react'
import { addDecorator } from '@storybook/react'

addDecorator((Story) => {
  return (
    <div style={{ backgroundColor: 'red' }}>
      <Story />
    </div>
  )
})

And displayed component:
Screen Shot 2021-03-20 at 8 06 03 AM

cc @peterp who opened #1595 and @Tobbe who pointed me to it from the Discord!

This allows a user to create a custom Storybook preview file in `web/config/storybook.preview.js` and have it imported into the framework's version.

I haven't added any documentation yet for two reasons. First, I want some confirmation that this approach is reasonable. But also, this actually took me a lot longer than it should have because I didn't realize that we're still on Storybook 5.3 and had to use the old `addDecorator` syntax. I suspect this will trip up other users as well, so perhaps it would be worthwhile to update to the latest Storybook before publicizing this feature? Anyone who tries to reference the Storybook docs for the content of this file will likely also be confused.
@@ -29,6 +29,10 @@ const baseConfig = {
}
}

if (fs.existsSync(getPaths().web.storybookPreviewConfig)) {
sbConfig.resolve.alias['~__REDWOOD__USER_STORYBOOK_PREVIEW_CONFIG'] = getPaths().web.storybookPreviewConfig
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since getPaths() needs to run in a node context, we can't run it from preview.js directly. Instead, I elected to run it on this side and expose the path to the user's preview file as a Webpack alias.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's 100% the way. I meant to document that in the comments, I hope it wasn't too painful to figure out.

@@ -14,3 +14,11 @@ require('~__REDWOOD__USER_WEB_DEFAULT_CSS')
addDecorator((storyFn, { id }) => {
return React.createElement(StorybookProvider, { storyFn, id })
})

try {
require('~__REDWOOD__USER_STORYBOOK_PREVIEW_CONFIG')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if there's a more idiomatic way to implement "require this path if it exists".

@corbt corbt changed the title Allows users to create a custom storybook config Allows users to create a custom storybook preview config Mar 20, 2021
@github-actions
Copy link

📦 PR Packages

Click to Show Package Download Links

https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/create-redwood-app-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-api-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-api-server-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-auth-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-cli-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-core-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-dev-server-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-eslint-config-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-eslint-plugin-redwood-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-forms-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-internal-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-prerender-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-router-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-structure-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-testing-0.27.1-c1d41e7.tgz
https://rw-pr-redwoodjs-com.s3.amazonaws.com/2048/redwoodjs-web-0.27.1-c1d41e7.tgz

Install this PR by running yarn rw upgrade --pr 2048:0.27.1-c1d41e7

@thedavidprice thedavidprice requested a review from peterp March 20, 2021 15:37
@thedavidprice
Copy link
Contributor

Thanks for getting this rolling @corbt!

I suspect this will trip up other users as well, so perhaps it would be worthwhile to update to the latest Storybook before publicizing this feature?

^^ Yes, we have this on our radar but have yet to start implementation. Might you be available/interested in helping to upgrade?

Looping in @peterp who has lead all the Storybook work.

@corbt
Copy link
Contributor Author

corbt commented Mar 20, 2021

Might you be available/interested in helping to upgrade?

I can probably spend a few hours looking into it next week, yes!

@corbt corbt mentioned this pull request Mar 21, 2021
@corbt
Copy link
Contributor Author

corbt commented Mar 21, 2021

Closing in favor of #2054, which includes this commit and updates it to work with Storybook 6.1.

@corbt corbt closed this Mar 21, 2021
@corbt corbt deleted the custom-storybook-preview branch March 21, 2021 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants