dioxus icon indicating copy to clipboard operation
dioxus copied to clipboard

Support optionally adding an attribute

Open ealmloff opened this issue 3 years ago • 2 comments

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,
  }
}

ealmloff avatar Aug 13 '22 22:08 ealmloff

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, 
}

jkelleyrtp avatar Aug 17 '22 18:08 jkelleyrtp

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.

TeFiLeDo avatar Aug 24 '22 08:08 TeFiLeDo