storybook: addon-docs Error: Cannot read property 'theme' of undefined
Describe the bug
When documenting a component styled with styled-components
I get:
Cannot read property ‘theme’ of undefined
To Reproduce Steps to reproduce the behavior:
Create styled button:
import styled from "styled-components";
/**
* Props
*/
interface Props {
/**
* Indicates if the button is disabled
*/
disabled?: boolean;
/**
* Type of button
*/
type: "submit" | "button" | "reset";
}
/**
* Button component
* @param props - Component props
* @returns a button component
*/
export const Button = styled.button.attrs<Props>((props) => ({
type: props.type
}))`
border-radius: 3px;
border: 1px solid #d2d2d2;
display: block;
margin: 0 0 1em;
background-color: ${(props) => (props.disabled ? "#ccc" : "transparent")};
cursor: ${(props) => (props.disabled ? "not-allowed" : "auto")};
`;
Create a story:
import React from "react";
import { Button } from "src/components";
// Default export required for Storybook
// eslint-disable-next-line import/no-default-export
export default {
title: "Form controls",
};
/**
* Button story
* @returns Button story
*/
export const ButtonStory = () => <Button>Submit</Button>;
In the MDX, enter the following:
import { Meta, Story, Preview, Props } from "@storybook/addon-docs/blocks";
import { Button } from "src/components";
# Preview
<Preview>
<Story name="Button Story">
<Button>hello</Button>
</Story>
</Preview>
# Props
<Props of={Button} />
Where I expect to see the Props description, I see “Cannot read property ‘theme’ of undefined”
Expected behavior I expected to see Props displayed
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.7.0 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.13.4 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: 44.18362.449.0
Additional context Package.json:
"dependencies": {
"final-form": "^4.18.7",
"moment": "^2.24.0",
"react": "^16.13.0",
"react-dom": "^16.13.0",
"react-final-form": "^6.3.5",
"react-router": "^5.1.2",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.0",
"shortid": "^2.2.15",
"styled-components": "^5.0.1"
},
"devDependencies": {
"@storybook/addon-actions": "^5.3.17",
"@storybook/addon-docs": "^5.3.17",
"@storybook/addon-links": "^5.3.17",
"@storybook/addons": "^5.3.17",
"@storybook/preset-create-react-app": "^2.1.0",
"@storybook/react": "^5.3.17",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"@types/jest": "^24.0.0",
"@types/node": "^12.0.0",
"@types/react": "^16.9.0",
"@types/react-dom": "^16.9.0",
"@types/react-router": "^5.1.4",
"@types/react-router-dom": "^5.1.3",
"@types/shortid": "0.0.29",
"@types/styled-components": "^5.0.1",
"@typescript-eslint/eslint-plugin": "^2.24.0",
"eslint-config-airbnb-typescript": "^7.2.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-jsdoc": "^22.1.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-react": "^7.19.0",
"eslint-plugin-react-hooks": "^2.5.0",
"prettier": "1.19.1",
"typescript": "~3.7.2"
}
Please let me know if a copy of the project would be helpful. Thanks for your efforts!
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 15 (4 by maintainers)
We have the same problem here. No props for styled components. And all our components are styled 😅
Updating to the latest RC also worked for me. One additional thing to keep in mind is that the official addons will bump along with storybook, and some of their integration/documentation has changed slightly.
Mostly that means registering them in your
.storybook/main.js
with@storybook/addon-a11y
instead of@storybook/addon-a11y/register
, but a couple of them also have minor changes to their usage - for example the@storybook/addon-backgrounds
addon changed the shape of thebackgrounds
configuration key in the story parameters slightly.If you see any weird addon behavior or anything isn’t working after upgrading, you can reference the latest 6.0 docs on this branch https://github.com/storybookjs/storybook/tree/6.0-docs/addons
I ran the following in @chrisahardie 's repo and it worked after the upgrade. YMMV:
I also had to
yarn add babel-loader --dev
and update themain.js
stories glob from../src/**/*.stories.(ts|tsx|js|jsx|mdx)
to../src/**/*.stories.@(ts|tsx|js|jsx|mdx)
This @chrisahardie! Using your repro, I found that the bug is due to this line, which is a (dodgy) heuristic for dealing with props for React forwardRefs. I’m not quite sure what the fix is, but if anybody wants to experiment and figure it out, it would be much appreciated:
https://github.com/storybookjs/storybook/blob/master/addons/docs/src/frameworks/react/extractProps.ts#L29
+1 from me on this issue - i’m seeing exactly the same thing