Skip to content

Dynamic CSS class names do not work #80

Open
@AleFossati

Description

@AleFossati

When using the directive class={someVariableWithTheClassName}, it fails because the generated css class will have a hash, but the applied class won't.

  1. This works perfectly:
<script lang="ts">
  export let black: boolean;
  export let white: boolean;
</script>

<button class:black={backgroundColor === 'black'} class:white={backgroundColor === 'white'} />

<style module>
  .white {
    background-color: #fff;
  }
  .black {
    background-color: #000;
  }
</style>

Result:
image

  1. This fails because the class "black" will have the hash, but the svelte app applies the name without the hash.
<script lang="ts">
  export let bgColor: 'white' | 'black';
</script>

<button class={bgColor} />

<style module>
  .white {
    background-color: #fff;
  }
  .black {
    background-color: #000;
  }
</style>

Result:
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions