NavigationTree
A tree of links, such as the pages of a site or of a guide, which shows the page that the user is on.
<script>
import { $bind, $signal } from 'brumate/signals';
import Button from '@brumate/ui/Button.brm';
import NavigationTree from '@brumate/ui/NavigationTree.brm';
import NavigationTreeItem from '@brumate/ui/NavigationTreeItem.brm';
import NavigationTreeItemLink from '@brumate/ui/NavigationTreeItemLink.brm';
import type { Key } from '@brumate/ui/collection.ts';
import type { NavigationNode } from '@brumate/ui/navigation-tree.ts';
import Icon from '../../src/components/Icon.brm';
interface Page extends NavigationNode {
title: string;
children?: Page[];
}
const pages: Page[] = [
{ id: 'start', title: 'Getting started', href: '#start' },
{
id: 'guides',
title: 'Guides',
children: [
{ id: 'forms', title: 'Forms', href: '#forms' },
{
id: 'data',
title: 'Data',
children: [
{ id: 'tables', title: 'Tables', href: '#tables' },
{ id: 'trees', title: 'Trees', href: '#trees' },
],
},
],
},
{ id: 'api', title: 'API', href: '#api' },
];
let open = $signal<Key[]>([]);
</script>
<nav aria-label="Guide">
<NavigationTree class="ui-tree" aria-label="Guide" items=pages item=page selectedRoute="#tables" expandedKeys=$bind(open) />
</nav>
#template page(page: Page) {
<NavigationTreeItem class="ui-treerow ui-navrow" textValue=(page.title) href=(page.href)>
#if (page.children) {
<Button slot="chevron" class="ui-chevron"><Icon name="chevron" size=(14) /></Button>
} #else {
<span class="ui-chevron" aria-hidden="true"></span>
}
<NavigationTreeItemLink class="ui-navlink">{page.title}</NavigationTreeItemLink>
</NavigationTreeItem>
}
<nav aria-label="Guide">
<NavigationTree aria-label="Guide" items=pages item=page selectedRoute=(route.url.hash) />
</nav>
#template page(page: Page) {
<NavigationTreeItem textValue=(page.title) href=(page.href)>
<Button slot="chevron">▸</Button>
<NavigationTreeItemLink>{page.title}</NavigationTreeItemLink>
</NavigationTreeItem>
}
- A
NavigationTreetakesitemsand anitemtemplate, as a Tree does. An item can have anhref. The template renders aNavigationTreeItemwith aNavigationTreeItemLink, which links to thehrefof its item, or shows its text when it has none. selectedRoutenames the page that shows. The item whosehrefis that route is the current one: its link hasaria-current="page", and it showsdata-current. An item that holds it showsdata-current-ancestorwhile it is closed, so a mark can show where the page is.- Nothing is selected.
expandedKeysholds the keys of the open items, andonExpandedChangehears of changes. - A NavigationTree is no landmark. Put it in a
navwith a label, as above. - A row shows
data-hrefand the states of a TreeItem, and its level is in the--tree-item-levelCSS variable.
Keyboard
- The links are in the order of Tab, as the links of a page are.
- A disabled item, by
isDisabledon it ordisabledKeyson the tree, has a link that is aspanwithrole="link",aria-disabledanddata-disabled, and nohref. It does not navigate and is not a tab stop, as a disabled Link. - Up and Down move to the link of the next or the previous row that shows, and Home and End to the first and the last.
- Right opens a closed row. Left closes an open row, or else moves to the row that holds it. Right to left, they turn around.
- Enter follows a link. Letters move to the row whose text starts with them.