Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2a437a8

Browse files
committed
Initial commit
0 parents  commit2a437a8

File tree

2 files changed

+142
-0
lines changed

2 files changed

+142
-0
lines changed

‎README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#CSS Modules
2+
3+
A**CSS Module** is a CSS file in which all class names and animation names are scoped locally by default. All URLs (`url(...)`) and`@imports` are in module request format (`./xxx` and`../xxx` means relative,`xxx` and`xxx/yyy` means in modules folder, i. e. in`node_modules`).
4+
5+
```css
6+
/* style.css*/
7+
.className {
8+
color:green;
9+
}
10+
```
11+
12+
When importing the**CSS Module** from an JS Module, it exports global names for all local names. As default export an object with all mappings is exported.
13+
14+
```js
15+
importstylesfrom"./style.css";
16+
// import { className } from "./style.css";
17+
18+
return'<div class="'+styles.className+'">';
19+
```
20+
21+
##Naming
22+
23+
For local class names camelCase naming is recommended (but not enforced).
24+
25+
##Exceptions
26+
27+
`:global` switches to global scope for the current selector resp. identifier.`:global(.xxx)` resp.`@keyframes :global(xxx)` resp.`animation: :global(xxx);` declares a the stuff in brackets in the global scope.
28+
29+
Similar`:local` and`:local(...)` for local scope.
30+
31+
If the selector is switched into global mode, global mode is also activated for the rules.
32+
33+
Example:`.localA :global .global-b .global-c :local(.localD.localE) .global-d`
34+
35+
##Extends
36+
37+
It's possible to extend a selector.
38+
39+
```css
40+
.className {
41+
color:green;
42+
background:red;
43+
}
44+
45+
.otherClassName {
46+
extends: className;
47+
color:yellow;
48+
}
49+
```
50+
51+
There can be multiple`extends` rules, but`extends` rules must be before other rules. Extending works only for local-scoped selectors and only if the selector is a single class name. When a class name extends from another class name, the CSS Module exports both class names for the local class. This can add up to multiple class names.
52+
53+
It's possible to extend from multiple classes with`extends: classNameA classNameB;`.
54+
55+
##Dependencies
56+
57+
It's possible to extend from class names from other**CSS Modules**.
58+
59+
```css
60+
.otherClassName {
61+
extends: className from"./style.css";
62+
}
63+
```
64+
65+
##Usage with preprocessors
66+
67+
Preprocessors can make it easy to define a block global or local.
68+
69+
i. e. with less.js
70+
71+
```less
72+
:global {
73+
.global-class-name {
74+
color:green;
75+
}
76+
}
77+
```
78+
79+
##Why?
80+
81+
**modular** and**reusable** CSS!
82+
83+
* No more conflicts.
84+
* Explicit dependencies.
85+
* No global scope.
86+
87+
##Examples
88+
89+
*[markdalgleish/css-modules-example](https://github.com/markdalgleish/css-modules-example);
90+
*[Theming](https://github.com/webpack/css-modules/blob/master/examples/theming.md)
91+
92+
#webpack implementation
93+
94+
The css-loader replaces every local-scoped identifier with a global unique name (hashed from module name and local identifier by default) and exports the used identifer.
95+
96+
Extending adds the source class name(s) to the exports.
97+
98+
Extending from other modules first imports the other module and than adds the class name(s) to the exports.

‎examples/theming.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
###Theming
2+
3+
Instead of importing a style in the component, the component can take a style as property. This way different themes can be used. The user can even define custom themes.
4+
5+
```css
6+
/* component/theme-a.css*/
7+
.outer {background:green; }
8+
.inner {color:blue; }
9+
```
10+
11+
```css
12+
/* component/theme-b.css*/
13+
.outer {background:red; }
14+
.inner {color:yellow; }
15+
```
16+
17+
```js
18+
/* component/index.js*/
19+
exportdefaultclassComponent {
20+
constructor(theme) {
21+
this.theme= theme;
22+
}
23+
render() {
24+
var theme=this.theme;
25+
return'<div class="'+theme.outer+'">'+
26+
'<div class="'+theme.inner+'">'+
27+
'</div></div>';
28+
}
29+
}
30+
```
31+
32+
```js
33+
/* application*/
34+
importthemeAfrom"component/theme-a.css";
35+
importthemeBfrom"component/theme-b.css";
36+
importcustomThemefrom"./custom-theme.css";
37+
38+
importComponentfrom"component";
39+
40+
// use the Component
41+
newComponent(themeA);
42+
newComponent(themeB);
43+
newComponent(customTheme);
44+
```

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp