stratum_core/component.rs
1use crate::event::{ComponentEvent, EventResult};
2use crate::props::Props;
3use crate::render::RenderOutput;
4use crate::state::State;
5
6/// The core trait all NexusStratum UI components implement.
7///
8/// `Component` defines the framework-agnostic lifecycle of a UI component:
9/// - Initialize state from props
10/// - Render a framework-agnostic description of the component
11/// - Handle events and update state
12///
13/// Framework adapters (stratum-leptos, stratum-dioxus) bridge this trait
14/// to each framework's specific rendering and reactivity model.
15pub trait Component: Sized + 'static {
16 /// The property type for this component.
17 type Props: Props;
18
19 /// The internal state type for this component.
20 type State: State;
21
22 /// Create the initial state from the given props.
23 fn initial_state(props: &Self::Props) -> Self::State;
24
25 /// Produce a framework-agnostic render description.
26 ///
27 /// The returned [`RenderOutput`] describes the component's attributes,
28 /// CSS classes, ARIA attributes, and children. Framework adapters
29 /// translate this into actual DOM/VDOM elements.
30 fn render(props: &Self::Props, state: &Self::State) -> RenderOutput;
31
32 /// Handle an event and optionally mutate state.
33 ///
34 /// Returns an [`EventResult`] indicating whether the event should
35 /// be prevented, stopped, or if state changed (triggering re-render).
36 fn on_event(
37 props: &Self::Props,
38 state: &mut Self::State,
39 event: ComponentEvent,
40 ) -> EventResult;
41
42 /// Called when props change. Returns true if state needs updating.
43 ///
44 /// Default implementation always returns false (state is independent of props).
45 /// Override for components where prop changes should sync to state.
46 fn props_changed(
47 _old_props: &Self::Props,
48 _new_props: &Self::Props,
49 _state: &mut Self::State,
50 ) -> bool {
51 false
52 }
53
54 /// Called before the component is unmounted.
55 ///
56 /// Default implementation is a no-op. Override for cleanup logic.
57 fn cleanup(_props: &Self::Props, _state: &mut Self::State) {}
58}