Selection controls
Selection controls let a user choose a value or set a preference without building each interaction from scratch. This page covers toggle groups, ratings, language selection, and color-scheme switching as reusable interfaces that connect visible state with form data, LiveView events, URLs, or browser preferences. It is a child page of Form controls and validation and relates to Text and structured inputs and Advanced form inputs and uploads.
Choice interface model
Each control presents a small set of choices and exposes the selected state in a form that suits its purpose. The choice may stay in server state, become a submitted form value, change the current URL, or update a browser preference.
| Interface | Main choice | State boundary |
|---|---|---|
| Toggle group | One option or several active options | LiveView assigns or a caller-managed event |
| Rating | A score or sentiment level | A read-only value or a submitted form field |
| Language select | A locale for the current page | A locale URL parameter read by the application |
| Color-scheme switch | Light, dark, or system display mode | Browser storage and OS preference |
Toggle groups
The toggle group in lib/petal_components/toggle_group.ex is a segmented choice rail for one selection or many selections. It is stateless: the caller passes the current value, and each press sends the pressed option through the configured change event or a caller-supplied client command. When the caller uses a LiveView event, the server remains the source of truth.
The following diagram shows how an option press becomes new rendered state when the group uses a LiveView event.
flowchart LR
A[User presses an option] --> B[Change event carries the option]
B --> C[LiveView updates the value]
C --> D[Toggle group renders pressed state]
Single selection
Single-select groups represent one current value with native radio behavior. The group has radiogroup semantics, gives the browser one tab stop with arrow-key movement, and does not add client state; when a LiveView change event is configured, the selected value is sent in the toggle event field.
The radio inputs are kept out of a surrounding Phoenix form so a view preference does not add an unintended form parameter. A missing value leaves every option unselected, which supports an explicit “not chosen” state.
References: lib/…/toggle_group.ex• L6-21 lib/…/toggle_group.ex• L135-165 test/…/toggle_group_test.exs• L5-47 test/…/toggle_group_test.exs• L80-95
Multiple selection
Multiple-select groups represent the current value as a list. Each option is a button with aria-pressed, and the caller adds or removes the pressed option when it handles the change event. Icon-only options need their own accessible label because the visible content may not name the choice.
The same group can also accept a caller-managed client command when server state is not needed. If no group-level change command is configured, the caller can put phx-click on individual items instead. Server-managed events are the safer choice when the selection affects application data.
References: lib/…/toggle_group.ex• L35-51 lib/…/toggle_group.ex• L78-85 lib/…/toggle_group.ex• L108-132 lib/…/toggle_group.ex• L22-61 test/…/toggle_group_test.exs• L97-120
Toggle group presentation
Toggle groups support solid, outline, and accent variants plus small, medium, and large sizes. Use the neutral rails for view or formatting preferences, the outline rail beside action buttons, and the accent rail when the choice is a committed setting; these variants change emphasis without changing the choice contract.
Each option can be disabled by itself, or the whole group can be disabled. Option values survive the string conversion made by LiveView event payloads, so an integer value remains selected when the caller assigns the received string back.
References: lib/…/toggle_group.ex• L53-69 lib/…/toggle_group.ex• L87-105 lib/…/toggle_group.ex• L168-191 lib/…/toggle_group.ex• L77-143
Rating controls
The rating control in lib/petal_components/rating.ex represents a score in either display or input form. A display rating communicates an existing value, while an interactive rating turns the scale into a native radio group that can submit with a Phoenix form.
Display ratings
A display rating is read-only and can show stars, hearts, faces, or a custom glyph. Stars and hearts fill along a cumulative scale, so fractional values can show a partial position; faces select one expression from a five-point sentiment scale. An optional generated label gives the value a text form such as a score out of the total or a sentiment name.
References: lib/…/rating.ex• L3-14 lib/…/rating.ex• L191-259 lib/…/rating.ex• L261-285 test/…/rating_test.exs• L5-36 test/…/rating_test.exs• L138-191
Interactive ratings
An interactive rating is a labeled fieldset of real radio inputs. It needs an input name, submits the chosen score under that name, and gains native keyboard and focus behavior without JavaScript. Whole-number precision provides one choice per icon; half precision provides choices such as 3.5 between whole values.
The control can use stars or hearts for cumulative scores and faces for ordered sentiment. Faces always use five whole-number positions because each position represents a distinct expression rather than part of a continuous score. The disabled state applies to the complete rating field.
References: lib/…/rating.ex• L34-53 lib/…/rating.ex• L87-188 lib/…/rating.ex• L5-27 test/…/rating_test.exs• L38-135
Language selection
The language select in lib/petal_components/language_select.ex is a locale switcher for a page header or footer. It uses a dropdown trigger and real links for each language, so it works in both LiveView and ordinary server-rendered views.
Locale links
Each option points to the supplied current path with a URL-encoded locale query parameter. The component adds & when the path already has a query and places the locale before a fragment, which keeps the browser destination and the page anchor intact. The application reads that parameter and decides how to load or persist the locale; the selector itself only creates the link.
The current option has aria-current and a visual check. If the current locale is not in the option list, the menu still renders and the trigger falls back to a language glyph, code, or raw locale instead of failing.
References: lib/…/language_select.ex• L3-13 lib/…/language_select.ex• L67-115 lib/…/language_select.ex• L123-136 test/…/language_select_test.exs• L24-128
Language trigger presentation
The trigger can show the current language as a flag, an upper-case locale code, or a language label. Flags are optional because a country flag does not always identify a language; when no flag is available, the flag mode uses a language glyph and the menu rows remain text based. Trigger labels and the chevron can be changed for localization and layout needs.
References: lib/…/language_select.ex• L24-32 lib/…/language_select.ex• L52-65 lib/…/language_select.ex• L118-121 lib/…/language_select.ex• L27-70 test/…/language_select_test.exs• L130-196
Color-scheme switching
The color-scheme switch provides light, dark, and system display preferences in three forms. The toggle is a compact light/dark switch, while the dropdown and segmented forms expose the system choice as well; all forms share one browser-side preference contract.
Scheme choice forms
The toggle flips between the currently resolved light and dark schemes. The dropdown presents Light, Dark, and System in a menu, while the segmented form keeps all three choices visible as a radio group. Labels can be replaced for localization, and icon slots can replace the default sun, moon, and monitor symbols without changing the choice behavior.
References: lib/…/color_scheme_switch.ex• L1-15 lib/…/color_scheme_switch.ex• L97-117 lib/…/color_scheme_switch.ex• L128-237 lib/…/color_scheme_switch.ex• L7-59 test/…/color_scheme_switch_test.exs• L23-128
Browser scheme state
Render color_scheme_script once in the root layout head before the application JavaScript. The script applies the saved or system scheme before the first paint, which avoids a light-to-dark flash, and exposes the PetalColorScheme browser contract used by every switch.
The preference is light, dark, or system. Explicit choices use localStorage.scheme; system mode removes that stored value and follows the operating system through its color preference. The resolved mode is the mode currently shown on the page. Tabs stay in sync through storage events, and each change emits petal:scheme-changed with both preference and resolved values.
The PetalColorScheme hook connects the three rendered forms to that contract. It updates checked and aria-checked state when a preference changes, so multiple switch instances on one page remain aligned.
The following diagram shows the browser preference flow.
flowchart LR
A[Saved preference or OS setting] --> B[Resolve light or dark mode]
B --> C[Apply document color state before paint]
D[User selects a scheme] --> E[Update browser preference]
E --> B
E --> F[Notify open switch instances and tabs]
References: lib/…/color_scheme_switch.ex• L17-36 lib/…/color_scheme_switch.ex• L44-95 assets/…/petal_components.js• L863-927 test/…/color_scheme_switch_test.exs• L5-20
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the toggle group event key and string value behavior aligned with the LiveView handler that restores the selected value or list.
- Keep interactive rating names and half-step values aligned with the receiving form schema; do not treat face ratings as fractional scores.
- Keep the language query parameter contract aligned with the locale plug or mount hook that reads
locale. - Keep
color_scheme_script, thePetalColorSchemehook, and the exported browser bundle aligned; a switch needs all three parts to change state. - Keep the
schemestorage key andpetal:scheme-changedevent shape stable for other tabs and application code that listen for preference changes.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
An interactive rating has no name | Rendering raises an argument error and no form field is produced | Give every interactive rating a form name |
| A multiple toggle group receives a non-list value | No option can match the current pressed state | Store the selected options as a list |
| An icon-only toggle item has no accessible label | Assistive technology cannot identify the option | Set an aria-label on the item |
| A color-scheme switch is rendered without the head script | The hook warns and the control cannot read or set the shared scheme state | Render color_scheme_script before app JavaScript |
| The color-scheme hook is not included in the JavaScript bundle | The server-rendered controls remain visually present but do not respond or stay synchronized | Register the PetalColorScheme hook |
An application changes language links without handling locale | The page URL changes but the active locale does not | Connect the query parameter to locale loading and persistence |
Key files
| File | Why to read it |
|---|---|
lib/petal_components/toggle_group.ex | Defines the single- and multiple-choice contract and its accessibility behavior. |
lib/petal_components/rating.ex | Defines read-only scores, interactive radio ratings, and the available scale types. |
lib/petal_components/language_select.ex | Defines locale links, trigger modes, and current-language state. |
lib/petal_components/color_scheme_switch.ex | Defines the three scheme switch forms and the no-flash browser contract. |
| assets/…/petal_components.js | Connects scheme controls to browser preference changes and cross-instance synchronization. |
lib/petal_components/showcase/toggle_group.ex | Shows the main toggle group choices, variants, and disabled states. |
lib/petal_components/showcase/rating.ex | Shows interactive, sentiment, display, and custom-glyph rating uses. |
lib/petal_components/showcase/language_select.ex | Shows flag, code, label, and fallback locale presentations. |
lib/petal_components/showcase/color_scheme_switch.ex | Shows the three scheme forms, localized labels, and custom icons. |