Skip to content

Auto propagate classes #10733

Open
Open
@PKWadsy

Description

@PKWadsy

Describe the problem

An EXTREMELY common pattern when developing web apps is to give a component a class property and then apply that class to the top-level DOM element of that component. It allows the consumer of the component to customise it, resize it, place it in a different location on the screen etc. This is a much better solution than just wrapping it in a div and styling that because it allows much greater flexibility.

But you often also want to apply your own classes to the same top-level element within the component. This leads to having to write the same ugly boilerplate code in almost all of my components:

<script lang="ts">
	let clazz: string = '';
	export { clazz as class };
</script>

<div class={`${clazz} foo bar ...`}>
    ..rest of my component
</div>

Sure, it's not that much. But, again, I am doing this on almost every component of my app.

Describe the proposed solution

Make all components have a class prop by default which is automatically merged with the top level element of the component.

So something like this

<!-- App.svelte -->
<MyComponent class="foo" />
<!-- MyComponent.svelte -->
<div class="bar bazz"></div>

Would compile to something like this

<div class="foo bar bazz"></div>

This simple feature would make svelte feel much more elegant and possibly save developers a bit of time too.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    cssStuff related to Svelte's built-in CSS handling

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions