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

Provide type checking support for HTML attribute values in rsx macro #2398

Open
mrgzi opened this issue May 8, 2024 · 1 comment
Open

Provide type checking support for HTML attribute values in rsx macro #2398

mrgzi opened this issue May 8, 2024 · 1 comment
Labels
html Related to the html crate

Comments

@mrgzi
Copy link

mrgzi commented May 8, 2024

Feature Request

Since Rust is a strongly typed language, it seems sensible to check types in HTML attribute values as well. Currently, rsx macro allows the following usage:

pub fn component() -> Element {
    rsx! {
        input {
            r#type: "text",
            maxlength: "10",
        }
    }
}

However, the maxlength attribute should actually be an integer:

pub fn component() -> Element {
    rsx! {
        input {
            r#type: "text",
            maxlength: 10,
        }
    }
}

Implement Suggestion

Instead of forcing developers to use strongly typed HTML, we can provide two options:

  1. Comprehensive Type Definitions: Attributes should have type definitions that can include primitive data types, enums, and custom structs, depending on the attribute's expected values.
  2. String Literal Validation: When attributes are specified using string literals, the rsx macro should validate these strings against the expected data types.

Examples;

pub fn component() -> Element {
   rsx! {
        input { 
            r#type: InputType::Email, 
            maxlength: 10
        }
        input { 
            r#type:  "email" 
            maxlength: "10"
        }
        input { 
            r#type:  "emaaill"  // `rsx` should show an error: invalid input type
            maxlength: "10"
        }
   }
}
@ealmloff
Copy link
Member

I have some concerns about the binary size impact of typed HTML in WASM. One of the largest pieces of dioxus-web today is just converting a typed enum key in events to and from a string. Each part of typed HTML would need similar logic which may have a large binary size.

@ealmloff ealmloff added the html Related to the html crate label May 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
html Related to the html crate
Projects
None yet
Development

No branches or pull requests

2 participants