react-docgen icon indicating copy to clipboard operation
react-docgen copied to clipboard

styled-components and Glamorous support

Open penx opened this issue 7 years ago • 6 comments

Further to this discussion https://github.com/styleguidist/react-styleguidist/issues/37

If you are using styled-components, you may have a component similar to:

import styled, { css } from "styled-components";
import PropTypes from "prop-types";

const Button = styled.a`
  background: transparent;
  color: white;

  ${({primary}) =>
    !!primary &&
    css`
      background: white;
      color: palevioletred;
    `};
`;

Button.propTypes = {
  primary: PropTypes.bool
};

export default Button;

or, with Glamorous:

import glamorous from "glamorous";
import PropTypes from "prop-types";

const Button = glamorous.button({
  background: "transparent",
  color: "white"
},
  ({ primary }) => (!!primary && {
    background: "white",
    color: "palevioletred"
  })
);

Button.propTypes = {
  primary: PropTypes.bool
};

This doesn't import React, so afaik is why the react-docgen resolver does not pick it up as a valid component.

No suitable component definition found.

Is there an existing way to get this to work e.g. with a custom resolver? Or is a bugfix/new feature needed?

#195 looks like it resolves this, and is marked as closed as the feature in v3. I've installed [email protected] but still get the issue with the above 2 examples.

penx avatar Feb 23 '18 17:02 penx

Related:

https://github.com/reactjs/react-docgen/issues/70 https://github.com/reactjs/react-docgen/issues/177 https://github.com/reactjs/react-docgen/issues/187 https://github.com/reactjs/react-docgen/pull/195 https://github.com/reactjs/react-docgen/commit/f847bd7f922f392a89aa331e46a7ada0a76cd132

penx avatar Feb 23 '18 18:02 penx

I am seeing this issue as well using styled-components (through styled-system's system function).

chpeters avatar Feb 26 '18 03:02 chpeters

Ran into this issue with the Storybook Info Addon. Tracked it down to this function in their code, which made me realize it is an issue with react-docgen parsing styled-components, this information brought me here - noting for others.

This is what react-docgen is able to get from the styled-component with a docgen style description on the prop:

{
  "property": "children",
  "propType": "node",
  "required": true,
  "description": null
}

When other react components look like:

{
  "property": "children",
  "propType": {
    "name": "node"
  },
  "required": true,
  "description": "Heading text or node"
}

ckihneman avatar Jul 11 '18 01:07 ckihneman

if anyone is interested this is what we came up with for css-literal-loader, but the syntax is the same for styled components or emotion, so this should work for them as well:

https://gist.github.com/jquense/c2a92f1c909552f295bb7953fcd2ce4d

jquense avatar Aug 30 '18 15:08 jquense

I have a solution/workaround for this issue: Normally a component exported as a styled component will look something like this

const foo = (props) => (...);
foo.propTypes = { ... };
export default styled(foo)``;

By changing this up a little and adding prop types to both the react-component and the styled-component wrapper it's now possible to read prop-types from the exported component and it still keeps eslint "react/prop-types" happy :)

const fooComponent = (props) => (...);
const foo = styled(fooComponent)``;
const propTypes = { ... };
foo.propTypes = propTypes;
fooComponent.propTypes = propTypes;
export default foo;

It adds a little bit more code, and it's not suuuuper sexy, but it gets the job done.

floppey avatar May 15 '20 10:05 floppey

I wonder if we should close this im using (and released) https://www.npmjs.com/package/react-docgen-styled-resolver which does need a README but works great

jquense avatar May 15 '20 12:05 jquense