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.

Option to customize injected className

See original GitHub issue

The problem

I’m working with @cdonohue on benefit.

It’s a similar solution to TailwindCSS in that it’s a utility CSS library, but powered by emotion.

<div class='bg-blue-500 text-white p-8 rounded'>...</div>

becomes (via our own jsx wrapper):

<div class='css-p8ywv3 css-1u78gm9 css-1rhef98 css-yrxnuw'>...</div>

Because we convert bg-blue-500 to css(declarations), emotion returns css-p8ywv3.

Proposed solution

When working in development, I’d like to keep the utility classes the same.

I’m having a hard time proposing a how, but my best guess would be introducing a prefix options to babel-plugin-emotion, similar to:

https://emotion.sh/docs/babel-plugin-emotion#labelformat

The default value of prefix would be css-[hash] for backwards compatibility:

{
  "plugins": [
    [
      "emotion",
      {
        // sourceMap is on by default but source maps are dead code eliminated in production
        "sourceMap": true,
        "autoLabel": process.env.NODE_ENV !== 'production',
        "labelFormat": "[local]",
        "prefix": "css-[hash]",
        "cssPropOptimization": true
      }
    ]
  ]
}

Users would be able to specify "prefix": false, which would ignore the prefix entirely and only output label.

Now, I fully understand the need for hashes for preventing conflicting rules under the same name.

In development, I’d propose that the computed className from a label would maintain it’s hash association so that, should that same className be generated with a different hash, a warning/error is thrown.

Alternative solutions

Because the declarations are generated dynamically, even local doesn’t work:

https://emotion.sh/docs/babel-plugin-emotion#labelformat

label comes close, but ends up being very verbose:

https://emotion.sh/docs/labels#___gatsby

Additional context

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:9
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
Andaristcommented, Sep 14, 2021

You should be able to do this:

css({
  padding: 10,
  margin: 2,
}, {
 label: 'myCustomString',
})
1reaction
a-tonchevcommented, Sep 14, 2021

I am also interested in this matter. Now we have css-[hash], it would be just fine if we have css-[hash]_myCustomString

Thus you don’t really need huge maintenance, since you can just parse the hash out of the custom string.

and maybe while creating the css:

css({
  padding: 10,
  margin: 2,
}, {
 customClassName: 'myCustomString',
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

Injected-class-name
The injected-class-name is the unqualified name of a class within the scope of said class. In a class template, the injected-class-name can ...
Read more >
Customizing components - Material-UI - MUI
You can easily customize the appearance of a Material-UI component. ... to inject custom styles into the DOM, and to pass the class...
Read more >
c++ - Injected class name as type
Neat corner case. · This question arises from transparent comparators, classes that define a type member named is_transparent . · A well posed ......
Read more >
How To Style React Components
After creating the custom hook, you passed the custom class names to the className property. Save the file. Notice that you are using...
Read more >
ClassName Inputs - Docs
CSS classNames can be injected into FullCalendar's DOM in various places. They can be provided in the following formats. These examples use eventClassNames ......
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