_hyperscript icon indicating copy to clipboard operation
_hyperscript copied to clipboard

Allow construct to listen to "reactive" events

Open jquesada2016 opened this issue 2 years ago • 1 comments

I've recently come to the conclusion that it'd be a really nice addition to the language to declare an event with the from clause to be dynamic to DOM changes.

For example:

<body _="on click from <button /> call alert('a button was clicked')">
  <!-- ...content -->
</body>

Now, the above will only react to button clicks for the buttons that existed at the time the <body /> loads. It would be nice to be able to dynamically add event listeners as elements are added and removed from the DOM, as long as they match the selector. Perhaps something like:

<body _="on click from all <button /> call alert('a button was clicked')">
  <!-- ...content -->
</body>

Here I introduced the all modifier, which would opt-in to this behavior.

Any thoughts on this?

jquesada2016 avatar Aug 01 '23 17:08 jquesada2016

Use event delegation for this. https://denizaksimsek.com/2021/hyperscript-event-delegation/

on click from body
  tell closest <button /> to target
    alert("a button was clicked")

dz4k avatar Aug 19 '23 20:08 dz4k