Skip to content

Commit f2cc577

Browse files
author
Michael Vurchio
committed
Merge branch 'release/1.0.0'
2 parents 867e9e3 + 76db978 commit f2cc577

19 files changed

+10645
-21
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
.DS_Store

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# Svelte preprocess CSS Modules, changelog
22

3+
# 1.0.0
4+
- Ability to use the same class on multiple elments
5+
- Use with svelte loader Ok
6+
- Add test
7+
38
# 0.1.1
49
- Fix modules exports
510
- Fix `includePaths` check

README.md

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ npm install --save-dev svelte-preprocess-cssmodules
88

99
## Rollup Configuration
1010

11-
To be used with the plugin `rollup-plugin-svelte`.
11+
To be used with the plugin [`rollup-plugin-svelte`](https://github.com/sveltejs/rollup-plugin-svelte).
1212

1313
```js
1414
import svelte from 'rollup-plugin-svelte';
@@ -27,6 +27,37 @@ export default {
2727
}
2828
```
2929

30+
## Webpack Configuration
31+
32+
To be used with the loader [`svelte-loader`](https://github.com/sveltejs/svelte-loader).
33+
34+
```js
35+
const cssModules = require('svelte-preprocess-cssmodules');
36+
37+
module.exports = {
38+
...
39+
module: {
40+
rules: [
41+
{
42+
test: /\.svelte$/,
43+
exclude: /node_modules/,
44+
use: [
45+
{
46+
loader: 'svelte-loader',
47+
options: {
48+
preprocess: [
49+
cssModules(),
50+
]
51+
}
52+
}
53+
]
54+
}
55+
]
56+
}
57+
...
58+
}
59+
```
60+
3061
## Options
3162
Pass an object of the following properties
3263

@@ -36,6 +67,7 @@ Pass an object of the following properties
3667
| `includePaths` | `{Array}` | `[]` (Any) | An array of paths to be processed |
3768

3869

70+
3971
## Usage on Svelte Component
4072

4173
**On the HTML markup** (not the CSS), Prefix any class name that require CSS Modules by *$style.* => `$style.My_CLASSNAME`
@@ -79,8 +111,7 @@ CSS Modules classname are generated to the html class values prefixed by `$style
79111

80112
```html
81113
<style>
82-
.blue { color: blue;
83-
}
114+
.blue { color: blue;}
84115
.red-2iBDzf { color: red; }
85116
.text-center { text-align: center; }
86117
</style>
@@ -145,6 +176,47 @@ kebab-case or camelCase, name the classes the way you're more comfortable with.
145176
<span class="redMajenta-2wdRa3">Majenta</span>
146177
```
147178

179+
### Use class multiple times
180+
A class can be naturally used on multiple elements.
181+
182+
*Before*
183+
184+
```html
185+
<style>
186+
.red { color: red; }
187+
.blue { color: blue; }
188+
.bold { font-weight: bold; }
189+
</style>
190+
191+
<p class="$style.red $style.bold">My red text</p>
192+
<p class="$style.blue $style.bold">My blue text</p>
193+
```
194+
195+
*After*
196+
197+
```html
198+
<style>
199+
.red-en-6pb { color: red; }
200+
.blue-oVk-n1 { color: blue; }
201+
.bold-2jIMhI { font-weight: bold; }
202+
</style>
203+
204+
<p class="red-en-6pb bold-2jIMhI">My red text</p>
205+
<p class="blue-oVk-n1 bold-2jIMhI">My blue text</p>
206+
```
207+
208+
### Work with class directives
209+
Toggling a class on an element.
210+
211+
```html
212+
<style>
213+
.bold { font-weight: bold; }
214+
</style>
215+
216+
<p class:$style.bold={isActive}>My red text</p>
217+
<p class="{isActive ? '$style.bold' : ''}">My blue text</p>
218+
```
219+
148220
## Example
149221

150222
*Rollup Config*

example/webpack/App.svelte

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<script>
2+
import Time from './Time.svelte';
3+
</script>
4+
5+
<style>
6+
.overlay {
7+
position: fixed;
8+
top: 0;
9+
right: 0;
10+
bottom: 0;
11+
left: 0;
12+
z-index: 5;
13+
background-color: rgba(0, 0, 0, 0.5);
14+
}
15+
.modal {
16+
position: fixed;
17+
top: 50%;
18+
left: 50%;
19+
z-index: 10;
20+
width: 400px;
21+
height: 80%;
22+
font-family: 'Helvetica Neue', Helvetica, Arial;
23+
background-color: #fff;
24+
transform: translateX(-50%) translateY(-50%);
25+
-webkit-font-smoothing: antialiased;
26+
}
27+
28+
section {
29+
flex: 0 1 auto;
30+
flex-direction: column;
31+
display: flex;
32+
height: 100%;
33+
}
34+
35+
header {
36+
padding: 1rem;
37+
font-size: 1.2rem;
38+
font-weight: bold;
39+
border-bottom: 1px solid #d9d9d9;
40+
}
41+
42+
.body {
43+
padding: 1rem;
44+
flex: 1 0 0;
45+
min-height: 0;
46+
max-height: 100%;
47+
overflow: scroll;
48+
-webkit-overflow-scrolling: touch;
49+
}
50+
51+
footer {
52+
padding: 1rem;
53+
text-align: right;
54+
border-top: 1px solid #d9d9d9;
55+
}
56+
button {
57+
padding: .5em 1em;
58+
min-width: 100px;
59+
font-size: .8rem;
60+
font-weight: 600;
61+
background: #fff;
62+
border: 1px solid #ccc;
63+
border-radius: 5px;
64+
}
65+
.cancel {
66+
background-color: #f2f2f2;
67+
}
68+
.active {
69+
font-weight: 700;
70+
}
71+
</style>
72+
73+
<div class="$style.overlay" />
74+
<div class="$style.modal">
75+
<section>
76+
<header class="$style.active">My Modal title</header>
77+
<div class="$style.body">
78+
<Time />
79+
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat, deserunt.</p>
80+
<p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Placeat, deserunt. Lorem ipsum dolor sit amet. </p>
81+
</div>
82+
<footer>
83+
<button class="$style.active">Ok</button>
84+
<button class="$style.cancel">Cancel</button>
85+
</footer>
86+
</section>
87+
</div>

example/webpack/Time.svelte

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<script>
2+
import { onDestroy } from 'svelte';
3+
4+
let date = new Date();
5+
const active = true;
6+
$: time = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`;
7+
8+
const interval = setInterval(() => date = new Date(), 1000);
9+
10+
onDestroy(() => {
11+
clearInterval(interval);
12+
})
13+
</script>
14+
15+
<style>
16+
div {
17+
text-align: right;
18+
font-size: 1.2rem;
19+
font-family: monospace;
20+
}
21+
.bold {
22+
font-weight: bold;
23+
}
24+
</style>
25+
26+
<div
27+
class="$style.datetime"
28+
class:$style.bold={true}>{time}</div>

example/webpack/dist/bundle.js

Lines changed: 149 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/webpack/dist/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>Svelte CSS modules loader</title>
5+
</head>
6+
<body>
7+
<div id="app"></div>
8+
<script src="bundle.js"></script>
9+
</body>
10+
</html>

example/webpack/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import App from './App.svelte'
2+
3+
new App({
4+
target: document.getElementById('app')
5+
})

0 commit comments

Comments
 (0)