question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Styling styled components acts unexpectedly

See original GitHub issue

Current behavior:

Styling information is lost for consecutive styles. I suspect but cannot confirm that it’s a regression, because it appears that the code no longer works. Nevertheless, currently it doesn’t throw any errors besides not functioning as expected.

To reproduce:

import { css } from "@emotion/css";
import styled from "@emotion/styled";

export const ClearButton = styled('button')`
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    font: inherit; 
    /* // Inherit font settings (doesn’t work in IE7-) */
    /* // line-height: normal; // Override line-height to avoid spacing issues */
    line-height: 0; /* // Due to Flexbox issues */
    /* Remove mystery padding in Gecko browsers.
     * See https://bugzilla.mozilla.org/show_bug.cgi?id=140562
     */
    ::-moz-focus-inner {
        padding: 0;
        border: 0;
    }
    cursor: pointer;
    :focus {
        outline: none;
    }
`;

const newstyle = css`
    :hover {
        opacity: 0.7;
    }
    :focus {
        opacity: 0.7;
        outline: none;
    }
`

// Result has no hover activity, however, no error is thrown either.
export const ClearButtonHover_Broken = styled(ClearButton)(newstyle)
export const ClearButtonHover_AlsoBroken = styled(ClearButton)`${newstyle}`
export const ClearButtonHover_Works = styled(ClearButton)`
    :hover {
        opacity: 0.7;
    }
    :focus {
        opacity: 0.7;
        outline: none;
    }
`;
`

export const ClearButtonHover = styled(ClearButton)(newstyle)

Expected behavior:

Consistent styling or errors if this isn’t valid usage of the new API

Environment information:

  • react version: 17.0.1
  • @emotion/react version: 11.1.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
awgeorgecommented, Jan 27, 2022

Is Emotion meant to support inherence in the way styled-components does? I assumed it should, but this suggests otherwise: https://codesandbox.io/s/trusting-boyd-uvc6x?file=/src/App.js

import React from "react";
import styled from "@emotion/styled";

const Green = ({ children }) => {
  return <StyledGreen>{children}</StyledGreen>;
};

const StyledGreen = styled("section")({
  backgroundColor: "#0f0"
});

const Red = ({ children }) => {
  return <StyledRed>{children}</StyledRed>;
};

const StyledRed = styled(Green)({
  backgroundColor: "#f00"
});

export default function App() {
  return <Red>Should be red</Red>;
}

Emotion-react v11.7.1 Emotion-styled v11.6.0

0reactions
rsxdalvcommented, Jan 27, 2022

Fair enough, since there’s a solution inside this thread that should suffice for now. I also got a lot busier since making this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

styled-component stopped working after refactoring
Using the following, I was able to render the component and styles no problem: import React from "react"; import ReactDOM from "react-dom"; ...
Read more >
Styled-Components in Action - Medium
Styled -Components is a new CSS tool, created by Max Stoiber and Glen Maddern, which helps you organize CSS in your React project....
Read more >
React Render Props with Ts & styled-components | Part 2
We want a component that acts as an adapter between the GridLayout and what ... A second prop, style , will categorically contain...
Read more >
Styling Components In React - Smashing Magazine
In this tutorial, we're going to learn how to style React components using four major styling strategies — with examples on how to...
Read more >
How common is using styled components? : r/reactjs - Reddit
Is this where the style is css in the components code? ... hope nobody places a new div between two elements and breaks...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found