Add support for CSS Modules
See original GitHub issueProposal
I would like to propose support for CSS modules using the solution from reactstrap.
Meaning for Product
We are building a snippet, which can be integrated into any arbitrary website. This has often resulted in the css of the snippet overriding that of the enclosing site or vice versa. A solution for this would be support for CSS modules.
Solution and code
This could be done by globally setting the CSS Modules as follows:
let globalCssModule;
export function setGlobalCssModule(cssModule) {
globalCssModule = cssModule;
}
We could optionally not set the CSS Module globally but rather make it an optional property in the components.
This globalCSSModule
could then be used to update the Component classNames with the following function:
export function mapToCssModules(className = '', cssModule = globalCssModule) {
if (!cssModule) return className;
return className
.split(' ')
.map(c => cssModule[c] || c)
.join(' ');
}
All of these methods were lifted off the reactstrap library. Sources:
Implementation
I am ready to create a pull request with the proposed implementation.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Adding a CSS Modules Stylesheet - Create React App
This project supports CSS Modules alongside regular stylesheets using the [name].module.css file naming convention. CSS Modules allows the ...
Read more >Basic Features: Built-in CSS Support - Next.js
Next.js supports including CSS files as Global CSS or CSS Modules, using `styled-jsx` for CSS-in-JS, or any other CSS-in-JS solution! Learn more here....
Read more >Documentation about css-modules - GitHub
A CSS Module is a CSS file in which all class names and animation names are scoped locally by default. All URLs (...
Read more >What are CSS Modules and why do we need them?
According to the repo, CSS modules are: CSS files in which all class names and animation names are scoped locally by default. So...
Read more >A deep dive into CSS Module - LogRocket Blog
Next, we'll add CSS module support for our app by simply renaming the App.css file to App.module.css . Update the import statement in...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi @rawroland and thanks for the detailed proposal. I haven’t used CSS modules extensively, so I’m not totally sure how they’d integrate with this project and what implications that would have more broadly for consumers of the library. For example, would things continue to work correctly for those using the UMD builds or not using modules in their own projects? I understand that style conflicts can be a problem, though I’ve tried to address that by scoping the styles to the component. Can you explain more about the specific issues you’re running into?
@ericgio Sorry for not responding for so long. It is ok to close the issue, since we found a workaround for our problem which really does not belong in the base package. Looking forward to using a new version of the package.