# `Macaw.State.Preferences`
[🔗](https://github.com/dcoai/macaw/blob/v0.6.0/lib/macaw/state/preferences.ex#L1)

The viewer's preferences, standard state sources every surface holds (spec/03 §1.7.3):
`theme`, `zone`, `units` and `shortcuts`. They live at `:viewer` scope when the host
knows the viewer (`Macaw.State.new/2`'s `instances:` gives `:viewer`) and at `:session`
otherwise, and they reach every component in its context, so one person's unit system
follows them across every component on every page they visit.

  * `theme`, the name of the theme the page renders in: a name, or `nil`, the host's
    default; the host swaps the root's theme class and gives the theme it names as
    `theme:` (spec/11);
  * `zone`, a time zone name (`"Europe/Paris"`), `"Etc/UTC"` by default;
  * `units`, a unit system, one of `Macaw.Format.Quantity.systems/0`, `:as_posted` by
    default, given as the atom or its spelling;
  * `shortcuts`, the page shortcuts the viewer has remapped or turned off, a map of an
    action's name to its key, or `false` for off (the page-shortcut registry, spec/10),
    `%{}` by default.

Each is judged by its kind:

    iex> Macaw.State.Preferences.judge("units", "metric")
    {:ok, :metric}
    iex> Macaw.State.Preferences.judge("zone", "")
    {:error, {:type, :zone}}

# `context`

```elixir
@spec context(map()) :: %{zone: String.t(), units: atom(), shortcuts: map()}
```

The preferences a component's context carries (spec/03 §1.7.3), from a state's values:
`%{zone, units, shortcuts}`, as `Macaw.Component.Context` takes them.

# `defaults`

```elixir
@spec defaults() :: %{required(String.t()) =&gt; term()}
```

Each preference's value before the viewer chooses one, by name.

# `judge`

```elixir
@spec judge(String.t(), term()) :: {:ok, term()} | {:error, term()}
```

A preference's value judged (spec/03 §1.7.3): `{:ok, value}`, `nil` its default, or
`{:error, reason}`: a theme that isn't a name `{:type, :name}`, a zone that isn't a zone's
name `{:type, :zone}`, units not a system `{:enum, systems}`, and shortcuts that aren't a
map of names to a key or `false` `{:type, :shortcuts}`.

# `names`

```elixir
@spec names() :: [String.t(), ...]
```

The preferences' names, in name order.

# `scope`

```elixir
@spec scope(%{required(atom()) =&gt; String.t() | nil}) :: :viewer | :session
```

The scope the preferences live at (spec/03 §2.1): `:viewer` when the host gives the
viewer's instance, `:session` otherwise.

---

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