-
-
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
Custom element boolean attribute incorrectly converted to true
#10871
Comments
The problem is the code generates for <div class="page">
<my-component a-boolean={x} />
</div> What it should be doing: div = element("div");
my_component = element("my-component");
set_custom_element_data(my_component, "a-boolean", x);
attr(div, "class", "page"); What is actually does: div = element("div");
div.innerHTML = `<my-component a-boolean="${false}"></my-component>`;
attr(div, "class", "page"); (you can see this in the js output tab of the REPL) The fix therefore involves bailing out of the "can use innerHTML here" optimization. The workaround is to put <script>
let x = false;
</script>
<div class="page">
<my-component a-boolean={x} />
</div> ...but that doesn't fix the issue either, because there's a second bug: This bug is present in both Svelte 4 and 5. |
Thanks for your replying. I tried this workaround, but the |
I'm sorry, I expressed myself poorly there - the workaround would only work if the second bug wasn't there. So: the second bug is a blocker, which needs fixing either way, and once that's done, you could be able to use the workaround. I adjusted the wording in my comment a bit. |
Checking this again - turns out we do make a conversion inside the |
I have updated my repro repository with additional usage site demos using Vue and React to provide further context. It appears that when rendering custom elements, all three frameworks exhibit similar behavior regarding the handling of boolean attribute values. Specifically, they convert boolean values to the strings "true" and "false" when setting these attributes, which may not align with the spec 🤔. I am uncertain whether this behavior will change in React 19. |
@dummdidumm does the core team have any updates to a possible workaround or fix? I'm currently attempting to upgrade my custom elements library from Svelte 3 to Svelte 4 and running into the same exact problem. |
One workaround is to use an action which is a) only used on the client (no risk of server output being clutter) b) you can set it as a property however you want in there. You could create a generic "set as property" action which takes the property name and the property value to set it accordingly. In Svelte 5 the "set as property" case is fixed, it will not set it as an attribute (and #12624 could help further to specificy exactly how you want to set it). |
Describe the bug
When passing a boolean attribute to a custom element, the prop value is alway
true
.Reproduction
https://github.com/CLDXiang/svelte-custom-elements-attribute-bug
In
demo/App.svelte
:But the rendered
aBoolean
value istrue
.Logs
No response
System Info
Severity
annoyance
The text was updated successfully, but these errors were encountered: