#[component]Expand description
Turn a function into a reusable component, React style.
use rok_ui::prelude::*;
/// A greeting card.
#[component]
pub fn Greeting(
name: SharedString,
#[default] excited: bool,
#[default] on_wave: Option<EventHandler<ClickEvent>>,
#[children] children: Vec<AnyElement>,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
div().child(format!("Hello, {name}{}", if excited { "!" } else { "." })).children(children)
}
// Usage: required props go to `new`, optional props are builder methods,
// children use the familiar `.child(..)`.
let greeting = Greeting::new("Ada").excited(true).on_wave(|_, _, _| {}).child("Welcome back");Parameter rules:
- A parameter named
windoworcxreceives the GPUI window or app context.cx: &mut Cxreceives both as oneCxhandle instead (then there is nowindowparameter). - Plain parameters are required props and become arguments of
new(..)(takingimpl Into<T>). #[default]parameters start atDefault::default(),#[default(expr)]ones atexpr, and get a builder method with the same name. ForOption<T>the method takesimpl Into<T>.#[prop(optional)]is the older spelling of#[default].- Props of type
EventHandler<E>(orOption<EventHandler<E>>) take a closureFn(&E, &mut Window, &mut App)directly. - One
#[children]parameter of typeVec<AnyElement>makes the component aParentElement, so.child(..)and.children(..)work. - One
#[style]parameter of typeStyleRefinementmakes the componentStyled, so callers can chain.w_full(),.mt_4(), … like aclassName. Apply it in the body with.apply_style_overrides(&style_overrides). - One
#[sx] sx: Sxparameter makes the component accept.sx(..)styles (StyleX’sxstyle). Apply them last in the body:.sx((&MY_STYLES.base, &sx)).