Search documentation of Plushie

# Plushie.Tree.Node protocol (Plushie v0.7.2)

Copy Markdown View Source

Protocol for converting typed structs to `ui_node()` maps.

Widgets and canvas elements define typed structs with builder functions. This protocol converts those structs to the plain `%{id, type, props, children}` maps that the runtime, tree differ, and wire encoder expect.

The runtime calls this protocol automatically during tree normalization, so structs can be returned directly from `view/1` without an explicit `build/1` call.

## Example

    alias Plushie.Widget.Button

    # Builder pattern: struct returned, runtime converts automatically
    Button.new("btn", "Click me")
    |> Button.style(:primary)
    |> Button.width(:fill)

    # Explicit conversion if you want the map
    Button.new("btn", "Click me")
    |> Button.style(:primary)
    |> Button.build()
    #=> %{id: "btn", type: "button", props: %{label: "Click me", style: "primary"}, children: []}

## Summary

### Types

t()

All the types that implement this protocol.

ui_node()

A UI tree node map. Every widget builder returns this shape.

### Functions

to_node(widget)

Converts a struct to a `Plushie.Tree.Node.ui_node()` map.

## Types

# t()

    @type t() :: term()

All the types that implement this protocol.

# ui_node()

    @type ui_node() :: %{
      id: String.t(),
      type: String.t(),
      props: %{optional(atom()) => term()},
      children: [ui_node()]
    }

A UI tree node map. Every widget builder returns this shape.

## Functions

# to_node(widget)

    @spec to_node(t()) :: ui_node()

Converts a struct to a `Plushie.Tree.Node.ui_node()` map.