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.

Emotion 9 to 10 - how to pass external classes properly

See original GitHub issue

This is maybe a question about design rather than a specific bug.

  • emotion version: 10.0.6
  • react version: 16.4.2

Relevant code:

Previous emotion 9 code:


let carousel = css`
.. some carousel styles
`;

let dots = css`
..some dots styles
`;

<div className={cx(carousel, className)}>
      <Slider dotsClass={dotsClassName || dots}>
        {children}
      </Slider>
</div>

className and dotsClassName are custom class names you can pass down to the component to customize it.

What you did:

Now with emotion 10 im not even sure how I would write this in a clean and concise way. I did get it to work with this:


let carousel =`
.. some carousel styles
`;

let dots = `
..some dots styles
`;

<ClassNames>
    {({ css, cx }) => (
      <div
        className={cx(
          css`${carousel};`,
          className,
        )}
      >
        <Slider
          dotsClass={dotsClassName || css`${dots};`}
        >
          {children}
        </Slider>
      </div>
    )}
  </ClassNames>

But this seems like a really cumbersome / hard to read with ClassNames (alot more jsx and harder to follow imo)

I know its probably not the best practice to be passing class names to components (anti pattern) but in this case Slider is a 3rd party component and the only way to customise its styles is to pass down a class name.

Suggested solution:

Not sure - maybe there is a better way to write the above that im missing from the docs.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jooj123commented, May 1, 2019

@acnebs can do that but im no longer using that dependancy - only installed @emotion/core as per docs Prefer not to use the legacy emotion lib

3reactions
acnebscommented, Apr 5, 2019

Just import css and cx from ‘emotion’ instead of ‘@emotion/core’.

It returns the name of the class.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Emotion - pass style object to external library - Stack Overflow
Inline style objects currently do not support media queries. The viable option here is to use the className prop to style the content....
Read more >
Emotion | Definition, Examples, Scope, Structures, & Facts
The proper development and functioning of emotions allow people to live well and to be happy. Love, respect, and compassion, for example, are...
Read more >
Building A Component Library With React And Emotion
In this tutorial, we'll learn how to build a component library, using Emotion in React to resolve inconsistencies.
Read more >
Teachers' Emotion and Identity Work During a Pandemic
This piece is a conceptual analysis of the care involved on the part of teachers during the COVID-19 era and the relationship it...
Read more >
Migrating to Recipe 10
Install emotion v10 packages · Configure emotion codemods · Running emotion codemods · Upgrading emotion server · Remove css helper usage of className...
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