Macaw.Component.Context (macaw_component v0.13.0)

Copy Markdown View Source

The context a component renders in (spec/02 §2): its mode, instance, size, the display's pixel ratio, root font size, heading level, theme, the viewer's preferences and motion preference, the holder's now, the cell's resolved bindings, each bound slot's schema, and the colors the page or design declares for a field's values.

ratio is how many device pixels a CSS pixel holds where the instance is shown, 1 until a hook's size report says otherwise; motion is :reduce when the viewer asks for less motion, so a value that eases on the server draws its targets at once.

The host computes it: on a page the core, elsewhere the adopter. The component never knows which, and never reads a clock: now is the holder's. bindings is %{slot => %{role => field | [field, ...]}}, each slot's binding as Macaw.Component.Slot.resolve/3 resolves it, so a component reads the field bound to a role with field/3 and never repeats the defaulting rule. schemas is %{slot => Macaw.Data.Schema.t()}, the schema each bound slot's source describes, so a component heads a field with its label/3 and unit/3. link is the host's link builder, set in :socketless when the host keeps state in the URL: a component asks it for the href an event would give with Macaw.Component.Client.link/3, never directly. selection is the selection the host hands the cell, nil for none, in the shape a contained cell is handed it through bound/3: one entry per identity field, in name order, %{fields: [field, ...], value: value}. A :pure component draws it by asking selected?/2 of each row.

iex> {:ok, ctx} = Macaw.Component.Context.new(mode: :live, instance: "cards-1", size: {640, 400}, now: ~U[2026-10-28 12:00:00Z])
iex> {Macaw.Component.Context.heading(ctx, 0), Macaw.Component.Context.heading(ctx, 1)}
{"h2", "h3"}
iex> Macaw.Component.Context.id(ctx, "item-3")
"cards-1-item-3"
iex> {:ok, ctx} = Macaw.Component.Context.new(mode: :live, instance: "chart-1", size: {640, 400}, now: ~U[2026-10-28 12:00:00Z], bindings: %{"points" => %{"x" => "t", "y" => ["in", "out"]}})
iex> {Macaw.Component.Context.field(ctx, "points", "x"), Macaw.Component.Context.field(ctx, "points", "y"), Macaw.Component.Context.field(ctx, "points", "label")}
{"t", ["in", "out"], nil}
iex> {:ok, ctx} = Macaw.Component.Context.new(mode: :live, instance: "map-1", size: {640, 400}, now: ~U[2026-10-28 12:00:00Z], selection: [%{fields: ["station", "site"], value: "s-2"}])
iex> {Macaw.Component.Context.selected?(ctx, %{"site" => "s-2"}), Macaw.Component.Context.selected?(ctx, %{"station" => "s-1"})}
{true, false}

Summary

Types

The page's or design's colors for a field's values (spec/02 §2, #101): %{field => %{value => color}}, each field named as the schema spells it and each color a color value (Macaw.Layers.Color: a literal, or a theme's color or a transform of one).

The viewer's motion preference (spec/02 §2): full motion, or reduced.

The selection the host hands the cell (spec/02 §2), in the shape a contained cell is handed it: nil for none, else one entry per field of the identity, in name order, each with the cell's fields that are that field through the surface's links and the value.

t()

Functions

