Last active
October 17, 2023 09:09
-
-
Save aDogCalledSpot/95bdb9945343a746f88b92b2b8f2d6b4 to your computer and use it in GitHub Desktop.
Patternfly Yew UseStateHandle in Backdrop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![recursion_limit = "1024"] | |
use patternfly_yew::prelude::*; | |
use yew::prelude::*; | |
#[function_component(Application)] | |
pub fn app() -> Html { | |
let title = use_state_eq(|| "This should change when you press the button".to_string()); | |
html! { | |
<BackdropViewer> | |
{(*title).clone()} | |
<MyButton {title}/> | |
</BackdropViewer> | |
} | |
} | |
#[derive(Debug, Clone, PartialEq, Properties)] | |
struct MyButtonProps { | |
title: UseStateHandle<String>, | |
} | |
#[function_component(MyButton)] | |
fn mybutton(props: &MyButtonProps) -> Html { | |
let onclick = { | |
let title = props.title.clone(); | |
let backdrop = use_backdrop().unwrap(); | |
Callback::from(move |_| { | |
let title = title.clone(); | |
backdrop.open(Backdrop::new(html! { | |
<Bullseye><MyModal {title}/></Bullseye> | |
})) | |
}) | |
}; | |
html! { | |
<Button label="Click me" variant={ButtonVariant::Primary} {onclick}/> | |
} | |
} | |
#[derive(Debug, Clone, PartialEq, Properties)] | |
struct MyModalProps { | |
title: UseStateHandle<String>, | |
} | |
#[function_component(MyModal)] | |
fn mymodal(props: &MyModalProps) -> Html { | |
let title = props.title.clone(); | |
// Creating the handle inside the backdrop view works fine, | |
// we only have issues when cloning from our props | |
// let title = use_state_eq(|| "This is the title".to_string()); | |
let onclick = { | |
let title = title.clone(); | |
Callback::from(move |_| title.set("You changed it!".to_string())) | |
}; | |
html! { | |
<Modal title={(*title).clone()} variant={ModalVariant::Medium}> | |
<Button variant={ButtonVariant::Primary} label="Change title" {onclick}/> | |
</Modal> | |
} | |
} | |
pub fn main() { | |
yew::Renderer::<Application>::new().render(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment