Skip to content

Instantly share code, notes, and snippets.

@pauleveritt
Created September 25, 2022 20:56
Show Gist options
  • Save pauleveritt/255162988b76e32f56d9630d3aa5bf1e to your computer and use it in GitHub Desktop.
Save pauleveritt/255162988b76e32f56d9630d3aa5bf1e to your computer and use it in GitHub Desktop.
Idiomorph with a custom element.
<html>
<head>
<title>Custom Element Morph</title>
<script src="https://unpkg.com/idiomorph"></script>
</head>
<body>
<hello-world name="World"></hello-world>
<button id="update">Update</button>
<script defer>
class HelloWorld extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
const name = this.getAttribute("name")
this.innerHTML = `<p>Hello <em>${name}</em></p>`
}
}
customElements.define("hello-world", HelloWorld);
const newHTML = "<div>I was <strong>updated</strong></div>";
const button = document.getElementById("update");
button.addEventListener("click", () => {
const targets = document.getElementsByTagName("hello-world");
Array.from(targets).forEach((target) => {
Idiomorph.morph(target, newHTML, {morphStyle: 'innerHTML'});
});
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment