Skip to content

Commit 2aefc54

Browse files
feat(elements): Expose ClassValue (#15035)
* Push * Cleanup * Add changeset * Remove redundant string * Update documentation/docs/03-template-syntax/18-class.md Co-authored-by: Paolo Ricciuti <[email protected]> * Update documentation/docs/03-template-syntax/18-class.md --------- Co-authored-by: Paolo Ricciuti <[email protected]>
1 parent 973b51d commit 2aefc54

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

.changeset/thirty-starfishes-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': minor
3+
---
4+
5+
feat: Expose `ClassValue` from `svelte/elements`

documentation/docs/03-template-syntax/18-class.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ The user of this component has the same flexibility to use a mixture of objects,
7171
</Button>
7272
```
7373

74+
Svelte also exposes the `ClassValue` type, which is the type of value that the `class` attribute on elements accept. This is useful if you want to use a type-safe class name in component props:
75+
76+
```svelte
77+
<script lang="ts">
78+
import type { ClassValue } from 'svelte/elements';
79+
80+
const props: { class: ClassValue } = $props();
81+
</script>
82+
83+
<div class={['original', props.class]}>...</div>
84+
```
85+
7486
## The `class:` directive
7587

7688
Prior to Svelte 5.16, the `class:` directive was the most convenient way to set classes on elements conditionally.

packages/svelte/elements.d.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ export interface HTMLAttributes<T extends EventTarget> extends AriaAttributes, D
741741
accesskey?: string | undefined | null;
742742
autocapitalize?: 'characters' | 'off' | 'on' | 'none' | 'sentences' | 'words' | undefined | null;
743743
autofocus?: boolean | undefined | null;
744-
class?: string | import('clsx').ClassArray | import('clsx').ClassDictionary | undefined | null;
744+
class?: ClassValue | undefined | null;
745745
contenteditable?: Booleanish | 'inherit' | 'plaintext-only' | undefined | null;
746746
contextmenu?: string | undefined | null;
747747
dir?: 'ltr' | 'rtl' | 'auto' | undefined | null;
@@ -1522,7 +1522,7 @@ export interface SvelteWindowAttributes extends HTMLAttributes<Window> {
15221522
export interface SVGAttributes<T extends EventTarget> extends AriaAttributes, DOMAttributes<T> {
15231523
// Attributes which also defined in HTMLAttributes
15241524
className?: string | undefined | null;
1525-
class?: string | import('clsx').ClassArray | import('clsx').ClassDictionary | undefined | null;
1525+
class?: ClassValue | undefined | null;
15261526
color?: string | undefined | null;
15271527
height?: number | string | undefined | null;
15281528
id?: string | undefined | null;
@@ -2059,3 +2059,5 @@ export interface SvelteHTMLElements {
20592059

20602060
[name: string]: { [name: string]: any };
20612061
}
2062+
2063+
export type ClassValue = string | import('clsx').ClassArray | import('clsx').ClassDictionary;

0 commit comments

Comments
 (0)