04 · Formulários — seleção

Segmented

Trilha cinza, escolhido em branco · com contagem, ícones, pequeno, bloco

Controle segmentado para alternar entre poucas opções sempre visíveis (Day · Month · Year).

O escolhido desliza

.heex
<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>

Valores da API que completam a paridade

size=sm
size=md
size=lg
shape=rounded
shape=pill
.heex
<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>

Uso

<.segmented id="range" value="month" options={~w(day month year)} />

Regras

  • Padrão do sistema: trilha bg-sunken, escolhido bg-panel com sombra xs e anel de 1px.
  • Até 5 opções; mais que isso vira Tabs ou Select.
  • Pílula só como opção de API (shape="pill"), nunca padrão.

Referência da API

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 of options). value defaults to the position; selected chooses it when value is not given.