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.

Difficulty understanding the usage of `cx` between packages

See original GitHub issue

Description:

I’ve just started migrating a project to emotion, and am struggling to understand the usage patterns of the cx function from @emotion/css, specifically around two areas.

  1. Short circuiting values for conditional classNames: cx(state && className)

    There’s no mention of this in the documentation, only the object-based { [className]: state } method, but I see it is possible after testing it out. Is this considered a bad or deprecated pattern? I’m coming from another library which supports it, and prefer the approach.

  2. Including SerializedStyles from @emotion/react’s css

    This doesn’t work in cx, and forces me to use css from @emotion/css instead, but leaves me wondering if I’m choosing the right method for adding extra classNames? It feels like the react package should be the best css function to use, since I’m styling a React component. I think I would prefer there to be a version of cx exported from /react, which supports combining SerializedStyles as well so that I can remove the /css package, if that’s possible.

Overall I feel like I’m juggling between three packages (/styled, /css, /react) a little too much, which often leaves me confused. Some extra documentation around these choices would be helpful.

Edit: in case it’s useful, I prefer the styled method over the css prop method.

Documentation links: https://emotion.sh/docs/@emotion/css#cx

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:23
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
max8hinecommented, Jul 4, 2021

Same here, confusing on how to import cx method from @emotion/react instead of installing @emotion/css

2reactions
srmaguracommented, Jul 31, 2022

If you’re using @emotion/react or @emotion/styled, there is no need to install the @emotion/css package at all. The cx function from @emotion/css should only be used when generating class names with @emotion/css.

You can use the css function from @emotion/react to combine multiple object styles, CSS strings, SerializedStyles, .etc. The css function and css prop also support conditional styles like this:

const baseCss = css({ color: 'blue' })
const activeCss = css({ backgroundColor: 'gray' })

function MyComponent({ active }) {
    return <div css={[baseCss, active && activeCss]} />
}

The only context where cx is relevant when using @emotion/react or @emotion/styled is when using <ClassNames>, which is documented here.

I’m going to try to improve the documentation around combining CSS & conditional CSS using the css function since we don’t currently have a great example of it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is Customer Experience (CX) and Why Is it Important?
Customer experience (CX) is the sum total of customers' perceptions and feelings resulting from interactions with a brand's products and services.
Read more >
Understanding Customer Experience
It occurs when the gap between customers' expectations and their subsequent experiences has been closed. To understand how to achieve satisfaction, a company ......
Read more >
CX Is NOT The Same As Customer Service. Know The ...
All too often, these terms are used interchangeably, but they are not synonymous—CS is ... It is vital to understand the difference between...
Read more >
CX vs. UX—and How Customer Data Improves Both
Still, there are significant differences between CX and UX, and it pays to understand them. But an even more pressing question is: How...
Read more >
How the right CX Operating Model can pave the way to future ...
customer expectations means a radical departure from the status quo. In the ... data to optimal use. ... understanding of their CX strategy....
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