Last active
January 13, 2025 21:29
-
-
Save knowler/4c846f854a1cdb4dc5a24d06f6cd755c to your computer and use it in GitHub Desktop.
Demonstration of how to patch the custom element registry to add a `connected` event.
This file contains hidden or 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
const originalDefine = CustomElementRegistry.prototype.define; | |
CustomElementRegistry.prototype.define = function(_name, constructor) { | |
const originalConnectedCallback = constructor.prototype.connectedCallback; | |
constructor.prototype.connectedCallback = function() { | |
// Maybe use a prefixed event name? | |
// Maybe configure some options (i.e. whether it bubbles) | |
this.dispatchEvent(new CustomEvent("connected")); | |
originalConnectedCallback?.call(this, ...arguments); | |
} | |
originalDefine.call(this, ...arguments); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment