Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make NodeRef generic to avoid manual casting #766

Open
AMNRG opened this issue Dec 17, 2024 · 1 comment
Open

Make NodeRef generic to avoid manual casting #766

AMNRG opened this issue Dec 17, 2024 · 1 comment
Labels
C-enhancement Category: new feature or improvement to existing feature

Comments

@AMNRG
Copy link
Contributor

AMNRG commented Dec 17, 2024

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.

@lukechu10 lukechu10 added the C-enhancement Category: new feature or improvement to existing feature label Dec 17, 2024
@lukechu10
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: new feature or improvement to existing feature
Projects
None yet
Development

No branches or pull requests

2 participants