Last active
September 28, 2017 21:01
-
-
Save goto-bus-stop/8bc6a6d35902c75068c9e8d1f657006a to your computer and use it in GitHub Desktop.
on-load with web components
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
var onload = require('./') | |
var el = document.createElement('div') | |
el = onload(el, function (a) { | |
console.log('mounting', a) | |
}, function (a) { | |
console.log('unmounting', a) | |
}) | |
var b = document.body | |
setTimeout(function () { | |
b.appendChild(el) | |
}, 500) | |
setTimeout(function () { | |
b.removeChild(el) | |
}, 1500) | |
console.log('booted') |
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
module.exports = function (el, onload, onunload) { | |
var wrapper = document.createElement('on-connected-listener') | |
wrapper.onload = onload | |
wrapper.onunload = onunload | |
wrapper.appendChild(el) | |
return wrapper | |
} | |
customElements.define('on-connected-listener', class Listener extends HTMLElement { | |
connectedCallback () { | |
this.onload && this.onload(this.firstChild) | |
} | |
disconnectedCallback () { | |
this.onunload && this.onunload(this.firstChild) | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment