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

styled-components and Glamorous support #256

Open
penx opened this issue Feb 23, 2018 · 6 comments
Open

styled-components and Glamorous support #256

penx opened this issue Feb 23, 2018 · 6 comments

Comments

@penx
Copy link

penx commented Feb 23, 2018

Further to this discussion styleguidist/react-styleguidist#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 react-docgen@3.0.0-beta11 but still get the issue with the above 2 examples.

@penx
Copy link
Author

penx commented Feb 23, 2018

Related:

#70
#177
#187
#195
f847bd7

@chpeters
Copy link

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

penx added a commit to govuk-react/govuk-react that referenced this issue Feb 27, 2018
Generate `API.md` by calling `npm run docs`.

This uses react-docgen.

Due to the default resolver of react-docgen not picking up Glamorous components, I've had to wrap some Glamorous only components with a JSX wrapper. I'd rather not have to do this but not sure of any alternative solution.

reactjs/react-docgen#256

I'm also not sure the format of the current API.md is useful but opening PR for consideration.
@ckihneman
Copy link

ckihneman commented Jul 11, 2018

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"
}

@jquense
Copy link
Collaborator

jquense commented Aug 30, 2018

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

@floppey
Copy link

floppey commented May 15, 2020

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.

@jquense
Copy link
Collaborator

jquense commented May 15, 2020

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

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

No branches or pull requests

5 participants