Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Single file Web Component
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<title>Single File Web Component</title>
<template id=single-file>
h1 {
color: red;
<h1>Hello World</h1>
<script type=module>
class HelloWorld extends HTMLElement {
constructor () {
const template = document.getElementById('single-file')
this.attachShadow({ mode: 'open' })
connectedCallback () {
console.log('Why hello there 👋')
customElements.define('hello-world', HelloWorld)
const sf = document.getElementById('single-file')
Copy link

simonw commented Oct 28, 2021

This is brilliant! I spent some time playing with it to figure out exactly how it works and an annotated version with my notes here:

Copy link

Theorized an alternate version as well:

Copy link

@simonw Really like your annotated version!

Copy link

kristoferjoseph commented Nov 1, 2021

@Westbrook I was thinking about that! Just wasn't sure when it would be generally available in all browsers.
Any idea?
This was the document I saw btw:

Looks like Chrome and possibly firefox but probably not safari :?

Copy link

The polyfill is so small that it’s hard to say “no”, if this use case suits you. We just listed API as “critical” as part of the Web Component Community Group’s presentation to TPAC: so hopefully soon. You can check out the issues linked there for any browser specific questions or worries on this one.

Copy link

@Westbrook That is very promising indeed. Thanks for the info!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment