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

Styles are lost after composing styled component and then using as prop #1644

Open
ajayjindal2 opened this issue Mar 11, 2024 · 1 comment
Open

Comments

@ajayjindal2
Copy link

Describe the bug

If i have styled component from @compiled/react like H3, and then reuse it to be styled again into Heading and then use as prop to render it as h1.

const H3 = styled.h3({
  fontSize: '1.5rem',
  lineHeight: '30px',
  fontWeight: '600',
});

const Heading = styled(H3)({
  marginBlock: '0 5px',
});

const SearchHeading = () => (
  <Heading as="h1">
         Search results
  </Heading>
);

i would only get styles of marginBlock: '0 5px', in UI rendering
the styles from H3 are lost
image

Expected behavior

styles from H3 are should also be applied.

Workaround

Using as props before 2nd styled on the component as suggested by @liamqma +++++

const H3 = styled.h3({
  fontSize: '1.5rem',
  lineHeight: '30px',
  fontWeight: '600',
});

const H1 = (props) => <H3 as="h1" {...props} />;

const Heading = styled(H1)({
  marginBlock: '0 5px',
});

const SearchHeading = () => (
  <Heading>
         Search results
  </Heading>
);
image
  • OS: MacOS
  • Browser: chrome (happens in all browsers)
@liamqma
Copy link
Collaborator

liamqma commented Mar 11, 2024

To add more context:

const Heading = styled(H3)({
  marginBlock: '0 5px',
});

gets compiled to

const Heading = forwardRef(
  ({ as: C = H3, style: __cmpls, ...__cmplp }, __cmplr) => {
    return (
      <CC>
        <CS>{[_]}</CS>
        <C
          {...__cmplp}
          style={__cmpls}
          ref={__cmplr}
          className={ax(["_1mou1s5a", __cmplp.className])}
        />
      </CC>
    );
  }
);

As you can see, as overwrites H3. I would recommend using css prop instead of styled. https://compiledcssinjs.com/docs/api-css

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

2 participants