8 releases

0.2.4 Aug 21, 2024
0.2.3 Aug 21, 2024
0.1.2 Aug 17, 2024

#1347 in Web programming

21 downloads per month
Used in yew-attrs-macro

MIT license

11KB
200 lines

yew-attrs

Dynamic attributes for Yew.

Documentation

Documentation for the crates is available on Docs.rs:


lib.rs:

Dynamic attributes for Yew.

Example

use yew::{prelude::*, virtual_dom::VTag};
use yew_attrs::{attrs, Attrs};

#[derive(PartialEq, Properties)]
struct ButtonProps {
    #[prop_or_default]
    pub node_ref: NodeRef,
    #[prop_or_default]
    pub attrs: Attrs,
    #[prop_or_default]
    pub children: Html,
}

#[function_component]
fn Button(props: &ButtonProps) -> Html {
    props
        .attrs
        .clone()
        .new_vtag(
            "button",
            props.node_ref.clone(),
            Default::default(),
            props.children.clone(),
        )
        .into()
}

#[function_component]
fn App() -> Html {
    let on_click = use_callback((), |_, _| {});

    html! {
        <Button attrs={attrs! {class="text-red" disabled=false onclick={on_click}}}>
            {"Click"}
        </Button>
    }
}

Dependencies

~11–20MB
~268K SLoC