Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 42 additions & 41 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ authors = ["Rust For Web <info@rustforweb.org>"]
edition = "2021"
license = "MIT"
repository = "https://github.com/RustForWeb/leptos-utils"
version = "0.0.3"
version = "0.0.4"

[workspace.dependencies]
leptos = "0.7.0"
leptos = "0.7.2"
leptos-struct-component-macro = { path = "./packages/leptos-struct-component-macro" }
leptos-node-ref = { path = "./packages/leptos-node-ref" }

48 changes: 46 additions & 2 deletions packages/leptos-node-ref/src/any_node_ref.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use leptos::{
prelude::{
guards::{Derefable, ReadGuard},
DefinedAt, ReadUntracked, RwSignal, Set, Track,
DefinedAt, ReadUntracked, RwSignal, Get, Set, Track, NodeRef
},
tachys::{html::node_ref::NodeRefContainer, renderer::types::Element},
};
Expand All @@ -12,7 +12,7 @@ use send_wrapper::SendWrapper;
pub struct AnyNodeRef(RwSignal<Option<SendWrapper<Element>>>);

impl AnyNodeRef {
/// Creates a new node reference.
/// Creates a new `AnyNodeRef`.
#[track_caller]
pub fn new() -> Self {
Self(RwSignal::new(None))
Expand Down Expand Up @@ -55,6 +55,16 @@ impl Track for AnyNodeRef {
}
}

pub trait ToAnyNodeRef {
/// Converts `self` into an `AnyNodeRef`.
///
/// # Returns
///
/// An `AnyNodeRef` that encapsulates the specific node reference in a type-erased manner,
/// enabling generic operations and composition.
fn to_any(self) -> AnyNodeRef;
}

macro_rules! impl_html_any_node_ref {
($($element:ident),*,) => {
$(impl NodeRefContainer<leptos::html::$element> for AnyNodeRef {
Expand All @@ -63,6 +73,15 @@ macro_rules! impl_html_any_node_ref {
// so it will always be accessed or dropped from the main thread
self.0.set(Some(SendWrapper::new(el.clone())));
}
}
impl ToAnyNodeRef for NodeRef<leptos::html::$element> {
fn to_any(self) -> AnyNodeRef {
let any_ref = AnyNodeRef::new();
if let Some(element) = self.get() {
NodeRefContainer::<leptos::html::$element>::load(any_ref, &element);
}
any_ref
}
})*
};
}
Expand All @@ -75,6 +94,15 @@ macro_rules! impl_math_any_node_ref {
// so it will always be accessed or dropped from the main thread
self.0.set(Some(SendWrapper::new(el.clone())));
}
}
impl ToAnyNodeRef for NodeRef<leptos::math::$element> {
fn to_any(self) -> AnyNodeRef {
let any_ref = AnyNodeRef::new();
if let Some(element) = self.get() {
NodeRefContainer::<leptos::math::$element>::load(any_ref, &element);
}
any_ref
}
})*
};
}
Expand All @@ -87,10 +115,26 @@ macro_rules! impl_svg_any_node_ref {
// so it will always be accessed or dropped from the main thread
self.0.set(Some(SendWrapper::new(el.clone())));
}
}
impl ToAnyNodeRef for NodeRef<leptos::svg::$element> {
fn to_any(self) -> AnyNodeRef {
let any_ref = AnyNodeRef::new();
if let Some(element) = self.get() {
NodeRefContainer::<leptos::svg::$element>::load(any_ref, &element);
}
any_ref
}
})*
};
}

// Implement `ToAnyNodeRef` for `AnyNodeRef` itself.
impl ToAnyNodeRef for AnyNodeRef {
fn to_any(self) -> AnyNodeRef {
self
}
}

impl_html_any_node_ref!(
A, Abbr, Address, Area, Article, Aside, Audio, B, Base, Bdi, Bdo, Blockquote, Body, Br, Button,
Canvas, Caption, Cite, Code, Col, Colgroup, Data, Datalist, Dd, Del, Details, Dfn, Dialog, Div,
Expand Down
29 changes: 29 additions & 0 deletions packages/leptos-node-ref/tests/node_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use leptos_node_ref::{AnyNodeRef, ToAnyNodeRef};
use leptos::{prelude::*, html};

#[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.to_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");
}
Loading