The color the page or design declares for value of field (spec/02 §2, #101): {:ok, color}, or :error when it declares none. The field is read by either spelling of its name, and a value by the term it is, an atom also by its spelling, as a table reads a category back as its string.

The field bound to role of slot: a string for a one-field role, the list of fields for a many role, nil for a slot or role the context does not bind.

The fields, in the order of spec/02 §2's table.

The heading element depth levels below the component's top (0 for its top): "h<level + depth>", never past "h6".

An element id unique to the instance, "<instance>-<local>", stable across renders. local is a non-empty string of letters, digits, - and _; anything else raises ArgumentError, a programming error in the component.

The words that head field of slot: its schema's label, else the field's name, so a header never needs the schema to exist.

The motion preferences, :full then :reduce (spec/02 §2).

A context from a keyword list or a map: {:ok, context} or every fault at once, each {[field], reason}, an unknown field :unknown_key and a missing required one :required. Each given field is judged once, in one pass over what is given, and a keyword list that repeats a key is judged by its last value. The theme defaults to Macaw.Layers.Theme.default(), evaluated only for a context given none. A bad binding is a fault at its path under [:bindings]: :not_a_map, :bad_name for a slot or role that is not a non-empty string, :not_a_field for a field that is neither a non-empty string nor a non-empty list of them. A bad schemas is a fault under [:schemas]: :not_a_map, :bad_name for a slot, :not_a_schema for a value that is no %Macaw.Data.Schema{}. A link that is neither nil nor a function of one argument is {[:link], :not_a_function}. A ratio that is no positive number is {[:ratio], :not_positive}, and a motion other than :full or :reduce {[:motion], {:unknown_motion, motion}}. The preferences are zone, units, shortcuts and locale, the viewer's %Macaw.Format.Locale{}: preferences that leave locale out carry Macaw.Format.Locale.english(), and a locale that is no Locale is {[:preferences, :locale], :not_a_locale}. A selection that is neither nil nor a non-empty list of maps of exactly fields, a non-empty list of non-empty strings, and value, a scalar (no nil, list or plain map), is {[:selection], :not_a_selection}. colors, %{} by default, is %{field => %{value => color}} (#101): a field that is not a non-empty string is {[:colors, field], :bad_name}, a field's values that are no map {[:colors, field], :not_a_map}, and a color that is no color value {[:colors, field, value], :not_a_color}.

Whether row holds the selection the context carries (spec/02 §2): for every entry, one of its fields holds the entry's value (==), so a composite identity needs every entry to match and a linked field matches as its canonical one does. A row is a map read by either spelling of a field's name, as Macaw.Data.Table.get/2 reads one. With no selection, no row holds it.

The fields that hold for a placement's life (spec/02 §1), in fields/0's order: those a component's prepare/2 may read. A host prepares a placement again when one of them changes, as when its settings do; the others (mode, size, measured, ratio, motion, now, link, selection) change from render to render.

The unit field of slot is posted in, its schema's UCUM unit, or nil for none.

A context from parts a host has already judged (#86): a keyword list or a map of the fields, built into the context without judging any of them again. It trusts its caller. Every given field must be one new/1 would keep, the four required fields among them, as a host's own validation produced them; this constructor checks none of it. For such input it answers the context new/1 answers, its defaults filled in the same way: the theme Macaw.Layers.Theme.default() when none is given, and preferences that leave locale out carrying Macaw.Format.Locale.english().

Types

colors()

@type colors() :: %{required(String.t()) => %{required(term()) => term()}}

The page's or design's colors for a field's values (spec/02 §2, #101): %{field => %{value => color}}, each field named as the schema spells it and each color a color value (Macaw.Layers.Color: a literal, or a theme's color or a transform of one).

motion()

@type motion() :: :full | :reduce

The viewer's motion preference (spec/02 §2): full motion, or reduced.

selection()

@type selection() :: nil | [%{fields: [String.t(), ...], value: term()}, ...]

The selection the host hands the cell (spec/02 §2), in the shape a contained cell is handed it: nil for none, else one entry per field of the identity, in name order, each with the cell's fields that are that field through the surface's links and the value.

t()

@type t() :: %Macaw.Component.Context{
  bindings: %{
    required(String.t()) => %{
      required(String.t()) => String.t() | [String.t(), ...]
    }
  },
  colors: colors(),
  heading_level: 1..6,
  instance: String.t(),
  link: (Macaw.Component.Client.link_event() -> String.t() | nil) | nil,
  measured: boolean(),
  mode: Macaw.Component.Mode.t(),
  motion: motion(),
  now: DateTime.t(),
  preferences: %{
    zone: String.t(),
    units: atom(),
    shortcuts: map(),
    locale: Macaw.Format.Locale.t()
  },
  ratio: number(),
  root_font_size: number(),
  schemas: %{required(String.t()) => Macaw.Data.Schema.t()},
  selection: selection(),
  size: {number(), number()},
  theme: Macaw.Layers.Theme.t()
}

Functions

color(context, field, value)

@spec color(t(), String.t() | atom(), term()) :: {:ok, term()} | :error

The color the page or design declares for value of field (spec/02 §2, #101): {:ok, color}, or :error when it declares none. The field is read by either spelling of its name, and a value by the term it is, an atom also by its spelling, as a table reads a category back as its string.

Precedence is the reader's: a component colors a value by this mapping first, then by the value's status role, then by its position in the series.

iex> {:ok, ctx} = Macaw.Component.Context.new(mode: :live, instance: "c", size: {1, 1}, now: ~U[2026-10-28 12:00:00Z], colors: %{"platform" => %{"ios" => {:color, "info"}}})
iex> {Macaw.Component.Context.color(ctx, :platform, :ios), Macaw.Component.Context.color(ctx, "platform", "web")}
{{:ok, {:color, "info"}}, :error}

field(context, slot, role)

@spec field(t(), String.t(), String.t()) :: String.t() | [String.t(), ...] | nil

The field bound to role of slot: a string for a one-field role, the list of fields for a many role, nil for a slot or role the context does not bind.

fields()

@spec fields() :: [atom(), ...]

The fields, in the order of spec/02 §2's table.

heading(context, depth)

@spec heading(t(), non_neg_integer()) :: String.t()

The heading element depth levels below the component's top (0 for its top): "h<level + depth>", never past "h6".

id(context, local)

@spec id(t(), String.t()) :: String.t()

An element id unique to the instance, "<instance>-<local>", stable across renders. local is a non-empty string of letters, digits, - and _; anything else raises ArgumentError, a programming error in the component.

label(context, slot, field)

@spec label(t(), String.t(), String.t()) :: String.t()

The words that head field of slot: its schema's label, else the field's name, so a header never needs the schema to exist.

motions()

@spec motions() :: [motion(), ...]

The motion preferences, :full then :reduce (spec/02 §2).

new(attrs)

@spec new(keyword() | map()) :: {:ok, t()} | {:error, [Macaw.Component.fault(), ...]}

A context from a keyword list or a map: {:ok, context} or every fault at once, each {[field], reason}, an unknown field :unknown_key and a missing required one :required. Each given field is judged once, in one pass over what is given, and a keyword list that repeats a key is judged by its last value. The theme defaults to Macaw.Layers.Theme.default(), evaluated only for a context given none. A bad binding is a fault at its path under [:bindings]: :not_a_map, :bad_name for a slot or role that is not a non-empty string, :not_a_field for a field that is neither a non-empty string nor a non-empty list of them. A bad schemas is a fault under [:schemas]: :not_a_map, :bad_name for a slot, :not_a_schema for a value that is no %Macaw.Data.Schema{}. A link that is neither nil nor a function of one argument is {[:link], :not_a_function}. A ratio that is no positive number is {[:ratio], :not_positive}, and a motion other than :full or :reduce {[:motion], {:unknown_motion, motion}}. The preferences are zone, units, shortcuts and locale, the viewer's %Macaw.Format.Locale{}: preferences that leave locale out carry Macaw.Format.Locale.english(), and a locale that is no Locale is {[:preferences, :locale], :not_a_locale}. A selection that is neither nil nor a non-empty list of maps of exactly fields, a non-empty list of non-empty strings, and value, a scalar (no nil, list or plain map), is {[:selection], :not_a_selection}. colors, %{} by default, is %{field => %{value => color}} (#101): a field that is not a non-empty string is {[:colors, field], :bad_name}, a field's values that are no map {[:colors, field], :not_a_map}, and a color that is no color value {[:colors, field, value], :not_a_color}.

selected?(context, row)

@spec selected?(t(), map()) :: boolean()

Whether row holds the selection the context carries (spec/02 §2): for every entry, one of its fields holds the entry's value (==), so a composite identity needs every entry to match and a linked field matches as its canonical one does. A row is a map read by either spelling of a field's name, as Macaw.Data.Table.get/2 reads one. With no selection, no row holds it.

stable_fields()

@spec stable_fields() :: [atom(), ...]

The fields that hold for a placement's life (spec/02 §1), in fields/0's order: those a component's prepare/2 may read. A host prepares a placement again when one of them changes, as when its settings do; the others (mode, size, measured, ratio, motion, now, link, selection) change from render to render.

iex> Macaw.Component.Context.stable_fields()
[:instance, :root_font_size, :heading_level, :theme, :preferences, :bindings, :schemas, :colors]

unit(context, slot, field)

@spec unit(t(), String.t(), String.t()) :: String.t() | nil

The unit field of slot is posted in, its schema's UCUM unit, or nil for none.

validated(attrs)

@spec validated(keyword() | map()) :: t()

A context from parts a host has already judged (#86): a keyword list or a map of the fields, built into the context without judging any of them again. It trusts its caller. Every given field must be one new/1 would keep, the four required fields among them, as a host's own validation produced them; this constructor checks none of it. For such input it answers the context new/1 answers, its defaults filled in the same way: the theme Macaw.Layers.Theme.default() when none is given, and preferences that leave locale out carrying Macaw.Format.Locale.english().

Use it where the host builds a context per cell per render from parts it validated once, such as a cell's bindings and schemas; anything else goes through new/1, which judges every field. A field outside fields/0 is a programming error in the caller.

iex> attrs = [mode: :live, instance: "chart-1", size: {640, 400}, now: ~U[2026-10-28 12:00:00Z], bindings: %{"points" => %{"x" => "t"}}]
iex> {:ok, Macaw.Component.Context.validated(attrs)} == Macaw.Component.Context.new(attrs)
true