Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

List

An input and layout view for displaying a scrollable collection of items.

When To Use It

Use List when you need to present a sequence of items that users can browse, focus, and optionally select. Lists work well for settings panels, menus, search results, file views, and other repeated content where each item shares the same structure.

Constructing a List

A List takes a reactive or static collection and a closure used to build each item view. The list automatically creates and updates internal signals for each item.

let items = Signal::new(vec!["One", "Two", "Three"]);

List::new(cx, items, |cx, _, item| {
	Label::new(cx, item).hoverable(false);
})
.selectable(Selectable::Single)
.on_select(|cx, index| cx.emit(AppEvent::SelectItem(index)));

Use modifiers such as orientation, selection_follows_focus, and scrollbar controls to change behavior:

List::new(cx, items, |cx, index, item| {
	Label::new(cx, item)
		.toggle_class("dark", index % 2 == 0)
		.width(Stretch(1.0))
		.height(Pixels(30.0))
		.hoverable(false);
})
.selectable(Selectable::Single)
.selection_follows_focus(true)
.orientation(Orientation::Horizontal)
.show_vertical_scrollbar(false)
.show_horizontal_scrollbar(true);

List Modifiers

ModifierTypeDescriptionDefault
selectionimpl Res<[usize]>Sets the selected item indices from an external source.No selection
on_selectFn(&mut EventContext, usize)Called when an item becomes selected.
selectableimpl Res<Selectable>Enables None, Single, or Multi item selection.Selectable::None
min_selectedimpl Res<usize>Minimum number of items that must remain selected.0
max_selectedimpl Res<usize>Maximum number of items that may be selected.No practical limit
selection_follows_focusimpl Res<bool>Selects focused items automatically during keyboard navigation.false
horizontalimpl Res<bool>Sets the list layout to horizontal when true.false
scroll_to_cursorboolMakes the scrollbar jump to the pointer when pressed.false
on_scrollFn(&mut EventContext, f32, f32)Called when the internal scroll view scrolls.
scroll_ximpl Res<f32>Sets the horizontal scroll position.0.0
scroll_yimpl Res<f32>Sets the vertical scroll position.0.0
show_horizontal_scrollbarimpl Res<bool>Controls visibility of the horizontal scrollbar.true
show_vertical_scrollbarimpl Res<bool>Controls visibility of the vertical scrollbar.true

Components and Styling

A List renders as a list element containing an internal scrollview, with each item rendered as a list-item.

SelectorDescription
listThe outermost list container.
list.horizontalApplied when the orientation is Horizontal.
list.selectableApplied when selection is enabled.
list list-itemThe element for each item in the list.
list list-item.focusedApplied to the currently focused item.
list list-item:checkedApplied to selected items.
list > scrollviewThe internal scroll view used by the list.
list.horizontal scroll-contentThe scroll content container laid out as a row.

Theming

SelectorPropertyDefault Theme Value
list list-itemheight30px
list.selectable list-itembackground-colortransparent
list.selectable list-item.focusedbackground-color#a3a3a3
list.selectable list-item:hoverbackground-color#7b7bff
list.selectable list-item:checkedbackground-color#51afef

Customize list items using the list and item selectors:

list list-item {
	padding-left: 8px;
	padding-right: 8px;
}

list.selectable list-item:hover {
	background-color: var(--accent);
}

list.selectable list-item:checked {
	background-color: var(--primary);
}

Accessibility

The list has a role of List, and each item has a role of ListItem. Focus and selection are exposed separately: keyboard navigation moves focus, and selection changes based on the configured selection mode.

Adding a Label

To associate visible text with a list, give the label an identifier and associate the list with it using labeled_by:

VStack::new(cx, |cx| {
	Label::new(cx, "Themes").id("themes_list_label");

	List::new(cx, items, |cx, _, item| {
		Label::new(cx, item).hoverable(false);
	})
	.labeled_by("themes_list_label")
	.selectable(Selectable::Single);
})
.height(Auto)
.gap(Pixels(8.0));

Using name Without a Label

If no visible label is present, provide an accessible name directly on the list with name:

List::new(cx, items, |cx, _, item| {
	Label::new(cx, item).hoverable(false);
})
.name("Themes")
.selectable(Selectable::Single);

Use this for compact layouts where a separate label is not shown.

Pointer Interaction

Users can interact with the list using the pointer in the following ways:

  • Click a list-item to focus the list and apply selection for that item.
  • In Selectable::Single, clicking the selected item again clears selection when min_selected is 0.
  • In Selectable::Multi, clicking toggles an item on or off, subject to min_selected and max_selected.

Keyboard Interaction

KeyAction
ArrowDown / ArrowUpMove focus to the next or previous item in a vertical list.
ArrowRight / ArrowLeftMove focus to the next or previous item in a horizontal list.
Space / EnterSelect the focused item.