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

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy