ライトボックス
ネイティブダイアログで、ページの上に画像を拡大表示します。
このページの内容
ライトボックスは、ページの上に画像を拡大表示するコンポーネントです。ネイティブの<dialog>に.lightboxを指定する構成で、サムネイルのボタンに付けたcommandforとcommand="show-modal"で開きます。ブラウザーがトップレイヤー、フォーカストラップ、Escキーの操作を提供するため、スクリプトは不要です。ダイアログ自体に背景面はなく、サイズは画像とぴったり一致します。背後のページは暗くなり、強くぼかされます。
ライトボックス
<button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal"> <img class="card-media" src="/demo/moonstone.svg" alt> <span class="card-header"> <span class="card-title">Moonstone</span> <span class="card-description">View the picture</span> </span></button><dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9"> <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills"> <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close"> <svg class="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"> <path d="M4 4L12 12" /><path d="M4 12L12 4" /> </svg> </button></dialog>import XIcon from '@master/icons-react/native/16/soft/x'<> <button className="card card-interactive w(240)" type="button" {...{ commandfor: 'moonstone', command: 'show-modal' }}> <img className="card-media" src="/demo/moonstone.svg" alt="" /> <span className="card-header"> <span className="card-title">Moonstone</span> <span className="card-description">View the picture</span> </span> </button> <dialog className="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style={{ '--lightbox-ratio': '16 / 9' } as React.CSSProperties}> <img className="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills" /> <p className="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button className="btn btn-sm btn-square lightbox-close" type="button" {...{ commandfor: 'moonstone', command: 'close' }} aria-label="Close"> <XIcon className="icon" /> </button> </dialog></><script> import XIcon from '@master/icons-svelte/native/16/soft/x'</script><button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal"> <img class="card-media" src="/demo/moonstone.svg" alt> <span class="card-header"> <span class="card-title">Moonstone</span> <span class="card-description">View the picture</span> </span></button><dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9"> <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills"> <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close"> <XIcon class="icon" /> </button></dialog><script setup>import XIcon from '@master/icons-vue/native/16/soft/x'</script><template> <button class="card card-interactive w(240)" type="button" commandfor="moonstone" command="show-modal"> <img class="card-media" src="/demo/moonstone.svg" alt> <span class="card-header"> <span class="card-title">Moonstone</span> <span class="card-description">View the picture</span> </span> </button> <dialog class="lightbox" id="moonstone" closedby="any" aria-label="Moonstone" style="--lightbox-ratio: 16 / 9"> <img class="lightbox-media" src="/demo/moonstone.svg" alt="A pale stone over grey hills"> <p class="lightbox-caption">Moonstone, the stone the light theme is named for.</p> <button class="btn btn-sm btn-square lightbox-close" type="button" commandfor="moonstone" command="close" aria-label="Close"> <XIcon class="icon" /> </button> </dialog></template>#開く・閉じる
- サムネイルのボタンに
commandfor="id"とcommand="show-modal"を指定すると、ライトボックスを開けます。閉じるボタンのcommand="close"は、ライトボックスを閉じてフォーカスをサムネイルに戻します。 closedby="any"を指定すると、外側をクリックして閉じられます。ダイアログは画像と同じサイズなので、画像の外側はすべて閉じる操作の対象です。この属性に対応していないブラウザーでも、Escキーと閉じるボタンは使えます。- ボタンからダイアログを操作するHTMLのコマンド機能に対応していないブラウザーでは、クリックハンドラーから
dialog.showModal()とdialog.close()を呼び出します。スタイルシートは共通です。
@starting-styleと離散トランジションにより、画像は少し小さい状態から拡大しながらフェードインし、閉じるときは開くときより短い時間で消えます。これらが使えない場合は、即座に表示・非表示が切り替わります。
#サイズ
lightbox-mediaは<img>、<video>、<iframe>のいずれかに指定します。表示サイズはファイル自体の寸法に依存しません。幅は最大64remで、ビューポートの上下左右に24pxずつ余白を確保し、幅と高さの両方に収まるように縮みます。縦横比は--lightbox-ratioで指定し、既定は3 / 2です。画像、動画、フレームはいずれも同じ方法でサイズが決まるため、読み込み時にレイアウトがずれません。
例の16 / 9のように、ダイアログの--lightbox-ratioに画像本来の縦横比を指定します。縦横比が異なる画像は、枠に合わせて切り抜かれます。--lightbox-widthは最大幅、--lightbox-gutterは外周の余白を指定します。
#キャプションと閉じるボタン
lightbox-captionは画像の下に浮かぶ、画像と同じ幅のバーです。画像の配置時にバーの分のスペースが確保されます。キャプションは1〜2行に収めてください。小さな正方形のボタンにlightbox-closeを指定すると、画像の上隅から8px内側に配置されます。立体感と独立した輪郭があるので、どの画像の上でも見つけやすくなります。
#ライトボックスとダイアログの使い分け
画像を1点だけ表示し、ユーザーに判断を求めない場合はライトボックスを使います。タイトル、本文、フォームなどを重ねて表示する場合はダイアログを使います。画像をキャプションとともにページ内に置く場合は図版を使います。
#アクセシビリティ
aria-labelでダイアログに名前を付けるか、aria-labelledbyでキャプションを参照します。- 拡大画像には、内容を伝える
altを指定します。何が開くかをボタンのテキストで伝えている場合は、ボタン内のサムネイルの代替テキストを空にできます。 - 外側のクリックやEscキーで閉じられることに気づかないユーザーもいるため、アクセシブルな名前を持つ閉じるボタンを必ず設けてください。
- 開くとフォーカスがライトボックス内に移り、閉じるとサムネイルに戻ります。動きを減らす設定では、画像のサイズを変えずにフェードします。
#クラス
| クラス | 説明 |
|---|---|
lightbox | 専用のシートを持たないダイアログ。画像と同じ大きさで、暗くぼかしたページの上に表示されます。 |
lightbox-media | 画像、動画、またはフレーム。縦横比は--lightbox-ratioで、幅は64remを上限とし、ビューポートに24pxの余白を確保できる範囲で最大になります。 |
lightbox-caption | 画像の下に浮かぶ、画像と同じ幅のバー |
lightbox-close | 閉じるボタンに指定すると、画像の上側の角から8px内側に配置 |
#カスタムプロパティ
| プロパティ | 既定値 | 説明 |
|---|---|---|
--lightbox-ratio | 3 / 2 | 画像のアスペクト比(幅÷高さ)。画像はこの比率に合わせて切り抜かれます。 |
--lightbox-width | 64rem | 画像の最大幅 |
--lightbox-gutter | 1.5rem | 画像とビューポートの各辺との間に確保する余白。 |
--lightbox-radius | var(--radius-control-lg) | 画像とそのキャプションの角の丸みです。 |