-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Unable to define some attributes on Custom Elements #3919
Comments
Svelte source code related to the issue: https://github.com/sveltejs/svelte/blob/master/src/runtime/internal/dom.ts
|
Explanation here #875 |
I understand why it was implemented this way. But in my opinion it's wrong. As I know, no other frameworks use similar logic for good reasons:
It's basically makes usage of Svelte dangerous and not future-proof. |
tl;dr custom elements are a badly designed primitive and I can't recommend them in good conscience. There are a few different strategies that frameworks can use to handle this problem:
None of these options are ideal, but it's what's been forced on us by the design of custom elements. (Imagine if we'd designed them in such a way that attributes were simply the inputs to a function that generated initial props when a custom element was instantiated, and from that point forward you only had to worry about props? We could have avoided all this silly confusion.) You're right that it's not future-proof. Unfortunately, custom elements are themselves not future-proof — any new attribute or property that applies to Unfortunately I just don't think there's a good solution to this. Which, along with other serious shortcomings (global namespace, no SVG support, no SSR — and no, declarative shadow DOM won't fix that), is why I personally advise against their use. A possible workaround in this case would be to implement class Foo extends HTMLElement {
get prefix() {
return null;
}
set prefix(value) {
this.setAttribute('prefix', value);
}
} |
Thanks for your answer. I really appreciate all the work you've done on Svelte & Rollup projects. But I don't agree with the thesis that "custom elements are a badly designed primitive". It just wasn't meant for the task the most developers try to solve with it. But it doesn't mean it's bad, useless or something. I created entire Design System and Framework based on native API and in our company we are very VERY happy with it. But in the same time we understand that our approach is barely popular. If you're interested and have like 10 minutes I can show you some interesting applications of Custom Elements on call. As for this issue, I understand that there is a very little chance something can be changed here. But here is my recommendation:
At least we should discuss this options. For those who are faced such problem right now I would recommend to create standalone utility to fix 3rd party Custom Elements. The following instructions are only for Svelte users: Get a list of all props that should be redeclared if you want to use it with CE:
Get a list of all global attributes that are supposed to be untouched by CE logic:
Get a list of CE attributes:
So we can create the utility that will add getter/setter for every CE attribute only if it's needed:
Don't forget to make a camelCase transformation for attribute name: It's won't make any Custom Element 100% compatible with Svelte, but it will cover the most of the cases. Good luck! |
@Rich-Harris I think the approach "Use some heuristic for determining when to use props and when to use attributes" is not a good choice. It basically gives users no control whatsoever over the binding process, and this control can sometimes be crucial. Correct me if I am wrong, but I have the feeling that your heuristic solution depends on the authors for providing a specific implementation policy for their w.c., but leave users completely powerless in cases where the authors, for whatever reason, chose to rely on an attribute instead of an existing field. My suggestion: Let the gross syntax be the exception, where users would need explicit control.
This way, we let things as they are for 99% of the use cases, but we leave the door open for any use case that will require more explicit control. Thanks in advance. |
Ok, I think I found a workaround to achieve explicit control on the attribute/prop issue.
It works fine (including updating when the values change).
and achieve the same result without the need of creating arrays and implementing custom util functions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been closed as it was previously marked as stale and saw no subsequent activity. |
Describe the bug
It's unable to define some attributes on 3rd-party Custom Elements.
To Reproduce
Open REPL
I created the simplest Custom Element that do
console.log
of attributeprefix
when connected. But Svelte can't create such attribute because there is a read-only propertyprefix
on HTMLElement prototype. Actually Svelte won't define any attribute if there is a property with the exact name. Instead it will try to assign the value directly to the property.Expected behavior
It should be possible to create ANY attribute with ANY value that does not contradict the standard.
Information about your Svelte project:
Your browser and the version: Chrome 78.0.3904.97
Your operating system: MacOS 10.15.1
Svelte version: 3.14.0
I use Rollup
Severity
I create a UI Framework that based entirely on Custom Elements. That bug is blocking me to use Svelte with it. I love Svelte but I really upset that it contradicts the standard.
The text was updated successfully, but these errors were encountered: