Passing props.children into element!()?
#159
Unanswered
thehappycheese
asked this question in
Q&A
Replies: 1 comment 1 reply
-
|
For example: element!{
MyComponent {
MyChild
MyOtherChild
}
}The So for your use-case, you would do something like this: use iocraft::prelude::*;
#[derive(Default, Props)]
struct HoverableProps<'a> {
pub children: Vec<AnyElement<'a>>,
}
#[component]
fn Hoverable<'a>(mut hooks: Hooks, props: &mut HoverableProps<'a>) -> impl Into<AnyElement<'a>> {
let mut has_mouse = hooks.use_state(|| false);
hooks.use_terminal_events(move |event| match event {
TerminalEvent::FullscreenMouse(FullscreenMouseEvent {
kind: MouseEventKind::Moved,
..
}) => {
has_mouse.set(false);
}
_ => {}
});
hooks.use_local_terminal_events({
move |event| match event {
TerminalEvent::FullscreenMouse(FullscreenMouseEvent {
kind: MouseEventKind::Moved,
..
}) => {
has_mouse.set(true);
}
_ => {}
}
});
element! {
View(
width: 20,
height: 5,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
background_color: if has_mouse.get() { Color::Green } else { Color::Blue },
) {
#(&mut props.children)
}
}
}
#[component]
fn Example(mut hooks: Hooks) -> impl Into<AnyElement<'static>> {
let (width, height) = hooks.use_terminal_size();
let mut system = hooks.use_context_mut::<SystemContext>();
let mut should_exit = hooks.use_state(|| false);
hooks.use_terminal_events({
move |event| match event {
TerminalEvent::Key(KeyEvent { code, kind, .. }) if kind != KeyEventKind::Release => {
match code {
KeyCode::Char('q') => should_exit.set(true),
_ => {}
}
}
_ => {}
}
});
if should_exit.get() {
system.exit();
}
element! {
View(
width,
height,
background_color: Color::DarkGrey,
border_style: BorderStyle::Double,
border_color: Color::Blue,
align_items: AlignItems::Center,
justify_content: JustifyContent::Center,
) {
Hoverable {
Text(content: "Hover over me!")
}
}
}
}
fn main() {
smol::block_on(element!(Example).fullscreen()).unwrap();
}The |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
May I please have help! I see that inside the macro
element!()the syntax#(<Iterable>)can be used to map data into elements but I cant figure out how to pass inprops.children? I don't see it in any of the examples?I am trying to make a button that supports hover styles; please see below code where I have hacked a long and ugly solution using
ComponentUpdater::update_children(...)ViewtoMyButtonuse_terminal_eventsANDuse_local_terminal_eventsiocraftsource code to remove#[non_exhaustive]fromCanvasTextStyleto be able to do this in user code 😟The code works but ends up being a horrible hacky duplication of the
Viewcomponent this way. Any help to find a better way would be much appreciated :)Beta Was this translation helpful? Give feedback.
All reactions