Skip to content

Instantly share code, notes, and snippets.

@kristoferjoseph
Created May 18, 2023 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristoferjoseph/17281865bbcb0d8027cd802a22975360 to your computer and use it in GitHub Desktop.
Save kristoferjoseph/17281865bbcb0d8027cd802a22975360 to your computer and use it in GitHub Desktop.
My message Shadow DOM edition
export default function MyMessage({ html, state }) {
const { attrs } = state
const { message='' } = attrs
return html`
<h1>${ message }</h1>
<script type="module">
class MyMessage extends HTMLElement {
constructor() {
super()
const templateID = `${this.tagName.toLowerCase()}-template`
const template = document.getElementById(templateID)
if (template) {
this.template = template
}
else {
this.template = document.createElement('template')
this.template.innerHTML = `<h1></h1>`
this.template.setAttribute('id', templateID)
}
this.attachShadow({ mode: 'open' })
.appendChild(this.template.content.cloneNode(true))
this.heading = this.querySelector('h1')
}
static get observedAttributes() {
return [ 'message' ]
}
attributeChangedCallback(name, oldValue, newValue) {
if (oldValue !== newValue) {
if (name === 'message') {
this.heading.textContent = newValue
}
}
}
}
customElements.define('my-message', MyMessage)
</script>
`
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment