# `Macaw.Frame.Legend`
[🔗](https://github.com/dcoai/macaw_frame/blob/v0.1.0/lib/macaw/frame/legend.ex#L1)

The shared Legend (spec/01 §7, spec/13 §7.4): one legend for several Frames, for a page
whose charts share a color mapping, so each of them can drop its own (`legend: "none"`).

| Component | Name | Kind | Isolation |
|---|---|---|---|
| Legend | `"macaw.legend"` | `:content` | `:pure` |

| Parameter | Type | Default | Bindable | Meaning |
|---|---|---|---|---|
| `label` | `:text` | `nil` | yes | the list's accessible name; without one, the `value` field's label from the schema |
| `hidden` | `:category` | `nil` | yes | the value hidden, bound to the page's state of hidden values, its entry drawn not pressed |

- **The mapping is data, not a Frame's internals.** The one slot, `"entries"`, has the
  roles `value` (`:category`), `label` (`:text`, optional) and `color` (`:term`, a column
  of colors, optional; data holds no color type). Bound to the field the Frames color by,
  whose schema declares its `values`, the entries are the declared values in order, each
  labeled by its declared name and painted its declared color (the page's, in the
  context's `colors`, else its `status` role's, as `Macaw.Frame.Fact`'s `:colors` reads
  it), else the theme's series color at its
  declared position:
  exactly what a Frame's ordinal color scale over the same field draws
  (`Macaw.Frame.Scales`, macaw_draw spec/06 §2.3). Bound to a derived source answering one row per
  entry, they are its rows' distinct values in order, labeled and painted by the
  `label` and `color` roles, a data color under the theme's 3:1 guard (spec/11 §8.2).
- **Entries are drawn as the frame's legend draws them:** each swatch is
  `Macaw.Frame.Furniture.Legend.swatch/2` filled with `paint/4`'s answer, rendered by
  `Macaw.Draw.Render.SVG`, and a label as the axis prints a value
  (`Macaw.Draw.Axis.default_label/1`) where no name is declared.
- **Hiding is state.** A click or Enter on an entry emits `"toggle"`, its `value` the
  entry's value and no identity (`Macaw.Component.Client.event/3`); a placement maps it
  to a state source of the hidden values, which each Frame and this Legend's `hidden`
  bind. Until multi-valued state (macaw#164) the Legend toggles one value.
- **It renders** `<div class="mw-legend-box" id="<instance>-legend">` holding a `<ul
  class="mw-legend">` of entries, each a `<button aria-pressed>` (pressed while its value
  is shown) holding its swatch, which carries its cue (`data-mw-cue="label"`, the label
  beside it), never a color alone, and its label. It declares no hook.
- **Its drawn states** are the contract's notices (`Macaw.Frame.Drawn.notice/3`), written
  before the list.

**Decision: the visual check set's `:image`, `:table` and `:tab_stop` checks do not apply
to the Legend.** The visual set (macaw_component spec/04 §4) asks a component that draws
data for a named image, a `<table>` beside it and exactly one tab stop. The Legend draws
no data: it is a list of controls whose every entry is already text, so an image would
name nothing the list does not, a table would repeat the list, and §7's entries are
buttons, one tab stop each, since moving among them by arrow keys would need a hook,
which the Legend declares none of (§1). It runs every core check and the visual set's
`:token` and `:svg` checks.

# `entry`

```elixir
@type entry() :: %{value: term(), label: String.t(), paint: term()}
```

One entry: its value, its label and its paint, as drawn.

# `entries`

```elixir
@spec entries(term(), term(), term()) :: [entry()] | {:error, [{list(), term()}, ...]}
```

The entries the Legend draws for `context` over `deliveries` (spec/01 §7), in order: the
declared values of the field bound to `value`, where its schema declares them, else the
delivered rows' distinct values. Each is `%{value, label, paint}`, `paint` resolved
against the context's theme in `mode` (`:css` by default, `:literal`) as the frame's
legend paints a swatch. A context or deliveries of the wrong type is a type fault at
its argument.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
