Macaw.Component.Client (macaw_component v0.13.0)

Copy Markdown View Source

The client protocols every adopter shares (spec/02 §8): the announcer's two regions, the announce event, the client bus, the event wire (§8.3), and a hook's requests and size reports (§8.5). Each is found, not created per component, so components share one announcer and one bus on any page.

The package ships a module implementing both for hooks, module_path/0, which a host serves and a hook imports at module_url/0. What a component declares reaches it as the data attributes attributes/1 renders.

Summary

Types

One render's builder of one event's attributes for many rows (spec/02 §8.3), from events/2: the payload's fixed part, encoded and escaped once. Hand it to row_event/2.

What a context's link builder is handed (spec/02 §8.4): the event, as terms.

A row's scalar, encoded and escaped once by row_scalar/1, for row_event/2 to splice wherever it takes that scalar (spec/02 §8.3).

Functions

The DOM event a hook dispatches to request an announcement.

The announcer's region ids.

The attribute carrying the announcement names a component declares (announces).

The event a host pushes a request's answer to the page as (spec/02 §8.5), its payload %{instance: id, name: name, answer: answer}.

The data attributes that hand a component's declared announcement names and bus keys to the client: announces in declared order and local's keys sorted, each a JSON list. A component puts them on the element its hook is attached to, or an ancestor of it within the component, for example <div phx-hook="Cards" {Client.attributes(@declarations)}>. Both are always present, so a component that declares nothing has nothing accepted.

The data attributes a component's hook reads (spec/02 §8.1): attributes/1's two, then the context's instance as data-mw-instance, the declared events, in declared order, as a JSON list in data-mw-events, and the declared requests, in declared order, as a JSON list in data-mw-requests. The client module's emit reads the instance and the events to send an event its hook works out in the browser (§8.2), and its request and onAnswer the instance and the requests (§8.5).

The property of window the client bus lives under.

The attributes that make an element emit an event (spec/02 §8.3), for HEEx to splice into its tag: <button type="button" {Client.event(@context, "sort", value: "p50")}>.

The attribute holding an event's JSON payload (spec/02 §8.3).

The one LiveView event every component's events arrive as (spec/02 §8.3).

A builder of name's attributes for many rows of one render (spec/02 §8.3), for a component that makes every row of a table or a list emit the same event with its own value: and identity:. The instance and the name are the same for every row, so they are encoded and HTML-escaped here, once; row_event/2 encodes each row's own part.

The attribute carrying the event names a component declares (events).

The attribute carrying the instance id of a component's element, for emit.

The href an event would give (spec/02 §8.4), for a control that works with no socket: in :socketless, what the context's link builder answers for the event, the context's instance, the name, and value: and identity: as event/3 takes them; nil in every other mode, and with no builder. A component that has one renders the control as <a href>, carrying no event wire; with nil it renders the control event/3 makes.

The attribute carrying the bus keys a component declares (local).

The shared client module's file in this package, relative to its root.

The same-origin path a hook imports the shared module from, where a host serves priv/static with Plug.Static, at: "/", from: {:macaw_component, "priv/static"}.

The LiveView event a hook asks its contained value through (spec/02 §8.5), its params %{"instance" => id, "name" => name, "payload" => payload}.

The attribute carrying the request names a component declares (requests).

One row's event attributes from events/2's builder (spec/02 §8.3), rendered as iodata to splice into a tag that a component builds as iodata: ["<button type=\"button\"", Client.row_event(events, value: v, identity: id), ">"].

A scalar a row writes more than once, encoded once (spec/02 §8.3): for a component that makes a row's value its identity, as a table does (value: v, identity: %{field => v}).

The DOM event a hook dispatches to report its instance's measured size (spec/02 §8.5), bubbling, with detail {width, height, ratio}.

Types

events()

@opaque events()

One render's builder of one event's attributes for many rows (spec/02 §8.3), from events/2: the payload's fixed part, encoded and escaped once. Hand it to row_event/2.

row_scalar()

@opaque row_scalar()

A row's scalar, encoded and escaped once by row_scalar/1, for row_event/2 to splice wherever it takes that scalar (spec/02 §8.3).

Functions

announce_event()

@spec announce_event() :: String.t()

The DOM event a hook dispatches to request an announcement.

announcer_ids()

@spec announcer_ids() :: %{polite: String.t(), assertive: String.t()}

The announcer's region ids.

announces_attribute()

@spec announces_attribute() :: String.t()

The attribute carrying the announcement names a component declares (announces).

answer_event()

@spec answer_event() :: String.t()

The event a host pushes a request's answer to the page as (spec/02 §8.5), its payload %{instance: id, name: name, answer: answer}.

attributes(declarations)

@spec attributes(Macaw.Component.Declarations.t()) :: [{String.t(), String.t()}, ...]

The data attributes that hand a component's declared announcement names and bus keys to the client: announces in declared order and local's keys sorted, each a JSON list. A component puts them on the element its hook is attached to, or an ancestor of it within the component, for example <div phx-hook="Cards" {Client.attributes(@declarations)}>. Both are always present, so a component that declares nothing has nothing accepted.

iex> {:ok, d} = Macaw.Component.Declarations.new([announces: ["matches"], local: %{"hover" => :time}], nil)
iex> Macaw.Component.Client.attributes(d)
[{"data-mw-announces", ~s(["matches"])}, {"data-mw-local", ~s(["hover"])}]

attributes(declarations, context)

The data attributes a component's hook reads (spec/02 §8.1): attributes/1's two, then the context's instance as data-mw-instance, the declared events, in declared order, as a JSON list in data-mw-events, and the declared requests, in declared order, as a JSON list in data-mw-requests. The client module's emit reads the instance and the events to send an event its hook works out in the browser (§8.2), and its request and onAnswer the instance and the requests (§8.5).

iex> {:ok, d} = Macaw.Component.Declarations.new([events: ["brush"]], nil)
iex> attrs = [instance: "status-chart", size: {640, 400}, now: ~U[2026-11-10 12:00:00Z]]
iex> {:ok, context} = Macaw.Component.Context.new([mode: :live] ++ attrs)
iex> Macaw.Component.Client.attributes(d, context)
[{"data-mw-announces", "[]"}, {"data-mw-local", "[]"}, {"data-mw-instance", "status-chart"}, {"data-mw-events", ~s(["brush"])}, {"data-mw-requests", "[]"}]

bus_name()

@spec bus_name() :: String.t()

The property of window the client bus lives under.

event(context, name, opts \\ [])

@spec event(Macaw.Component.Context.t(), String.t(), keyword()) :: [
  {String.t(), String.t()}
]

The attributes that make an element emit an event (spec/02 §8.3), for HEEx to splice into its tag: <button type="button" {Client.event(@context, "sort", value: "p50")}>.

In :live and :socketless, phx-click is event_name/0 and event_attribute/0 holds the event as JSON, the context's instance, the name, and value: and identity: when given; in :neutral and :print, nothing.

This is the wire's outbound edge: a component hands it the terms it holds, and it encodes them. value is a string, a number, a boolean, nil, a DateTime, a point {lon, lat}, a literal Macaw.Layers.Color, or a list or string-keyed map of these; identity a string-keyed map of strings, numbers, booleans, nil and DateTimes. A DateTime is written as its ISO 8601 spelling in UTC, shifted there if it is in another zone; a point as the list [lon, lat]; a color as its CSS hex (Color.to_css/1); everything else as JSON has it. Anything else is a wrong call.

iex> attrs = [instance: "status-table", size: {640, 400}, now: ~U[2026-11-10 12:00:00Z]]
iex> {:ok, context} = Macaw.Component.Context.new([mode: :live] ++ attrs)
iex> Macaw.Component.Client.event(context, "sort", value: %{"sort" => "p50", "order" => "desc"})
[{"phx-click", "mw-event"}, {"phx-value-mw", ~s({"instance":"status-table","name":"sort","value":{"order":"desc","sort":"p50"}})}]
iex> Macaw.Component.Client.event(context, "select", identity: %{"t" => ~U[2026-11-10 11:00:00Z]})
[{"phx-click", "mw-event"}, {"phx-value-mw", ~s({"identity":{"t":"2026-11-10T11:00:00Z"},"instance":"status-table","name":"select"})}]
iex> {:ok, print} = Macaw.Component.Context.new([mode: :print] ++ attrs)
iex> Macaw.Component.Client.event(print, "sort", value: "p50")
[]

event_attribute()

@spec event_attribute() :: String.t()

The attribute holding an event's JSON payload (spec/02 §8.3).

event_name()

@spec event_name() :: String.t()

The one LiveView event every component's events arrive as (spec/02 §8.3).

events(context, name)

@spec events(Macaw.Component.Context.t(), String.t()) :: events()

A builder of name's attributes for many rows of one render (spec/02 §8.3), for a component that makes every row of a table or a list emit the same event with its own value: and identity:. The instance and the name are the same for every row, so they are encoded and HTML-escaped here, once; row_event/2 encodes each row's own part.

It takes the context and the name as event/3 does, and an empty or non-string name is the same wrong call. In :neutral and :print it is inert, and every row's attributes are empty.

events_attribute()

@spec events_attribute() :: String.t()

The attribute carrying the event names a component declares (events).

instance_attribute()

@spec instance_attribute() :: String.t()

The attribute carrying the instance id of a component's element, for emit.

link(context, name, opts \\ [])

@spec link(Macaw.Component.Context.t(), String.t(), keyword()) :: String.t() | nil

The href an event would give (spec/02 §8.4), for a control that works with no socket: in :socketless, what the context's link builder answers for the event, the context's instance, the name, and value: and identity: as event/3 takes them; nil in every other mode, and with no builder. A component that has one renders the control as <a href>, carrying no event wire; with nil it renders the control event/3 makes.

Only :socketless links: once the socket connects the page renders :live, where the control is the button again, so a link is the first render's and a socketless visit's alone. It carries no phx-click, since LiveView would both push the event and follow a real href. A builder answering anything but a string or nil is a wrong call.

