06 · Exibição

Treeview

variant canvas · lineone · checked true/false/mixed · expansão

Hierarquia navegável (pastas, contas, departamentos).

Abrindo e fechando a pasta

Fiscal12
NF-e emitidas9
Canceladas3
Estoque41
Inventário nov
.heex
<.treeview id="tree-1" class="w-[300px] p-1.5"><.tree_item label="Fiscal" expanded count="12" chevron_class="animate-[mo-rot_5s_ease-in-out_infinite]"><:icon><.icon name="folder" size="md" /></:icon></.tree_item><div class="overflow-hidden animate-[mo-expand_5s_ease-in-out_infinite]" style="--mh:80px"><.tree_item depth={1} label="NF-e emitidas" count="9"><:icon><.icon name="file" size="md" /></:icon></.tree_item><.tree_item depth={1} label="Canceladas" count="3" selected><:icon><.icon name="file" size="md" /></:icon></.tree_item></div><.tree_item label="Financeiro" branch count="28"><:icon><.icon name="folder" size="md" /></:icon></.tree_item><.tree_item label="Estoque" expanded count="41"><:icon><.icon name="folder" size="md" /></:icon></.tree_item><.tree_item depth={1} label="Inventário nov"><:icon><.icon name="file" size="md" /></:icon></.tree_item></.treeview>

variant canvas · checked true/false/mixed

Fiscal12
NF-e emitidas9
Canceladas3
Estoque41
Inventário nov
Fiscal12
NF-e emitidas9
Canceladas3
Estoque41
Inventário nov
.heex
<div class="flex flex-wrap items-center gap-2.5"><.treeview id="tree-2a" class="w-[300px] p-1.5"><.tree_item label="Fiscal" expanded count="12"><:icon><.icon name="folder" size="md" /></:icon></.tree_item><.tree_item depth={1} label="NF-e emitidas" count="9"><:icon><.icon name="file" size="md" /></:icon></.tree_item><.tree_item depth={1} label="Canceladas" count="3" selected><:icon><.icon name="file" size="md" /></:icon></.tree_item><.tree_item label="Financeiro" branch count="28"><:icon><.icon name="folder" size="md" /></:icon></.tree_item><.tree_item label="Estoque" expanded count="41"><:icon><.icon name="folder" size="md" /></:icon></.tree_item><.tree_item depth={1} label="Inventário nov"><:icon><.icon name="file" size="md" /></:icon></.tree_item></.treeview><.treeview id="tree-2b" class="w-[300px] p-1.5"><.tree_item id="t2-fiscal" label="Fiscal" expanded count="12" checked="mixed"><:icon><.icon name="folder" size="md" /></:icon><.tree_item id="t2-nfe" label="NF-e emitidas" count="9" checked={true}><:icon><.icon name="file" size="md" /></:icon></.tree_item><.tree_item id="t2-canc" label="Canceladas" count="3" selected checked={false}><:icon><.icon name="file" size="md" /></:icon></.tree_item></.tree_item><.tree_item id="t2-fin" label="Financeiro" branch count="28" checked={false}><:icon><.icon name="folder" size="md" /></:icon></.tree_item><.tree_item id="t2-est" label="Estoque" expanded count="41" checked={false}><:icon><.icon name="folder" size="md" /></:icon><.tree_item id="t2-inv" label="Inventário nov" checked={false}><:icon><.icon name="file" size="md" /></:icon></.tree_item></.tree_item></.treeview></div>

Valores da API que completam a paridade

<li> por itemtag=li
.heex
<div class="grid grid-cols-3 gap-3.5"><div class="flex flex-col items-start gap-1.5"><span class="inline-flex h-[30px] items-center gap-2 rounded-lg bg-sunken px-2.5 font-mono text-[13px]">&lt;li&gt; por item</span><span class="font-mono text-[11px] text-muted">tag=li</span></div></div>

Uso

<.tree items={@folders} selected={@current} />

Regras

  • Indente 18px por nível; contagem em mono à direita.

Referência da API

Todos os function components, atributos, valores e slots que este componente cobre.

A tree view: a hierarchical list whose branches expand and collapse.

