-
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create proper DOM events from string (completed 💚) #133
Comments
I was able to compile a list of dom events but I ran into a bit of trouble when writing the TypeScript definition since the event name needs to be detected to determine the return value. |
It's not super easy, but you can use generics to maintain the mapping. Something like: const events = {
click: MouseEvent
} as const;
type Events = typeof events;
function createEvent<Name extends keyof Events >(name: Name): Events[Name] {
//
} What's missing from here:
|
Object.keys(HTMLElement.prototype).filter(key => key.startsWith('on')).map(ev => ev.slice(2)); |
That only builds the |
It's finally out: https://github.com/fregante/proper-event Suggestion welcome. |
When creating events, you should use the right constructor. For example
new MouseEvent('click')
instead of a plainnew Event('click')
.As far as I know there's no way to this automatically so there could be DOM event creator that includes such map.
This particularly useful when your app can trigger a variety of events and you can't be bothered to match them manually 😃
The text was updated successfully, but these errors were encountered: