Macro intuitive::render

source ·
render!() { /* proc-macro */ }
Expand description

Macro for rendering components.

Usage

This macro is meant to be used when returning from components::Component::render, and uses a SwiftUI-like syntax.

For example:

let _: AnyComponent = render! {
  VStack() {
    Section(title: "Top Section") {
      Text(text: "Hello")
    }

    Section(title: "Bottom Section") {
      Text(text: "World")
    }
  }
};

is rendering a VStack (with default parameters), and two children. The child components are Sections, each with their own Text child components.

Parameters

Parameters passed to components look like function arguments but are actually much closer to structure initialization. Like struct fields, they can be passed in any order, and they require the field name, unless the parameter and value are the same identifier. Unlike struct fields, you can omit parameters, as any omitted parameters are implicitly passed in with their default values.

Automatic Parameter Conversion

When passing parameters to components within a render! macro invocation, an implicit TryInto::try_into call is made for each parameter. This means that you can omit any .into() calls when passing parameters to components. This is very useful when working with Spans and Style, as they implement From from a variety of types.

Children

Children to a component come after the component surrounded by braces ({ ... }). Like parameters, children are optional, but are only valid for components that accept them (for example Text accepts no children, but Section does).

Children are passed as arrays ([AnyComponent; N]), so components specify exactly how many children they take in. Some components, like VStack and HStack take in a variable number of children, while some, like Section, only accept a single child component.

Macro for rendering components.

See the documentation in the intuitive crate for details.