Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat(leptos-node-ref): add ToAnyNodeRef trait for type-erased ref han…
…dling

- Introduced the  trait to enable conversion of typed s to

- Facilitates generic operations on various node references across HTML, SVG, and Math node types

- Added comprehensive tests to ensure the functionality and reliability of the new trait

- Updated  to reflect version bump to 0.0.4
  • Loading branch information
geoffreygarrett committed Dec 22, 2024
commit 861fb32af79d5587aba3fcb1256d72b12135a762
75 changes: 38 additions & 37 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = ["Rust For Web <[email protected]>"]
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"
54 changes: 52 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,8 @@
use leptos::{
wasm_bindgen::JsCast,
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,11 +13,16 @@ 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))
}

/// Attempts to cast the `AnyNodeRef` to a specific element type.
pub fn cast<E: JsCast + Clone>(&self) -> Option<E> {
self.0.get()?.dyn_ref::<E>().cloned()
}
}

impl Default for AnyNodeRef {
Expand Down Expand Up @@ -55,6 +61,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 +79,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 +100,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 +121,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");
}