Macaw.Container behaviour (macaw v0.6.0)

Copy Markdown View Source

The core's side of a container (spec/05 §5): a component of kind :container whose content is surfaces, its panes.

macaw_component owns the kind, its patterns and rendering the panes a container is handed in @settings["panes"]; the core owns the rest (spec/05 §7):

  • panes are surfaces a container cell lists under panes (Macaw.Surface.Cell), each with its own state, resolved by Macaw.Page.validate/3 and held once on the page (Macaw.Page.surfaces/1);
  • they render lazily and compute eagerly: every pane is a surface of the page, shown or not, and shown/3 says which ones render now; render/3 renders a page surface with its shown panes, and no other;
  • opening one is a view change, the container's open event setting its per-cell state <cell>.open (spec/03 §1.3), never a navigation;
  • nesting is only through containers, to a depth of max_depth/0.

It is also a behavior the built-in containers implement beside Macaw.Component, with two optional callbacks a third-party container may give too: panes/0, the count of panes it takes, one or more without it; and shown/2, the panes it shows for its settings, without it the pane its open setting names, else the first.

Summary

Callbacks

The panes the container takes, {min, max}, max a count or :infinity.

The panes a container shows, of its resolved settings and its panes' names in order: a sublist of names, in order.

Functions

How deep surfaces nest through containers (spec/05 §5.4): a region's surface is at depth 1, and a pane one deeper than the surface its container sits on.

A page surface rendered with its shown panes, depth first (spec/05 §5.2): {:ok, markup}, a safe value, or {:error, faults}, never a raise.

The panes a container cell shows now (spec/05 §5.2), in pane order: {:ok, names}.

Callbacks

panes()

(optional)
@callback panes() :: {pos_integer(), pos_integer() | :infinity}

The panes the container takes, {min, max}, max a count or :infinity.

shown(settings, names)

(optional)
@callback shown(settings :: map(), names :: [String.t()]) :: [String.t()]

The panes a container shows, of its resolved settings and its panes' names in order: a sublist of names, in order.

Functions

max_depth()

@spec max_depth() :: pos_integer()

How deep surfaces nest through containers (spec/05 §5.4): a region's surface is at depth 1, and a pane one deeper than the surface its container sits on.

iex> Macaw.Container.max_depth()
3

render(page, name, env)

@spec render(Macaw.Page.Validated.t(), String.t(), keyword()) ::
  {:ok, Phoenix.HTML.safe()} | {:error, [Macaw.Base.Fault.t(), ...]}

A page surface rendered with its shown panes, depth first (spec/05 §5.2): {:ok, markup}, a safe value, or {:error, faults}, never a raise.

validated is a validated page and name one of its surfaces, a region's or a pane's. For each container cell it fetches and renders each shown pane, and no other, with the pane's own state, its heading level one below the container's cells' and its width the container cell's declared width, then hands the markup to the container in panes: (Macaw.Surface.cells/3).

env is Macaw.Surface.fetch/2's and render/3's, the same for every surface, with states:, %{surface => %Macaw.State{}}, each surface's state, at its initial values when not given. Its faults: a surface the page doesn't hold, {[:name], {:unknown_surface, name}}; states: that isn't a map, {[:states], {:type, :map}}; a state its surface refuses, at [:states, surface | path]; an env that isn't a keyword list, {[], {:type, :keyword}}; and an env Macaw.Surface.readers/2 or cells/3 refuses, at [:surfaces, surface | path].

shown(validated, cell, opts)

@spec shown(Macaw.Surface.Validated.t(), String.t(), keyword()) ::
  {:ok, [String.t()]} | {:error, [Macaw.Base.Fault.t(), ...]}

The panes a container cell shows now (spec/05 §5.2), in pane order: {:ok, names}.

validated is the container's surface, validated, and cell the container cell's name. opts: state:, the surface's %Macaw.State{} (each state at its initial value when not given), at which its bound settings resolve as rendering resolves them; mode:, :socketless by default. In :print and :neutral, where nothing can open a pane, every pane shows. A pane not shown is never rendered, but it computes as any surface of the page does.

Its faults, every one at once: a name no cell has, {[:cell], {:unknown_cell, name}}; a cell that isn't a container, {[:cell], {:not_a_container, name}}; a mode that is none, {[:mode], {:enum, modes}}; an unknown option, :unknown_key; a state of another surface, {[:state], {:surface_mismatch, name}}, or one that is no state, {[:state], {:type, :state}}; opts that aren't a keyword list, {[], {:type, :keyword}}.