LineOne's tree view: a hierarchical list whose branches expand and collapse.
<.treeview id="files">
<.tree_item id="app" label="Application" expanded>
<.tree_item label="Chrome" />
<.tree_item label="VS Code" />
</.tree_item>
<.tree_item id="docs" label="Document">
<.tree_item label="React App" />
</.tree_item>
</.treeview>An item with children is a branch (a native <details>), one without is a leaf. Give an
item an :icon slot and it takes LineOne's "advanced" look (folder icons, darker text):
<.tree_item label="Design">
<:icon><svg class="size-6 text-warning">…</svg></:icon>
<.tree_item label="Web Apps"><:icon>…</:icon></.tree_item>
</.tree_item>State
A branch opens from its expanded attr, or from the treeview's expanded list of item ids,
and toggles in the browser without a round trip. A branch the person toggled stays that way
across patches; when the server changes expanded for it, the server wins.
Events
on_toggle runs when the person opens or closes a branch, on_select when they click a
leaf (or press Enter on it). See treeview/1 for the payloads.
Streams
<.treeview id="files" rows={@streams.files}>
<:row :let={{_dom_id, file}}>
<.tree_item tag="div" id={"file-#{file.id}"} label={file.name} />
</:row>
</.treeview>
Summary
Functions
Renders one item: a branch when it has children, a leaf when it does not.
Renders the tree's root list.
Functions
Renders one item: a branch when it has children, a leaf when it does not.
id names the item in events and in the treeview's expanded list. tag="div" renders it
without its own <li>, for a treeview :row that already sits in one.
<.tree_item id="design" label="Design" expanded>
<:icon><svg class="size-6 text-warning">…</svg></:icon>
<.tree_item id="web" label="Web Apps" />
</.tree_item>Attributes
id(:string) - Names the item in events and inexpanded. Defaults tonil.label(:string) - Defaults tonil.expanded(:boolean) - Whether the branch is open (server state). Defaults tofalse.tag(:string) - Defaults to"li". Must be one of"li", or"div".class(:any) - Defaults tonil.- Global attributes are accepted.
Slots
icon- An icon before the label; switches to LineOne's advanced look.inner_block- The children; makes the item a branch.
Renders the tree's root list.
on_toggle (an event name or a %JS{}) is pushed with %{"id" => item_id, "expanded" => boolean}
when the person opens or closes a branch. on_select is pushed with %{"id" => item_id} when a
leaf is clicked or gets Enter. Both go to target when given; a %JS{} is executed instead.
<.treeview id="files" expanded={@open_ids} on_toggle="toggled" on_select="opened">
<.tree_item id="src" label="src">
<.tree_item id="src/app.ex" label="app.ex" />
</.tree_item>
</.treeview>
def handle_event("toggled", %{"id" => id, "expanded" => open?}, socket), do: ...
def handle_event("opened", %{"id" => id}, socket), do: ...Attributes
id(:string) (required)expanded(:list) - Ids of the branches that are open, in addition to each item's ownexpanded. Defaults to[].on_toggle(:any) - Event name or%JS{}; pushed with%{"id" => id, "expanded" => boolean}. Defaults tonil.on_select(:any) - Event name or%JS{}; pushed with%{"id" => id}when a leaf is picked. Defaults tonil.target(:any) - Thephx-targetthe events go to. Defaults tonil.rows(:list) - A list or a stream (@streams.x) rendered by:row. Defaults tonil.row_id(:any) - A function giving each row's DOM id (streams give it). Defaults tonil.class(:any) - Defaults tonil.- Global attributes are accepted.
Slots
inner_blockrow- Renders one row; takes the row ({dom_id, item}for a stream).