Skip to content
IzzyBlazor

Enter all results ↑ ↓ move Esc close

TreeView

Since 0.1.0-alpha.21
Shows items nested in items, such as folders, as branches that open and close.

Items nested in items, from data: folders, categories, a company's teams. Items are the top level, and ItemChildren gives each item's own.

Each item with children opens and closes, with no script. A branch starts open when it holds the current item (ItemCurrent), a selected one, or ItemExpanded says so, so a page arrives with its own place in the tree showing. With script, the branches a person opened stay open as they move between pages; on an interactive page @bind-ExpandedItems follows them.

With no template, ItemText, ItemDescription and ItemIcon draw an icon, the text and a muted second line; ChildContent draws each item instead. ItemHref makes items links. In a tree of links each item's arrow sits at its end, as in NavGroup, and an item that is a link and has children gets its own button to open them; other trees put the arrow first.

SelectionMode makes a tree to choose from, as ListView's selection: Multiple gives each item a checkbox, Single selects one item. In a form the keys post under SelectionName; on an interactive page bind them with @bind-Selection:

Set Items to the top level and ItemChildren to each item's own, with ItemKey and ItemText. To make each item a link, set ItemHref; to mark the item the page is showing, set ItemCurrent, and its branch arrives open. A link with children gets its own button at its end that opens them.

Static

Code

Usage.razor
@* Each folder links to this page with ?folder=, and the one in the address is current: its branch
   arrives open, in every mode, with no script. *@
@inject NavigationManager Navigation
<TreeView Items="_folders" ItemChildren="f => f.Folders" ItemKey="f => f.Id" ItemText="f => f.Name"
          ItemIcon="_ => Lucide.Folder" ItemHref="@(f => Navigation.GetUriWithQueryParameter("folder", f.Id))"
          ItemCurrent="@(f => f.Id == (Folder ?? "invoices"))" Label="Folders" MaxWidth="18.Rem" Grow />

@code {
    [SupplyParameterFromQuery] private string? Folder { get; set; }

    private readonly Item[] _folders =
    [
        new("documents", "Documents",
        [
            new("contracts", "Contracts", []),
            new("finance", "Finance", [new("invoices", "Invoices", []), new("receipts", "Receipts", [])]),
        ]),
        new("photos", "Photos", [new("2025", "2025", []), new("2026", "2026", [])]),
        new("shared", "Shared", []),
    ];
}

Add this example to a page with the izzy CLI:

bash
izzy add component TreeView --at <file>:<line>

Topics

  • Selection: Checkboxes or a single choice. The keys post with a form or bind to C#.
  • Large trees: Children that load when a branch first opens, and a search box that narrows the tree.

Text and descriptions

Set ItemChildren to each item's children. With no template, ItemText, ItemDescription and ItemIcon draw each item; to start branches open, set ItemExpanded.

Static

Preview

  • EngineeringAda Lovelace
    • PlatformAlan Turing
      • StorageBarbara Liskov
      • NetworkingRadia Perlman
    • AppsGrace Hopper
  • DesignSusan Kare
    • ResearchDon Norman
  • OperationsKatherine Johnson

Code

Teams.razor
@* A tree to read, not to navigate: each team with a muted line under it, the top level open. *@
<TreeView Items="_teams" ItemChildren="t => t.Teams" ItemText="t => t.Name" ItemDescription="t => t.Lead"
          ItemIcon="_ => Lucide.Users" ItemExpanded="t => t.Teams.Length > 0 && _teams.Contains(t)" MaxWidth="20.Rem" Grow />

@code {
    private readonly Team[] _teams =
    [
        new("Engineering", "Ada Lovelace",
        [
            new("Platform", "Alan Turing", [new("Storage", "Barbara Liskov", []), new("Networking", "Radia Perlman", [])]),
            new("Apps", "Grace Hopper", []),
        ]),
        new("Design", "Susan Kare", [new("Research", "Don Norman", [])]),
        new("Operations", "Katherine Johnson", []),
    ];
}

Details

Checking a parent checks its branch, and a parent with some of its branch checked shows a mixed box (Cascade="false" makes each box its own). The selection holds every checked key, parents included.

For a large tree, ItemHasChildren and ChildrenProvider load a branch's children the first time it opens: on a static page the script fetches the page with the branch's key, so there's no endpoint to write (two such trees on a page need ids). Searchable adds a box that narrows the tree to matches and the branches that hold them.

Mode support

What works in each mode, TreeView
FeatureStaticInteractive
Links, the current item, and branches that openYesYes
Branches opened by hand stay open across pagesYesYes
Selection that posts with a form or binds in C#YesYes
Checking a parent checks its branch; mixed parentsYesYes
One Tab stop, arrow keys and typeahead in a tree to choose fromYesYes
Children that load when a branch first opensYesYes
A search box that narrows the treeYesYes
The open branches bound to C#NoYes

