Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Provide classname utility for style encapsulation between parent/child component #7776

Copy link
Copy link
Open
@zheeeng

Description

@zheeeng
Issue body actions

Describe the problem

Sometimes style switching logic is placed in the parent component and controlled by switching the classNames. To avoid using the global CSS className, we need a utility to create a unique className and mark it as used.

Describe the proposed solution

There is a proposal API:

// Parent.svelte
<script>
import Child from './Child.svelte'
import { classname } from 'svelte/classname'

const luckyClassName = classname('lucky')
const unluckyClassName = classname('unlucky')

let isLucky = false

</script>

<Child class={isLucky ? luckyClassName : unluckyClassName} on:click={() => isLucky = !isLucky} />

<style>
// mark as used
.lucky {
  color: red;
}

// mark as used
.unlucky {
  color: blue;
}
</style>

Alternatives considered

No

Importance

would make my life easier

FeldrinH, jrmoynihan, I-am-tk, Baxterboom, aolose and 4 more

Metadata

Metadata

Assignees

No one assigned

    Labels

    cssStuff related to Svelte's built-in CSS handlingStuff related to Svelte's built-in CSS handlingfeature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.