Skip to content

Commit 13a22b2

Browse files
Sync svelte docs (#1065)
sync svelte docs Co-authored-by: Rich-Harris <[email protected]>
1 parent 97ad9c1 commit 13a22b2

File tree

9 files changed

+113
-38
lines changed

9 files changed

+113
-38
lines changed

apps/svelte.dev/content/docs/svelte/03-template-syntax/16-class.md

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
---
2+
NOTE: do not edit this file, it is generated in apps/svelte.dev/scripts/sync-docs/index.ts
3+
title: class
4+
---
5+
6+
There are two ways to set classes on elements: the `class` attribute, and the `class:` directive.
7+
8+
## Attributes
9+
10+
Primitive values are treated like any other attribute:
11+
12+
```svelte
13+
<div class={large ? 'large' : 'small'}>...</div>
14+
```
15+
16+
> [!NOTE]
17+
> For historical reasons, falsy values (like `false` and `NaN`) are stringified (`class="false"`), though `class={undefined}` (or `null`) cause the attribute to be omitted altogether. In a future version of Svelte, all falsy values will cause `class` to be omitted.
18+
19+
### Objects and arrays
20+
21+
Since Svelte 5.16, `class` can be an object or array, and is converted to a string using [clsx](https://github.com/lukeed/clsx).
22+
23+
If the value is an object, the truthy keys are added:
24+
25+
```svelte
26+
<script>
27+
let { cool } = $props();
28+
</script>
29+
30+
<!-- results in `class="cool"` if `cool` is truthy,
31+
`class="lame"` otherwise -->
32+
<div class={{ cool, lame: !cool }}>...</div>
33+
```
34+
35+
If the value is an array, the truthy values are combined:
36+
37+
```svelte
38+
<!-- if `faded` and `large` are both truthy, results in
39+
`class="saturate-0 opacity-50 scale-200"` -->
40+
<div class={[faded && 'saturate-0 opacity-50', large && 'scale-200']}>...</div>
41+
```
42+
43+
Note that whether we're using the array or object form, we can set multiple classes simultaneously with a single condition, which is particularly useful if you're using things like Tailwind.
44+
45+
Arrays can contain arrays and objects, and clsx will flatten them. This is useful for combining local classes with props, for example:
46+
47+
```svelte
48+
<!--- file: Button.svelte --->
49+
<script>
50+
let props = $props();
51+
</script>
52+
53+
<button {...props} class={['cool-button', props.class]}>
54+
{@render props.children?.()}
55+
</button>
56+
```
57+
58+
The user of this component has the same flexibility to use a mixture of objects, arrays and strings:
59+
60+
```svelte
61+
<!--- file: App.svelte --->
62+
<script>
63+
import Button from './Button.svelte';
64+
let useTailwind = $state(false);
65+
</script>
66+
67+
<Button
68+
onclick={() => useTailwind = true}
69+
class={{ 'bg-blue-700 sm:w-1/2': useTailwind }}
70+
>
71+
Accept the inevitability of Tailwind
72+
</Button>
73+
```
74+
75+
## The `class:` directive
76+
77+
Prior to Svelte 5.16, the `class:` directive was the most convenient way to set classes on elements conditionally.
78+
79+
```svelte
80+
<!-- These are equivalent -->
81+
<div class={{ cool, lame: !cool }}>...</div>
82+
<div class:cool={cool} class:lame={!cool}>...</div>
83+
```
84+
85+
As with other directives, we can use a shorthand when the name of the class coincides with the value:
86+
87+
```svelte
88+
<div class:cool class:lame={!cool}>...</div>
89+
```
90+
91+
> [!NOTE] Unless you're using an older version of Svelte, consider avoiding `class:`, since the attribute is more powerful and composable.

apps/svelte.dev/content/docs/svelte/98-reference/.generated/client-errors.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,19 @@ A component is attempting to bind to a non-bindable property `%key%` belonging t
2121
### component_api_changed
2222

2323
```
24-
%parent% called `%method%` on an instance of %component%, which is no longer valid in Svelte 5. See https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes for more information
24+
%parent% called `%method%` on an instance of %component%, which is no longer valid in Svelte 5
2525
```
2626

27+
See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more information.
28+
2729
### component_api_invalid_new
2830

2931
```
30-
Attempted to instantiate %component% with `new %name%`, which is no longer valid in Svelte 5. If this component is not under your control, set the `compatibility.componentApi` compiler option to `4` to keep it working. See https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes for more information
32+
Attempted to instantiate %component% with `new %name%`, which is no longer valid in Svelte 5. If this component is not under your control, set the `compatibility.componentApi` compiler option to `4` to keep it working.
3133
```
3234

35+
See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more information.
36+
3337
### derived_references_self
3438

3539
```

apps/svelte.dev/content/docs/svelte/98-reference/.generated/client-warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Your `console.%method%` contained `$state` proxies. Consider using `$inspect(...
5252
5353
When logging a [proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), browser devtools will log the proxy itself rather than the value it represents. In the case of Svelte, the 'target' of a `$state` proxy might not resemble its current value, which can be confusing.
5454
55-
The easiest way to log a value as it changes over time is to use the [`$inspect`](https://svelte.dev/docs/svelte/$inspect) rune. Alternatively, to log things on a one-off basis (for example, inside an event handler) you can use [`$state.snapshot`](https://svelte.dev/docs/svelte/$state#$state.snapshot) to take a snapshot of the current value.
55+
The easiest way to log a value as it changes over time is to use the [`$inspect`](/docs/svelte/$inspect) rune. Alternatively, to log things on a one-off basis (for example, inside an event handler) you can use [`$state.snapshot`](/docs/svelte/$state#$state.snapshot) to take a snapshot of the current value.
5656
5757
### event_handler_invalid
5858

apps/svelte.dev/content/docs/svelte/98-reference/.generated/compile-warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Enforce that `autofocus` is not used on elements. Autofocusing elements can caus
6262
### a11y_click_events_have_key_events
6363

6464
```
65-
Visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as `<button type="button">` or `<a>` might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details
65+
Visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as `<button type="button">` or `<a>` might be more appropriate
6666
```
6767

6868
Enforce that visible, non-interactive elements with an `onclick` event are accompanied by a keyboard event handler.

apps/svelte.dev/content/docs/svelte/98-reference/20-svelte.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ constructor(options: ComponentConstructorOptions<Properties<Props, Slots>>);
8080

8181
- <span class="tag deprecated">deprecated</span> This constructor only exists when using the `asClassComponent` compatibility helper, which
8282
is a stop-gap solution. Migrate towards using `mount` instead. See
83-
[migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info.
83+
[migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more info.
8484

8585
</div>
8686

@@ -98,7 +98,7 @@ $destroy(): void;
9898
<div class="ts-block-property-bullets">
9999

100100
- <span class="tag deprecated">deprecated</span> This method only exists when using one of the legacy compatibility helpers, which
101-
is a stop-gap solution. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
101+
is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
102102
for more info.
103103

104104
</div>
@@ -120,7 +120,7 @@ $on<K extends Extract<keyof Events, string>>(
120120
<div class="ts-block-property-bullets">
121121

122122
- <span class="tag deprecated">deprecated</span> This method only exists when using one of the legacy compatibility helpers, which
123-
is a stop-gap solution. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
123+
is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
124124
for more info.
125125

126126
</div>
@@ -139,7 +139,7 @@ $set(props: Partial<Props>): void;
139139
<div class="ts-block-property-bullets">
140140

141141
- <span class="tag deprecated">deprecated</span> This method only exists when using one of the legacy compatibility helpers, which
142-
is a stop-gap solution. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
142+
is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
143143
for more info.
144144

145145
</div>
@@ -576,13 +576,13 @@ interface Component<
576576
): {
577577
/**
578578
* @deprecated This method only exists when using one of the legacy compatibility helpers, which
579-
* is a stop-gap solution. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
579+
* is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
580580
* for more info.
581581
*/
582582
$on?(type: string, callback: (e: any) => void): () => void;
583583
/**
584584
* @deprecated This method only exists when using one of the legacy compatibility helpers, which
585-
* is a stop-gap solution. See [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
585+
* is a stop-gap solution. See [migration guide](https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes)
586586
* for more info.
587587
*/
588588
$set?(props: Partial<Props>): void;

apps/svelte.dev/content/docs/svelte/98-reference/30-compiler-warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Enforce that `autofocus` is not used on elements. Autofocusing elements can caus
8383
### a11y_click_events_have_key_events
8484

8585
```
86-
Visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as `<button type="button">` or `<a>` might be more appropriate. See https://svelte.dev/docs/accessibility-warnings#a11y-click-events-have-key-events for more details
86+
Visible, non-interactive elements with a click event must be accompanied by a keyboard event handler. Consider whether an interactive element such as `<button type="button">` or `<a>` might be more appropriate
8787
```
8888

8989
Enforce that visible, non-interactive elements with an `onclick` event are accompanied by a keyboard event handler.

apps/svelte.dev/content/docs/svelte/98-reference/30-runtime-errors.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ A component is attempting to bind to a non-bindable property `%key%` belonging t
2828
### component_api_changed
2929

3030
```
31-
%parent% called `%method%` on an instance of %component%, which is no longer valid in Svelte 5. See https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes for more information
31+
%parent% called `%method%` on an instance of %component%, which is no longer valid in Svelte 5
3232
```
3333

34+
See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more information.
35+
3436
### component_api_invalid_new
3537

3638
```
37-
Attempted to instantiate %component% with `new %name%`, which is no longer valid in Svelte 5. If this component is not under your control, set the `compatibility.componentApi` compiler option to `4` to keep it working. See https://svelte.dev/docs/svelte/v5-migration-guide#Components-are-no-longer-classes for more information
39+
Attempted to instantiate %component% with `new %name%`, which is no longer valid in Svelte 5. If this component is not under your control, set the `compatibility.componentApi` compiler option to `4` to keep it working.
3840
```
3941

42+
See the [migration guide](/docs/svelte/v5-migration-guide#Components-are-no-longer-classes) for more information.
43+
4044
### derived_references_self
4145

4246
```

apps/svelte.dev/content/docs/svelte/98-reference/30-runtime-warnings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Your `console.%method%` contained `$state` proxies. Consider using `$inspect(...
5959
6060
When logging a [proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy), browser devtools will log the proxy itself rather than the value it represents. In the case of Svelte, the 'target' of a `$state` proxy might not resemble its current value, which can be confusing.
6161
62-
The easiest way to log a value as it changes over time is to use the [`$inspect`](https://svelte.dev/docs/svelte/$inspect) rune. Alternatively, to log things on a one-off basis (for example, inside an event handler) you can use [`$state.snapshot`](https://svelte.dev/docs/svelte/$state#$state.snapshot) to take a snapshot of the current value.
62+
The easiest way to log a value as it changes over time is to use the [`$inspect`](/docs/svelte/$inspect) rune. Alternatively, to log things on a one-off basis (for example, inside an event handler) you can use [`$state.snapshot`](/docs/svelte/$state#$state.snapshot) to take a snapshot of the current value.
6363
6464
### event_handler_invalid
6565

0 commit comments

Comments
 (0)