dioxus
dioxus copied to clipboard
Support optionally adding an attribute
Specific Demand
There is currently no way to optionally add a attribute within the rsx macro. This is needed to complete #394.
Implement Suggestion
- React only adds the property if it is truthy, we could mimic it, but it doesn't seem very "rusty"...
- We could only add a Option<AttributeValue> when attribute if the option is Some:
let x = Some("myClass");
rsx!{
div{
class: x,
}
}
- We could have a opt in way to pass a Option<AttributeValue> instead that only adds the attribute if the option is Some:
let x = Some("myClass");
rsx!{
div{
class?: x,
}
}
I like the syntax you proposed here! It would be helpful for things like classes/classnames.
For multiple attributes, we could merge them somehow.
let colorful = Some("blue");
let enabled = None;
div {
class: "default classes",
class?: colorful,
class?: enabled,
}
Dioxus could provide a trait for merging multiple occurrences of a single attribute. This trait would only be needed when multiple occurrences are actually detected.
Also, IIRC, macros have a way of requiring trait implementations for specific types. This maybe could be used to provide more helpful error messages, when the merging trait is required.
The merging trait would also need to be implemented for various std types, like Vec and String.