Corex.ToggleGroup (Corex v0.2.0)

View Source

Toggle group for Phoenix LiveView. Behavior follows Zag.js Toggle Group.

Anatomy

Minimal

<.toggle_group class="toggle-group">
  <:item value="lorem">Lorem</:item>
  <:item value="duis">Duis</:item>
  <:item value="donec">Donec</:item>
</.toggle_group>

With indicator

<.toggle_group class="toggle-group">
  <:item value="bold">
    <.heroicon name="hero-bold" />
    Bold
  </:item>
</.toggle_group>

<.toggle_group class="toggle-group">
  <:item value="bold" aria_label="Bold">
    <.heroicon name="hero-bold" />
    <span class="sr-only">Bold</span>
  </:item>
</.toggle_group>

Single selection

Set multiple={false} so only one item can be selected at a time.

<.toggle_group
  class="toggle-group"
  multiple={false}
  value={["duis"]}
>
  <:item value="lorem">Lorem</:item>
  <:item value="duis">Duis</:item>
  <:item value="donec">Donec</:item>
</.toggle_group>

API

Requires a stable id on <.toggle_group>.

FunctionActionReturns
set_value/2Set selected values (client)%Phoenix.LiveView.JS{}
set_value/3Set selected values (server)socket

Events

Pick an event name and pass it to on_* on <.toggle_group>.

Server events

EventWhenPayload
on_value_change="toggle_group_changed"Selected values change%{"id" => id, "value" => values} — list of selected item value strings

on_value_change

<.toggle_group
  class="toggle-group"
  on_value_change="toggle_group_changed"
  multiple
>
  <:item value="lorem">Lorem</:item>
  <:item value="duis">Duis</:item>
  <:item value="donec">Donec</:item>
</.toggle_group>
def handle_event("toggle_group_changed", %{"id" => id, "value" => value}, socket) do
  {:noreply, assign(socket, :toggle_group_value, value)}
end

Client events

EventWhenevent.detail
on_value_change_client="toggle-group-changed"Selected values changeid, value

on_value_change_client

<.toggle_group
  id="toggle-group-events-client"
  class="toggle-group"
  on_value_change_client="toggle-group-changed"
  multiple
>
  <:item value="lorem">Lorem</:item>
  <:item value="duis">Duis</:item>
  <:item value="donec">Donec</:item>
</.toggle_group>
const el = document.getElementById("toggle-group-events-client");
el?.addEventListener("toggle-group-changed", (event) => console.log(event.detail));

Patterns

Controlled

For server-owned selection, set controlled, bind value, and handle on_value_change in LiveView.

<.toggle_group
  class="toggle-group"
  value={@value}
  multiple
  controlled
  on_value_change="toggle_group_pattern"
>
  <:item value="lorem">Lorem</:item>
  <:item value="duis">Duis</:item>
  <:item value="donec">Donec</:item>
</.toggle_group>
def mount(_params, _session, socket) do
  {:ok, assign(socket, :value, ["lorem"])}
end

def handle_event("toggle_group_pattern", %{"value" => v}, socket) do
  {:noreply, assign(socket, :value, v || [])}
end

Style

Target parts with data-scope and data-part, or use Corex Design: import tokens and toggle-group.css, then set class="toggle-group" on <.toggle_group>.

[data-scope="toggle-group"][data-part="root"] {}
[data-scope="toggle-group"][data-part="item"] {}
@import "../corex/corex.css";

Stack modifiers on the host (class on <.toggle_group>). Combine axes, for example toggle-group ui-accent ui-size-lg.

Axes: Semantic (ui-accent, ui-brand, ui-alert, ui-info, ui-success), Size (ui-size-smui-size-xl), Radius (ui-rounded-*). No variant axis. See the modifier guide.

Semantic modifiers set palette variables for the filled selected state. Idle items stay neutral. Selected items always fill.

Semantic

Palette variables for the filled selected state.

ModifierClasses
Defaulttoggle-group
Accenttoggle-group ui-accent
Brandtoggle-group ui-brand
Alerttoggle-group ui-alert
Infotoggle-group ui-info
Successtoggle-group ui-success

Size

ModifierClasses
SMtoggle-group ui-size-sm
MDtoggle-group ui-size-md
LGtoggle-group ui-size-lg
XLtoggle-group ui-size-xl

Rounded

ModifierClasses
Nonetoggle-group ui-rounded-none
SMtoggle-group ui-rounded-sm
MDtoggle-group ui-rounded-md
LGtoggle-group ui-rounded-lg
XLtoggle-group ui-rounded-xl
Fulltoggle-group ui-rounded-full

Summary

Components

Renders a toggle group component.

API

Set selected values from a control (phx-click). Pass a list of item value strings (or compatible input validated by the component).

Set selected values from handle_event.

Components

toggle_group(assigns)

Renders a toggle group component.

Attributes

  • id (:string) - The id of the toggle group, useful for API to identify the toggle group.
  • value (:list) - The initial value or the controlled value of the toggle group, must be a list of strings. Defaults to [].
  • controlled (:boolean) - Whether the toggle group is controlled. Defaults to false.
  • deselectable (:boolean) - Whether the toggle group is deselectable. Defaults to false.
  • loopFocus (:boolean) - Whether the toggle group is loopFocus. Defaults to true.
  • rovingFocus (:boolean) - Whether the toggle group is rovingFocus. Defaults to true.
  • disabled (:boolean) - Whether the toggle group is disabled. Defaults to false.
  • multiple (:boolean) - Whether the toggle group allows multiple items to be selected. Defaults to true.
  • orientation (:string) - The orientation of the toggle group. Defaults to "horizontal". Must be one of "horizontal", or "vertical".
  • dir (:string) - The direction of the toggle group. When nil, derived from document (html lang + config :rtl_locales). Defaults to nil. Must be one of nil, "ltr", or "rtl".
  • on_value_change (:string) - The server event name when the value change. Defaults to nil.
  • on_value_change_client (:string) - The client event name when the value change. Defaults to nil.
  • Global attributes are accepted.

Slots

  • label - Optional visible or screen-reader label for the group. Accepts attributes:
    • class (:string)
  • item (required) - Accepts attributes:
    • value (:string) - The value of the item, useful in controlled mode and for API to identify the item.
    • disabled (:boolean) - Whether the item is disabled.
    • class (:string) - The class of the item.
    • aria_label (:string) - Accessibility label for the item button. If not provided, the value will be used as a fallback.

API

set_value(toggle_group_id, value)

Set selected values from a control (phx-click). Pass a list of item value strings (or compatible input validated by the component).

<.action phx-click={Corex.ToggleGroup.set_value("my-toggle-group", ["a"])}>Pick A</.action>
<.toggle_group id="my-toggle-group" class="toggle-group" multiple={false} value={[]}>
  <:item value="a">A</:item>
  <:item value="b">B</:item>
</.toggle_group>
document.getElementById("my-toggle-group")?.dispatchEvent(
  new CustomEvent("corex:toggle-group:set-value", {
    detail: { value: ["a"] },
  })
);

set_value(socket, toggle_group_id, value)

Set selected values from handle_event.

<.action phx-click="pick_a">Pick A</.action>
<.toggle_group id="my-toggle-group" class="toggle-group" multiple={false} value={[]}>
  <:item value="a">A</:item>
  <:item value="b">B</:item>
</.toggle_group>
def handle_event("pick_a", _, socket) do
  {:noreply, Corex.ToggleGroup.set_value(socket, "my-toggle-group", ["a"])}
end