Segmented
Gray track, chosen in white · with count, icons, small, block
A segmented control to switch between a few always-visible options (Day · Month · Year).
The chosen one slides
<div class="flex flex-wrap items-center gap-2.5"> <.segmented id="seg-01-a" class="w-[300px] relative [--n:3] before:absolute before:inset-y-[3px] before:left-[3px] before:w-[calc((100%-6px-2px*(var(--n)-1))/var(--n))] before:rounded-[7px] before:bg-panel before:shadow-[var(--lu-shadow-xs),0_0_0_1px_var(--lu-line)] before:animate-[lu-segslide_6s_cubic-bezier(.4,0,.2,1)_infinite] [&>button]:relative [&>button]:flex-1 [&>button]:justify-center" options={["Dia", "Semana", "Mês"]} /> <.segmented id="seg-01-b" size="sm" class="w-[240px] relative [--n:3] before:absolute before:inset-y-[3px] before:left-[3px] before:w-[calc((100%-6px-2px*(var(--n)-1))/var(--n))] before:rounded-[7px] before:bg-panel before:shadow-[var(--lu-shadow-xs),0_0_0_1px_var(--lu-line)] before:animate-[lu-segslide_6s_cubic-bezier(.4,0,.2,1)_infinite] [&>button]:relative [&>button]:flex-1 [&>button]:justify-center" options={["Lista", "Quadro", "Tabela"]} /> </div>
API values that complete the parity
<div class="grid grid-cols-5 gap-3.5"><div class="flex flex-col items-start gap-1.5"><.segmented id="seg-02-sm" size="sm" value="Dia" options={["Dia", "Mês"]} /><span class="font-mono text-[11px] text-muted">size=sm</span></div><div class="flex flex-col items-start gap-1.5"><.segmented id="seg-02-md" value="Dia" options={["Dia", "Mês"]} /><span class="font-mono text-[11px] text-muted">size=md</span></div><div class="flex flex-col items-start gap-1.5"><.segmented id="seg-02-lg" size="lg" value="Dia" options={["Dia", "Mês"]} /><span class="font-mono text-[11px] text-muted">size=lg</span></div><div class="flex flex-col items-start gap-1.5"><.segmented id="seg-02-rounded" value="Dia" options={["Dia", "Mês"]} /><span class="font-mono text-[11px] text-muted">shape=rounded</span></div><div class="flex flex-col items-start gap-1.5"><.segmented id="seg-02-pill" shape="pill" value="Dia" options={["Dia", "Mês"]} /><span class="font-mono text-[11px] text-muted">shape=pill</span></div></div>
Usage
<.segmented id="range" value="month" options={~w(day month year)} />
Rules
- System default:
bg-sunkentrack, chosenbg-panelwith an xs shadow and a 1px ring. - Up to 5 options; more than that becomes Tabs or Select.
- Pill only as an API option (
shape="pill"), never the default.
API reference
Todos os function components, atributos, valores e slots que este componente cobre.
A segmented control: a row of toggle buttons where exactly one is chosen.
<.segmented id="range" value={@range} on_change="set-range" options={~w(day month year)} />
<.segmented field={@form[:view]} options={[{"List", "list"}, {"Board", "board"}]} />
<.segmented id="view" value="board" options={[
%{label: "Board", value: "board", icon: "kanban"},
%{label: "All", value: "all", count: 128}
]} />
Options are strings, {label, value} tuples or maps with :label, :value, :icon, :count
and :disabled. It is a role="group" (see role) of buttons with aria-pressed, which is what the chosen look reads. The choice is made in
the browser (hook LedgerUI.Segmented, priv/static/ledger_ui/hooks/segmented.js): a new
value from the server wins, a patch that does not change it leaves the person's choice
alone, arrow keys/Home/End move the focus (one segment is in the Tab order).
With a name (or field) the control submits through a hidden input that fires input, so a
form's phx-change runs. on_change is an event name (pushed with
%{"id" => id, "value" => value} to target) or a %JS{}.
<.segmented>
| Attr | Type | Default | Values / notes |
|---|---|---|---|
id |
:string |
nil |
|
name |
:string |
nil |
|
value |
:any |
nil |
|
field |
{:struct, Phoenix.HTML.FormField} |
nil |
|
options |
:list |
[] |
Strings, {label, value} tuples or maps; or write <:option> slots. |
label |
:string |
nil |
|
help |
:string |
nil |
|
errors |
:list |
[] |
|
size |
:string |
"md" |
sm, md, lg |
shape |
:string |
"rounded" |
rounded, pill |
full |
:boolean |
false |
|
stretch |
:boolean |
false |
The canvas form of a full-width control: width:100% on the group, flex:1 per button. |
role |
:string |
"group" |
group (default) or what the surface needs. |
disabled |
:boolean |
false |
|
on_change |
:any |
nil |
|
target |
:any |
nil |
|
wrapper_class |
:any |
nil |
|
class |
:any |
nil |
Any other HTML/phx attribute goes to the root element, including form.
Slots:
<:option>- attrs:value,label,icon,count,disabled,selected- One segment, its content the label (instead ofoptions).valuedefaults to the position;selectedchooses it whenvalueis not given.