# `Macaw.Region`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/region.ex#L1)

A region's declaration (spec/06 §4): `%Macaw.Region{name, persistent}`, the least a
persistent region needs before templates.

  * `name`, the region's name, lower-case words joined by `_`, as a surface's is; the
    instance of its content's `:region` state (spec/03 §2.1); required;
  * `persistent`, whether it survives live navigation between pages that share it
    (LiveView's sticky nested LiveView, macaw_live 04 §3); `false` by default.

A template (spec/06 §2.1) carries its regions as `%Macaw.Region{}`s by name, the struct
holding the keys a template gives it, beside `name` and `persistent`, whose meaning and
judgment are unchanged:

  * `role`, its landmark role, one of `roles/0`, `:region` by default;
  * `label`, its accessible name, a non-empty string or `nil`;
  * `fillable`, whether a page's own layer may fill it, and `hidden`, whether the page
    leaves it out, `false` by default;
  * `collapse`, what it does on a narrow screen (`collapses/0`, `:stack` by default),
    below its `breakpoint` (`breakpoints/0`, `:md` by default);
  * `content`, its default surfaces in a flow, by name, `[]` by default;
  * `classes`, its own CSP-safe classes (`class?/1`), `[]` by default;
  * `kinds`, the kinds a cell in it may take, `nil` for its role's (`kinds/1`).

It writes each as `node/1` does.

`kept/2` says what a surface instance in the region keeps across a change, and
`Macaw.State.follow/3` gives the state it holds then: its state follows its scope.

# `change`

```elixir
@type change() :: :navigate | :refill
```

# `role`

```elixir
@type role() ::
  :banner
  | :navigation
  | :main
  | :complementary
  | :contentinfo
  | :search
  | :form
  | :region
```

# `t`

```elixir
@type t() :: %Macaw.Region{
  breakpoint: :sm | :md | :lg | :xl,
  classes: [String.t()],
  collapse: :stack | :drawer | :hide | :none,
  content: [String.t()],
  fillable: boolean(),
  hidden: boolean(),
  kinds: [Macaw.Component.kind(), ...] | nil,
  label: String.t() | nil,
  name: String.t(),
  persistent: boolean(),
  role: role()
}
```

# `breakpoints`

```elixir
@spec breakpoints() :: [atom(), ...]
```

The widths below which a region collapses, narrowest first.

# `class?`

```elixir
@spec class?(term()) :: boolean()
```

Whether a term is a CSP-safe class: lower-case letters, digits, `-` and `_`, starting
with a letter, and never the library's prefix `mw-`.

    iex> Macaw.Region.class?("site-nav")
    true
    iex> Macaw.Region.class?("mw-grid")
    false

# `classes`

```elixir
@spec classes(t()) :: String.t()
```

The region's element's classes: `"mw-region mw-region-<name> mw-collapse-<collapse>
mw-below-<breakpoint>"`, then its own, in order. Placement is by classes only.

    iex> Macaw.Region.classes(%Macaw.Region{name: "sidebar", collapse: :drawer, classes: ["nav"]})
    "mw-region mw-region-sidebar mw-collapse-drawer mw-below-md nav"

# `collapses`

```elixir
@spec collapses() :: [atom(), ...]
```

What a region may do on a narrow screen: `:stack`, `:drawer`, `:hide` or `:none`.

# `kept`

```elixir
@spec kept(t(), change()) :: [atom(), ...]
```

The scopes a surface instance in the region keeps across a change (spec/06 §4), in
`Macaw.State.scopes/0`'s order; every other held scope starts again at its initial
values, then takes any value its owner holds when the instance joins it.

  * `:navigate`, a live navigation to a page sharing the region: a persistent region
    keeps everything but `:page`, which is the new page's; another keeps only what an
    owner outside the page holds, `:session`, `:viewer` and `:shared`.
  * `:refill`, a layer filling the region with different content, and the old content
    returning: `:surface` and `:region` state is gone with the content, and `:page`,
    `:session`, `:viewer` and `:shared` return with the instance.

A change that is neither is a wrong call.

    iex> Macaw.Region.kept(%Macaw.Region{name: "sidebar", persistent: true}, :refill)
    [:page, :session, :viewer, :shared]

# `kind`

```elixir
@spec kind(t()) :: Macaw.Component.kind()
```

The kind a cell in the region takes when it names none (spec/06 §2.6): `:navigation` in
a navigation region, `:content` elsewhere.

# `kinds`

```elixir
@spec kinds(t()) :: [Macaw.Component.kind(), ...]
```

The kinds a cell in the region may take: its `kinds`, or by its role, `[:navigation,
:control]` in a navigation region and every kind elsewhere.

# `name?`

```elixir
@spec name?(term()) :: boolean()
```

Whether a term is a region's name: a string of lower-case words joined by `_`.

    iex> Macaw.Region.name?("site_menu")
    true
    iex> Macaw.Region.name?("Site Menu")
    false

# `new`

```elixir
@spec new(map() | keyword()) :: {:ok, t()} | {:error, [Macaw.Base.Fault.t(), ...]}
```

A region from a keyword list or a map with atom keys: `{:ok, region}` or every fault at
once, by path. A missing `name` is `:required`, one that isn't a name `{:type, :name}`
(an atom read as its spelling, no atom made); a `persistent`, `fillable` or `hidden`
that isn't a boolean `{:type, :boolean}`; a `role`, `collapse`, `breakpoint` or kind
outside its set `{:enum, set}`; a `label` that isn't a non-empty string or `nil`
`{:type, :label}`; `content` that isn't a list `{:type, :content}` and an entry that
isn't a surface's name, `{:surface, name}` or `%{surface: name}` `{:type, :surface}` at
`[:content, i]`; `classes` that isn't a list `{:type, :classes}` and a class that isn't
CSP-safe `{:type, :class}` at `[:classes, i]`; `kinds` that isn't a non-empty list
`{:type, :kinds}`; another key `:unknown_key`; what isn't a map `{[], {:type, :map}}`.

    iex> Macaw.Region.new(name: :sidebar, persistent: true)
    {:ok, %Macaw.Region{name: "sidebar", persistent: true}}
    iex> Macaw.Region.new(%{name: "Side Bar", persistent: "yes"})
    {:error, [{[:name], {:type, :name}}, {[:persistent], {:type, :boolean}}]}
    iex> {:ok, region} = Macaw.Region.new(name: :menu, role: "navigation", content: [surface: :site_menu])
    iex> {region.role, region.content}
    {:navigation, ["site_menu"]}

# `node`

```elixir
@spec node(t()) :: map()
```

The region as a document writes it: its map, every key at its default left out.
`new/1` of it answers the region again.

    iex> Macaw.Region.node(%Macaw.Region{name: "main"})
    %{name: "main"}
    iex> Macaw.Region.node(%Macaw.Region{name: "main", role: :main, fillable: true})
    %{name: "main", role: :main, fillable: true}

# `roles`

```elixir
@spec roles() :: [role(), ...]
```

The landmark roles a region may take.

---

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