@@ -8,9 +8,8 @@ npm install --save-dev svelte-preprocess-cssmodules@next
8
8
9
9
- [ Usage] ( #usage )
10
10
- [ Modes] ( #modes )
11
- - [ Target any classname format] ( #target-any-classname-format )
12
11
- [ Work with class directive] ( #work-with-class-directive )
13
- - [ Local selector] ( #local-selector )
12
+ - [ Local selector] ( #local-selector )
14
13
- [ Import styles from an external stylesheet] ( #import-styles-from-an-external-stylesheet )
15
14
- [ Destructuring import] ( #destructuring-import )
16
15
- [ kebab-case situation] ( #kebab-case-situation )
@@ -20,6 +19,7 @@ npm install --save-dev svelte-preprocess-cssmodules@next
20
19
- [ Rollup] ( #rollup )
21
20
- [ Webpack] ( #webpack )
22
21
- [ Options] ( #options )
22
+ - [ Migration from v1] ( #migration-from-v1 )
23
23
- [ Code example] ( #code-example )
24
24
- [ Why CSS Modules on Svelte] ( #why-css-modules-on-svelte )
25
25
@@ -57,7 +57,7 @@ Preprocessor can operate in the following modes:
57
57
58
58
The mode can be set globally from the preprocessor options or locally to override the global settings per component.
59
59
60
- * Mixed mode*
60
+ ** Mixed mode* *
61
61
``` html
62
62
<style module =" mixed" >
63
63
p { font-size : 14px ; }
@@ -78,7 +78,7 @@ The mode can be set globally from the preprocessor options or locally to overrid
78
78
<p class =" red-30_1IC svelte-teyu13r" >My red text</p >
79
79
```
80
80
81
- * Scoped mode*
81
+ ** Scoped mode* *
82
82
``` html
83
83
<style module =" scoped" >
84
84
p { font-size : 14px ; }
@@ -99,38 +99,6 @@ The mode can be set globally from the preprocessor options or locally to overrid
99
99
<p class =" red-30_1IC svelte-teyu13r" >My red text</p >
100
100
```
101
101
102
- ### Target any classname format
103
-
104
- kebab-case or camelCase, name the classes the way you're more comfortable with.
105
-
106
- * Before*
107
-
108
- ``` html
109
- <style module >
110
- .red { color : red ; }
111
- .red-crimson { color : crimson ; }
112
- .redMajenta { color : magenta ; }
113
- </style >
114
-
115
- <span class =" red" >Red</span >
116
- <span class =" red-crimson" >Crimson</span >
117
- <span class =" redMajenta" >Majenta</span >
118
- ```
119
-
120
- * After*
121
-
122
- ``` html
123
- <style >
124
- .red-2xTdmA { color : red ; }
125
- .red-crimson-1lu8Sg { color : crimson ; }
126
- .redMajenta-2wdRa3 { color : magenta ; }
127
- </style >
128
-
129
- <span class =" red-2xTdmA" >Red</span >
130
- <span class =" red-crimson-1lu8Sg" >Crimson</span >
131
- <span class =" redMajenta-2wdRa3" >Majenta</span >
132
- ```
133
-
134
102
### Work with class directive
135
103
136
104
Toggle a class on an element.
@@ -148,7 +116,7 @@ Toggle a class on an element.
148
116
<p class =" {isActive ? 'bold' : ''}" >My blue text</p >
149
117
```
150
118
151
- * After *
119
+ * Generating *
152
120
153
121
``` html
154
122
<style >
@@ -159,33 +127,35 @@ Toggle a class on an element.
159
127
<p class =" bold-2jIMhI" >My blue text</p >
160
128
```
161
129
162
- Use of shorthand
130
+ ** Use of shorthand**
163
131
164
132
``` html
165
133
<script module >
166
- let bold = true ;
134
+ let active = true ;
167
135
</script >
168
136
169
137
<style >
170
- .bold { font-weight : bold ; }
138
+ .active { font-weight : bold ; }
171
139
</style >
172
140
173
- <p class:bold >My bold text</p >
141
+ <p class:active >My active text</p >
174
142
```
175
143
176
144
* Generating*
177
145
178
146
``` html
179
147
<style >
180
- .bold -2jIMhI { font-weight : bold ; }
148
+ .active -2jIMhI { font-weight : bold ; }
181
149
</style >
182
150
183
- <p class =" bold -2jIMhI" >My bold text</p >
151
+ <p class =" active -2jIMhI" >My active text</p >
184
152
```
185
153
186
- ### Local selector
154
+ ## Local selector
155
+
156
+ Force a selector to be scoped within a component to prevent style inheritance on child components.
187
157
188
- Force a selector to be scoped within the component to prevent style inheritance in child components .
158
+ ` :local() ` is doing the opposite of ` :global() ` and can only be used with the ` native ` and ` mixed ` mode .
189
159
190
160
``` html
191
161
<!-- Parent Component-->
@@ -208,7 +178,7 @@ Force a selector to be scoped within the component to prevent style inheritance
208
178
.child em { color : black ; }
209
179
210
180
/* *
211
- * Not needed rule
181
+ * Not needed rule because of the use of :local()
212
182
.secondary strong { font-weight: 700 }
213
183
*/
214
184
</style >
@@ -497,10 +467,10 @@ Pass an object of the following properties
497
467
| ------------- | ------------- | ------------- | ------------- |
498
468
| ` mode ` | ` native\|mixed\|scoped ` | ` native ` | The preprocess mode to use
499
469
| ` localIdentName ` | ` {String} ` | ` "[local]-[hash:base64:6]" ` | A rule using any available token |
500
- | ` includePaths ` | ` {Array} ` | ` [] ` (Any) | An array of paths to be processed |
501
- | ` getLocalIdent ` | ` Function ` | ` undefined ` | Generate the classname by specifying a function instead of using the built-in interpolation |
502
470
| ` hashSeeder ` | ` {Array} ` | ` ['style', 'filepath', 'classname'] ` | An array of keys to base the hash on |
503
471
| ` allowedAttributes ` | ` {Array} ` | ` [] ` | An array of attributes to parse along with ` class ` |
472
+ | ` includePaths ` | ` {Array} ` | ` [] ` (Any) | An array of paths to be processed |
473
+ | ` getLocalIdent ` | ` Function ` | ` undefined ` | Generate the classname by specifying a function instead of using the built-in interpolation |
504
474
505
475
** ` localIdentName ` **
506
476
@@ -517,6 +487,16 @@ Inspired by [webpack interpolateName](https://github.com/webpack/loader-utils#in
517
487
- other digestTypes, i. e. ` hex ` , ` base26 ` , ` base32 ` , ` base36 ` , ` base49 ` , ` base52 ` , ` base58 ` , ` base62 ` , ` base64 `
518
488
- and ` length ` the length in chars
519
489
490
+ ** ` hashSeeder ` **
491
+
492
+ Set the resource content of ` [hash] ` and ` [contenthash] ` .
493
+
494
+ The list of available keys are:
495
+
496
+ - ` style ` the content of the style tag (or the imported stylesheet)
497
+ - ` filepath ` the path of the component
498
+ - ` classname ` the local className
499
+
520
500
** ` getLocalIdent ` **
521
501
522
502
Customize the creation of the classname instead of relying on the built-in function.
@@ -575,12 +555,28 @@ preprocess: [
575
555
...
576
556
```
577
557
578
- ** ` hashSeeder ` **
579
- The list of available keys are:
558
+ ## Migrating from v1
559
+ If you want to migrate an existing project to ` v2 ` keeping the approach of the 1st version, follow the steps below:
560
+
561
+ - Set the ` mixed ` mode from the global settings.
562
+ ``` js
563
+ // Preprocess config
564
+ ...
565
+
566
+ preprocess: [
567
+ cssModules ([
568
+ mode: ' mixed' ,
569
+ ]),
570
+ ],
571
+ ```
572
+ - Remove all ` $style. ` prefix from the html markup
573
+ - Add the attribute ` module ` to ` <style> ` for all your components.
574
+ ``` html
575
+ <style module >
576
+ ...
577
+ </style >
578
+ ```
580
579
581
- - ` style ` the content of the style tag (or the imported stylesheet)
582
- - ` filepath ` the path of the component
583
- - ` classname ` the local className
584
580
585
581
## Code Example
586
582
0 commit comments