Created
November 1, 2021 03:26
-
-
Save egillespie/c63e15b33fb31b1e35e0c6be9d34d526 to your computer and use it in GitHub Desktop.
A web component as a module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Web components as ES Modules</title> | |
<link rel="preload" href="page-title.mjs" as="script" crossorigin="anonymous"> | |
</head> | |
<body> | |
<page-title></page-title> | |
<script src="page-title.mjs" type="module"></script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export default class PageTitle extends HTMLElement { | |
constructor () { | |
super() | |
this.attachShadow({ mode: 'open' }) | |
this.shadowRoot.innerHTML = /* html */ ` | |
<style> | |
:host { | |
display: inline-block; | |
font-family: sans-serif; | |
} | |
p { | |
text-decoration: underline; | |
font-weight: bold; | |
} | |
</style> | |
<p>${document.title}</p> | |
` | |
} | |
} | |
customElements.define('page-title', PageTitle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment