# `LedgerUI.Components.ThemeSwitch`
[🔗](https://github.com/devaction-labs/ledger_ui/blob/v0.2.0/lib/ledger_ui/components/theme_switch.ex#L1)

The light / dark / system choice.

    <.theme_switch id="theme" />
    <.theme_switch id="theme" variant="icons" size="sm" />
    <.theme_switch id="theme" variant="switch" />
    <.theme_switch id="theme" system={false} light_label="Claro" dark_label="Escuro" />

`variant` is `"segmented"` (a track with the chosen icon raised), `"icons"` (the same without the
track, round) or `"switch"` (a toggle: on is dark). `size="sm"` is the compact one;
`system={false}` leaves only light and dark.

## What it does

The `LedgerUI.ThemeSwitch` hook (`priv/static/ledger_ui/hooks/theme_switch.js`) reads the stored
choice (`localStorage["theme"]`: `"light"`, `"dark"` or `"system"`), puts the resolved mode on
`<html data-theme="light|dark">` (the choice itself on `data-theme-choice`), keeps `on`,
`aria-checked` and `tabindex` in step, and makes the markup a `radiogroup` of `radio`s (arrow keys
move the choice) or a `switch`. "System" follows `prefers-color-scheme` live. Every switch on the
page listens to the `ledger:theme` window event and follows.

The mode is apart from the palette (`<html data-palette="ledger|crepusculo">`, chosen with
`theme_picker/1`): every palette has a light and a dark token set, so any mode works with any palette.

Put `<.theme_script />` in the root layout `<head>` so the first paint already has the mode and the palette.

The server always renders the light choice; the hook owns the state. The labels travel as
`data-lu-light|dark|system|label` (read by the hook, which turns them into accessible names).

# `theme_script`

Inline script for the root layout `<head>`: applies the stored mode (`data-theme`) and palette
(`data-palette`) before the first paint, so there is no flash. It is plain and synchronous on
purpose (it must block rendering).

    <head>
      <.theme_script />
    </head>

## Attributes

* `theme` (`:string`) - The named palette when the user chose none (`config :ledger_ui, :theme`). Defaults to `nil`.
* `default` (`:string`) - The mode when nothing is stored. Defaults to `"system"`. Must be one of `"light"`, `"dark"`, or `"system"`.

# `theme_switch`

## Attributes

* `id` (`:string`) (required)
* `variant` (`:string`) - Defaults to `"segmented"`. Must be one of `"segmented"`, `"icons"`, or `"switch"`.
* `size` (`:string`) - Defaults to `"md"`. Must be one of `"md"`, or `"sm"`.
* `system` (`:boolean`) - Defaults to `true`.
* `light_label` (`:string`) - Defaults to `"Light"`.
* `dark_label` (`:string`) - Defaults to `"Dark"`.
* `system_label` (`:string`) - Defaults to `"System"`.
* `label` (`:string`) - Names the group (default "Theme") or the switch (default "Dark mode"). Defaults to `nil`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

# `theme_toggle`

A single button that flips light and dark: a moon in light mode, a sun in dark (the hook swaps
the icon). It shares the storage and the `ledger:theme` event with `theme_switch/1`.

## Attributes

* `id` (`:string`) - Defaults to `"theme-toggle"`.
* `class` (`:any`) - Defaults to `nil`.
* Global attributes are accepted.

---

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