enes

Tree View New

The Tree View displays hierarchical data in a nested list where each item can be expanded, checked or selected. Items are composed with the TreeViewItem and TreeViewItemContent components — the tree does not accept a data prop.

  • Fruits
    • Apple
    • Banana
    • Citrus
      • Orange
        • Valencia
      • Lemon
  • Vegetables
    • Carrot
    • Spinach
    • Broccoli
  • Grains
    • Rice
    • Oats
  • Dairy
    • Milk
    • Cheese
  • Eggs

Usage

<script>
	import { TreeView, TreeViewItem, TreeViewItemContent } from 'fluentui-svelte';
</script>

<TreeView selectionMode="multiple">
	<TreeViewItem id="fruits" type="branch">
		<TreeViewItemContent>Fruits</TreeViewItemContent>
		<TreeView>
			<TreeViewItem id="apple">
				<TreeViewItemContent>Apple</TreeViewItemContent>
			</TreeViewItem>
			<TreeViewItem id="banana" disabled>
				<TreeViewItemContent>Banana</TreeViewItemContent>
			</TreeViewItem>
		</TreeView>
	</TreeViewItem>
	<TreeViewItem id="vegetables">
		<TreeViewItemContent>Vegetables</TreeViewItemContent>
	</TreeViewItem>
</TreeView>

Nesting a TreeView inside a TreeViewItem of type='branch' creates a subtree. Use openItems / checkedItems on the root TreeView (bindable) to control open and checked state programmatically.

Checkable Items

Set selectionMode to "multiple" | "single" to render checkboxes on selectable items.

  • Fruits
    • Apple
    • Banana

Single selection

Set selectionMode to single so checking an item unchecks the previous one, and branches no longer cascade to their children.

  • Fruits
    • Apple
    • Banana
  • Vegetables

Checked:

Icons

Use iconBefore and iconAfter to render icons around the label. Every item reserves the space of the expand chevron, so checkboxes and icons stay aligned between branches and leaves.

  • Documents
    • Report
    • Photo
  • Notes

Aside and actions

aside is pinned to the end of the row. actions comes after it and is revealed when the row is hovered or focused.

  • Report 2 KB
  • Annual summary 2 KB

Sizes

Set size to small, medium or large on the root TreeView.

  • Small
    • Apple
  • Medium
    • Apple
  • Large
    • Apple

Disabled items

A disabled item can't be opened or checked, and it is skipped when its parent checks its children, which leaves the parent in its mixed state.

  • Fruits
    • Apple
    • Banana

Controlling Open and Checked State

Use the bindable openItems and checkedItems props on the root TreeView to control state programmatically.

  • Fruits
    • Apple
    • Banana

Open: controlled-fruits

Checked:

Events

onCheckedChange and onOpenChange on the TreeView receive the resulting ids. The same props on a TreeViewItem receive the item and its new state, including the children a branch cascades to.

  • Fruits
    • Apple

Tree:

Item:

Component Props (TreeView)

NameTypeDefaultDescription
ref bindableHTMLUListElement | nullThe DOM reference of the tree element.
size'small' | 'medium' | 'large''medium'The size of every item in the tree.
navigationMode'tree' | 'treegrid''tree'Whether the tree takes a single tab stop, or every row is reachable on its own.
onOpenChange(e: Event, openItems: string[]) => voidCalled whenever a branch opens or closes.
selectionMode'single' | 'multiple''multiple'How many items can be checked at a time.
onCheckedChange(e: Event, checkedItems: string[]) => voidCalled whenever an item is checked or unchecked.
virtualizerTreeViewVirtualizerBridge to a windowing library. Only the rendered slice of items lives in the DOM.
openItems bindablestring[] | SvelteSet<string>The ids of the open branches. A SvelteSet is required when a virtualizer is set.
checkedItems bindablestring[] | SvelteSet<string>The ids of the checked items. A SvelteSet is required when a virtualizer is set.
childrenSnippetThe items of the tree.
TreeViewVirtualProps | TreeViewStaticProps
Element Attributes (ul)

Component Props (TreeViewItem)

NameTypeDefaultDescription
idstringThe id of the item, used by the tree to track its state. Falls back to a generated id.
type'item' | 'branch''item'Whether the item holds children, and can therefore be opened.
refHTMLLIElement | nullThe DOM reference of the item element.
valuestring | numberidThe value reported by the tree events. Falls back to the id of the item.
textstringThe label of the item, used by the type-ahead navigation.
openbooleanWhether the branch is open.
checkedbooleanWhether the item is checked.
indeterminatebooleanRenders the checkbox in its mixed state, when only some children are checked.
disabledbooleanDisables the user interaction.
indexnumberThe real index of the item in the data set. Required when the tree is virtualized.
onOpenChange(e: Event, data: { id: string; open: boolean }) => voidCalled when this branch opens or closes.
onCheckedChange(e: Event, data: { id: string; checked: boolean }) => voidCalled when this item is checked or unchecked.
Element Attributes (li)

Component Props (TreeViewItemContent)

NameTypeDefaultDescription
refHTMLDivElement | nullThe DOM reference of the layout element.
expandIconSnippet | ComponentCustom icon for the control that opens a branch.
iconBeforeSnippet | ComponentContent rendered before the label.
iconAfterSnippet | ComponentContent rendered after the label.
asideSnippet | ComponentContent pinned to the end of the row.
actionsSnippet | ComponentContent pinned to the end of the row, revealed on hover and on focus.
Element Attributes (div)
Edit this Doc View as Markdown