diff --git a/leptos_dom/src/node_ref.rs b/leptos_dom/src/node_ref.rs index 0f71b0b46a..003bf90e15 100644 --- a/leptos_dom/src/node_ref.rs +++ b/leptos_dom/src/node_ref.rs @@ -4,38 +4,76 @@ use leptos_reactive::{ }; use std::cell::Cell; -/// Contains a shared reference to a DOM node created while using the `view` -/// macro to create your UI. -/// -/// ``` -/// # use leptos::{*, logging::log}; -/// use leptos::html::Input; -/// -/// #[component] -/// pub fn MyComponent() -> impl IntoView { -/// let input_ref = create_node_ref::(); -/// -/// let on_click = move |_| { -/// let node = -/// input_ref.get().expect("input_ref should be loaded by now"); -/// // `node` is strongly typed -/// // it is dereferenced to an `HtmlInputElement` automatically -/// log!("value is {:?}", node.value()) -/// }; -/// -/// view! { -///
-/// // `node_ref` loads the input -/// -/// // the button consumes it -/// -///
-/// } -/// } -/// ``` -#[repr(transparent)] -pub struct NodeRef( - RwSignal>>, +cfg_if::cfg_if!( + if #[cfg(debug_assertions)] { + /// Contains a shared reference to a DOM node created while using the `view` + /// macro to create your UI. + /// + /// ``` + /// # use leptos::{*, logging::log}; + /// use leptos::html::Input; + /// + /// #[component] + /// pub fn MyComponent() -> impl IntoView { + /// let input_ref = create_node_ref::(); + /// + /// let on_click = move |_| { + /// let node = + /// input_ref.get().expect("input_ref should be loaded by now"); + /// // `node` is strongly typed + /// // it is dereferenced to an `HtmlInputElement` automatically + /// log!("value is {:?}", node.value()) + /// }; + /// + /// view! { + ///
+ /// // `node_ref` loads the input + /// + /// // the button consumes it + /// + ///
+ /// } + /// } + /// ``` + pub struct NodeRef { + defined_at: &'static std::panic::Location<'static>, + element: RwSignal>>, + } + } else { + /// Contains a shared reference to a DOM node created while using the `view` + /// macro to create your UI. + /// + /// ``` + /// # use leptos::{*, logging::log}; + /// use leptos::html::Input; + /// + /// #[component] + /// pub fn MyComponent() -> impl IntoView { + /// let input_ref = create_node_ref::(); + /// + /// let on_click = move |_| { + /// let node = + /// input_ref.get().expect("input_ref should be loaded by now"); + /// // `node` is strongly typed + /// // it is dereferenced to an `HtmlInputElement` automatically + /// log!("value is {:?}", node.value()) + /// }; + /// + /// view! { + ///
+ /// // `node_ref` loads the input + /// + /// // the button consumes it + /// + ///
+ /// } + /// } + /// ``` + #[repr(transparent)] + pub struct NodeRef { + element: RwSignal>>, + } + } ); /// Creates a shared reference to a DOM node created while using the `view` @@ -67,9 +105,13 @@ pub struct NodeRef( /// } /// } /// ``` +#[track_caller] #[inline(always)] pub fn create_node_ref() -> NodeRef { - NodeRef(create_rw_signal(None)) + NodeRef { + defined_at: std::panic::Location::caller(), + element: create_rw_signal(None), + } } impl NodeRef { @@ -122,7 +164,7 @@ impl NodeRef { where T: Clone, { - self.0.get() + self.element.get() } /// Gets the element that is currently stored in the reference. @@ -134,7 +176,7 @@ impl NodeRef { where T: Clone, { - self.0.get_untracked() + self.element.get_untracked() } #[doc(hidden)] @@ -146,7 +188,7 @@ impl NodeRef { where T: Clone, { - self.0.update(|current| { + self.element.update(|current| { if current.is_some() { crate::debug_warn!( "You are setting a NodeRef that has already been filled. \ @@ -178,6 +220,7 @@ impl NodeRef { } impl Clone for NodeRef { + #[track_caller] fn clone(&self) -> Self { *self } @@ -186,6 +229,7 @@ impl Clone for NodeRef { impl Copy for NodeRef {} impl Default for NodeRef { + #[track_caller] fn default() -> Self { Self::new() }