How could be dynamic attributes for dynamic styling? #120
-
Hello. Could you please tell me if you have the idea for good dynamic style separation 🙇 More detailed, I want to do the following: What is aiming?
What is I wanted to do?
Dummy code:Following is my ideal image for this. use dioxus::prelude::*;
#[derive(Copy,Clone)]
pub enum Kind {
Primary,
Secondary,
}
impl Kind {
// define styles(attributes) for each Kind.
fn style<'a>(self) -> Attributes<'a> {
match self {
// Any better way? Ideally, I want to use kind of `rsx!` for generating this
Primary => Some(
&[
dioxus::core::Attribute { namespace: None, name: "color", value: "white", is_static: false, is_volatile: false },
dioxus::core::Attribute { namespace: None, name: "background-color", value: "#3F7CB9", is_static: false, is_volatile: false },
],
),
Secondary => Some(
&[
dioxus::core::Attribute { namespace: None, name: "color", value: "#333", is_static: false, is_volatile: false },
dioxus::core::Attribute { namespace: None, name: "background-color", value: "transparent", is_static: false, is_volatile: false },
dioxus::core::Attribute { namespace: None, name: "box-shadow", value: "rgba(0, 0, 0, 0.15) 0px 0px 0px 1px inset", is_static: false, is_volatile: false },
],
),
}
}
}
#[derive(Copy,Clone)]
pub enum Size {
Small,
Medium,
Large,
}
impl Size {
// As same as Kind, it defines style for each Size.
fn style<'a>(self) -> Attributes<'a> {
match self {
Small => Some(
&[
dioxus::core::Attribute { namespace: None, name: "font-size", value: "12px", is_static: false, is_volatile: false },
dioxus::core::Attribute { namespace: None, name: "padding", value: "10px 16px", is_static: false, is_volatile: false },
],
),
Medium => Some(
&[
dioxus::core::Attribute { namespace: None, name: "font-size", value: "14px", is_static: false, is_volatile: false },
dioxus::core::Attribute { namespace: None, name: "padding", value: "11px 20px", is_static: false, is_volatile: false },
],
),
Large => Some(
&[
dioxus::core::Attribute { namespace: None, name: "font-size", value: "12px", is_static: false, is_volatile: false },
dioxus::core::Attribute { namespace: None, name: "padding", value: "12px 24px", is_static: false, is_volatile: false },
],
),
}
}
}
#[derive(Props)]
pub struct ButtonProps {
kind: Kind,
size: Size,
}
pub fn Button(cx: Scope<ButtonProps>) -> Element {
rsx!{
cx,
button {
display: "inline-block",
font_family: "Nunito Sans, Helvetica Neue, Helvetica, Arial, sans-serif",
font_weight: "700",
line_height: "1",
border: "0",
border_radius: "3em",
cursor: "pointer",
// Currently, it's not work...
..cx.props.kind.style(),
..cx.props.size.style(),
},
}
} But it's not working. How could I do? What is the best practice in dioxus? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
I have copy the |
Beta Was this translation helpful? Give feedback.
-
I've noticed there are two kind of problems related with this. 1.
|
Beta Was this translation helpful? Give feedback.
-
From official discord, @jkelleyrtp says: so it will ok on ver 0.1.8. |
Beta Was this translation helpful? Give feedback.
From official discord, @jkelleyrtp says:
so it will ok on ver 0.1.8.