use std::io;
use std::time::Duration;
use eye_declare::{Application, Elements, Hooks, Span, Text, component, element, props};
use ratatui_core::style::{Color, Modifier, Style};
#[props]
struct MountLabel {
value: String,
}
#[derive(Default)]
struct MountLabelState {
label: Option<String>,
}
#[component(props = MountLabel, state = MountLabelState)]
fn mount_label(
_props: &MountLabel,
state: &MountLabelState,
hooks: &mut Hooks<MountLabel, MountLabelState>,
) -> Elements {
hooks.use_mount(|props, state| {
state.label = Some(format!("Mounted with: {}", props.value));
});
let (text, style) = match &state.label {
Some(label) => (label.clone(), Style::default().fg(Color::Green)),
None => (
"[mount has not fired yet]".to_string(),
Style::default().fg(Color::Red).add_modifier(Modifier::BOLD),
),
};
element! {
Text {
Span(text: text, style: style)
}
}
}
struct AppState {
show_component: bool,
}
fn app_view(state: &AppState) -> Elements {
element! {
Text {
Span(
text: "Mount re-render test",
style: Style::default().fg(Color::White).add_modifier(Modifier::BOLD),
)
}
#(if state.show_component {
MountLabel(key: "mount-label", value: "hello from mount")
})
Text {
Span(text: "---", style: Style::default().fg(Color::DarkGray))
}
}
}
#[tokio::main]
async fn main() -> io::Result<()> {
let (mut app, handle) = Application::builder()
.state(AppState {
show_component: false,
})
.view(app_view)
.build()?;
tokio::spawn(async move {
handle.update(|s| {
s.show_component = true;
});
tokio::time::sleep(Duration::from_secs(3)).await;
});
app.run().await?;
println!();
Ok(())
}