Static: static server-side rendering, with no circuit and no WebAssembly. Interactive: Server, WebAssembly or Auto.

Without JavaScript

Every page starts as plain HTML: before IzzyBlazor's script loads on a static page, while an interactive page prerenders, and with JavaScript off. With JavaScript off, a tree you select from is a list of checkboxes or radio buttons in branches that open: no arrow keys, and checking a parent doesn't check its children. Children that load and the search box need it.

Accessibility

  • It's nested lists (ul with role="list"), so screen readers say how many items each level has and how deep an item is. Put a tree of links for moving around the site in a nav, or name it with Label.
  • An item with children opens and closes with a details element: its summary is a button that says whether it's open. A link with children has a separate button after it, named by the item, so Tab reaches the link, then its button.
  • The current item (ItemCurrent) is marked aria-current ("page" on a link).
  • A disabled item (ItemDisabled) has no link, or its box is disabled, and it's dimmed; its children still open.
  • In a tree of links, Tab goes through the links and buttons in order. Arrow keys don't move through it: a list of links is read and reached as one.
  • With SelectionMode and script, it's an ARIA tree (give it an Label): one Tab stop, the arrow keys between items, and each item a treeitem that says whether it's open and whether it's checked (aria-checked, "mixed" for a part-checked parent) or selected. With JavaScript off it's a list of checkboxes or radio buttons in branches.

Keyboard navigation

TreeView keys
Press this keyTo do this
Up or DownMove to the item above or below
RightOpen an item's branch, or move to its first child (Left on right-to-left pages)
LeftClose an item's branch, or move to its parent (Right on right-to-left pages)
Home or EndMove to the first or last item
*Open every branch beside the focused item
SpaceCheck or uncheck the item, or select it; one that can't be, open or close its branch
EnterSelect the item; with checkboxes, check or uncheck it; one that can't be, open or close its branch
A letterMove to the next item starting with it

Parameters

TreeView parameters
PropTypeDefault
ItemsThe items at the top of the tree.IEnumerable<TItem>?
ItemChildrenAn item's children, or null or empty when it has none.Func<TItem, IEnumerable<TItem>?>?
ItemKeyA key that tells items apart, such as an id.Func<TItem, string>?
ChildContentAn item's content.RenderFragment<TItem>?
ItemTextAn item's text, with no template.Func<TItem, string?>?computed
ItemDescriptionA muted second line under an item's text, with no template.Func<TItem, string?>?
ItemIconAn icon before an item's text, with no template.Func<TItem, IconData?>?
ItemLabelNames an item for screen readers: the button that opens its children, and in a tree to choose from the item itself.Func<TItem, string>?computed
ItemHrefMakes an item a link to this address; null leaves it plain.Func<TItem, string?>?
ItemCurrentMarks the item the page is showing.Func<TItem, bool>?
ItemHasChildrenSays an item has children to load with ChildrenProvider when its branch first opens, for items whose ItemChildren is empty.Func<TItem, bool>?
ChildrenProviderLoads an item's children, given its key, the first time its branch opens: in the component on an interactive page, and on a static page by the script fetching the page with the key.Func<string, CancellationToken, ValueTask<IEnumerable<TItem>>>?
SearchableA search box above the tree that narrows it as you type: matches and their parents stay, their branches open.boolfalse
SearchLabelThe search box's name and placeholder.string?computed
ItemExpandedStarts an item's children open.Func<TItem, bool>?
ItemDisabledDims an item and takes away its link, or disables its box.Func<TItem, bool>?
@bind-ExpandedItemsThe keys of the items whose children are open, on an interactive page; bind it with @bind-ExpandedItems.IReadOnlyCollection<string>?
SelectionModeSelectionMode.Multiple gives each item a checkbox; SelectionMode.Single selects one item.SelectionModeSelectionMode.None
@bind-SelectionThe selected items' keys.Selection?
SelectionNameThe form name the selected keys post under, as {name}.Keys; read them with [SupplyParameterFromForm(Name = "...")] Selection.string?selected
ItemSelectableWhether an item can be selected; one that can't keeps its place, with its box disabled, and with script a click on it opens or closes its branch.Func<TItem, bool>?
CascadeWith checkboxes, checking a parent checks its branch, and a parent shows how much of its branch is checked.booltrue
LabelNames the tree for screen readers, when nothing around it does.string?
EmptyTextShown when there are no items.string?computed
EmptyContentContent shown when there are no items, in place of EmptyText.RenderFragment?

See also

  • NavGroup: a collapsible group of nav items, built on <details>, so it opens and closes with no JavaScript.
  • ListView: displays a list of items users can open, select, page through and reorder.

Connection lost

Rejoining the server…