パンくずリスト
今のページより上のページへのリンクを並べ、今のページで終わるパンくずリストです。
実行結果
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("パンくずリスト")
.link(
"コンポーネント",
PAGE.link_with(at(site, Doc::ComponentsHome)),
)
.link(
"ナビゲーション",
PAGE.link_with(at(site, Doc::Components(Component::Navigation))),
)
.current("パンくずリスト");
Page::new(
"ドキュメント",
Docs {
title: Doc::Components(Component::Breadcrumb).title(site).into(),
},
)
.child(
div()
.css(stack(rem(1)))
.child(trail)
.child(h2().bind_text(Docs::title())),
)
}アクセシビリティ
パンくずリストはラベルを名前に持つ <nav> で、中に順序付きリストを持ちます。そのため、スクリーンリーダーはページのランドマークの一覧にそれを出し、項目がいくつあるかを読み上げます。今のページは aria-current="page" を持つテキストとしてリストの最後に置かれ、スクリーンリーダーはそれを現在のページとして読み上げます。項目の間の山形の印は支援技術から隠します。リンクはほかのリンクと同じく Tab と Enter で操作します。
パンくずリストを作る
Breadcrumb::new は空のパンくずリストを作り、link は今のページより上のページを追加し、current は訪問者がいるページでリストを終えます。
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 はルートの link か link_with で作る Link を受け取るので、どの href もアプリケーションのルートになります。Breadcrumb から要素を取り出す方法は current だけで、current が返す El<S> には link がありません。そのため、パンくずリストは必ず今のページで終わり、そのあとには何も続けられません。
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());出力される HTML
2 つのリンクのあとに課題のページで終わるパンくずリストは、次のように描画されます。
<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>山形の印は role="presentation" を持つリストの項目なので、スクリーンリーダーが数えるのは 3 つのページだけです。
検査されないこと
リンクは追加した順に並び、各ページが次のページより上にあるか、リンクのルートが今のページを含むかは検査されません。順番を間違えて追加したパンくずリストは、その順番のまま描画されます。ラベルと各テキストはただの文字列です。
例: 課題のページ
ハンドラはページを指すのと同じ引数からパンくずリストを作ります。
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)))
}次の ページネーション では長い一覧のページをリンクで結びます。