storybook: addon-docs not showing all props for styled-component
Describe the bug
Please note this is for the pre-release version as styled-components were not able to be documented until this issue was resolved.
When documenting a styled-component, not all props are rendered out into the props table.
To Reproduce Steps to reproduce the behavior:
- Clone the repro repo
npx sb@next upgrade --prerelease
yarn add babel-loader --dev
- update the main.js stories glob from
../src/**/*.stories.(ts|tsx|js|jsx|mdx)
to../src/**/*.stories.@(ts|tsx|js|jsx|mdx)
- View the button story
- The props table is missing the
disabled
andtype
props
Expected behavior
The props table should include all props.
Screenshots
Code snippets
// Button.tsx
interface Props {
/**
* Indicates if the button is disabled
*/
disabled?: boolean;
/**
* Type of button
*/
type: "submit" | "button" | "reset";
}
/**
* Button component
* @returns a button component
*/
export const Button = styled.button.attrs<Props>((props) => ({
type: props.type
}))`
...
`
System:
Environment Info:
System:
OS: Windows 10 10.0.18363
CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
Binaries:
Node: 12.16.1 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - C:\Program Files\nodejs\yarn.CMD
npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 84.0.4147.105
Edge: Spartan (44.18362.449.0)
npmPackages:
@storybook/addon-actions: ^6.0.2 => 6.0.2
@storybook/addon-docs: ^6.0.2 => 6.0.2
@storybook/addon-links: ^6.0.2 => 6.0.2
@storybook/addons: ^6.0.2 => 6.0.2
@storybook/preset-create-react-app: ^3.1.4 => 3.1.4
@storybook/react: ^6.0.2 => 6.0.2
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 60
- Comments: 26 (3 by maintainers)
Ping to maintain the issue
I was actually able to fix this issue simply by renaming my component file names to use
.tsx
instead of.ts
. I was using the.ts
extension as there was no JSX used in the file (only using Styled Components).After this fix, all my props showed up with the component description populated with the JSDoc as well. However, the
theme
,as
andforwardedAs
props from Styled Components were shown as well. To remove them, I added this configuration in my.storybook/main.js
file:I hope this helps someone!
@ebellumat This is the workaround I used without touching the original component:
I think this is the same issue I ran into with low-level styled components:
Example/index.js:
Example/index.stories.js:
If I wrap the component in a dummy wrapper…
…then the documentation and the auto-generated controls work:
But of course I don’t want to do that, because it adds another level in the React component browser every time I use the component. Dumping the component to the browser console could hint at the root cause: it is an object and not a function that returns react components:
This is still an issue in “@storybook/react”: “^6.4.17”
@sdalonzo an upvote on the original issue is the best way to help get this fixed (aside from contributing a fix)
I have the same issue with storybook 7.0.6.
My workaround is the following:
The downside are:
@YuCJ you could do a workaround like:
Then in your stories file:
Still not working =(
I tried a lot of work arounds and nothing.
I don’t want to write these props on docs manually.
I’m using MDX with Styled components here.
I have tried all of the above, work arounds an none of them work, I have styled components and also am pulling types from a third party lib and my props come through but the third party ones dont? any reason for this?
What worked for me, was removing
Story<Props>
, so I replaced this:With this:
I now get the full props table. And
MyComponent
is a pure styled-component, no need for React wrappers.👋 Hello there! I’ve written a blog post that references this issue! You can view the applicable excerpt here: Styled Components: If using JS and PropTypes, ArgsTable won’t pull props
GitHub discussion: https://github.com/storybookjs/storybook/discussions/18915
✌️