シート
画面の右か下から重ねて開く面です。ダイアログより長い中身や、詳しい情報に使います。
見本で種類と状態を確かめる
右からのシートは幅 min(480, 画面の幅)・高さいっぱい、下からのシート(スマホの既定)は幅いっぱい・高さ 90% までで、上につまみを置きます。面はどちらも不透明の raised です。
右から
下から
開く操作
コードを写す
HTML
<button class="mz-button" type="button" commandfor="sheet-r-light" command="show-modal" data-demo-open="sheet-right">家賃の明細を見る</button>
<dialog class="mz-sheet" id="sheet-r-light" data-side="right" data-demo="sheet-right" aria-labelledby="sheet-r-light-title">
<span class="mz-sheet__grabber" aria-hidden="true"></span>
<div class="mz-sheet__header">
<h2 class="mz-sheet__title" id="sheet-r-light-title">家賃の明細</h2>
<button class="mz-icon-button mz-sheet__close" type="button" aria-label="閉じる" commandfor="sheet-r-light" command="close"><svg class="" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg></button>
</div>
<div class="mz-sheet__body" tabindex="0" role="group" aria-label="家賃の明細の中身(入らないときは横にスクロールします)" data-scroll-frame>
<dl class="mz-kv">
<div class="mz-kv__row"><dt>日付</dt><dd>11/20(金)</dd></div>
<div class="mz-kv__row"><dt>金額</dt><dd><span class="mz-num">−78,000</span><span class="mz-kv__unit">円</span></dd></div>
<div class="mz-kv__row"><dt>口座</dt><dd>給与の口座</dd></div>
<div class="mz-kv__row"><dt>分類</dt><dd>住まい・家賃</dd></div>
<div class="mz-kv__row"><dt>メモ</dt><dd>12月分。更新料は2027年3月。</dd></div>
</dl>
</div>
<div class="mz-sheet__footer">
<div class="mz-button-group">
<button class="mz-button" data-variant="quiet" type="button" commandfor="sheet-r-light" command="close">閉じる</button>
<button class="mz-button" data-variant="primary" type="button" commandfor="sheet-r-light" command="close">分類を変える</button>
</div>
</div>
</dialog>React の呼び出し方
const [open, setOpen] = useState(false);
return (
<>
<Button onClick={() => setOpen(true)}>家賃の明細を見る</Button>
<Sheet
open={open}
onClose={() => setOpen(false)}
title="家賃の明細"
side="right"
closeIcon={<X strokeWidth={1.5} aria-hidden="true" />}
bodyLabel="家賃の明細の中身"
footer={
<>
<Button variant="quiet" onClick={() => setOpen(false)}>閉じる</Button>
<Button variant="primary">分類を変える</Button>
</>
}
>
<Kv
items={[
{ key: 'date', label: '日付', value: '11/20(金)' },
{ key: 'amount', label: '金額', value: <span className="mz-num">−78,000</span>, unit: '円' },
{ key: 'account', label: '口座', value: '給与の口座' },
{ key: 'category', label: '分類', value: '住まい・家賃' },
{ key: 'memo', label: 'メモ', value: '12月分。更新料は2027年3月。' },
]}
/>
</Sheet>
</>
);実装を見る(react/Sheet.tsx)
React の実装(react/Sheet.tsx)
/**
* Sheet — シート(ドロワー)(docs/spec/components.md「sheet」)。見た目は css/components/sheet.css だけが持つ。
* - 操作は Dialog と同じ(showModal・Esc で閉じる・フォーカスを戻す)
* - side:'right'(右から)/'bottom'(下から)。渡さないと SP は下から、768〜 は右から
*/
import { forwardRef, useId, useImperativeHandle, useRef, type DialogHTMLAttributes, type ReactNode } from 'react';
import { useModalOpen } from './Dialog';
import { scrollFrameProps, useScrollFrame } from './ScrollFrame';
export type SheetSide = 'right' | 'bottom';
export interface SheetProps extends Omit<DialogHTMLAttributes<HTMLDialogElement>, 'open' | 'title'> {
open: boolean;
onClose: () => void;
title: ReactNode;
side?: SheetSide;
closeLabel?: string;
closeIcon?: ReactNode;
footer?: ReactNode;
/** 中身の枠の名前(既定 'シートの中身')。中身がはみ出すときだけ Tab で止まり、この名前に「(入らないときは横にスクロールします)」を足して読む(ScrollFrame.ts) */
bodyLabel?: string;
}
export const Sheet = forwardRef<HTMLDialogElement, SheetProps>(function Sheet(
{ open, onClose, title, side, closeLabel = '閉じる', closeIcon, footer, bodyLabel = 'シートの中身', className, children, ...rest },
ref,
) {
const inner = useRef<HTMLDialogElement>(null);
useImperativeHandle(ref, () => inner.current as HTMLDialogElement);
const titleId = useId();
const body = useRef<HTMLDivElement>(null);
useScrollFrame(body);
useModalOpen(inner, open, onClose);
return (
<dialog
ref={inner}
className={className ? `mz-sheet ${className}` : 'mz-sheet'}
data-side={side}
aria-labelledby={titleId}
{...rest}
>
<span className="mz-sheet__grabber" aria-hidden="true" />
<div className="mz-sheet__header">
<h2 className="mz-sheet__title" id={titleId}>
{title}
</h2>
<button
type="button"
className="mz-icon-button mz-sheet__close"
aria-label={closeLabel}
onClick={() => inner.current?.close()}
>
{closeIcon}
</button>
</div>
<div ref={body} className="mz-sheet__body" {...scrollFrameProps(bodyLabel)}>
{children}
</div>
{footer ? (
<div className="mz-sheet__footer">
<div className="mz-button-group">{footer}</div>
</div>
) : null}
</dialog>
);
});つまみ(__grabber)は下からのシートでだけ見えます。開き方はダイアログと同じです。
いつ使うか
- 一覧の 1件の詳しい中身、絞り込みの条件など、画面を移らずに見せたい長めの中身に使います。
- スマホでは下から、広い画面では右から出すのが既定です。
- 取り消せない操作の確認だけなら、ダイアログにします。
読み上げとキーボードにどう応えるか
- ダイアログと同じく、ネイティブの
<dialog>をshowModal()で開きます。Esc で閉じ、フォーカスは開いたボタンへ戻ります。 - 名前は
aria-labelledbyで見出しと結びます。つまみは飾りなのでaria-hidden="true"にします。
よい例と悪い例
更新日