ナビゲーション

v2.1.0
オーバーレイ

ライトボックス

ネイティブダイアログで、ページの上に画像を拡大表示します。

このページの内容

ライトボックスは、ページの上に画像を拡大表示するコンポーネントです。ネイティブの<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>
カードを押すと開きます。画像の外側をクリックするか、閉じるボタンまたはEscキーを押すと閉じます。

#開く・閉じる

  • サムネイルのボタンに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-ratio3 / 2画像のアスペクト比(幅÷高さ)。画像はこの比率に合わせて切り抜かれます。
--lightbox-width64rem画像の最大幅
--lightbox-gutter1.5rem画像とビューポートの各辺との間に確保する余白。
--lightbox-radiusvar(--radius-control-lg)画像とそのキャプションの角の丸みです。

#ソース

@master/ui/components/lightbox.css
/* * Lightbox: a picture opened large over the page. * A native <dialog>, opened as a modal by the thumbnail's own button with commandfor and command="show-modal". * The dialog has no sheet: it is exactly as large as the picture it holds, so a click anywhere else is a * click outside it and, with closedby="any", closes it. The page behind is dimmed and blurred well out of * focus, and the picture stands on it with the shadow of an overlay. * The picture is 64rem wide, or as wide as the viewport lets it be with a 24px gutter on every side, at the * proportions set in --lightbox-ratio, 3 by 2 unless told otherwise. Its size comes from that and never from * the file, so an image, a video and a frame are all sized the same way and nothing moves when one loads. * A caption is a floating bar under the picture, as wide as it is. A close button rests 8px inside its corner. */@layer components.ui {  .lightbox {    --lightbox-width: 64rem;    --lightbox-ratio: 3 / 2;    --lightbox-radius: var(--radius-control-lg);    --lightbox-gutter: 1.5rem;    /* The room a caption takes under the picture. */    --lightbox-room: 0rem;    width: fit-content;    max-width: none;    height: fit-content;    max-height: none;    margin: auto;    padding: 0;    border: 0;    overflow: visible;    background: transparent;    color: var(--color-text-body);    /* Closed is where it leaves to: it fades and settles back half as far as it came, faster than it arrived. */    opacity: 0;    scale: .98;    transition-property: opacity, scale, overlay, display;    transition-duration: var(--duration-quick);    transition-timing-function: var(--easing-glide);    transition-behavior: allow-discrete;    &[open] {      display: grid;      gap: .5rem;      opacity: 1;      scale: 1;      transition-duration: var(--duration-base);      @starting-style {        opacity: 0;        scale: .96;      }    }    /* The page steps well back: the same scrim as a dialog, with a deeper blur. */    &::backdrop {      background: var(--color-surface-scrim);      backdrop-filter: blur(16px);      opacity: 0;      transition-property: opacity, overlay, display;      transition-duration: var(--duration-quick);      transition-timing-function: var(--easing-glide);      transition-behavior: allow-discrete;    }    &[open]::backdrop {      opacity: 1;      transition-duration: var(--duration-base);    }    /* The scrim's first frame is a rule of its own: a selector that ends in a pseudo-element cannot hold it. */    @starting-style {      &[open]::backdrop {        opacity: 0;      }    }    /* With a caption under it, the picture leaves room for the bar. */    &:has(.lightbox-caption) {      --lightbox-room: 3rem;    }    @media (prefers-reduced-motion: reduce) {      scale: none;      &[open] {        scale: none;        @starting-style {          scale: none;        }      }    }    /* A reader who asks for less transparency gets the scrim without the blur behind it. */    @media (prefers-reduced-transparency: reduce) {      &::backdrop {        backdrop-filter: none;      }    }    @media (forced-colors: active) {      &::backdrop {        background: Canvas;        backdrop-filter: none;      }    }  }  /* The picture, a video or a frame: as wide as its limit, the viewport's width or the viewport's height allows. */  .lightbox-media {    display: block;    width: min(var(--lightbox-width), 100vw - var(--lightbox-gutter) * 2, (100dvh - var(--lightbox-gutter) * 2 - var(--lightbox-room)) * var(--lightbox-ratio));    height: auto;    aspect-ratio: var(--lightbox-ratio);    margin: 0;    border: 0;    border-radius: calc(var(--lightbox-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-inset);    box-shadow: var(--shadow-overlay);    outline: 1px solid var(--color-line-image);    outline-offset: -1px;    object-fit: cover;    @media (forced-colors: active) {      outline-color: CanvasText;    }  }  /* The caption: a floating bar as wide as the picture, which never makes the dialog wider than it. */  .lightbox-caption {    box-sizing: border-box;    width: 0;    min-width: 100%;    margin: 0;    padding: .5rem .875rem;    --surface-color: var(--color-surface-floating);    border-radius: calc(var(--lightbox-radius) * var(--corner-scale));    corner-shape: var(--corner-shape);    background: var(--color-surface-floating);    color: var(--color-text-body);    box-shadow: var(--shadow-floating);    font-size: .8125rem;    line-height: 1.45;    text-align: center;    text-wrap: balance;    overflow-wrap: anywhere;    @media (forced-colors: active) {      outline: 1px solid CanvasText;      outline-offset: -1px;    }  }  /* On the close button: it rests inside the picture's top corner. */  .lightbox-close {    position: absolute;    z-index: 1;    inset-block-start: .5rem;    inset-inline-end: .5rem;  }  /* While the picture is open the page behind it does not scroll, as behind a modal dialog. */  :root:has(.lightbox:modal) {    overflow: hidden;  }}