🎉 New Year Sale! Now through the New Year, get 40% off your first year of Lean Web Club membership. Level-up as a developer. Click here to learn more. One of my favorite ways to customize Web Components is with custom attributes. Since writing that last article, I've gotten some questions around using custom attributes in Web Components, and I've also see their popularity increase a fair bit. Let's talk about some caveats and things to know. Why custom attributes?Probably the most common question I get is why use a custom attribute instead of Personally, I think it provides a nicer authoring experience. Prefixing every customization with This is, of course, entirely subjective! Linters won't like themHTML linters will typically tell you that custom attributes are invalid. Every single browser I've used handles them just fine, and you can get, set, and update them with JavaScript just like you would any other attribute. If having a completely error and warning free linting experience is your end goal, don't use custom attributes. Otherwise, they'll work just fine despite what the linter says. You could run into future collisionsPerhaps the biggest danger of custom attributes is that a future update to the web platform adds an attribute whose name is the same as one you've used in your Web Components. For example, if One way to reduce the likelihood of this is to always use hyphenated attributes (in the same way custom elements must always have a dash). I sometimes use things like This can erode the benefits of using custom attributes traditional data attributes, but it can also make it more clear which attributes are specific to your Web Component. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] Custom attributes in Web Components"