Comments (16)
There was an issue
earlier, see #18
It's kind of related and I still don't know the answer to the problem.
I switched to a module called jss
, because it gives me all the flexibility i need.
It allows:
- defining component based stylesheets (parameterized, because its javascript)
- updating a components stylesheet at runtime (even individual properties)
- author your styling in
css
or incss es6 template strings
and use jss-cli to convert it tojss
on the fly
from css-modules.
If you compare it with javascript it basically means that instead of requireing
or importing
X, thus re-using X and "customize" it to your needs (=calling a function with arguments)....
While you write X (e.g. define a function in javascript), you already import
or require
all the arguments you might need...
While writing header.css
, how can i possibly know all the scenarios in which i want to use it?
Maybe I could look about everything that is imported by the modules I will use and just overwrite color.css
in my new project - but does that work if you have nested dependencies?
For me, when writing re-usable components, the approach css-modules take doesnt work for me and variables or the lack of passing in arguments when importing a css file is only one reason. Another one is dynamically adapting styling based on user interaction, so i switched to jss
for now.
There it's easy to use variables, so I can write something like:
module.exports = function header (params) {
return `
.header {
color: ${params.blue};
background-color: ${params.red};
}
`
}
Then when re-using my component somehwere else :-)
var colors = { blue: '#0c77f8', red: '#ff0000', white: '#fff' }
var header = require('./header')(colors)
// use css2jss and jss to attach header css template string to DOM
// write rest of custom module that uses the "header module"
// ...
from css-modules.
I'd like to do that and afaik an es6 import or commonJS require call can get me the generated class names to add them, for example, to my "jsx markup".
It might be awesome to pass in or update "style variables" at runtime.
from css-modules.
I think the following README from react-css-modules
might show-case how css preprocessors and css work together in webpack.
https://github.com/gajus/react-css-modules#sass-scss-less-and-other-css-preprocessors
example:
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('style', 'css?modules&importLoaders=1&localIdentName=[name]__[local]___[hash:base64:5]!sass')
}
Note: loader-queries are processed from right to left.
from css-modules.
You can use any css preprocessor before applying css-modules, like sass, less, or stylus. And use variables system from that preprocessor.
We do stylus -> autoprefixer -> css-modules
and it works pretty nice for us.
from css-modules.
How do you update those variables at runtime?
from css-modules.
Maybe I didn't understand original question right, but I think @nsisodiya didn't want to update them at runtime.
from css-modules.
I can use compass
-> css files
-> css-module
.
only problem I am having, I do not want to keep generated css files in this flow.
currently I am having .css files in code repo. If I move to scss then I will keep only scss files and generated css files will go in /temp folder, in this case, referencing will be bit difficult.
currently I have having one js file and one css file per component folder.
from css-modules.
With webpack you do not need all those tricks. Just use pipe of loaders.
On Sep 16, 2015 8:22 PM, "Narendra Sisodiya" [email protected]
wrote:
I can use compass -> css files -> css-module.
only problem I am having, I do not want to keep generated css files in
this flow.
currently I am having .css files in code repo. If I move to scss then I
will keep only scss files and generated css files will go in /temp folder,
in this case, referencing will be bit difficult.
currently I have having one js file and one css file per component folder.—
Reply to this email directly or view it on GitHub
#50 (comment)
.
from css-modules.
Yeah, webpack makes it easy to use transformation pipeline as long as you want. You just specify in the config that you want stylus -> autoprefixer -> css-modules
for *.styl
files, for example, and then simply do:
css = require('file.styl')
css.myClass // contains name of class `.myClass` from `file.styl`
Probably similar can be done with browserify.
from css-modules.
@rpominov Do you use composes
statement from css-modules in your stylus code, or you decided to never use it?
I tried similar pipeline with Sass. Have following files:
// mixins.scss
@mixin myMixin {
...
}
// style1.scss
@import "mixins.scss";
@include myMixin;
.someClass { ... }
// style2.scss
.anotherClass {
composes: someClass from "style1.scss"
}
This simple code fails with
Module not found: Error: Cannot resolve module 'mixins.scss' in /path/to/style1.scss
After removing composes
from style2.scss
everything just works.
So, do you use css-modules only for providing unique classnames, without any additional features, and prepocessor for everything else?
from css-modules.
So, do you use css-modules only for providing unique classnames, without any additional features, and prepocessor for everything else?
Yeah, basically this. I haven't used it very long yet, but so far I only needed unique classnames.
from css-modules.
or it can actually be achieved without using any css-processors. Check this out: https://github.com/MadLittleMods/postcss-css-variables
from css-modules.
The best approach I have found so far is to export your variables as @value
's
colors.css
@value blue: #0c77f8;
@value red: #ff0000;
@value white: #fff;
header.css
@value colors: "./colors.css";
@value blue, white, red from colors;
.header {
color: blue;
background-color: red;
}
Using postcss-modules-values plugin
from css-modules.
I use stylus -> postcss-modules
to get the superior syntax and power magic powers that stylus offers. One important issue to note, though, is how much extra fluff this generates. Anything that uses stylus's @import/@require
automatically receives all the styles that are imported, as well as all the styles that are composed
recursively, in both directions. This generates enormous "modules" for each component, particularly if a comopse
references a @require
or vice-versa: #145 .
from css-modules.
Here's something I ran into, using composes
and @value
from various files.
/* Themes/Kratos/Fonts.css */
.lightFontStack {
font-family: "Helvetica Neue Light", HelveticaNeue-Light, "Helvetica Neue", Helvetica,
-apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", "Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", sans-serif;
font-weight: 200;
}
/* Themes/Kratos/index.css */
@value colors: "./Colors.css";
@value fonts: "./Fonts.css";
/* Inputs.css */
@value Kratos: "../Themes/Kratos/index.css";
@value Fonts: "./Themes/Kratos/Fonts.css"; /*
❌ UNREACHABLE, incorrect relative path, produces NO ERROR */
@value Fonts: "../Themes/Kratos/Fonts.css";
.inputField {
composes: lightFontStack from Kratos.fonts; ❌
composes: lightFontStack from Fonts; ❌
composes: lightFontStack from "../Themes/Kratos/Fonts.css"; ✅
CssSyntaxError: postcss-modules-scope: /css-loader!/[...]/Inputs/Input.css:8:3: referenced class name "lightFontStack" in composes not found
Another attempt:
@value Fonts: "../Themes/Kratos/Fonts.css";
@value lightFontStack from Fonts;
.inputField {
composes: lightFontStack; ❌
Module not found: Error: Cannot resolve module '"../Themes/"../Themes/Kratos/index.css"/Fonts.css"
Yet a 3rd technique, attempting to use the old CSS @import
declaration.
/* Themes/Kratos/Layout.css */
.defaultBorder {
border-radius: 0;
border: 2px solid;
border-color: currentColor;
}
/* Input.css */
@import url("../Themes/Kratos/Fonts.css");
@import url("../Themes/Kratos/Layout.css");
.inputField {
composes: defaultBorder;
composes: lightFontStack;
background-color: coreLightGrey; ❌
referenced class name "defaultBorder" in composes not found
Is this related to #123?
from css-modules.
Related Issues (20)
- Late styles loading in Next 13 HOT 3
- Class name prop not overriding component styles HOT 4
- Issue with duplicate class hashes
- Does the order of implementation of a class matter when there are two classes with the same parameters? HOT 1
- typo in https://github.com/css-modules/css-modules/blob/master/docs/local-scope.md HOT 1
- RFC: Support for hashing more types of identifiers HOT 8
- Another rant about not being able to import a class from another file
- not so good logo HOT 1
- Compose classes with attribute selector
- Best way to override styling found in module a based on module b. HOT 1
- How pseudo class :global should work? HOT 2
- ID vs Class
- Rules nesting HOT 1
- Understanding "composes" peer classes HOT 2
- It should throw an error if you use a class that doesn't exist
- Proposal: Handover Official Lead of CSS Modules to Devon Govett HOT 1
- Define how CSS modules interacts with `@layer` HOT 1
- bug: composes keyword does not work inside nested class
- how to style child Component when child and parent both use css.module? HOT 1
- Late loading of CSS Module creating layout shift - Next.js 14
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from css-modules.