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.

className interpolation alternative

See original GitHub issue
  • emotion version: 10.0.6
  • react version: 16.8.0-alpha.1

Relevant code:

import { css } from 'emotion';

const childClass = css`
  display: none;
`;

const containerClass = css`
  &:hover .${childClass} {
    display: block;
  }
`;

const Home = () => (
  <div className={containerClass}>
    <h1>Hello world</h1>
    <div className={childClass}>Hi there!</div>
  </div>
);

Problem description:

Hello, I’m getting a warning that class interpolation will not be supported in the next major release of Emotion, but I didn’t find any migration instructions for it and I’m not sure how to implement the example above without interpolation and without using “regular” class names.

I know its use is problematic when using it with composition, but I also don’t know about the alternative in the use cases like the one above. Does the warning mean the feature will be removed or does it mean it will be undocumented in 11 and there will be no guarantees after that (could be removed without notice)?

https://github.com/emotion-js/emotion/blob/86700d21ccd8bcc6fc2740c5d6b381dfe8a9bce1/packages/serialize/src/index.js#L208

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:28 (8 by maintainers)

github_iconTop GitHub Comments

21reactions
tkh44commented, Jan 22, 2019

The easiest way is to use attributes or class names as you would before.

Class Names

import { css, cx } from 'emotion';

const childClass = css`
  display: none;
`;

const containerClass = css`
  &:hover .hidden {
    display: block;
  }
`;

const Home = () => (
  <div className={containerClass}>
    <h1>Hello world</h1>
    <div className={cx('hidden', childClass)}>Hi there!</div>
  </div>
);

Attributes

import { css } from 'emotion';

const childClass = css`
  display: none;
`;

const containerClass = css`
  &:hover [data-hidden] {
    display: block;
  }
`;

const Home = () => (
  <div className={containerClass}>
    <h1>Hello world</h1>
    <div className={childClass} data-hidden>Hi there!</div>
  </div>
);

Note that this works for any version of emotion. If the className is not recognized by emotion it will simply be appended to the className prop.

15reactions
Andaristcommented, May 24, 2020

Sorry, we don’t plan to bring back support for this - truth to be told we don’t believe using any descendant selectors is a good idea. It’s sometimes convenient - that’s for sure, but it makes for a tightly coupled styles which are harder to maintain over the long period of time.

And we are not alone with that. Let’s take a look at a recent post by Facebook - https://engineering.fb.com/web/facebook-redesign/ . We can find this there:

We guarantee that the styles are applied in a stable order, and we don’t support CSS descendant selectors.

If you really need to target a descendant element with emotion the best you can do is to use data attributes to select them, but again - we don’t recommend this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

LESS class name string interpolation not working
I am currently translating my grid into LESS, but I can't seem to figure out string interpolation.
Read more >
Features In-Depth | Less.js
Less extends CSS with dynamic behavior such as variables, mixins, operations and functions. Less runs on both the server-side (with Node.js and Rhino)...
Read more >
Interpolation - Sass
Interpolation can be used almost anywhere in a Sass stylesheet to embed the result of a SassScript expression into a chunk of CSS....
Read more >
String Interpolation in JavaScript - Dmitri Pavlutin
String interpolation is replacing placeholders with values in a string literal. The string interpolation in JavaScript is performed by ...
Read more >
Guide to String Interpolation With React - Stack Abuse
Depending on the state, a different className will thus be applied to the heading! With concatenation, we will first place our actual string...
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