Solid JSX HTMLElement types do not allow on:event
props
#1441
Replies: 22 comments 1 reply
-
You need to extend declare module "solid-js" {
namespace JSX {
interface CustomEvents {
click: (ev: MouseEvent) => void;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Right, good point, I was looking at the wrong type. Where would that belong if I were to submit a PR for this here? I couldn't find any JSX type declarations in the source code here. |
Beta Was this translation helpful? Give feedback.
-
You misunderstand: this goes into your typescript file. Using The |
Beta Was this translation helpful? Give feedback.
-
The problem with deferred events in my case is that I rely on stopping propagation on components (like In Solid's case, if I attach an event handler on the document and another deferred event handler on a child component, the parent's handler will be triggered first, contrary to assigning the event handler on the child which stops the propagation. It's impossible for the child to prevent the parent from receving the event, unless I also keep track of various signals that combined give information in the parent event handler to ignore the event. As far as I'm aware, this mechanism in Solid is available for just such cases, and the technique is something the DOM provides and is a valid use case. |
Beta Was this translation helpful? Give feedback.
-
You can add the missing handlers via a mapped type like so: https://playground.solidjs.com/?hash=300022548&version=1.3.3 The JSX types are declared in dom-expressions; if they are to be changed to include |
Beta Was this translation helpful? Give feedback.
-
If it can't prevent propagation between delegated events there is a bug or missing feature. My expectation is this should work. It is possible for a non-delegated event to be hit first, but anything in Solid won't start its bubbling until the actual event bubbles to the document and then it bubbles from the target back up again by our own mechanism and still should be cancellable. |
Beta Was this translation helpful? Give feedback.
-
https://codesandbox.io/s/modest-snow-1y1v9?file=/src/index.tsx Note the use of the global |
Beta Was this translation helpful? Give feedback.
-
Same thing in the playground: https://playground.solidjs.com/?hash=1871281921&version=1.3.3 I might just be misunderstanding how interop code should work with Solid's delegated events, but I would expect being able to stop propagation at at least the component root node. |
Beta Was this translation helpful? Give feedback.
-
I see so the case is a delegated and non-delegated handler. In that case non-delegated tend to run first. Delegation happens at the time of the document handler. So its a matter of which was registered first I think. And stop propagation might be too late at that point. Using the custom event syntax is one choice. I wonder if there is another. Like exposing a method that would imperatively add the event and play nice. |
Beta Was this translation helpful? Give feedback.
-
Yeah, I tend to work on systems that need to play nice with other legacy components, so relying on precise DOM event binding is key here. I don't mind opting into the |
Beta Was this translation helpful? Give feedback.
-
Ah.. mostly not to confuse things with different ways to add native events. I'm not a TS user so I don't really care. If people have opinions glad to hear them. We can make |
Beta Was this translation helpful? Give feedback.
-
So should I close this issue and write up a PR per @otonashixav's comment on |
Beta Was this translation helpful? Give feedback.
-
As a related note - I encounter the issue of delegated vs non-delegated event listeners when creating libraries that use ref/use: callbacks for convenience. In the callback I setup listeners using |
Beta Was this translation helpful? Give feedback.
-
It's possible I should just expose the helpers in a way that are more usable. There are delegated equivalents to I suppose the ideal form would have the ability to let the compiler augment elements after the fact with a JSX syntax it could apply the right things. |
Beta Was this translation helpful? Give feedback.
-
That sounds interesting to me (and I think being able to decide on the library author side whether to delegate or not would be useful). Let me know if there is something now/in future I can try out for my use case and report back. |
Beta Was this translation helpful? Give feedback.
-
I would definitely like the documentation and tools to describe what the idiomatic way of opting into/out of delegated events would look like in Solid, and if there is delegation happening, which events are affected by this. I can see a lot of other developers coming from Preact and similar libraries being caught out by this if they're used to leveraging the web platform's native event affordances. |
Beta Was this translation helpful? Give feedback.
-
Just an FYI: Preact doesn't do event delegation. It's similar to solidjs in how browser events are handled. |
Beta Was this translation helpful? Give feedback.
-
do deferred events bubble up? if so I would rather just add another on:keydown farther up in my app if I needed a global keyevent handler |
Beta Was this translation helpful? Give feedback.
-
If you mean delegated events, they bubble but do so with custom bubbling that happens when the original event has already bubbled up to the document. |
Beta Was this translation helpful? Give feedback.
-
I'm not TS savvy to know what the preferred choice is here. But this issue is about as stale as it gets and no one commented on whether we should do this. I think that it is interesting whether we should basically use types to direct people to the overload as it isn't the intended usage but I can see why people would want these types if they used them all the time. In either case we can probably take this to DOM Expressions or at least a discussion. |
Beta Was this translation helpful? Give feedback.
-
I am new to Solid, but the current doc states usecases for native events and even when they are a better solution. But when I use them in TS project, I get errors. This is inconsistent. |
Beta Was this translation helpful? Give feedback.
-
To summarize the workaround: in declare module 'solid-js' {
namespace JSX {
interface CustomEvents extends HTMLElementEventMap {}
interface CustomCaptureEvents extends HTMLElementEventMap {}
}
} in import './shims/solid.ts' |
Beta Was this translation helpful? Give feedback.
-
Describe the bug
When using the
on:event
syntax on HTML elements in JSX in TypeScript the props are considered invalid:Expected behavior
The behaviour of
on:[event]
handlers should be typed correctly. I've tried extending theHTMLElement
interface to allow this, but I'm stuck with being unable to correctly:Unfortunately, the error with this type seems to suggest that I'd have to write out each individual
on:${event}
JSX handler, unless someone can suggest how to create a mapped type from the keys presented above. Before I attempt that, would this type extension belong insolidjs
ordom-expressions
?Beta Was this translation helpful? Give feedback.
All reactions