ナビゲーション
アプリの画面を切り替える枠のナビです。1 つの nav が、画面の幅で下のタブ・左のレール・左のサイドバーに形を変えます。
見本で種類と状態を確かめる
767 以下は下のタブ(項目 5 つまで)、768〜1279 は幅 84 のレール、1280 以上は幅 224 のサイドバーです。台の上の見本も、いま見ている画面の幅に合わせて形が変わります。このサイトの左のメニューも、この部品でできています。
明細
設定
コードを写す
HTML
<nav class="mz-app__nav mz-nav" aria-label="家計の画面">
<a class="mz-nav__brand" href="/" aria-label="mizolabo のホーム">
<svg class="mz-nav__mark" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M4 18V8l8 6 8-6v10"/></svg>
<span class="mz-nav__wordmark" aria-hidden="true">mizolabo</span>
</a>
<div class="mz-nav__section">
<h2 class="mz-nav__heading">家計</h2>
<ul class="mz-nav__list" role="list">
<li><a class="mz-nav__item" href="/"><svg class="mz-nav__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8"/><path d="M3 10a2 2 0 0 1 .709-1.528l7-5.999a2 2 0 0 1 2.582 0l7 5.999A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg><span class="mz-nav__label">ホーム</span></a></li>
<li><a class="mz-nav__item" href="/outlook"><svg class="mz-nav__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 3v16a2 2 0 0 0 2 2h16"/><path d="m19 9-5 5-4-4-3 3"/></svg><span class="mz-nav__label">見通し</span></a></li>
<li><a class="mz-nav__item" href="/transactions" aria-current="page"><svg class="mz-nav__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 12h.01"/><path d="M3 18h.01"/><path d="M3 6h.01"/><path d="M8 12h13"/><path d="M8 18h13"/><path d="M8 6h13"/></svg><span class="mz-nav__label">明細</span></a></li>
</ul>
</div>
<div class="mz-nav__section">
<h2 class="mz-nav__heading">管理</h2>
<ul class="mz-nav__list" role="list">
<li><a class="mz-nav__item" href="/accounts"><svg class="mz-nav__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M19 7V4a1 1 0 0 0-1-1H5a2 2 0 0 0 0 4h15a1 1 0 0 1 1 1v4h-3a2 2 0 0 0 0 4h3a1 1 0 0 0 1-1v-2a1 1 0 0 0-1-1"/><path d="M3 5v14a2 2 0 0 0 2 2h15a1 1 0 0 0 1-1v-4"/></svg><span class="mz-nav__label">口座</span></a></li>
<li><a class="mz-nav__item" href="/settings"><svg class="mz-nav__icon" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M10 5H3"/><path d="M12 19H3"/><path d="M14 3v4"/><path d="M16 17v4"/><path d="M21 12h-9"/><path d="M21 19h-5"/><path d="M21 5h-7"/><path d="M8 10v4"/><path d="M8 12H3"/></svg><span class="mz-nav__label">設定</span></a></li>
</ul>
</div>
</nav>React の呼び出し方
<Nav aria-label="家計の画面">
<NavSection title="家計">
<NavItem href="/" icon={<House {...icon} />}>ホーム</NavItem>
<NavItem href="/outlook" icon={<ChartLine {...icon} />}>見通し</NavItem>
<NavItem href="/transactions" icon={<ReceiptText {...icon} />} current>明細</NavItem>
</NavSection>
<NavSection title="管理">
<NavItem href="/accounts" icon={<Wallet {...icon} />}>口座</NavItem>
<NavItem href="/settings" icon={<Settings {...icon} />}>設定</NavItem>
</NavSection>
</Nav>実装を見る(react/Nav.tsx)
React の実装(react/Nav.tsx)
/**
* Nav — 画面の枠のナビゲーション(docs/spec/components.md「nav」)。見た目は css/components/nav.css だけが持つ。
* - 1 つの <nav> が、幅で tabbar(〜767)・rail(768〜1279)・sidebar(1280〜)に変わる
* - 選択中の項目は current を渡し、aria-current="page" を付ける。区分の中のほかのページにいるときは current="true"(aria-current="true")
* - 項目が多い区分は、sidebar で NavDisclosure(<details>・<summary>)の群に畳んでよい。開くのは、いまのページがある群だけ
* - tabbar に並ぶのは 5 つまで。節の見出し(NavSection の title)は sidebar でだけ見える
* - 枠(.mz-app)の中に置くときは frame(既定 true)で .mz-app__nav を付ける
*/
import { forwardRef, type AnchorHTMLAttributes, type HTMLAttributes, type ReactNode } from 'react';
export interface NavProps extends HTMLAttributes<HTMLElement> {
/** ナビの名前(例:「画面の切り替え」) */
'aria-label': string;
frame?: boolean;
}
export const Nav = forwardRef<HTMLElement, NavProps>(function Nav({ frame = true, className, ...rest }, ref) {
const classes = ['mz-nav', frame ? 'mz-app__nav' : '', className ?? ''].filter(Boolean).join(' ');
return <nav ref={ref} className={classes} {...rest} />;
});
export interface NavBrandProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
/** ロゴの印(rail で見せる) */
mark: ReactNode;
/** ワードマーク(sidebar で見せる) */
wordmark: ReactNode;
/** リンクの名前(rail ではワードマークが見えないため必ず渡す) */
'aria-label': string;
}
export const NavBrand = forwardRef<HTMLAnchorElement, NavBrandProps>(function NavBrand(
{ mark, wordmark, className, ...rest },
ref,
) {
return (
<a ref={ref} className={className ? `mz-nav__brand ${className}` : 'mz-nav__brand'} {...rest}>
<span className="mz-nav__mark" aria-hidden="true">
{mark}
</span>
<span className="mz-nav__wordmark" aria-hidden="true">
{wordmark}
</span>
</a>
);
});
export interface NavSectionProps extends Omit<HTMLAttributes<HTMLDivElement>, 'title'> {
title?: ReactNode;
}
export const NavSection = forwardRef<HTMLDivElement, NavSectionProps>(function NavSection(
{ title, className, children, ...rest },
ref,
) {
return (
<div ref={ref} className={className ? `mz-nav__section ${className}` : 'mz-nav__section'} {...rest}>
{title ? <h2 className="mz-nav__heading">{title}</h2> : null}
<ul className="mz-nav__list" role="list">
{children}
</ul>
</div>
);
});
export interface NavItemProps extends AnchorHTMLAttributes<HTMLAnchorElement> {
icon: ReactNode;
/** true か 'page':行き先そのもの(aria-current="page")。'true':行き先の区分の中のほかのページ(aria-current="true") */
current?: boolean | 'page' | 'true';
}
export const NavItem = forwardRef<HTMLAnchorElement, NavItemProps>(function NavItem(
{ icon, current = false, className, children, ...rest },
ref,
) {
return (
<li>
<a
ref={ref}
className={className ? `mz-nav__item ${className}` : 'mz-nav__item'}
aria-current={current === 'true' ? 'true' : current ? 'page' : undefined}
{...rest}
>
<span className="mz-nav__icon" aria-hidden="true">
{icon}
</span>
<span className="mz-nav__label">{children}</span>
</a>
</li>
);
});
export interface NavDisclosureProps extends Omit<HTMLAttributes<HTMLDetailsElement>, 'title'> {
/** 群の見出し(summary の文字) */
title: ReactNode;
/** 左の印(ChevronRight。size-icon-sm。開いているとき CSS が 90 度回す) */
chevron: ReactNode;
/** いまのページがある群だけ true */
open?: boolean;
}
/**
* 区分の中の群(sidebar だけ)。JS がなくても開け閉めできる。開け閉めの状態は保存しない
* (ページを移るたびに、いまの群だけ開いた状態に戻すには、呼ぶ側で key にパスを渡して作り直す)。
* NavSection の中に、NavItem と並べて置く
*/
export const NavDisclosure = forwardRef<HTMLDetailsElement, NavDisclosureProps>(function NavDisclosure(
{ title, chevron, open = false, className, children, ...rest },
ref,
) {
return (
<li>
<details ref={ref} className={className ? `mz-nav__group ${className}` : 'mz-nav__group'} open={open} {...rest}>
<summary className="mz-nav__summary">
<span className="mz-nav__chevron" aria-hidden="true">
{chevron}
</span>
<span className="mz-nav__label">{title}</span>
</summary>
<ul className="mz-nav__list" role="list">
{children}
</ul>
</details>
</li>
);
});画面の枠の中での場所(下に貼り付く・左に並ぶ)は、css/layout.css の .mz-app__nav が持ちます。
いつ使うか
- アプリの主な画面(3〜5 つ)を切り替えるのに使います。画面の中の切り替えはタブにします。
- 下のタブのラベルは全角 4 字までにします。文字を 200% にした 390 幅で、5 字以上は 3 行になるためです。
- 画面を切り替えてもナビは動かしません(
view-transition-name: mz-nav)。
読み上げとキーボードにどう応えるか
<nav aria-label>で名前を付けます。選んでいる画面の項目にaria-current="page"を付けると、「現在のページ」と読まれます。- レールとサイドバーの中のフォーカスの枠は nav-text の色です。濃紺の面の上で accent が見えにくいためです。
- アイコンには必ず文字のラベルを添え、アイコンは
aria-hidden="true"にします。
よい例と悪い例
更新日