Support custom events
See original GitHub issueIs your feature request related to a problem?
Many web components dispatch custom events like this.dispatchEvent(new CustomEvent('MDCDrawer:opened', {bubbles: true}))
but it seems that the Qwik event syntax cannot support those events.
Describe the solution you’d like
Maybe something extending the syntax to onAnyCustomEvent$={() => ...}
would be enough.
export const App = component$(() => {
const state = useStore({
drawerOpened: false,
});
useClientEffect$(async () => {
await import('@material/mwc-drawer');
});
return (
<Host>
<mwc-drawer
type="modal"
open={state.drawerOpened}
onMDCDrawer:opened$={() => state.drawerOpened = false}
>
<div>
<p>Drawer content!</p>
</div>
<div slot="appContent">
<button onClick$={() => state.drawerOpened = !state.drawerOpened}>toggle menu</button>
</div>
</mwc-drawer>
</Host>
);
});
Describe alternatives you’ve considered
I’ve considered to manually register the event listeners on client side hook but it’s a lot of work.
Additional context
No response
Issue Analytics
- State:
- Created a year ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
CustomEvent() - Web APIs - MDN Web Docs
An event-dependent value associated with the event. This value is then available to the handler using the CustomEvent.detail property. It defaults to null...
Read more >[GA4] Custom events - Analytics Help - Google Support
A custom event is an event with a name and set of parameters that you define so you can collect information that's specific...
Read more >Dispatching custom events - The Modern JavaScript Tutorial
Custom events can be used to create “graphical components”. For instance, a root element of our own JS-based menu may trigger events telling ......
Read more >Custom events in JavaScript: A complete guide
In this tutorial, you can learn how to create custom events in JavaScript for your application to enhance the user experience.
Read more >About standard and custom website events - Facebook
Custom events are actions that fall outside those covered by our standard events. You can give them a unique name to represent the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Adding more context.
There are several issues with having custom events:
JSX Encoding
Let’s say you have
MDCDrawer:opened
event type. The first problem is how to express that in code.produces:
there is an additional problem with namespacing as
document:on-MDCDrawer:opened$
is not valid JSX because it has double:
Camel Case
Next problem is that
MDCDrawer:opened
has some upercase characters and attributes are all lowercase. I think this is handled by translatingon-MDCDrawer:opened$
toon:-m-d-c-drawer:opened
It is not clear to me ifqwikloader
has code that knows how to translate the extra-
to uppercase characters in the event name.Looking at the code it seems like
broadcast
has the code to translate to uppercase but not for regular events: https://github.com/BuilderIO/qwik/blob/main/packages/qwik/src/qwikloader.ts#L14Type System
We need to extend the TypeScript type definition to allow for the new type. This should be straightforward, but I just don’t remember how to re-open an interface definition in TypeScript.
qwikloader
Finally, the
qwikloader
needs to be told about this event, as by defaultqwikloader
only listens to the events that browsers emit. This can be done by passing explicit set of events here: https://github.com/BuilderIO/qwik/blob/main/packages/qwik/src/server/types.ts#L82So it looks like that the only custom events we can listen to right now are ones which are all lowercase and which don’t have a
:
in their name.Fixed by https://github.com/BuilderIO/qwik/pull/1212