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

PropTable does not work with styled-component #568

Closed
marcuslindfeldt opened this issue Jan 8, 2019 · 11 comments
Closed

PropTable does not work with styled-component #568

marcuslindfeldt opened this issue Jan 8, 2019 · 11 comments
Labels

Comments

@marcuslindfeldt
Copy link

Bug Report

Describe the bug
PropTable won't render for a styled-component like the following:

// Header.js
import React from 'react';
import PropTypes from 'prop-types';

import styled from 'styled-components';

const Header = styled.header`
  z-index: 100;
  background-color: ${props => props.bg};
  min-height: 3rem;
  width: 100%;
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1rem;
  display: flex;
  align-items: center;
`;

Header.defaultProps = {
  bg: #333,
};

Header.propTypes = {
  children: PropTypes.node.isRequired,
  bg: PropTypes.string,
};

export default Header;
---
name: Header
menu: Components
---

import { PropsTable } from 'docz';
import Header from './Header';

# Header

## Properties

<PropsTable of={Header} />

To Reproduce

  1. Create a styled-component
  2. Import it into an .mdx file
  3. start docz dev server
  4. See error
@0xpatrickdev
Copy link

@marcuslindfeldt Running into the same issue. The current workaround seems to be something like this:

// Header.js
import React from 'react';
import PropTypes from 'prop-types';

import styled from 'styled-components';

const StyledHeader = styled.header`
  z-index: 100;
  background-color: ${props => props.bg};
  min-height: 3rem;
  width: 100%;
  font-size: 0.875rem;
  font-weight: 600;
  line-height: 1rem;
  display: flex;
  align-items: center;
`;

const Header = props => <StyledHeader {...props} />

Header.defaultProps = {
  bg: #333,
};

Header.propTypes = {
  children: PropTypes.node.isRequired,
  bg: PropTypes.string,
};

export default Header;

Although it's less than ideal...

@0xpatrickdev
Copy link

Digging a bit more, it seems to be related to the 'docgen parser':
#347 (comment)

And will be resolved in v0.14:
#240 (comment)

@diondiondion
Copy link

Any news/progress on this issue? It's a bit of a shame we have to add extra wrapper components to make the Props component work with styled-components.

Is there maybe some other way (i.e. annotation via comments) to tell the parser to pick up the propTypes & defaultProps?

@zainxyz
Copy link

zainxyz commented May 31, 2019

I've heard react-docgen v5 (link) is suppose to fix this issue and parse the props out of native styled-components correctly.

But for now, to continue working and moving forward, I've done something similar in our implementation. Yes it adds an extra step, but it works and doesn't break any of our components.

const ButtonStyled = styled.button`
    background-color: ${themeGet('bgColor')};
    color: ${themeGet('textColor')};
    box-shadow: ${themeGet('elevation')};
`;

const Button = React.forwardRef((props, ref) => <ButtonStyled {...props} ref={ref} />);

Button.displayName = 'Button';

We use React.forwardRef due to some refs that we need access to. So we just wrap around our styled variants with a forwardRef comp.

We tried to move this logic to an HOC but HOC breaks with the Props table.

Hopefully the anticipated react-docgen@5 fixes most of these issues. 👍

@josebrito
Copy link

josebrito commented Feb 24, 2020

Can this issue be reopened? This is still not supported, and I guess that docz still depends on react-docgen@4.1.1.

=> Found "react-docgen@4.1.1"
info Reasons this module exists
   - "docz#docz-core" depends on it
   - Hoisted from "docz#docz-core#react-docgen"
   - Hoisted from "docz#docz-core#react-docgen-actual-name-handler#react-docgen"

Any chance of upgrading react-docgen to v5?

@rakannimer rakannimer reopened this Feb 24, 2020
@sciotta
Copy link

sciotta commented Mar 27, 2020

+1

@stale
Copy link

stale bot commented May 26, 2020

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 26, 2020
@stale stale bot closed this as completed Jun 2, 2020
@josebrito
Copy link

josebrito commented Jun 3, 2020

No news on this? What's preventing docz from upgrading react-docgen to v5?

@Raphalex46
Copy link

Why is this closed ? I think a lot of React projects use styled components, and using a workaround like the one that was suggested is not an option for everybody. This seems like quite a simple fix, and it would be great to get rid of this little bug. The problem with the workaround is also that it makes little sense to change your real code for the sake of documentation, it's weird.

The stale bot is also a real problem here. It's not because the issue is stale that the issue is invalid...

@sdb1228
Copy link

sdb1228 commented Jul 22, 2020

This is still an issue for us, but the workaround above solved this issue.

@prxg22
Copy link

prxg22 commented May 11, 2021

This is still an issue for us. We are developing a design-system and need to export the styled-components. We need to be able to extended the component's styles in other styled-components

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants