日本語

Breadcrumb

A trail of links to the pages above the current one, ending at the current page.

Result

breadcrumb.rs
use hypersig::html::{div, h2};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Rest, Signals};
use hypersig_ui::Breadcrumb;
use serde::{Deserialize, Serialize};

use crate::doc::{Component, Doc};
use crate::{PAGE, Site};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Docs {
    /// The title of the page the demo stands on.
    title: String,
}

/// The path of `doc` inside this site, as `PAGE` takes it.
fn at(site: Site, doc: Doc) -> Rest {
    Rest::parse(doc.url(site).trim_start_matches('/'))
        .expect("a component's page has a directory without `.` or `..` parts")
}

pub(super) fn page() -> Page<Docs> {
    let trail = Breadcrumb::new("Breadcrumb")
    .link(
        "Components",
        PAGE.link_with(at(site, Doc::ComponentsHome)),
    )
    .link(
        "Navigation",
        PAGE.link_with(at(site, Doc::Components(Component::Navigation))),
    )
    .current("Breadcrumb");
    Page::new(
        "Docs",
        Docs {
            title: Doc::Components(Component::Breadcrumb).title(site).into(),
        },
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(trail)
            .child(h2().bind_text(Docs::title())),
    )
}

Accessibility

The trail is a <nav> named by its label, holding an ordered list, so a screen reader lists it among the page's landmarks and reads how many items the trail has. The current page ends the list as text with aria-current="page", which a screen reader announces as the current page. The chevrons between the items are hidden from assistive technology. The links take Tab and Enter as any link does.

Building a trail

Breadcrumb::new starts an empty trail, link adds a page above the current one, and current ends the trail at the page the visitor is on:

impl<S> Breadcrumb<S> {
    pub fn new(label: impl Into<String>) -> Self;
    pub fn link(self, text: impl Into<String>, link: Link<S>) -> Self;
    pub fn current(self, text: impl Into<String>) -> El<S>;
}

link takes a Link, which a route builds with link or link_with, so each href is a route of the application. current is the only way to get the element out of a Breadcrumb, and it returns an El<S>, which has no link. A trail therefore always ends at the page it is on, and nothing can follow it:

use hypersig::{Route, Signals};
use hypersig_ui::Breadcrumb;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Tracker {}

hypersig::routes! {
    fn pages(Router<()>);
    const HOME: Route = "/" => page;
}

// error[E0599]: no method named `link` found for struct `El<S>` in the current scope
let trail = Breadcrumb::<Tracker>::new("Breadcrumb")
    .current("Issue 41")
    .link("Home", HOME.link());

The HTML

A trail of two links ending at the issue page renders as:

<nav aria-label="Breadcrumb">
  <ol>
    <li><a href="/">Home</a></li>
    <li role="presentation" aria-hidden="true"><svg aria-hidden="true" …></svg></li>
    <li><a href="/projects/3">Project 3</a></li>
    <li role="presentation" aria-hidden="true"><svg aria-hidden="true" …></svg></li>
    <li><span aria-current="page">Issue 41</span></li>
  </ol>
</nav>

The chevrons are list items with role="presentation", so a screen reader counts only the three pages.

What is not checked

The links appear in the order they are added, and nothing checks that each page is above the next or that the route of a link holds the current page. A trail added out of order renders out of order. The label and the texts are plain strings.

Example: an issue page

The handler builds the trail from the same arguments that name the page:

use hypersig::html::h1;
use hypersig::{Page, Route, Signals};
use hypersig_ui::Breadcrumb;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Tracker {}

hypersig::routes! {
    fn pages(Router<()>);
    const HOME: Route = "/" => home;
    const PROJECT: Route<u32> = "/projects/{id}" => project;
    const ISSUE: Route<(u32, u32)> = "/projects/{id}/issues/{number}" => issue;
}

async fn issue((): (), (id, number): (u32, u32)) -> Option<Page<Tracker>> {
    let title = format!("Issue {number}");
    let trail = Breadcrumb::new("Breadcrumb")
        .link("Home", HOME.link())
        .link(format!("Project {id}"), PROJECT.link_with(&id))
        .current(title.clone());
    Some(Page::new(title.clone(), Tracker {}).child(trail).child(h1().text(title)))
}

Next, Pagination links the pages of a long list.