-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
class:
directives do not work when used on elements with both bind:this
and prop spreading
#2707
Comments
Hi @mtlewis! Thank you so much for this great research and example. The reason why it's not working is because the component fragment change handler contains this code: p: function update(changed, ctx) {
// ...
set_attributes(a, get_spread_update(a_levels, [
{ other: "props" },
{ href: "/foo" },
{ class: "svelte-1qq9alg" }
]));
if (changed.primary) {
toggle_class(a, "primary", primary);
}
}
TL;DR: I think it boils down to a bug in how Svelte generates code for The reason why it works when you...
set_attributes(a, get_spread_update(a_levels, [
(changed.primary) && { class: "" + (primary ? 'primary' : '') + " svelte-1qq9alg" },
{ other: 'props' },
{ href: "/foo" }
])); |
Is this the same as the following error? Error: ValidationError: Classes can only be applied to DOM elements, not components (56:31)
54: <div class="btn-group" role="group">
55: {#each Object.keys(types) as type}
56: <Button on:click={setType} class:active="{types[type] === endpoint.type}" value={types[type]}>{type}</Button>
^
57: {/each}
58: </div>
at preproces[omitted] Essentially, I'm working on Bootstrap components and need the button to be "active" (have the class "active") when the type matches. Also, for some reason, I can't get it to work if I use the computed class: <Button on:click={setType} class="{types[type] === endpoint.type ? 'active' : ''}" value={types[type]}>{type}</Button> Should I open a new issue for this? |
@TheBosZ Does it make sense to pass the class itself into a component when CSS is component scoped? 🤔
|
Unfortunately, this is for a Bootstrap library where we need to allow any classes to be set on the component itself. It looks like classes on components don't really work the way I would expect. I can open a new ticket with it. |
In that case, I believe the most viable option for you would be to implement it similarly to how React handles passing down classes to components:
You have to use |
To try to put it in my own words, the special Is that correct? If that's the case, what's the point of the docs mentioning how to use // you can use export { ... as ... } to have
// props whose names are reserved keywords
let clazz;
export { clazz as class }; |
@varholak-peter @TheBosZ I don't think this discussion is relevant for the original issue. Consider opening up a new issue to discuss the possibility of adding support for the |
Sorry to pollute this ticket! I figured it out: I need to mark things as reactive. Still not used to that! |
Fixed back in 3.6.8 via #3242. |
@Conduitry I think this should be reopened - I just retested in 3.6.8, and the issue is still occurring in my example above. |
Seems to still be broken in latest master. I just opened #3668, which adds a failing test for this issue. |
I've noticed that when elements have the
bind:this
directive as well as props spread onto the element,class:
directives do not result in the appropriate class being applied. This can be worked around by using a class attribute with an expression (e.g.class={primary ? 'primary' : ''} works, but
class:primarydoes not
).Here's a demonstration of the issue. You'll note that the anchor will be green as expected if you do any of the following three things:
bind:this={anchor}
),{...{ other: 'props' }}
), orclass={primary ? 'primary' : ''}
.Thanks for all the great work you've done on this library!
The text was updated successfully, but these errors were encountered: