theme-ui icon indicating copy to clipboard operation
theme-ui copied to clipboard

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

Open trustin opened this issue 4 years ago • 6 comments

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.

trustin avatar Apr 13 '20 12:04 trustin

There might be a way to do this, but I’m not aware of it. I’ve run into this as well though.

lachlanjc avatar Apr 13 '20 17:04 lachlanjc

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 :D

hasparus avatar May 10 '20 15:05 hasparus

thanks for the idea @hasparus .

muhajirdev avatar May 19 '20 10:05 muhajirdev

It looks like Emotion doesn’t directly offer this functionality, so I don’t think Theme UI can/should. The css utility we export offers support for parsing Theme UI’s sx type of CSS, & there’s the type of workarounds Piotr suggested. If you see a good way to implement this though, go for it, PRs welcome!

lachlanjc avatar Dec 02 '20 16:12 lachlanjc

It looks like Emotion doesn’t directly offer this functionality

I just wanted to disagree — there's this one https://emotion.sh/docs/class-names.

I think we'd accept a PR for @theme-ui/classnames package, but I'm happy enough with my workaround, that I'd probably never use it.

Another thing, you can add your global classNames to styles.root. They will have higher specificity than ones made from ClassNames component, but I never found it a problem.

hasparus avatar Dec 02 '20 19:12 hasparus

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?

lachlanjc avatar Dec 03 '20 00:12 lachlanjc