Skip to content

Dynamic conditional CSS classes #7294

@janosh

Description

@janosh

Describe the problem

In components that want to support use with CSS frameworks like Tailwind, it would be nice to allow users to pass in custom class names as props which are applied depending on an internal condition in the component:

Describe the proposed solution

MyComponent.svelte:

export let dynamicClass = ''

let cond = false

<button class:{dynamicClass}={cond} on:click={() => cond = true}>click me</button>

Alternatives considered

This already works

<button class={cond ? dynamicClass : ''} on:click={() => cond = true}>click me</button>

but it looks cleaner here than in the real world where you might already have class attr in which case you need to manually combine classes before applying.

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions