You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since `class` itself takes these values, you can use the same syntax on component properties when forwarding those to the `class` attribute.
31
+
32
+
```svelte
33
+
<!--- file: App.svelte --->
34
+
<script>
35
+
import Button from './Button.svelte';
36
+
let useTailwind = $state(false);
37
+
</script>
38
+
39
+
<Button
40
+
onclick={() => useTailwind = true}
41
+
class={{ 'bg-blue-700 sm:w-1/2': useTailwind }}
42
+
>
43
+
Give in
44
+
</Button>
45
+
```
46
+
47
+
```svelte
48
+
<!--- file: Button.svelte --->
49
+
<script>
50
+
let { children, ...rest } = $props();
51
+
</script>
52
+
53
+
<!-- rest contains class, and the value is appropriately stringified -->
54
+
<button {...rest}>
55
+
{@render children()}
56
+
</button>
57
+
```
58
+
59
+
Under the hood this is using [`clsx`](https://github.com/lukeed/clsx), so if you need more details on the syntax, you can visit its documentation.
60
+
61
+
## class:
62
+
5
63
The `class:` directive is a convenient way to conditionally set classes on elements, as an alternative to using conditional expressions inside `class` attributes:
6
64
7
65
```svelte
@@ -21,3 +79,5 @@ Multiple `class:` directives can be added to a single element:
0 commit comments