Skip to content

Commit 7a63e39

Browse files
committed
docs: add objects and arrays in readme
1 parent aa37089 commit 7a63e39

File tree

1 file changed

+68
-3
lines changed

1 file changed

+68
-3
lines changed

README.md

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ for `svelte 4` and below, use version 2 of the preprocessor.
1212

1313
- [Usage](#usage)
1414
- [Approach](#approach)
15+
- [Class objects and arrays](#class-object-and-arrays)
1516
- [Class directive](#class-directive)
1617
- [Local selector](#local-selector)
1718
- [CSS binding](#css-binding)
@@ -109,6 +110,70 @@ _transformed to_
109110
</style>
110111
```
111112

113+
### Class objects and arrays
114+
115+
#### Object with thruthy values
116+
117+
```html
118+
<script>
119+
let active = $state(true);
120+
</script>
121+
122+
<div class={{ active, red: !active, 'bold': active }}>...</div>
123+
124+
<style module>
125+
.active { color: green; }
126+
.red { color: red; }
127+
.bold { font-weight: bold; }
128+
</style>
129+
```
130+
131+
*generating*
132+
133+
```html
134+
<div class="active-aft3ew bold-sfkje6">...</div>
135+
<!-- OR -->
136+
<div class="red-bft3ed">...</div>
137+
138+
<style>
139+
.active-aft3ew { color: green; }
140+
.red-bft3ed { color: red; }
141+
.bold-sfkje6 { font-weight: bold; }
142+
</style>
143+
```
144+
145+
#### Array with thruthy values
146+
147+
```html
148+
<script>
149+
let active = $state(true);
150+
</script>
151+
152+
<div class={[ active && 'green bold', 'italic', !active && 'red' ]}>...</div>
153+
154+
<style module>
155+
.green { color: green; }
156+
.red { color: red; }
157+
.bold { font-weight: bold; }
158+
.italic { font-style: italic; }
159+
</style>
160+
```
161+
162+
*generating*
163+
164+
```html
165+
<div class="green-dhy6wu bold-uytsge italic-b65wfq">...</div>
166+
<!-- OR -->
167+
<div class="red-uyqrw4 italic-b65wfq">...</div>
168+
169+
<style>
170+
.green-dhy6wu { color: green; }
171+
.red-uyqrw4 { color: red; }
172+
.bold-uytsge { font-weight: bold; }
173+
.italic-b65wfq { font-style: italic; }
174+
</style>
175+
```
176+
112177
### Class directive
113178

114179
Toggle a class on an element.
@@ -357,11 +422,11 @@ CSS Modules allows you to pass a scoped classname to a child component giving th
357422
```html
358423
<!-- Child Component Button.svelte -->
359424
<script>
360-
let { class: className } = $props();
425+
let props = $props();
361426
</script>
362427

363-
<button class="btn {className}">
364-
<slot />
428+
<button class={['btn', props.class]}>
429+
{@render props.children?.()}
365430
</button>
366431

367432
<style module>

0 commit comments

Comments
 (0)