Skip to content

Instantly share code, notes, and snippets.

@sc0ttj
Forked from malisipi/web-component-starter.js
Created September 19, 2025 17:11
Show Gist options
  • Select an option

  • Save sc0ttj/1347397fbad1a0367802d2120d16f612 to your computer and use it in GitHub Desktop.

Select an option

Save sc0ttj/1347397fbad1a0367802d2120d16f612 to your computer and use it in GitHub Desktop.
You can use the gist to init your web component.
"use strict";
// Licensed by MIT License
// You can use the gist to init your web component.
customElements.define('web-component',
class extends HTMLElement {
#root;
constructor() {
super();
this.attachShadow({mode: "open"});
this.#root = document.createElement("div");
this.#root.className = "root";
this.shadowRoot.append(document.createElement('slot'));
this.shadowRoot.append(this.#root);
let style = document.createElement("style");
style.textContent = `
:host {
position: relative;
display: block;
width: 400px;
height: 200px;
background: #222222;
border-radius: 5px;
}
.root {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
height: 100%;
padding: 25px;
box-sizing: border-box;
}
`;
this.shadowRoot.append(style);
}
connectedCallback() {}
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment