Skip to content

Commit 213c8c1

Browse files
author
Michael Vurchio
committed
Update Readme
1 parent 68b20d8 commit 213c8c1

File tree

1 file changed

+49
-53
lines changed

1 file changed

+49
-53
lines changed

README.md

Lines changed: 49 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ npm install --save-dev svelte-preprocess-cssmodules@next
88

99
- [Usage](#usage)
1010
- [Modes](#modes)
11-
- [Target any classname format](#target-any-classname-format)
1211
- [Work with class directive](#work-with-class-directive)
13-
- [Local selector](#local-selector)
12+
- [Local selector](#local-selector)
1413
- [Import styles from an external stylesheet](#import-styles-from-an-external-stylesheet)
1514
- [Destructuring import](#destructuring-import)
1615
- [kebab-case situation](#kebab-case-situation)
@@ -20,6 +19,7 @@ npm install --save-dev svelte-preprocess-cssmodules@next
2019
- [Rollup](#rollup)
2120
- [Webpack](#webpack)
2221
- [Options](#options)
22+
- [Migration from v1](#migration-from-v1)
2323
- [Code example](#code-example)
2424
- [Why CSS Modules on Svelte](#why-css-modules-on-svelte)
2525

@@ -57,7 +57,7 @@ Preprocessor can operate in the following modes:
5757

5858
The mode can be set globally from the preprocessor options or locally to override the global settings per component.
5959

60-
*Mixed mode*
60+
**Mixed mode**
6161
```html
6262
<style module="mixed">
6363
p { font-size: 14px; }
@@ -78,7 +78,7 @@ The mode can be set globally from the preprocessor options or locally to overrid
7878
<p class="red-30_1IC svelte-teyu13r">My red text</p>
7979
```
8080

81-
*Scoped mode*
81+
**Scoped mode**
8282
```html
8383
<style module="scoped">
8484
p { font-size: 14px; }
@@ -99,38 +99,6 @@ The mode can be set globally from the preprocessor options or locally to overrid
9999
<p class="red-30_1IC svelte-teyu13r">My red text</p>
100100
```
101101

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-
134102
### Work with class directive
135103

136104
Toggle a class on an element.
@@ -148,7 +116,7 @@ Toggle a class on an element.
148116
<p class="{isActive ? 'bold' : ''}">My blue text</p>
149117
```
150118

151-
*After*
119+
*Generating*
152120

153121
```html
154122
<style>
@@ -159,33 +127,35 @@ Toggle a class on an element.
159127
<p class="bold-2jIMhI">My blue text</p>
160128
```
161129

162-
Use of shorthand
130+
**Use of shorthand**
163131

164132
```html
165133
<script module>
166-
let bold = true;
134+
let active = true;
167135
</script>
168136

169137
<style>
170-
.bold { font-weight: bold; }
138+
.active { font-weight: bold; }
171139
</style>
172140

173-
<p class:bold>My bold text</p>
141+
<p class:active>My active text</p>
174142
```
175143

176144
*Generating*
177145

178146
```html
179147
<style>
180-
.bold-2jIMhI { font-weight: bold; }
148+
.active-2jIMhI { font-weight: bold; }
181149
</style>
182150

183-
<p class="bold-2jIMhI">My bold text</p>
151+
<p class="active-2jIMhI">My active text</p>
184152
```
185153

186-
### Local selector
154+
## Local selector
155+
156+
Force a selector to be scoped within a component to prevent style inheritance on child components.
187157

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.
189159

190160
```html
191161
<!-- Parent Component-->
@@ -208,7 +178,7 @@ Force a selector to be scoped within the component to prevent style inheritance
208178
.child em { color: black; }
209179
210180
/**
211-
* Not needed rule
181+
* Not needed rule because of the use of :local()
212182
.secondary strong { font-weight: 700 }
213183
*/
214184
</style>
@@ -497,10 +467,10 @@ Pass an object of the following properties
497467
| ------------- | ------------- | ------------- | ------------- |
498468
| `mode` | `native\|mixed\|scoped` | `native` | The preprocess mode to use
499469
| `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 |
502470
| `hashSeeder` | `{Array}` | `['style', 'filepath', 'classname']` | An array of keys to base the hash on |
503471
| `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 |
504474

505475
**`localIdentName`**
506476

@@ -517,6 +487,16 @@ Inspired by [webpack interpolateName](https://github.com/webpack/loader-utils#in
517487
- other digestTypes, i. e. `hex`, `base26`, `base32`, `base36`, `base49`, `base52`, `base58`, `base62`, `base64`
518488
- and `length` the length in chars
519489

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+
520500
**`getLocalIdent`**
521501

522502
Customize the creation of the classname instead of relying on the built-in function.
@@ -575,12 +555,28 @@ preprocess: [
575555
...
576556
```
577557

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+
```
580579
581-
- `style` the content of the style tag (or the imported stylesheet)
582-
- `filepath` the path of the component
583-
- `classname` the local className
584580
585581
## Code Example
586582

0 commit comments

Comments
 (0)