iex> attrs = [instance: "status-table", size: {640, 400}, now: ~U[2026-11-10 12:00:00Z]]
iex> link = fn %{name: name, value: %{"sort" => sort}} -> "/status?" <> name <> "=" <> sort end
iex> {:ok, socketless} = Macaw.Component.Context.new([mode: :socketless, link: link] ++ attrs)
iex> Macaw.Component.Client.link(socketless, "sort", value: %{"sort" => "p50"})
"/status?sort=p50"
iex> {:ok, live} = Macaw.Component.Context.new([mode: :live, link: link] ++ attrs)
iex> Macaw.Component.Client.link(live, "sort", value: %{"sort" => "p50"})
nil

local_attribute()

@spec local_attribute() :: String.t()

The attribute carrying the bus keys a component declares (local).

module_path()

@spec module_path() :: String.t()

The shared client module's file in this package, relative to its root.

module_url()

@spec module_url() :: String.t()

The same-origin path a hook imports the shared module from, where a host serves priv/static with Plug.Static, at: "/", from: {:macaw_component, "priv/static"}.

request_event()

@spec request_event() :: String.t()

The LiveView event a hook asks its contained value through (spec/02 §8.5), its params %{"instance" => id, "name" => name, "payload" => payload}.

requests_attribute()

@spec requests_attribute() :: String.t()

The attribute carrying the request names a component declares (requests).

row_event(arg1, opts)

@spec row_event(events(), keyword()) :: iodata()

One row's event attributes from events/2's builder (spec/02 §8.3), rendered as iodata to splice into a tag that a component builds as iodata: ["<button type=\"button\"", Client.row_event(events, value: v, identity: id), ">"].

The bytes are those of event/3's attributes for the same context, name, value and identity once rendered with Phoenix.HTML.attributes_escape/1: a space, phx-click and phx-value-mw holding the payload as escaped JSON, the same JSON. value: and identity: are as event/3 takes them and are written by the same encoding rule; each may be left out, and they may come in either order. Another option or a value outside the rule is the same wrong call as for event/3. An inert builder answers [] for every row without encoding it.

Per row it builds no payload map and no attribute list, and validates no options: the row's value and identity are encoded with JSON.encode_to_iodata!/2 and escaped, and the rest was built once by events/2. A row_scalar/1 token, as value: or as a value of identity:, is spliced as the bytes it holds. In HEEx, where markup is safe data, use event/3.

iex> attrs = [instance: "status-table", size: {640, 400}, now: ~U[2026-11-10 12:00:00Z]]
iex> {:ok, context} = Macaw.Component.Context.new([mode: :live] ++ attrs)
iex> events = Macaw.Component.Client.events(context, "select")
iex> events |> Macaw.Component.Client.row_event(value: "a<b", identity: %{"host" => "a<b"}) |> IO.iodata_to_binary()
~s( phx-click="mw-event" phx-value-mw="{&quot;identity&quot;:{&quot;host&quot;:&quot;a&lt;b&quot;},&quot;instance&quot;:&quot;status-table&quot;,&quot;name&quot;:&quot;select&quot;,&quot;value&quot;:&quot;a&lt;b&quot;}")
iex> {:ok, print} = Macaw.Component.Context.new([mode: :print] ++ attrs)
iex> print |> Macaw.Component.Client.events("select") |> Macaw.Component.Client.row_event(value: "a")
[]

row_scalar(scalar)

@spec row_scalar(String.t() | number() | boolean() | nil | DateTime.t()) ::
  row_scalar()

A scalar a row writes more than once, encoded once (spec/02 §8.3): for a component that makes a row's value its identity, as a table does (value: v, identity: %{field => v}).

scalar is what an identity's value may be, a string, a number, a boolean, nil or a DateTime, and is written by the same encoding rule as row_event/2 writes it, then HTML-escaped. The token is opaque. row_event/2 takes it as value: and as a value of the identity: map, and splices its bytes in place of encoding the term, so a row's attributes are the bytes the term itself renders to. Anything else is the same wrong call as an identity value row_event/2 refuses, a FunctionClauseError, and so is a token anywhere else: nested in a value, or given to event/3.

An inert builder answers [] whatever it is given, so in :neutral and :print a component may skip the call, or make it and pay once.

iex> attrs = [instance: "status-table", size: {640, 400}, now: ~U[2026-11-10 12:00:00Z]]
iex> {:ok, context} = Macaw.Component.Context.new([mode: :live] ++ attrs)
iex> events = Macaw.Component.Client.events(context, "select")
iex> t = Macaw.Component.Client.row_scalar(~U[2026-11-10 11:00:00Z])
iex> events |> Macaw.Component.Client.row_event(value: t, identity: %{"t" => t}) |> IO.iodata_to_binary()
~s( phx-click="mw-event" phx-value-mw="{&quot;identity&quot;:{&quot;t&quot;:&quot;2026-11-10T11:00:00Z&quot;},&quot;instance&quot;:&quot;status-table&quot;,&quot;name&quot;:&quot;select&quot;,&quot;value&quot;:&quot;2026-11-10T11:00:00Z&quot;}")

size_event()

@spec size_event() :: String.t()

The DOM event a hook dispatches to report its instance's measured size (spec/02 §8.5), bubbling, with detail {width, height, ratio}.