<.treeview id="notas" on_toggle="toggled" on_select="opened">
  <.tree_item id="2026" label="Notas 2026" expanded>
    <:icon><.icon name="folder" size="md" /></:icon>
    <.tree_item id="out" label="Outubro" branch />
    <.tree_item id="nfe" label="NF-e 48.211.xml" selected />
  </.tree_item>
</.treeview>

An item with children is a branch, one without is a leaf. branch draws a closed branch before its children are there (loaded when it opens). checked (true, false or "mixed") puts a checkbox before the label; chevrons={false} leaves the chevrons out.

Branches open and close in the browser (LedgerUI.Treeview hook), with no round trip; a branch the person toggled stays that way across patches, and the server wins when it changes expanded for it. on_toggle is pushed with %{"id" => id, "expanded" => boolean}, on_select with %{"id" => id} when a leaf is clicked (or gets Enter), on_check with %{"id" => id, "checked" => boolean}; all to target when given. A %JS{} is executed instead.

The ARIA is rendered by the server: the root is role="tree", each row a role="treeitem" with aria-selected, aria-expanded (branches), aria-checked (when checked is given), aria-owns pointing at its role="group" of children and a roving tabindex (0 on the selected row, -1 on the others; the hook makes sure one row, the first when none is selected, is in the tab order). The hook only updates aria-expanded/aria-selected/tabindex as the person interacts.

The look is flat rows (data-part="tree-item"), styled only with Tailwind utilities: a selected row is aria-selected="true". Nested tree_items are wrapped in a data-part="tree-children" box that indents them (and is hidden while closed), so no depth is needed; rows written side by side (as the design does) take depth.

Checkboxes

A row with checked cascades in the browser: (un)checking a branch (un)checks everything under it, and a branch shows the dash (indeterminate, aria-checked="mixed") while only some of its children are checked. on_check is pushed for the row that was clicked.

Streams

<.treeview id="files" rows={@streams.files}>
  <:row :let={{_dom_id, file}}>
    <.tree_item id={"file-#{file.id}"} label={file.name} />
  </:row>
</.treeview>

<.treeview>

Attr Type Default Values / notes
id (required) :string
expanded :list [] Ids of the branches that are open.
on_toggle :any nil
on_select :any nil
on_check :any nil
variant :string "canvas" canvas, lineone - lineone is reserved: the design has no style for it.
chevrons :boolean true
target :any nil
rows :any nil A list or a stream rendered by :row.
row_id :any nil A function giving each row's DOM id (lists only).
class :any nil

Any other HTML/phx attribute goes to the root element.

Slots:

  • <:inner_block>
  • <:row> - Renders one row; takes {dom_id, item}.

<.tree_item>

Attr Type Default Values / notes
id :string nil Names the item in events and in expanded.
label :string nil
expanded :boolean false
selected :boolean false
branch :boolean false A branch even with no children yet.
checked :any nil nil, true, false, mixed
tag :string nil nil, li, div - Wraps the item in this tag.
count :string nil A trailing count (the mono figure at the right).
depth :integer 0 Indent level for rows written side by side.
chevron :boolean true Leaves room for / draws the chevron.
chevron_class :any nil
class :any nil

Any other HTML/phx attribute goes to the root element, including icon.

Slots:

  • <:icon> - An icon before the label.
  • <:inner_block> - The children; makes the item a branch.

<.tree>

A tree written as data: the same as treeview/1 with one tree_item/1 per map.

<.tree id="folders" items={[
  %{id: "fiscal", label: "Fiscal", icon: "folder", count: 12, expanded: true,
    children: [%{id: "nfe", label: "NF-e", icon: "file"}]}
]} selected="nfe" />
Attr Type Default Values / notes
id :string "tree"
items (required) :list Maps with :label and optionally :id, :icon, :count, :checked, :expanded and :children (the same shape, nested).
selected :any nil The id (or the label, without ids) of the selected item.
expanded :list [] Ids of the branches that are open.
on_toggle :any nil
on_select :any nil
on_check :any nil
variant :string "canvas" canvas, lineone
chevrons :boolean true
target :any nil
class :any nil

Any other HTML/phx attribute goes to the root element.