iced-cube

Navigation

Sidebar

Persistent app navigation down one side of the window, which folds to an icon rail and becomes a drawer on small screens.

use iced_cube::navigation::sidebar::{self, Event, Output, State, group, item, menu_button, sidebar};
API reference
Default: A workspace switcher, groups with icons and a badge, a parent with nested items, a collapsible group with a disabled item, and a user menu in the footer. On a phone it becomes a drawer.Default: A workspace switcher, groups with icons and a badge, a parent with nested items, a collapsible group with a disabled item, and a user menu in the footer. On a phone it becomes a drawer.
Static preview
Default. A workspace switcher, groups with icons and a badge, a parent with nested items, a collapsible group with a disabled item, and a user menu in the footer. On a phone it becomes a drawer.

Usage

The app owns the state: which item is active, which groups and parents are open, whether the sidebar is folded to a rail, and whether the drawer is open. Clicking a parent item opens its children instead of activating it, and in the rail it expands the sidebar first. The item that holds the page keeps the accent background, and a closed parent keeps it while one of its children is active.

The sidebar is drawn on a surface a shade off the page, with a one pixel edge, so it sits comfortably beside cards and other content. Rows use the same look as menus and lists.

Small screens

With .content(...), the sidebar notices when its width falls below the breakpoint and sends Event::Narrow(true). From then on it is an off-canvas drawer: a bar above the page holds the trigger, and the drawer slides over a scrim. A tap on the scrim, Escape or choosing an item closes it, and widening the window closes it too. Event::Toggle does whichever fits, so one trigger button works at every size.

Keyboard focus

iced 0.14 has no general focus model, so the sidebar takes focus the way a text field does: a press inside it gives it focus, and a press outside or Escape takes it away. An open drawer always has focus. While focused, the sidebar resolves its own keymap and captures the keys it uses. Ctrl+B is meant to work anywhere, so route keys::subscription through state.shortcut, which only resolves that one action. Some browsers keep Ctrl+B for themselves, so the rail story also has a button.

Examples

Collapsed to a rail

Icons only, with the label in a tooltip. Ctrl+B toggles the rail through the default keymap, routed from keys::subscription with State::shortcut.

Collapsed to a rail: Icons only, with the label in a tooltip. Ctrl+B toggles the rail through the default keymap, routed from keys::subscription with State::shortcut.Collapsed to a rail: Icons only, with the label in a tooltip. Ctrl+B toggles the rail through the default keymap, routed from keys::subscription with State::shortcut.
Static preview

Drawer on narrow windows

Below the breakpoint the sidebar slides over the page on a scrim, opened by the trigger in the bar above the content. This example sets the breakpoint so high that it always shows the drawer.

Drawer on narrow windows: Below the breakpoint the sidebar slides over the page on a scrim, opened by the trigger in the bar above the content. This example sets the breakpoint so high that it always shows the drawer.Drawer on narrow windows: Below the breakpoint the sidebar slides over the page on a scrim, opened by the trigger in the bar above the content. This example sets the breakpoint so high that it always shows the drawer.
Static preview

In a resizable panel

The sidebar fills a collapsible panel beside the page. Dragging the panel below its minimum snaps it to the width of the rail.

In a resizable panel: The sidebar fills a collapsible panel beside the page. Dragging the panel below its minimum snaps it to the width of the rail.In a resizable panel: The sidebar fills a collapsible panel beside the page. Dragging the panel below its minimum snaps it to the width of the rail.
Static preview

API

ItemDescription
group(label, items)A labelled group of items. An empty label draws no heading. Add .collapsible(true) to let the heading open and close it.
item(id, label)A navigation item. Add .icon(glyph), .badge(text), .disabled(true) or .children(items) for one level of nested items.
State::new(groups)Holds the navigation with every group open and nothing active.
.with_active(id) / .with_rail(bool)Starts on an item, opening its parent, or collapsed to the icon rail.
state.update(Event)Handles clicks, keyboard navigation, the rail and the drawer. Returns Output::Activated(id) when an item is chosen.
state.shortcut(&keymap, &key)Resolves the app-wide Ctrl+B from keys::subscription. The sidebar handles its other keys while it has focus.
state.active() / state.item(id) / state.is_compact()The active item, an item's details, and whether the rail is showing, for the header and footer buttons.
sidebar(&state)Renders the navigation. Items render disabled until .on_event is set.
.header(element) / .footer(element)Content above and below the groups, such as a workspace switcher and the signed in user.
.content(element)The page beside the sidebar. With content, the sidebar becomes a drawer below the breakpoint; without it only the panel is drawn.
.breakpoint(width)The window width below which the sidebar becomes a drawer. Defaults to 640.
.trigger(bool)Whether narrow windows get a bar holding the drawer's trigger. Turn it off to put sidebar::trigger in your own header.
.width(length)The expanded width, 256 by default. Use Length::Fill inside a resizable panel.
.keymap(Keymap<Action>) / sidebar::default_keymap()The shortcuts the focused sidebar resolves.
sidebar::trigger(message)The icon button that toggles the rail, or the drawer on a narrow window. Send Event::Toggle from it.
menu_button(title)A header or footer button with .subtitle, .icon or .initials, and .compact(state.is_compact()) for the rail. Wrap it in a dropdown menu for a switcher or a user menu.

Keyboard

Default shortcuts. Every one can be changed or removed with a keymap; see Keyboard shortcuts.

KeysAction
ArrowDownNext Highlights the next row, wrapping.
ArrowUpPrevious Highlights the previous row, wrapping.
HomeFirst Highlights the first row.
EndLast Highlights the last row.
Enter or SpaceActivate Activates the highlighted item or opens and closes its group.
ArrowRightExpand Opens the highlighted group or parent item, or moves into it.
ArrowLeftCollapse Closes the highlighted group or parent item, or moves out to it.
Ctrl+BToggleRail Collapses the sidebar to an icon rail and back, or toggles the drawer on a narrow window.
EscapeClose Closes the drawer, or leaves the sidebar.
Esc