Corex.ToggleGroup (Corex v0.1.0-alpha.24)
View SourcePhoenix implementation of Zag.js Toggle Group.
Minimal
<.toggle_group
class="toggle-group">
<:item value="a">
A
</:item>
<:item value="b">
B
</:item>
<:item value="c">
C
</:item>
</.toggle_group>Extended
This example assumes the import of .icon from Core Components
<.toggle_group multiple={false}
on_value_change="on_value_change"
id="toggle-group-id"
value=["center"]
class="toggle-group">
<:item value="left">
<.icon name="hero-bars-3-bottom-left" />
</:item>
<:item value="center">
<.icon name="hero-bars-3" />
</:item>
<:item value="right">
<.icon name="hero-bars-3-bottom-right" />
</:item>
</.toggle_group>
Controlled
defmodule MyAppWeb.ToggleGroupLive do
use MyAppWeb, :live_view
def mount(_params, _session, socket) do
{:ok, assign(socket, :value, ["lorem"])}
end
def handle_event("on_value_change", %{"value" => value}, socket) do
{:noreply, assign(socket, :value, value)}
end
def render(assigns) do
~H"""
<.toggle_group
class="toggle-group"
controlled
value={@value} on_value_change="on_value_change" >
<:item value="a">
A
</:item>
<:item value="b">
B
</:item>
<:item value="c">
C
</:item>
</.toggle_group>
"""
end
end
Programmatic Control
Client-side
<button phx-click={Corex.ToggleGroup.set_value("my-toggle-group", ["item-1"])}>
Toggle Group Item 1
</button>
Server-side
def handle_event("open_item", _, socket) do
{:noreply, Corex.ToggleGroup.set_value(socket, "my-toggle-group", ["item-1"])}
end## Styling
Use data attributes to target elements:
- [data-scope="toggle-group"][data-part="root"] - Container
- [data-scope="toggle-group"][data-part="item"] - Item wrapper
If you wish to use the default Corex styling, you can use the class toggle-group on the component.
This requires to install mix corex.design first and import the component css file.
@import "../corex/main.css";
@import "../corex/tokens/themes/neo/light.css";
@import "../corex/components/toggle-group.css";You can then use modifiers
<.toggle_group class="toggle-group toggle-group--accent toggle-group--lg">Learn more about modifiers and Corex Design
Summary
Components
Renders a toggle group component.
API
Sets the toggle group value from client-side. Returns a Phoenix.LiveView.JS command.
Sets the toggle group value from server-side. Pushes a LiveView event.
Components
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 tofalse.deselectable(:boolean) - Whether the toggle group is deselectable. Defaults tofalse.loopFocus(:boolean) - Whether the toggle group is loopFocus. Defaults totrue.rovingFocus(:boolean) - Whether the toggle group is rovingFocus. Defaults totrue.disabled(:boolean) - Whether the toggle group is disabled. Defaults tofalse.multiple(:boolean) - Whether the toggle group allows multiple items to be selected. Defaults totrue.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 tonil. Must be one ofnil,"ltr", or"rtl".on_value_change(:string) - The server event name when the value change. Defaults tonil.on_value_change_client(:string) - The client event name when the value change. Defaults tonil.- Global attributes are accepted.
Slots
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
Sets the toggle group value from client-side. Returns a Phoenix.LiveView.JS command.
Examples
<button phx-click={Corex.ToggleGroup.set_value("my-toggle-group", ["item-1"])}>
Open Item 1
</button>
Sets the toggle group value from server-side. Pushes a LiveView event.
Examples
def handle_event("open_item", _params, socket) do
socket = Corex.ToggleGroup.set_value(socket, "my-toggle-group", ["item-1"])
{:noreply, socket}
end