Svelte: Can I bind element's class or style?

Created on 23 Dec 2016  Â·  5Comments  Â·  Source: sveltejs/svelte

Hi, can I do something like this?

<div class="a b" bind:class="aComputedClass" bind:style="aComputedStyle"></div>

As I tried this, it doesn't work.
I would expect it to behave like Vue.

Btw, I like the idea of svelte very much!

Most helpful comment

@paulocoghi thanks for replying!

My reply was a bit misleading.
What I meant was that a good number of times when we manipulate doms, we are just trying to manipulate their classes or styles.
So the syntax in my first comment may be more clear and friendly to use :D

All 5 comments

Svelte avoids having lots of special syntax that you have to learn – you can just use regular JavaScript expressions inside tags:

<div class="a b {{foo ? 'foo' : ''}}" style="color: {{bar}};"></div>

Thanks for replying!
But a lot of dom manipulation actually toggles classes or styles,
so it may be meaningful to support such syntax.

@f15gdsy Performance and syntax are not always related. Sometimes we get the wrong impression that if the syntax is too easy, then it's slow.

Since everything in Svelte is compiled, and pretty much everything processes in O (1), it's ready for really huge dom manipulations, and it's amazingly fast (more than Vue).

Don't worry by the syntax, because it's not only easier, it's faster.

@paulocoghi thanks for replying!

My reply was a bit misleading.
What I meant was that a good number of times when we manipulate doms, we are just trying to manipulate their classes or styles.
So the syntax in my first comment may be more clear and friendly to use :D

For those looking now, you can do this https://svelte.dev/tutorial/classes

Was this page helpful?
0 / 5 - 0 ratings

Related issues

st-schneider picture st-schneider  Â·  3Comments

Rich-Harris picture Rich-Harris  Â·  3Comments

noypiscripter picture noypiscripter  Â·  3Comments

ricardobeat picture ricardobeat  Â·  3Comments

mmjmanders picture mmjmanders  Â·  3Comments