Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
<div class="container">
Flip the card
<button
class="card"
class:flipped={flipped}
class={["card", { flipped }]}
onclick={() => flipped = !flipped}
>
<div class="front">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: The class directive
title: The class attribute
---

Like any other attribute, you can specify classes with a JavaScript attribute. Here, we could add a `flipped` class to the card:
Expand All @@ -14,15 +14,16 @@ Like any other attribute, you can specify classes with a JavaScript attribute. H

This works as expected — if you click on the card now, it'll flip.

We can make it nicer though. Adding or removing a class based on some condition is such a common pattern in UI development that Svelte includes a special directive to simplify it:
We can make it nicer though. Adding or removing a class based on some condition is such a common pattern in UI development that Svelte allows you to pass an object or array that is converted to a string by [clsx](https://github.com/lukeed/clsx).

```svelte
/// file: App.svelte
<button
class="card"
+++class:flipped={flipped}+++
+++class={["card", { flipped }]}+++
onclick={() => flipped = !flipped}
>
```

This directive means 'add the `flipped` class whenever `flipped` is truthy'.
This means 'always add the `card` class, and add the `flipped` class whenever `flipped` is truthy'.

For more examples of how to combine conditional classes, [consult the `class` documentation](/docs/svelte/class).

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ul class="todos">
{#each todos as todo}
<li class:done={todo.done}>
<li class={{ done: todo.done }}>
<input
type="checkbox"
checked={todo.done}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ul class="todos">
{#each todos as todo}
<li class:done={todo.done}>
<li class={{ done: todo.done }}>
<input
type="checkbox"
bind:checked={todo.done}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ You can bind to properties inside an `each` block.
```svelte
/// file: App.svelte
{#each todos as todo}
<li class:done={todo.done}>
<li class={{ done: todo.done }}>
<input
type="checkbox"
+++bind:+++checked={todo.done}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
</script>

<div class="player" class:paused>
<div class={['player', { paused }]}>
<button
class="play"
aria-label={paused ? 'play' : 'pause'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
</script>

<div class="player" class:paused>
<div class={['player', { paused }]}>
<audio
{src}
bind:currentTime={time}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ First, add the `<audio>` element along with its bindings (we'll use the shorthan

```svelte
/// file: AudioPlayer.svelte
<div class="player" class:paused>
<div class={['player', { paused }]}>
+++ <audio
{src}
bind:currentTime={time}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ul class="todos">
{#each todos as todo (todo)}
<li
class:done={todo.done}
class={{ done: todo.done }}
>
<label>
<input type="checkbox" bind:checked={todo.done}/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ul class="todos">
{#each todos as todo (todo)}
<li
class:done={todo.done}
class={{ done: todo.done }}
in:receive={{ key: todo.id }}
out:send={{ key: todo.id }}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Then, add them to the `<li>` element, using the `todo.id` property as a key to m
```svelte
/// file: TodoList.svelte
<li
class:done={todo.done}
class={{ done: todo.done }}
+++in:receive={{ key: todo.id }}+++
+++out:send={{ key: todo.id }}+++
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<ul class="todos">
{#each todos as todo (todo)}
<li
class:done={todo.done}
class={{ done: todo.done }}
in:receive={{ key: todo.id }}
out:send={{ key: todo.id }}
animate:flip={{ duration: 200 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Then add it to the `<li>` elements:
```svelte
/// file: TodoList.svelte
<li
class:done={todo.done}
class={{ done: todo.done }}
in:receive={{ key: todo.id }}
out:send={{ key: todo.id }}
+++animate:flip+++
Expand All @@ -35,7 +35,7 @@ The movement is a little slow in this case, so we can add a `duration` parameter
```svelte
/// file: TodoList.svelte
<li
class:done={todo.done}
class={{ done: todo.done }}
in:receive={{ key: todo.id }}
out:send={{ key: todo.id }}
animate:flip+++={{ duration: 200 }}+++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

<!-- creative commons BY-NC http://www.pngall.com/kitten-png/download/7247 -->
<img
class:curious={hereKitty}
class={{ curious: hereKitty }}
alt="Kitten wants to know what's going on"
src={kitten}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<!-- creative commons BY-NC http://www.pngall.com/kitten-png/download/7247 -->
<img
class:curious={hereKitty}
class={{ curious: hereKitty }}
alt="Kitten wants to know what's going on"
src={kitten}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
</script>

<div class="player" class:paused>
<div class={['player', { paused }]}>
<audio
{src}
bind:currentTime={time}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
}
</script>

<div class="player" class:paused>
<div class={['player', { paused }]}>
<audio
{src}
bind:currentTime={time}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}
</script>

<div class="player" class:paused>
<div class={['player', { paused }]}>
<audio
{src}
bind:currentTime={time}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
}
</script>

<button class:expanded onclick={toggle}>{name}</button>
<button class={{ expanded }} onclick={toggle}>{name}</button>

{#if expanded}
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
}
</script>

<button class:expanded onclick={toggle}>{name}</button>
<button class={{ expanded }} onclick={toggle}>{name}</button>

{#if expanded}
<ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</script>

<nav
class:has-color={!!page.data.color}
class={[page.data.color && 'has-color']}
style:background={page.data.color ?? 'var(--bg-2)'}
>
<a href="/">home</a>
Expand All @@ -21,4 +21,4 @@
nav.has-color a {
color: white;
}
</style>
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</script>

<nav
class:has-color={!!page.data.color}
class={[page.data.color && 'has-color']}
style:background={page.data.color ?? 'var(--bg-2)'}
>
<a href="/">home</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ We can now use the `component` returned from these `load` functions like any oth
```svelte
/// file: src/routes/+layout.svelte
<nav
class:has-color={!!page.data.color}
class={[page.data.color && 'has-color']}
style:background={page.data.color ?? 'var(--bg-2)'}
>
<a href="/">home</a>
Expand Down
Loading