Detect if Element is a Web Component

By  on  

I've advocated for web components since before they became a spec, mostly inspired by the Dojo Toolkit's dijit framework. Empowering first class JavaScript widgets, as opposed to a mess of DIVs and templates, always made the most sense. Now that web components exist, and awesome frameworks like Ionic are based on them, I wanted to discover how to detect a web component, as opposed to a regular HTML element, with JavaScript. As it turns out, it's much easier than you'd think.

Assuming you have a reference to an element, it's as easy as detecting a dash in the element's tag:

function isWebComponent(element) {
  return element.tagName.includes("-");
}

The web component spec requires a dash in the HTMLElement's tagName, so detecting a web component is essentially nailed down to a string comparison.

If you haven't played with web components, I really hope you find the time. Having lived through decades of "widgets" and over-nesting of arbitrary DIVs and unreadable code, I've learned to appreciate these gems!

Recent Features

  • By
    Write Better JavaScript with Promises

    You've probably heard the talk around the water cooler about how promises are the future. All of the cool kids are using them, but you don't see what makes them so special. Can't you just use a callback? What's the big deal? In this article, we'll...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    Reverse Element Order with CSS Flexbox

    CSS is becoming more and more powerful these days, almost to the point where the order of HTML elements output to the page no longer matters from a display standpoint -- CSS lets you do so much that almost any layout, large or small, is possible.  Semantics...

  • By
    pointer Media Query

    As more devices emerge and differences in device interaction are implemented, the more important good CSS code will become.  In order to write good CSS, we need some indicator about device capabilities.  We've used CSS media queries thus far, with checks for max-width and pixel ratios.

Discussion

  1. Wouldn’t that just check whether it is a custom element? To my knowledge, custom elements can live on their own and do not have to be actual web components.

    • Šime Vidas

      Could you explain the difference between a custom element and a web component?

    • Jv

      My exact thought, Catalin! This isn’t really robust enough..

  2. Yaisiel

    I feel like that will not work if you declare your web component as a “customized built-in” (https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements#Customized_built-in_elements). That type of component is built on top of an existing element (like a for example) and the tag names don’t contain hyphens. I’m not sure, however, how many people would follow that approach in practice. It might be that, for the majority of the cases, checking for hyphens is enough.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!