leptos_node_ref/
any_node_ref.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use std::marker::PhantomData;

use leptos::{
    attr::{Attribute, NextAttribute},
    html::ElementType,
    prelude::{
        guards::{Derefable, ReadGuard},
        DefinedAt, Get, NodeRef, ReadUntracked, RwSignal, Set, Track,
    },
    tachys::{html::node_ref::NodeRefContainer, renderer::types::Element},
};
use send_wrapper::SendWrapper;

/// A reactive reference to a DOM node that can be used with the `node_ref` attribute.
#[derive(Debug)]
pub struct AnyNodeRef(RwSignal<Option<SendWrapper<Element>>>);

impl AnyNodeRef {
    /// Creates a new [`AnyNodeRef`].
    #[track_caller]
    pub fn new() -> Self {
        Self(RwSignal::new(None))
    }
}

impl Default for AnyNodeRef {
    fn default() -> Self {
        Self::new()
    }
}

impl Clone for AnyNodeRef {
    fn clone(&self) -> Self {
        *self
    }
}

impl Copy for AnyNodeRef {}

impl DefinedAt for AnyNodeRef {
    fn defined_at(&self) -> Option<&'static std::panic::Location<'static>> {
        self.0.defined_at()
    }
}

impl<T: ElementType> From<NodeRef<T>> for AnyNodeRef
where
    NodeRef<T>: IntoAnyNodeRef,
{
    fn from(value: NodeRef<T>) -> Self {
        value.into_any()
    }
}

impl<E: ElementType> NodeRefContainer<E> for AnyNodeRef {
    fn load(self, el: &Element) {
        self.0.set(Some(SendWrapper::new(el.clone())));
    }
}

impl ReadUntracked for AnyNodeRef {
    type Value = ReadGuard<Option<Element>, Derefable<Option<Element>>>;

    fn try_read_untracked(&self) -> Option<Self::Value> {
        Some(ReadGuard::new(Derefable(
            self.0.try_read_untracked()?.as_deref().cloned(),
        )))
    }
}

impl Track for AnyNodeRef {
    fn track(&self) {
        self.0.track();
    }
}

/// Allows converting any node reference into our type-erased [`AnyNodeRef`].
pub trait IntoAnyNodeRef {
    /// Converts `self` into an [`AnyNodeRef`].
    fn into_any(self) -> AnyNodeRef;
}

impl<E> IntoAnyNodeRef for NodeRef<E>
where
    E: ElementType,
    E::Output: AsRef<Element>,
    NodeRef<E>: Get<Value = Option<E::Output>>,
{
    fn into_any(self) -> AnyNodeRef {
        let any_ref = AnyNodeRef::new();
        if let Some(element) = self.get() {
            NodeRefContainer::<E>::load(any_ref, element.as_ref());
        }
        any_ref
    }
}

impl IntoAnyNodeRef for AnyNodeRef {
    fn into_any(self) -> AnyNodeRef {
        self
    }
}

/// Attribute wrapper for node references that allows conditional rendering across elements.
///
/// Useful when distributing node references across multiple rendering branches.
#[derive(Debug)]
pub struct AnyNodeRefAttr<E, C> {
    container: C,
    ty: PhantomData<E>,
}

impl<E, C> Clone for AnyNodeRefAttr<E, C>
where
    C: Clone,
{
    fn clone(&self) -> Self {
        Self {
            container: self.container.clone(),
            ty: PhantomData,
        }
    }
}

impl<E, C> Attribute for AnyNodeRefAttr<E, C>
where
    E: ElementType + 'static,
    C: NodeRefContainer<E> + Clone + 'static,
    Element: PartialEq,
{
    const MIN_LENGTH: usize = 0;
    type State = Element;
    type AsyncOutput = Self;
    type Cloneable = Self;
    type CloneableOwned = Self;

    #[inline(always)]
    fn html_len(&self) -> usize {
        0
    }

    fn to_html(
        self,
        _buf: &mut String,
        _class: &mut String,
        _style: &mut String,
        _inner_html: &mut String,
    ) {
    }

    fn hydrate<const FROM_SERVER: bool>(self, el: &Element) -> Self::State {
        self.container.load(el);
        el.clone()
    }

    fn build(self, el: &Element) -> Self::State {
        self.container.load(el);
        el.clone()
    }

    fn rebuild(self, state: &mut Self::State) {
        self.container.load(state);
    }

    fn into_cloneable(self) -> Self::Cloneable {
        self
    }

    fn into_cloneable_owned(self) -> Self::CloneableOwned {
        self
    }

    fn dry_resolve(&mut self) {}

    async fn resolve(self) -> Self::AsyncOutput {
        self
    }
}

