Skip to content

Instantly share code, notes, and snippets.

@wolfadex
Last active September 6, 2023 12:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolfadex/45ae81f73c79a1c092cdba60aa218201 to your computer and use it in GitHub Desktop.
Save wolfadex/45ae81f73c79a1c092cdba60aa218201 to your computer and use it in GitHub Desktop.
A Portal web component for Elm
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