pub trait Component {
    // Required method
    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 Elements. Elements 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

  1. When rendering a frame, Component::render must be called on every Component, 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.
  2. Component::render must never be called outside of Component::render. This is to continue the assurances made in the previous point.
  3. Structures implementing Component, must also implement Default.
  4. Structures implementing Component must have all of their fields public.
  5. Structures implementing Component should have an on_key parameter if they also take in children. This on_key parameter should be of type KeyHandler 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§

Implementors§

source§

impl Component for Input

source§

impl Component for Modal

source§

impl Component for Scroll

source§

impl Component for Centered

source§

impl Component for Embed

source§

impl Component for Empty

source§

impl Component for Section

source§

impl Component for Text

source§

impl<const N: usize> Component for Table<N>

source§

impl<const N: usize> Component for intuitive::components::HStack<N>

source§

impl<const N: usize> Component for intuitive::components::VStack<N>