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.

Ability to create a class name string from a style object.

See original GitHub issue

Is your feature request related to a problem? Please describe.

I need to work with other components that don’t work well with Styled System. They allow me to style them by specifying one or more class names, though.

For example, I need to specify two class names to style <CookieConsent /> (https://github.com/Mastermindzh/react-cookie-consent):

<CookieConsent
  declineButtonText="Opt out"
  buttonClasses="..."
  declineButtonClasses="..."
  ...
/>

Emotion provides a component called <ClassNames /> so a user can generate a cached class name from a style object, but it doesn’t understand Styled System’s theme object.

Describe the solution you’d like

To solve this issue, I wrote an alternative component which applies css() to a style object before passing the style object to Emotion’s ClassNames:

import { ClassNames as EmotionClassNames, Interpolation } from '@emotion/core';
// @ts-ignore
import { useThemeUI } from '@theme-ui/core';
// @ts-ignore
import { css as themeUICss } from '@theme-ui/css';
import React from 'react';

interface ClassNamesContent {
  cssToClassName(styleObject: Interpolation): string;
}

interface ClassNamesProp {
  children(content: ClassNamesContent): React.ReactNode;
}

const ClassNames: React.FC<ClassNamesProp> = props => {
  const { theme } = useThemeUI();

  return (
    <EmotionClassNames>
      {({ css: emotionCss }) =>
        props.children({
          cssToClassName: styleObject =>
            emotionCss(themeUICss(styleObject)(theme)),
        })
      }
    </EmotionClassNames>
  );
};

export default ClassNames;

With this component, I was able to do this:

<ClassNames>
  {({ cssToClassName }) => (
    <CookieConsent
      declineButtonText="Opt out"
      buttonClasses={cssToClassName({ width: ['100%', 1/2 ] })}
      declineButtonClasses={cssToClassName({ width: ['100%', 1/2 ] })}
      ...
    />
  )}
</ClassNames>

Describe alternatives you’ve considered

I’m not an expert in frontend technology, so I’m not sure if this is the best way to solve this problem. Any better suggestions would be appreciated.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
hasparuscommented, May 10, 2020

I wanted to propose a workaround I usually use

<div
  sx={{
    [".CookieConsent__Button"]: {
      color: 'blue'
    }
  }}
>
    <CookieConsent
      declineButtonText="Opt out"
      buttonClasses={"CookieConsent__Button"}
    />
</div>

but your solution is pretty awesome. Nice 😄

0reactions
lachlanjccommented, Dec 3, 2020

You’re totally right, I missed that, thanks! Yep, it’d be cool to have a package, though I’m not interested in contributing it personally. Did you want to reopen @hasparus?

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Create a new object from a class name and pass ...
Is it possible to pass the class name itself as a string argument to the object creator? clnm: string = "testclass"; var dds2:...
Read more >
JavaScript adding a class name to the element - GeeksforGeeks
Using .className property: This property is used to add a class name to the selected element. Syntax: It is used to set the...
Read more >
How To Style React Components | DigitalOcean
In this tutorial, you'll learn three different ways to style React components: plain Cascading Style Sheets (CSS), inline styles with ...
Read more >
Object and Collection Initializers - C# Programming Guide
The object initializers syntax allows you to create an instance, and after that it assigns the newly created object, with its assigned ...
Read more >
15. Classes and Objects — the Basics — How to Think Like a ...
Every class should have a method with the special name __init__. ... It gives the programmer the opportunity to set up the attributes...
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