impl<E, C> NextAttribute for AnyNodeRefAttr<E, C>
where
    E: ElementType + 'static,
    C: NodeRefContainer<E> + Clone + 'static,
    Element: PartialEq,
{
    type Output<NewAttr: Attribute> = (Self, NewAttr);

    fn add_any_attr<NewAttr: Attribute>(self, new_attr: NewAttr) -> Self::Output<NewAttr> {
        (self, new_attr)
    }
}

/// Constructs an attribute to attach an [`AnyNodeRef`] to an element.
///
/// Enables adding node references in conditional/dynamic rendering branches.
pub fn any_node_ref<E, C>(container: C) -> AnyNodeRefAttr<E, C>
where
    E: ElementType,
    C: NodeRefContainer<E>,
{
    AnyNodeRefAttr {
        container,
        ty: PhantomData,
    }
}

pub mod prelude {
    pub use super::*;
    pub use any_node_ref;
    pub use AnyNodeRef;
    pub use IntoAnyNodeRef;
}

#[cfg(test)]
mod tests {
    use leptos::{html, prelude::*};

    use super::{any_node_ref, prelude::*};

    #[test]
    fn test_any_node_ref_creation() {
        let node_ref = AnyNodeRef::new();
        assert!(node_ref.get().is_none(), "New AnyNodeRef should be empty");
    }

    #[test]
    fn test_to_any_node_ref() {
        let div_ref: NodeRef<html::Div> = NodeRef::new();
        let any_ref = div_ref.into_any();
        assert!(
            any_ref.get().is_none(),
            "Converted AnyNodeRef should be initially empty"
        );
    }

    #[test]
    fn test_clone_and_copy() {
        let node_ref = AnyNodeRef::new();
        let cloned_ref = node_ref;
        let _copied_ref = cloned_ref; // Should be copyable
        assert!(
            cloned_ref.get().is_none(),
            "Cloned AnyNodeRef should be empty"
        );
    }

    #[test]
    fn test_default() {
        let node_ref = AnyNodeRef::default();
        assert!(
            node_ref.get().is_none(),
            "Default AnyNodeRef should be empty"
        );
    }

    #[test]
    fn test_into_any_node_ref_trait() {
        let div_ref: NodeRef<html::Div> = NodeRef::new();
        let _any_ref: AnyNodeRef = div_ref.into_any();

        let input_ref: NodeRef<html::Input> = NodeRef::new();
        let _any_input_ref: AnyNodeRef = input_ref.into_any();
    }

    #[test]
    fn test_from_node_ref() {
        let div_ref: NodeRef<html::Div> = NodeRef::new();
        let _any_ref: AnyNodeRef = div_ref.into();
    }

    #[test]
    fn test_any_node_ref_attr() {
        let node_ref = AnyNodeRef::new();
        let _attr = any_node_ref::<html::Div, _>(node_ref);
    }

    #[test]
    fn test_defined_at() {
        let node_ref = AnyNodeRef::new();
        assert!(node_ref.defined_at().is_some());
    }

    #[test]
    fn test_track_and_untracked() {
        let node_ref = AnyNodeRef::new();
        // Just testing that these don't panic
        node_ref.track();
        let _untracked = node_ref.try_read_untracked();
    }

    #[test]
    fn test_into_any_identity() {
        let node_ref = AnyNodeRef::new();
        let same_ref = node_ref.into_any();

        // Instead of checking pointer equality, we should verify:
        // 1. Both refs are initially empty
        assert!(node_ref.get().is_none());
        assert!(same_ref.get().is_none());

        // 2. When we set one, both should reflect the change
        // (This would require a mock Element to test properly)

        // 3. They should have the same defined_at location
        assert_eq!(node_ref.defined_at(), same_ref.defined_at());
    }
}