sycamore icon indicating copy to clipboard operation
sycamore copied to clipboard

Make `NodeRef` generic to avoid manual casting

Open AMNRG opened this issue 11 months ago • 1 comments

Currently NodeRef returns always a web_sys::Node. To improve type safety and code readability, we could make NodeRef generic, allowing it to be typed directly as e.g. web_sys::HtmlInputElement.

Current state:

let username_input = create_node_ref();
...
let username = username_input.get().dyn_into::<HtmlInputElement>().unwrap().value();

Desired state:

let username_input: NodeRef<HtmlInputElement> = create_node_ref();
...
let username = username_input.get().value();

It would be super convenient if the type could be determined directly from the view macro.

AMNRG avatar Dec 17 '24 12:12 AMNRG

This shouldn’t be too difficult I don’t think. I think the biggest drawback would be that we would need to enable a bunch of features on web-sys for all the different HTML elements (and to be consistent, all the SVG elements as well) so I’ll have to see how big of a difference it makes.

As to implementing this, I think it should be pretty simple to add an associated type to every element definition representing the underlying DOM type.

lukechu10 avatar Dec 17 '24 14:12 lukechu10