{"id":350889,"date":"2021-09-03T07:21:38","date_gmt":"2021-09-03T14:21:38","guid":{"rendered":"https:\/\/css-tricks.com\/?p=350889"},"modified":"2022-02-28T07:41:00","modified_gmt":"2022-02-28T15:41:00","slug":"supercharging-built-in-elements-with-web-components-is-easier-than-you-think","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/supercharging-built-in-elements-with-web-components-is-easier-than-you-think\/","title":{"rendered":"Supercharging Built-In Elements With Web Components “is” Easier Than You Think"},"content":{"rendered":"\n
We\u2019ve already discussed how creating web components is easier than you think<\/a>, but there\u2019s another aspect of the specification that we haven\u2019t discussed yet and it\u2019s a way to customize (nay, supercharge<\/em>) a built-in element. It\u2019s similar to creating fully custom or \u201cautonomous\u201d elements\u2009\u2014\u2009like the Customized built-in elements use an Supercharging a mild-mannered element not only gives us the benefits of the element\u2019s formatting, syntax, and built-in features, but we also get an element that search engines and screen readers already know how to interpret. The screen reader has to guess what\u2019s going on in a It\u2019s important to note here that Safari (and a handful of more niche browsers) only support autonomous elements and not these customized built-in elements. We\u2019ll discuss polyfills for that later.<\/p>\n\n\n\n Until we get the hang of this, let\u2019s start by rewriting the The changes are actually fairly simple. Instead of extending the generic Lastly, we\u2019ll update our HTML from Reminder: If you’re looking at the below in Safari, you won’t see any beautiful web component goodness *shakes fist at Safari* <\/p>\n\n\n\n<zombie-profile><\/code> element from the previous articles\u2014but requires a few differences.<\/p>\n\n\n\n\n\n\n
Article series<\/h2>\n\n\n
is<\/code> attribute to tell the browser that this built-in element is no mild-mannered, glasses-wearing element from Kansas, but is, in fact, the faster than a speeding bullet, ready to save the world, element from planet Web Component. (No offense intended, Kansans. You\u2019re super too.)<\/p>\n\n\n\n
<my-func><\/code> element, but has some idea of what\u2019s happening in a
<nav is=\"my-func\"><\/code> element. (If you have func, please, for the love of all that is good, don\u2019t<\/em> put it in an element. Think of the children.)<\/p>\n\n\n\n
<apocalyptic-warning><\/code> element we created back in our first article<\/a> as a customized built-in element. (The code is also available in the CodePen demo<\/a>.)<\/p>\n\n\n\n
HTMLElement<\/code>, we\u2019ll extend a specific element, in this case the
<div><\/code> element which has the class
HTMLDivElement<\/code>. We\u2019ll also add a third argument to the
customElements.defines<\/code> function:
{extends: 'div'}<\/code>.<\/p>\n\n\n\n
customElements.define(\n \"apocalyptic-warning\",\n class ApocalypseWarning extends HTMLDivElement {\n constructor() {\n super();\n let warning = document.getElementById(\"warningtemplate\");\n let mywarning = warning.content;\n\n const shadowRoot = this.attachShadow({ mode: \"open\" }).appendChild(\n mywarning.cloneNode(true)\n );\n }\n },\n { extends: \"div\" }\n);<\/code><\/pre>\n\n\n\n
<apocalyptic-warning><\/code> tags to
<div><\/code> tags that include an
is<\/code> attribute set to \u201capocalyptic-warning\u201d like this:<\/p>\n\n\n\n
<div is=\"apocalyptic-warning\">\n <span slot=\"whats-coming\">Undead<\/span>\n<\/div><\/code><\/pre>\n\n\n\n