Function leptos_use::on_click_outside

source ·
pub fn on_click_outside<El, T, F>(
    target: El,
    handler: F,
) -> impl FnOnce() + Clone
where El: Clone + Into<ElementMaybeSignal<T, EventTarget>>, T: Into<EventTarget> + Clone + 'static, F: FnMut(Event) + Clone + 'static,
Expand description

Listen for clicks outside of an element. Useful for modals or dropdowns.

§Demo

Link to Demo

§Usage

let target = create_node_ref::<Div>();

on_click_outside(target, move |event| { log!("{:?}", event); });

view! {
    <div node_ref=target>"Hello World"</div>
    <div>"Outside element"</div>
}

This function uses Event.composedPath() which is not supported by IE 11, Edge 18 and below. If you are targeting these browsers, we recommend you to include this code snippet on your project.

§Excluding Elements

Use this to ignore clicks on certain elements.

on_click_outside_with_options(
    target,
    move |event| { log!("{:?}", event); },
    OnClickOutsideOptions::default().ignore(["input", "#some-id"]),
);

§Server-Side Rendering

On the server this amounts to a no-op.