This tutorial teaches Macaw from nothing to the depths of what macaw 0.5.0 ships. It's for Elixir developers new to Macaw: you'll write sources, compose surfaces, build and test components, store documents, theme them and serve them from Phoenix, with every example run against the released code.

What Macaw is

Macaw is a family of building blocks for live, data-driven applications on Phoenix. It composes pages out of independent components that share typed data through one source contract. You declare the arrangement and the data flow in validated documents, and Macaw renders them on the server: complete HTML, nothing inline, safe under a strict Content Security Policy.

The reason to compose through Macaw is composition effects: components that declare themselves once (their data inputs, the state they write, what they observe, how they render) gain behavior none of them has alone, for every combination, without wiring each pair. Shared fetching, one "now" across a page, honest notices when data is missing, one theme and one heading outline are the effects the released core already delivers; the rest arrive with the runtime still to come.

The family

macaw_layers ← macaw_data ← macaw_component ← macaw
  documents,     sources,     the component     the core: surfaces,
  faults,        schemas,     contract, its     layouts, the registry,
  fragments,     tables,      context, drawn    validation, fetching,
  stores,        deliveries,  states and its    rendering, the built-in
  themes         operations,  conformance kit   components
                 derived
                 sources
  • macaw_layers: the document kernel (names, faults, digests, and the JSON adapter with its gates), fragments and stacking, stores and bundles, colors and themes.
  • macaw_data: the data model (schemas, tables, deliveries, units), the source contract and its conformance kit, operations, and derived sources.
  • macaw_component: the component contract, rendering rules, drawn states and the component conformance kit. Component packages depend on it without the core.
  • macaw, this package: surfaces and their layouts (the grid, the stack, the flow and named areas), the registry of enabled components and layouts, validation, fetching and rendering, the one-call show/2 and the short code form, and the built-in components.

Dependencies run one way, left to right. Installing macaw brings the other three.

Across the family, internal data is BEAM terms. A surface, a schema, a theme or a cell's settings is the Elixir value it is: a color is a %Macaw.Layers.Color{}, a time a DateTime, a point a {longitude, latitude} tuple, and every name a string. Identity and validity are judged on that value. JSON is an adapter at the edge, for storing and sending documents, and it refuses what it can't hold.

How to read it

Read chapters 1 to 4 in order: they build one running example and introduce every core idea. After that, chapters stand on their own; read the ones you need. Each chapter starts with what you'll learn and ends with pitfalls and a recap.

Code blocks build on earlier blocks in the same chapter. Modules (defmodule) go in your project's lib/ (tests in test/); the rest you can run in iex -S mix. A line #=> value shows what the expression above it returns. The examples use modules from earlier chapters, MyApp.Services and MyApp.Latency most of all.

Contents

  1. Your first surface: install Macaw, write a source, render a surface in five lines, and see what they expand to.
  2. Sources: the source contract, descriptions, parameters, requests, finishing, the ceiling and pooling, and pushing sources.
  3. Schemas, tables and deliveries: what data means, units and quantities, columnar tables, and deliveries' served facts.
  4. Surfaces in depth: cells, layouts (the grid, the stack, the flow and named areas), cells as a list, the registry, validation and its faults, fetching, viewers, drawn states, rendering options.
  5. The built-in table: settings, sorting, selection, columns and headers, budgets, notices, its stylesheet, and what it leaves to you.
  6. Writing components: the component contract, slots and roles, rendering, contained components, packages and hooks.
  7. Testing with the conformance kits: the component kit, the source kit, and testing surfaces.
  8. Surfaces as documents: a surface's value and its JSON adapter, gates and limits, faults, layering with fragments, stores and bundles.
  9. Themes and styles: themes, the stylesheet, colors, data colors and styling surfaces.
  10. Surfaces in a Phoenix app: configuration, controllers, LiveViews and serving stylesheets.
  11. Operations and derived sources: table operations, statistics, derived sources and their engine.

What's released, and what isn't

macaw 0.5.0 holds surfaces, validated and rendered complete without a socket, and the built-in components (the table, the status grid, the stat, the meter, the record and the feed; chapter 5 teaches the table, and the others are in their modules' documentation and spec/05 §6), on macaw_layers 0.5, macaw_data 0.14 and macaw_component 0.11. A surface's layout is a behavior, Macaw.Layout, with the grid (6, 12 or 24 columns), the stack, the flow and named areas built in, and a host's registry holds the layouts it allows as it holds components. A surface and its cells' settings are terms, checked at validation against the types the components and sources declare, with JSON as their adapter; validation reports every fault a check can find; and a component reads the fields a document bound to its roles, and their labels and units, from its context. The host's defaults (its configured registry, the clock read once, its ceiling) let Macaw.Surface.show/2 validate, fetch and render in one call, and build!/2 write a surface in a few lines. The table chooses its columns by a role, heads them with the schema's labels, and has its own stylesheet. A surface holds state (chapter 4): typed values declared on it or given per cell by a component's default actions, so a table sorts with no declarations; a pure Macaw.State value that applies a visitor's events, read at the edge from what the browser sends; persistence in the URL or the session, as links that work with no socket; and snapshots. Everything in this tutorial runs on it.

Not yet released, and so not taught here: the state that waits (legal values from data, several values, the scopes past :surface, the preferences), exports and the page graph, pages, templates and regions, records and replay, the web plans, the built-in prose and cards, and macaw_live, the LiveView runtime that will notify changes, fetch only what they affect and isolate components in processes. The specification describes them; the order of work is in spec/00 §5.

Macaw is a very early release, and every 0.x version may change its API.

Where to go next