Improving event bindings/listeners #512
Replies: 6 comments 20 replies
-
|
I actually love this idea. @leonidaz what do you think? |
Beta Was this translation helpful? Give feedback.
-
|
Yeah, I think we should definitely add it. I'm thinking to have an object with a listener and options, instead of mixing everything in one. const click = { handleEvent: (e) => {}, options: {capture: true} }
<div onClick={click} />
const events = {
onClick: { handleEvent: (e) => {}, options: {capture: true} },
onMouseOut: { handleEvent: (e) => {}, options: {once: true} },
}
<div {...events} /> |
Beta Was this translation helpful? Give feedback.
-
|
Forgot to ask, should we then get rid of the |
Beta Was this translation helpful? Give feedback.
-
|
Let's summarize.
We have a few proposals on the syntax but let's prioritize DX, as using
// keep the current function syntax for browser-defined dom events
<button onClick={(e) => {console.log(e.target}} />
// with object notation browser-defined dom events:
<button onClick={{ handleEvent: (e) => {console.log(e.target}, capture: true, passive: true, signal, once: true }} />
// for custom events use object notation with `custom: true`
<button onwhateverMyheartdesires={{ handleEvent: (e) => {console.log(e.target}, custom: true, capture: true }} />
|
Beta Was this translation helpful? Give feedback.
-
|
I've change my mind, I think we should largely keep what we have today as is, expect we'd remove the |
Beta Was this translation helpful? Give feedback.
-
|
implemented in #561 @titoBouzout thank you for the feedback! we ended up keeping
The object properties ended up being: handleEvent, capture, once, passive, signal, delegated, customName. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Ive worked in SolidJS related to this topic, and Im fresh on some things. So I want to share what I think could be improved here.
Im not too familiar with ripple syntax so ignore my "typos", but assuming the framework supports spreads and some stuff is done in runtime.
Support native object syntax
There is https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#listener
as follows
pure js demo: https://playground.solidjs.com/anonymous/91b42fc0-4699-497d-a3df-15f9cfaee447
Plus points:
onClickCaptureand friendsDefault to no-case-change
onClickhas the problem that you need to lowercase theC. And unless you ship an array with the list of native events, then any custom event that has some case will break.Even then, someone could come with a custom
onKeyDown, and lowering will break it.So in my opinion the non-brainer solution is to just remove the
onand do not lowercase or transform the event name in any way. This makes it automatically compatible with any kind of event.The downside is that some component authors may prefer to call some of their event-alike things
onThingClosed, even when these things are just callbacks and no events. Which makes you have a mixed bag of things like{onclick:()=>{}, onThingClosed:()=>{}}. Maybe if you can convince component authors to name thisonThingCloseddifferently you can get away with it.A alternative aproach could be to prefix event names, I am not sure of ripple syntax but I can imagine something like, or any other way. pota does this.
--
Well thats my 2 cents, as a framework that learns from the mistakes of the others, this seems to be a good candidate.
Beta Was this translation helpful? Give feedback.
All reactions