Last active
May 7, 2024 17:28
-
-
Save wolfadex/45ae81f73c79a1c092cdba60aa218201 to your computer and use it in GitHub Desktop.
A Portal web component for Elm
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
window.customElements.define("elm-portal", class extends HTMLElement { | |
// Base custom element stuff | |
constructor() { | |
super(); | |
this._targetNode = document.createElement('div'); | |
} | |
connectedCallback() { | |
document.querySelector(this.getAttribute("data-target-selector")).appendChild(this._targetNode); | |
} | |
disconnectedCallback() { | |
document.querySelector(this.getAttribute("data-target-selector")).removeChild(this._targetNode); | |
} | |
// Re-implementations of HTMLElement functions | |
get childNodes() { | |
return this._targetNode.childNodes; | |
} | |
replaceData(...args) { | |
return this._targetNode.replaceData(...args); | |
} | |
removeChild(...args) { | |
return this._targetNode.removeChild(...args); | |
} | |
insertBefore(...args) { | |
return this._targetNode.insertBefore(...args); | |
} | |
appendChild(...args) { | |
// To cooperate with the Elm runtime | |
requestAnimationFrame(() => { | |
return this._targetNode.appendChild(...args); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment