2023-07-14 22:43:19 +01:00
|
|
|
#![cfg_attr(feature = "ssr", allow(unused_variables, unused_imports))]
|
|
|
|
|
2023-10-21 15:21:11 -05:00
|
|
|
use crate::{use_document, use_event_listener_with_options, use_window, UseEventListenerOptions};
|
2023-06-17 14:05:17 +01:00
|
|
|
use leptos::ev::{blur, focus};
|
|
|
|
use leptos::html::{AnyElement, ToHtmlElement};
|
|
|
|
use leptos::*;
|
|
|
|
|
2023-07-14 16:43:42 +01:00
|
|
|
/// Reactive `document.activeElement`
|
2023-06-17 14:05:17 +01:00
|
|
|
///
|
|
|
|
/// ## Demo
|
|
|
|
///
|
|
|
|
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_active_element)
|
|
|
|
///
|
|
|
|
/// ## Usage
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # use leptos::*;
|
2023-09-12 15:24:32 +01:00
|
|
|
/// # use leptos::logging::log;
|
2023-06-17 14:05:17 +01:00
|
|
|
/// use leptos_use::use_active_element;
|
|
|
|
/// #
|
|
|
|
/// # #[component]
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # fn Demo() -> impl IntoView {
|
|
|
|
/// let active_element = use_active_element();
|
2023-06-17 14:05:17 +01:00
|
|
|
///
|
2023-07-27 18:06:36 +01:00
|
|
|
/// create_effect(move |_| {
|
2023-06-21 13:09:00 +02:00
|
|
|
/// log!("focus changed to {:?}", active_element.get());
|
2023-06-17 14:05:17 +01:00
|
|
|
/// });
|
|
|
|
/// #
|
2023-07-27 18:06:36 +01:00
|
|
|
/// # view! { }
|
2023-06-17 14:05:17 +01:00
|
|
|
/// # }
|
|
|
|
/// ```
|
2023-07-14 22:43:19 +01:00
|
|
|
///
|
|
|
|
/// ## Server-Side Rendering
|
|
|
|
///
|
|
|
|
/// On the server this returns a `Signal` that always contains the value `None`.
|
2023-07-27 18:06:36 +01:00
|
|
|
pub fn use_active_element() -> Signal<Option<HtmlElement<AnyElement>>> {
|
2023-09-13 19:39:56 +01:00
|
|
|
let get_active_element = move || {
|
|
|
|
use_document()
|
|
|
|
.active_element()
|
|
|
|
.map(|el| el.to_leptos_element())
|
|
|
|
};
|
2023-06-17 14:05:17 +01:00
|
|
|
|
2023-07-27 18:06:36 +01:00
|
|
|
let (active_element, set_active_element) = create_signal(get_active_element());
|
2023-06-17 14:05:17 +01:00
|
|
|
|
2023-09-13 19:39:56 +01:00
|
|
|
let listener_options = UseEventListenerOptions::default().capture(true);
|
2023-06-17 14:05:17 +01:00
|
|
|
|
2023-09-13 19:39:56 +01:00
|
|
|
let _ = use_event_listener_with_options(
|
2023-10-21 15:21:11 -05:00
|
|
|
use_window(),
|
2023-09-13 19:39:56 +01:00
|
|
|
blur,
|
|
|
|
move |event| {
|
|
|
|
if event.related_target().is_some() {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-17 14:05:17 +01:00
|
|
|
|
2023-09-13 19:39:56 +01:00
|
|
|
set_active_element.update(|el| *el = get_active_element());
|
|
|
|
},
|
|
|
|
listener_options,
|
|
|
|
);
|
2023-06-17 14:05:17 +01:00
|
|
|
|
2023-09-13 19:39:56 +01:00
|
|
|
let _ = use_event_listener_with_options(
|
2023-10-21 15:21:11 -05:00
|
|
|
use_window(),
|
2023-09-13 19:39:56 +01:00
|
|
|
focus,
|
|
|
|
move |_| {
|
|
|
|
set_active_element.update(|el| *el = get_active_element());
|
|
|
|
},
|
|
|
|
listener_options,
|
|
|
|
);
|
2023-06-17 14:05:17 +01:00
|
|
|
|
|
|
|
active_element.into()
|
|
|
|
}
|