Trait intuitive::components::Component
source · [−]pub trait Component {
fn render(&self) -> AnyElement;
}
Expand description
A trait describing structures that can be rendered to an Element
.
Before implementing the Component
trait directly, make sure that what you are trying
to do can’t be done through the component
attribute macro, as there are
a few nuances and implicit requirements when implementing Component
.
Implementing Component
The general idea behind the Component
trait is that it orchestrates the construction
of Element
s. Element
s know how to be drawn and how to handle keys.
Before implementing component, it’s important to understand the implicit expectations that Intuitive makes when rendering components.
Invariants & Expectations
- When rendering a frame,
Component::render
must be called on everyComponent
, even if it is not being drawn this frame.- This is to ensure that hooks, such as
use_state
, are always called in the same order. - This can typically be guaranteed by always calling
Component::render
on your component’s children.
- This is to ensure that hooks, such as
Component::render
must never be called outside ofComponent::render
. This is to continue the assurances made in the previous point.- Structures implementing
Component
, must also implementDefault
. - Structures implementing
Component
must have all of their fields public. - Structures implementing
Component
should have anon_key
parameter if they also take inchildren
. Thison_key
parameter should be of typeKeyHandler
and should default to forwarding the key events to the children.
Refer to the Section
component source as an example component that
adheres to these invariants.
Custom Appearance
In order to customize how a component is drawn by the Terminal
, you must
create a struct that implements Element
. This is typically done by
creating two structs, one that implements Component
, and a “frozen” struct
that implements Element
, and the one implementing Component
returns the
custom Element
on Component::render
.
Typically, when a component accepts Children<N>
and returns a custom Element
,
the “frozen” structure that is constructed takes in [AnyElement; N]
as its
children, because Component::render
was called on the Children<N>
. Again,
refer to the Section
component source that also returns a custom Element
.
Required Methods
fn render(&self) -> AnyElement
Implementors
impl Component for Modal
experimental
only.impl Component for Centered
impl Component for Embed
impl Component for Empty
impl Component for Section
impl Component for Text
impl<const N: usize> Component for Table<N>
experimental
only.