Created
June 20, 2025 12:06
-
-
Save ChrisShank/97156c8027980b62a64939365b4c2c32 to your computer and use it in GitHub Desktop.
How I define custom events
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
import {MyEvent} from './my-event.ts'; | |
document.addEventListener(MyEvent.name, (e) => {...}); |
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
export class MyEvent extends Event { | |
static readonly name = 'my-event'; | |
#data; | |
get data() { return this.#data } | |
constructor(data: MyData, options: EventInit = {}) { | |
super(MyEvent.name, {bubbles: true, ...options}); | |
this.#data = data; | |
} | |
} | |
declare global { | |
interface ElementEventMap { | |
[MyEvent.name]: MyEvent; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment