Skip to content
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

Some events not firing #54

Closed
jquesada2016 opened this issue Nov 6, 2022 · 3 comments
Closed

Some events not firing #54

jquesada2016 opened this issue Nov 6, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@jquesada2016
Copy link
Contributor

Given the following code:

use leptos::*;
use wasm_bindgen::prelude::*;

#[wasm_bindgen(start)]
pub fn start() {
  mount_to_body(view_fn);
}

fn view_fn(cx: Scope) -> impl Mountable {
  let (focus, set_focus) = create_signal(cx, false);

  let focused = move || {
    if focus() {
      view! { cx, <p></p> }
    } else {
      view! { cx, <span /> }
    }
  };

  view! { cx,
    <div>
      <input on:focus=move |_| set_focus(true) on:blur=move |_| set_focus(false) />
      {focused}
    </div>
  }
}

on:focus and on:blur never fire. on:click does, however.

@gbj
Copy link
Collaborator

gbj commented Nov 7, 2022

Oh interesting. Yeah this is because I'm using event delegation, but focus/blur don't bubble.

I guess add_event_listener (or the view macro) needs to check whether an event is in a set of "non-bubbling" events and use a normal event listener rather than event delegation.

https://github.com/gbj/leptos/blob/b9ca0b11a2d8846a83af6a8f37af3dde23c16179/leptos_dom/src/operations.rs#L230-L239

@jquesada2016
Copy link
Contributor Author

Thankfully, we're in luck. here's a list of all of them. One think to keep in mind however, and those are custom events, what should one do if someone creates a custom event which does not bubble? Should there be a special syntax for this?

@gbj
Copy link
Collaborator

gbj commented Nov 7, 2022

See #56. I did test this with a quick input example using focus and blur and the events fire.

I'm open to suggestions for an alternate syntax for custom events which don't bubble. This is tricky because technically it's only known whether any given event bubbles at runtime (by checking its bubbles property or whatever) but by that point it's too late (because you're catching the event within a handler that's either been delegated or not.)

Fortunately erring on the side of caution here is not a big deal, as it's a small deoptimization in any case.

@gbj gbj added the bug Something isn't working label Nov 7, 2022
@gbj gbj closed this as completed Nov 7, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants