Skip to content

Commit

Permalink
docs: improve NodeRef warning (#2414) (#2467)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinfrances107 authored May 6, 2024
1 parent ac3b95d commit 47bcee0
Showing 1 changed file with 48 additions and 39 deletions.
87 changes: 48 additions & 39 deletions leptos_dom/src/node_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,41 @@ use crate::{html::ElementDescriptor, HtmlElement};
use leptos_reactive::{create_render_effect, signal_prelude::*};
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::<Input>();
///
/// 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! {
/// <div>
/// // `node_ref` loads the input
/// <input _ref=input_ref type="text"/>
/// // the button consumes it
/// <button on:click=on_click>"Click me"</button>
/// </div>
/// }
/// }
/// ```
#[repr(transparent)]
pub struct NodeRef<T: ElementDescriptor + 'static>(
RwSignal<Option<HtmlElement<T>>>,
);
/// 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::<Input>();
///
/// 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! {
/// <div>
/// // `node_ref` loads the input
/// <input _ref=input_ref type="text"/>
/// // the button consumes it
/// <button on:click=on_click>"Click me"</button>
/// </div>
/// }
/// }
/// ```
#[cfg_attr(not(debug_assertions), repr(transparent))]
pub struct NodeRef<T: ElementDescriptor + 'static> {
element: RwSignal<Option<HtmlElement<T>>>,
#[cfg(debug_assertions)]
defined_at: &'static std::panic::Location<'static>,
}

/// Creates a shared reference to a DOM node created while using the `view`
/// macro to create your UI.
Expand Down Expand Up @@ -65,9 +67,14 @@ pub struct NodeRef<T: ElementDescriptor + 'static>(
/// }
/// }
/// ```
#[track_caller]
#[inline(always)]
pub fn create_node_ref<T: ElementDescriptor + 'static>() -> NodeRef<T> {
NodeRef(create_rw_signal(None))
NodeRef {
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
element: create_rw_signal(None),
}
}

impl<T: ElementDescriptor + 'static> NodeRef<T> {
Expand Down Expand Up @@ -120,7 +127,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
where
T: Clone,
{
self.0.get()
self.element.get()
}

/// Gets the element that is currently stored in the reference.
Expand All @@ -132,7 +139,7 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
where
T: Clone,
{
self.0.get_untracked()
self.element.get_untracked()
}

#[doc(hidden)]
Expand All @@ -144,13 +151,15 @@ impl<T: ElementDescriptor + 'static> NodeRef<T> {
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. \
"You are setting the NodeRef defined at {}, which has \
already been filled \
It’s possible this is intentional, but it’s also \
possible that you’re accidentally using the same NodeRef \
for multiple _ref attributes."
for multiple _ref attributes.",
self.defined_at
);
}
*current = Some(node.clone());
Expand Down

0 comments on commit 47bcee0

Please sign in to comment.