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 component not applying styles #3149

Open
gayathrirajendran opened this issue Jan 12, 2024 · 3 comments
Open

Styled component not applying styles #3149

gayathrirajendran opened this issue Jan 12, 2024 · 3 comments

Comments

@gayathrirajendran
Copy link

Current behavior:
Have used styled to style the contents of a component. But it is not working.
In the code, have created a small component and styled it to be color red. But its not applying.
Codesandbox: https://codesandbox.io/p/sandbox/styled-component-demo-gtvf4l

Expected behavior:
Red color is applied to the component

Environment information:
"@emotion/react": "^11.11.3",
"@emotion/styled": "^11.11.0",

@Thinkscape
Copy link

This might be caused by the patch version mismatch after bump on /react while the /styled package has a direct dep pointing to a different version.

Example of a resolution by bun 1.0.22:

❯ npm ls @emotion/react
project@0.1.0 /Users/thinkscape/project
├── @emotion/react@11.11.3
├─┬ @emotion/styled@11.11.0
│ └── @emotion/react@11.11.1
├─┬ @mui/lab@5.0.0-alpha.160
│ ├── @emotion/react@11.11.3 deduped
│ └─┬ @mui/system@5.15.4
│   ├── @emotion/react@11.11.3 deduped
│   └─┬ @mui/styled-engine@5.15.4
│     └── @emotion/react@11.11.3 deduped
└─┬ @mui/material@5.15.4
  └── @emotion/react@11.11.3 deduped

Unsure why this happens, didn't have time to investigate.

overrides and resolutions and version pinning to 11.11.1 helped in my case, i.e.

  "overrides": {
    "@emotion/react": "11.11.1"
  },
  "dependencies": {
    "@emotion/react": "11.11.1",
    "@emotion/styled": "^11.11.0"
  }

@therealgilles
Copy link

therealgilles commented Apr 7, 2024

See: https://emotion.sh/docs/styled#styling-any-component

Your component needs to have a className prop and pass it down. This will fix it:

const errorMsg = ({ children, className }) => {
  return (
    <span className={`zds-error inline-flex ${className}`}>
      <span>{children}</span>
    </span>
  );
};

The basic idea is that the class needs to be attached to a DOM tag element to work.

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

No branches or pull requests

3 participants