ナビゲーション

v2.1.0
メディア

カルーセル

CSS だけで実装した、スクロールしてスナップする横または縦のスライド列。

このページの内容

カルーセルは、スクロールすると所定の位置にスナップする、横または縦のスライド列です。トラックはCSSだけでスクロールスナップ付きのスクロールコンテナーとして実装されているため、タッチ操作、トラックパッド、キーボード、支援技術で通常のスクロールと同じように操作できます。対応ブラウザーでは、スタイルシートが前後の移動ボタンとスライドごとのドットを追加し、ブラウザーが実際にフォーカスできるコントロールとして生成します。

カルーセル

<div class="carousel" role="region" aria-label="Featured projects" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Atlas</h3>      <p class="card-description">A map of every deployment, live.</p>    </header>    <div class="card-body">1.28M requests in the last 24 hours.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Borealis</h3>      <p class="card-description">Edge functions close to the reader.</p>    </header>    <div class="card-body">Served from 14 regions.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Cirrus</h3>      <p class="card-description">Storage that follows the project.</p>    </header>    <div class="card-body">7.4 of 10 GB in use.</div>  </article></div>
<div className="carousel" role="region" aria-label="Featured projects" tabIndex={0}>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Atlas</h3>      <p className="card-description">A map of every deployment, live.</p>    </header>    <div className="card-body">1.28M requests in the last 24 hours.</div>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Borealis</h3>      <p className="card-description">Edge functions close to the reader.</p>    </header>    <div className="card-body">Served from 14 regions.</div>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Cirrus</h3>      <p className="card-description">Storage that follows the project.</p>    </header>    <div className="card-body">7.4 of 10 GB in use.</div>  </article></div>
<div class="carousel" role="region" aria-label="Featured projects" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Atlas</h3>      <p class="card-description">A map of every deployment, live.</p>    </header>    <div class="card-body">1.28M requests in the last 24 hours.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Borealis</h3>      <p class="card-description">Edge functions close to the reader.</p>    </header>    <div class="card-body">Served from 14 regions.</div>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Cirrus</h3>      <p class="card-description">Storage that follows the project.</p>    </header>    <div class="card-body">7.4 of 10 GB in use.</div>  </article></div>
<template>  <div class="carousel" role="region" aria-label="Featured projects" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment, live.</p>      </header>      <div class="card-body">1.28M requests in the last 24 hours.</div>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions close to the reader.</p>      </header>      <div class="card-body">Served from 14 regions.</div>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>      <div class="card-body">7.4 of 10 GB in use.</div>    </article>  </div></template>

#コントロール

ボタンやドットに対応する要素はマークアップに記述せず、擬似要素で生成します。

  • ::scroll-button(inline-start)と::scroll-button(inline-end)はトラックを1ページ分スクロールします。ほかのボタンと同様に、通常時も浮き上がって見え、ポインターを重ねるとさらに浮き、押すと沈みます。先頭と末尾では、ブラウザーがそれぞれ該当するボタンを無効にし、スタイルシートがフェードアウトさせて非表示にするため、下のスライドを覆わなくなります。
  • 各スライドの::scroll-markerがドットになり、::scroll-marker-groupがドットをトラックの下にまとめます。表示中のスライドのドットは:target-currentにマッチし、横長になってインク色で表示されます。各ドットの周囲に余白を設けて操作領域を広げ、タッチ画面ではさらに広くするため、指で6pxの範囲を狙う必要はありません。

コントロールはアンカー配置で各カルーセルを基準に配置されます。アンカー名のスコープがカルーセルごとに限定されるため、同じページに複数あってもコントロールが混ざりません。これらの擬似要素に対応していないブラウザーでも、ボタンとドット以外の機能は維持され、スワイプやスクロールで操作できます。その場合は細いスクロールバーが表示され、スワイプできないマウスでも横に移動できます。ボタンとドットが描かれる場合やcarousel-viewの中では、スクロールバーは表示されません。

#複数枚を表示

carousel-2とcarousel-3は、スライドを一度に2枚または3枚表示します。トラックの幅からスライド間の隙間を引いた残りを均等に割り当てるため、最後に見えるスライドの端がトラックの端とそろいます。

複数枚を表示

<div class="carousel carousel-3" role="region" aria-label="Team" tabindex="0">  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span>    <span class="text-sm fg-text-strong">Ada</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-2.svg" alt></span>    <span class="text-sm fg-text-strong">Alan</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-3.svg" alt></span>    <span class="text-sm fg-text-strong">Katherine</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Grace">GH</span>    <span class="text-sm fg-text-strong">Grace</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>    <span class="text-sm fg-text-strong">Margaret</span>  </div></div>
<div className="carousel carousel-3" role="region" aria-label="Team" tabIndex={0}>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg">      <img className="avatar-image" src="/demo/portrait-1.svg" alt="" />    </span>    <span className="text-sm fg-text-strong">Ada</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg">      <img className="avatar-image" src="/demo/portrait-2.svg" alt="" />    </span>    <span className="text-sm fg-text-strong">Alan</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg">      <img className="avatar-image" src="/demo/portrait-3.svg" alt="" />    </span>    <span className="text-sm fg-text-strong">Katherine</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg" role="img" aria-label="Grace">GH</span>    <span className="text-sm fg-text-strong">Grace</span>  </div>  <div className="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span className="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>    <span className="text-sm fg-text-strong">Margaret</span>  </div></div>
<div class="carousel carousel-3" role="region" aria-label="Team" tabindex="0">  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-1.svg" alt></span>    <span class="text-sm fg-text-strong">Ada</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-2.svg" alt></span>    <span class="text-sm fg-text-strong">Alan</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg"><img class="avatar-image" src="/demo/portrait-3.svg" alt></span>    <span class="text-sm fg-text-strong">Katherine</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Grace">GH</span>    <span class="text-sm fg-text-strong">Grace</span>  </div>  <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">    <span class="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>    <span class="text-sm fg-text-strong">Margaret</span>  </div></div>
<template>  <div class="carousel carousel-3" role="region" aria-label="Team" tabindex="0">    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg">        <img class="avatar-image" src="/demo/portrait-1.svg" alt>      </span>      <span class="text-sm fg-text-strong">Ada</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg">        <img class="avatar-image" src="/demo/portrait-2.svg" alt>      </span>      <span class="text-sm fg-text-strong">Alan</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg">        <img class="avatar-image" src="/demo/portrait-3.svg" alt>      </span>      <span class="text-sm fg-text-strong">Katherine</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg" role="img" aria-label="Grace">GH</span>      <span class="text-sm fg-text-strong">Grace</span>    </div>    <div class="carousel-slide sheet sheet-flat flex flex-col items-center gap-xs p-md">      <span class="avatar avatar-lg" role="img" aria-label="Margaret">MH</span>      <span class="text-sm fg-text-strong">Margaret</span>    </div>  </div></template>

#次のスライドを一部表示

--carousel-slideをトラックの幅より小さくすると、次のスライドの端が見えます。次のスライドの端が見えるだけで、ドットに目を向ける前に続きがあると分かります。ドットが表示されないブラウザーでは、その端が唯一の手がかりになります。画像には図版を使います。トラックに--figure-ratioを指定すると、読み込み前からすべての画像の縦横比がそろい、読み込み時にスライド列のレイアウトがずれません。

次のスライドを一部表示

<div class="carousel" style="--carousel-slide: 84%; --figure-ratio: 16 / 10" role="region" aria-label="Field notes" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water">    <figcaption class="figure-caption">The jetty at high water</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water">    <figcaption class="figure-caption">Low tide at seven</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges">    <figcaption class="figure-caption">Moonrise over the ridges</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="A line of pines in mist">    <figcaption class="figure-caption">Pines in the morning fog</figcaption>  </figure></div>
<div className="carousel" style={{ '--carousel-slide': '84%', '--figure-ratio': '16 / 10' } as React.CSSProperties} role="region" aria-label="Field notes" tabIndex={0}>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water" />    <figcaption className="figure-caption">The jetty at high water</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water" />    <figcaption className="figure-caption">Low tide at seven</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges" />    <figcaption className="figure-caption">Moonrise over the ridges</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/pines.svg" alt="A line of pines in mist" />    <figcaption className="figure-caption">Pines in the morning fog</figcaption>  </figure></div>
<div class="carousel" style="--carousel-slide: 84%; --figure-ratio: 16 / 10" role="region" aria-label="Field notes" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water">    <figcaption class="figure-caption">The jetty at high water</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water">    <figcaption class="figure-caption">Low tide at seven</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges">    <figcaption class="figure-caption">Moonrise over the ridges</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="A line of pines in mist">    <figcaption class="figure-caption">Pines in the morning fog</figcaption>  </figure></div>
<template>  <div class="carousel" style="--carousel-slide: 84%; --figure-ratio: 16 / 10" role="region" aria-label="Field notes" tabindex="0">    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/harbor.svg" alt="A wooden jetty running out over calm water">      <figcaption class="figure-caption">The jetty at high water</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/tide.svg" alt="A pale sun low over flat water">      <figcaption class="figure-caption">Low tide at seven</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/moonstone.svg" alt="A full moon above two ridges">      <figcaption class="figure-caption">Moonrise over the ridges</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/pines.svg" alt="A line of pines in mist">      <figcaption class="figure-caption">Pines in the morning fog</figcaption>    </figure>  </div></template>

#中央揃え

carousel-centerは表示中のスライドを中央に置き、左右に隣のスライドの端を見せます。先頭の前と末尾の後ろにも余白を取り、最初と最後のスライドも中央に配置できるようにします。スライドの幅はトラックの80%です。隣のスライドの見える範囲は--carousel-slideで調整できます。両端の余白はパディングではなく空のフレックスアイテム2つで確保するため、スライドの幅はトラック全体を基準に算出されます。

中央揃え

<div class="carousel carousel-center" style="--figure-ratio: 16 / 10" role="region" aria-label="Material studies" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light">    <figcaption class="figure-caption">Pearl, in morning light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it">    <figcaption class="figure-caption">Obsidian, under a softbox</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun">    <figcaption class="figure-caption">Dunes, at first light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water">    <figcaption class="figure-caption">Tide, at dusk</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor">    <figcaption class="figure-caption">Arches, in low sun</figcaption>  </figure></div>
<div className="carousel carousel-center" style={{ '--figure-ratio': '16 / 10' } as React.CSSProperties} role="region" aria-label="Material studies" tabIndex={0}>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light" />    <figcaption className="figure-caption">Pearl, in morning light</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it" />    <figcaption className="figure-caption">Obsidian, under a softbox</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun" />    <figcaption className="figure-caption">Dunes, at first light</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water" />    <figcaption className="figure-caption">Tide, at dusk</figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor" />    <figcaption className="figure-caption">Arches, in low sun</figcaption>  </figure></div>
<div class="carousel carousel-center" style="--figure-ratio: 16 / 10" role="region" aria-label="Material studies" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light">    <figcaption class="figure-caption">Pearl, in morning light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it">    <figcaption class="figure-caption">Obsidian, under a softbox</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun">    <figcaption class="figure-caption">Dunes, at first light</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water">    <figcaption class="figure-caption">Tide, at dusk</figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor">    <figcaption class="figure-caption">Arches, in low sun</figcaption>  </figure></div>
<template>  <div class="carousel carousel-center" style="--figure-ratio: 16 / 10" role="region" aria-label="Material studies" tabindex="0">    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/pearl.svg" alt="A pearl on a stone plinth, in soft morning light">      <figcaption class="figure-caption">Pearl, in morning light</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/obsidian.svg" alt="A polished obsidian sphere on a dark glossy floor, a softbox reflected in it">      <figcaption class="figure-caption">Obsidian, under a softbox</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/dune.svg" alt="Dunes at first light, their ridges catching a low sun">      <figcaption class="figure-caption">Dunes, at first light</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/tide.svg" alt="A still sea at dusk, the moon low over the horizon and its light broken on the water">      <figcaption class="figure-caption">Tide, at dusk</figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/home/slides/arches.svg" alt="A plaster hall with three arched openings, low sun laying their shapes across the floor">      <figcaption class="figure-caption">Arches, in low sun</figcaption>    </figure>  </div></template>

#固定幅のスライド列

同じ固定幅のスライドを、収まる枚数だけ表示し、次のスライドはトラックの端で一部だけ見せます。近接スナップでは、素早くフリックするとスライドごとに止まらずに数枚進み、スクロールが止まる位置に最も近いスライドにスナップします。

固定幅のスライド列

<div class="carousel (scroll-snap-type:x|proximity)" style="--carousel-slide: 8.5rem; --carousel-gap: .75rem; --figure-ratio: 4 / 5" role="region" aria-label="Prints" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Cairn</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Dunes</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Headland</span>$64    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Pines</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Steps</span>$40    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Jetty</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="The sea at slack tide">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Slack tide</span>$48    </figcaption>  </figure></div>
<div className="carousel (scroll-snap-type:x|proximity)" style={{ '--carousel-slide': '8.5rem', '--carousel-gap': '.75rem', '--figure-ratio': '4 / 5' } as React.CSSProperties} role="region" aria-label="Prints" tabIndex={0}>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Cairn</span>$48    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Dunes</span>$56    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Headland</span>$64    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Pines</span>$48    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Steps</span>$40    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Jetty</span>$56    </figcaption>  </figure>  <figure className="carousel-slide figure">    <img className="figure-media" src="/demo/tide.svg" alt="The sea at slack tide" />    <figcaption className="figure-caption">      <span className="block fg-text-strong">Slack tide</span>$48    </figcaption>  </figure></div>
<div class="carousel (scroll-snap-type:x|proximity)" style="--carousel-slide: 8.5rem; --carousel-gap: .75rem; --figure-ratio: 4 / 5" role="region" aria-label="Prints" tabindex="0">  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Cairn</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Dunes</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Headland</span>$64    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Pines</span>$48    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Steps</span>$40    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Jetty</span>$56    </figcaption>  </figure>  <figure class="carousel-slide figure">    <img class="figure-media" src="/demo/tide.svg" alt="The sea at slack tide">    <figcaption class="figure-caption">      <span class="block fg-text-strong">Slack tide</span>$48    </figcaption>  </figure></div>
<template>  <div class="carousel (scroll-snap-type:x|proximity)" style="--carousel-slide: 8.5rem; --carousel-gap: .75rem; --figure-ratio: 4 / 5" role="region" aria-label="Prints" tabindex="0">    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/cairn.svg" alt="Five stones balanced on a beach">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Cairn</span>$48      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/dunes.svg" alt="Dunes in the afternoon, their lee sides in shadow">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Dunes</span>$56      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/lighthouse.svg" alt="A lighthouse on a dark headland, its beam over the sea">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Headland</span>$64      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/pines.svg" alt="Rows of pines fading into fog">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Pines</span>$48      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/steps.svg" alt="Concrete steps half in shadow">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Steps</span>$40      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/harbor.svg" alt="Jetty posts standing in the water">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Jetty</span>$56      </figcaption>    </figure>    <figure class="carousel-slide figure">      <img class="figure-media" src="/demo/tide.svg" alt="The sea at slack tide">      <figcaption class="figure-caption">        <span class="block fg-text-strong">Slack tide</span>$48      </figcaption>    </figure>  </div></template>

#幅に応じて表示枚数を調整

表示枚数は、ウィンドウの幅ではなくカルーセルに割り当てられた幅に応じて変えられます。親要素をクエリーコンテナーにし、幅の区分ごとに、トラックの幅から隙間を引いて等分した値を--carousel-slideに指定します。carousel-2とcarousel-3も同じ計算で幅を指定します。プレビューを狭めると、表示枚数が3枚から2枚、さらに1枚に変わります。

幅に応じて表示枚数を調整

656 px
<div class="container-inline">  <div class="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions near the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Delta</h3>        <p class="card-description">Previews for every branch.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Ember</h3>        <p class="card-description">Logs that keep a week.</p>      </header>    </article>  </div></div>
<div className="container-inline">  <div className="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabIndex={0}>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Atlas</h3>        <p className="card-description">A map of every deployment.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Borealis</h3>        <p className="card-description">Edge functions near the reader.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Cirrus</h3>        <p className="card-description">Storage that follows the project.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Delta</h3>        <p className="card-description">Previews for every branch.</p>      </header>    </article>    <article className="carousel-slide card card-flat">      <header className="card-header">        <h3 className="card-title">Ember</h3>        <p className="card-description">Logs that keep a week.</p>      </header>    </article>  </div></div>
<div class="container-inline">  <div class="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions near the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Delta</h3>        <p class="card-description">Previews for every branch.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Ember</h3>        <p class="card-description">Logs that keep a week.</p>      </header>    </article>  </div></div>
<template>  <div class="container-inline">    <div class="carousel (--carousel-slide:calc((100%-1rem)/2))@container(--sm) (--carousel-slide:calc((100%-2rem)/3))@container(--xl)" role="region" aria-label="Projects" tabindex="0">      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Atlas</h3>          <p class="card-description">A map of every deployment.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Borealis</h3>          <p class="card-description">Edge functions near the reader.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Cirrus</h3>          <p class="card-description">Storage that follows the project.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Delta</h3>          <p class="card-description">Previews for every branch.</p>        </header>      </article>      <article class="carousel-slide card card-flat">        <header class="card-header">          <h3 class="card-title">Ember</h3>          <p class="card-description">Logs that keep a week.</p>        </header>      </article>    </div>  </div></template>

#縦方向

carousel-verticalは、スライド列を高さ--carousel-viewport(既定は20rem)の縦の列に変え、上下方向にスクロールとスナップを行います。各スライドの高さは--carousel-slideで指定します。ボタンは上端と下端の中央に置かれ、ドットは終端側の辺に沿って縦に並びます。

縦方向

<div class="carousel carousel-vertical w-full max-w(360)" style="--carousel-viewport: 15rem; --carousel-slide: 50%" role="region" aria-label="This week" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Monday</h3>      <p class="card-description">Planning, then the design review.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Tuesday</h3>      <p class="card-description">Pairing on the schedule view.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Wednesday</h3>      <p class="card-description">Release notes and the changelog.</p>    </header>  </article></div>
<div className="carousel carousel-vertical w-full max-w(360)" style={{ '--carousel-viewport': '15rem', '--carousel-slide': '50%' } as React.CSSProperties} role="region" aria-label="This week" tabIndex={0}>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Monday</h3>      <p className="card-description">Planning, then the design review.</p>    </header>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Tuesday</h3>      <p className="card-description">Pairing on the schedule view.</p>    </header>  </article>  <article className="carousel-slide card card-flat">    <header className="card-header">      <h3 className="card-title">Wednesday</h3>      <p className="card-description">Release notes and the changelog.</p>    </header>  </article></div>
<div class="carousel carousel-vertical w-full max-w(360)" style="--carousel-viewport: 15rem; --carousel-slide: 50%" role="region" aria-label="This week" tabindex="0">  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Monday</h3>      <p class="card-description">Planning, then the design review.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Tuesday</h3>      <p class="card-description">Pairing on the schedule view.</p>    </header>  </article>  <article class="carousel-slide card card-flat">    <header class="card-header">      <h3 class="card-title">Wednesday</h3>      <p class="card-description">Release notes and the changelog.</p>    </header>  </article></div>
<template>  <div class="carousel carousel-vertical w-full max-w(360)" style="--carousel-viewport: 15rem; --carousel-slide: 50%" role="region" aria-label="This week" tabindex="0">    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Monday</h3>        <p class="card-description">Planning, then the design review.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Tuesday</h3>        <p class="card-description">Pairing on the schedule view.</p>      </header>    </article>    <article class="carousel-slide card card-flat">      <header class="card-header">        <h3 class="card-title">Wednesday</h3>        <p class="card-description">Release notes and the changelog.</p>      </header>    </article>  </div></template>

#表示中のスライドの取得と設定

どのスライドが表示中かを知る必要があるページや、特定のスライドを表示したいページでは、トラックをcarousel-viewの中に入れ、独自の前後ボタンとドットの列を置きます。ビューの中では生成されるコントロールが表示されなくなるため、ボタンは1組だけになります。これらはcarouselビヘイビアが制御します。

  • IntersectionObserver で各スライドの表示割合を追跡します。scrollend でスクロールが止まると、最も多く表示されているスライドが現在の項目になります。このイベントに対応していないブラウザーでは、スクロールが少し途切れた後に判定します。同率の場合は、その中に現在のスライドがあれば選択を保ち、なければ文書順で最初のスライドを選びます。複数のスライドが同時に収まる場合、ブラウザーのスナップ先が別のスライドになることがあります。
  • 前後のボタンとドットは、スライドをそれぞれのスナップ位置までスクロールして表示するようプラットフォームに要求します。動きを抑える設定では即座に移動します。ビヘイビアがスクロール位置を直接設定することはありません。
  • 現在のスライドのドットにはaria-current="true"が付き、先頭と末尾では前後のボタンが無効になります。複数のスライドが表示されていても、現在のスライドは1つだけで、ほかのスライドが隠されることはありません。
  • スライドはdata-value、なければidで識別されるため、前にスライドが追加されても現在のスライドは変わりません。読み手の操作による変更ではinputとchangeが発行されます。ビヘイビアが返す関数には読み書きできるvalueがあり、値を設定してもイベントは発行されません。

表示中のスライドの取得と設定

<div class="carousel-view w-full">  <div class="carousel" role="region" aria-label="Releases" tabindex="0">    <article class="carousel-slide card card-flat" id="release-atlas" data-value="atlas">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment, live.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-borealis" data-value="borealis">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions close to the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>  </div>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform="rotate(180 8 8)" />    </svg>  </button>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" />    </svg>  </button>  <div class="carousel-dots">    <button type="button" class="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>  </div></div><script type="module">  import { attach } from '@master/ui/behaviors'  attach()</script>
import { useEffect, useRef } from 'react'import { attach } from '@master/ui/behaviors'import ChevronLeftIcon from '@master/icons-react/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-react/native/16/soft/chevron-right'export function Example() {  const root = useRef<HTMLDivElement>(null)  useEffect(() => attach(root.current!), [])  return (    <div ref={root} className="carousel-view w-full">      <div className="carousel" role="region" aria-label="Releases" tabIndex={0}>        <article className="carousel-slide card card-flat" id="release-atlas" data-value="atlas">          <header className="card-header">            <h3 className="card-title">Atlas</h3>            <p className="card-description">A map of every deployment, live.</p>          </header>        </article>        <article className="carousel-slide card card-flat" id="release-borealis" data-value="borealis">          <header className="card-header">            <h3 className="card-title">Borealis</h3>            <p className="card-description">Edge functions close to the reader.</p>          </header>        </article>        <article className="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">          <header className="card-header">            <h3 className="card-title">Cirrus</h3>            <p className="card-description">Storage that follows the project.</p>          </header>        </article>      </div>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">        <ChevronLeftIcon className="icon" />      </button>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">        <ChevronRightIcon className="icon" />      </button>      <div className="carousel-dots">        <button type="button" className="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>        <button type="button" className="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>        <button type="button" className="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>      </div>    </div>  )}
<script>  import { attach } from '@master/ui/behaviors'  import ChevronLeftIcon from '@master/icons-svelte/native/16/soft/chevron-left'  import ChevronRightIcon from '@master/icons-svelte/native/16/soft/chevron-right'</script><div class="carousel-view w-full" {@attach attach}>  <div class="carousel" role="region" aria-label="Releases" tabindex="0">    <article class="carousel-slide card card-flat" id="release-atlas" data-value="atlas">      <header class="card-header">        <h3 class="card-title">Atlas</h3>        <p class="card-description">A map of every deployment, live.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-borealis" data-value="borealis">      <header class="card-header">        <h3 class="card-title">Borealis</h3>        <p class="card-description">Edge functions close to the reader.</p>      </header>    </article>    <article class="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">      <header class="card-header">        <h3 class="card-title">Cirrus</h3>        <p class="card-description">Storage that follows the project.</p>      </header>    </article>  </div>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">    <ChevronLeftIcon class="icon" />  </button>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">    <ChevronRightIcon class="icon" />  </button>  <div class="carousel-dots">    <button type="button" class="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>    <button type="button" class="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>  </div></div>
<script setup>import { onMounted, onUnmounted, ref } from 'vue'import { attach } from '@master/ui/behaviors'import ChevronLeftIcon from '@master/icons-vue/native/16/soft/chevron-left'import ChevronRightIcon from '@master/icons-vue/native/16/soft/chevron-right'const root = ref(null)let detachonMounted(() => { detach = attach(root.value) })onUnmounted(() => detach?.())</script><template>  <div ref="root" class="carousel-view w-full">    <div class="carousel" role="region" aria-label="Releases" tabindex="0">      <article class="carousel-slide card card-flat" id="release-atlas" data-value="atlas">        <header class="card-header">          <h3 class="card-title">Atlas</h3>          <p class="card-description">A map of every deployment, live.</p>        </header>      </article>      <article class="carousel-slide card card-flat" id="release-borealis" data-value="borealis">        <header class="card-header">          <h3 class="card-title">Borealis</h3>          <p class="card-description">Edge functions close to the reader.</p>        </header>      </article>      <article class="carousel-slide card card-flat" id="release-cirrus" data-value="cirrus">        <header class="card-header">          <h3 class="card-title">Cirrus</h3>          <p class="card-description">Storage that follows the project.</p>        </header>      </article>    </div>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label="Previous slide">      <ChevronLeftIcon class="icon" />    </button>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label="Next slide">      <ChevronRightIcon class="icon" />    </button>    <div class="carousel-dots">      <button type="button" class="carousel-dot" aria-controls="release-atlas" aria-label="Slide 1 of 3"></button>      <button type="button" class="carousel-dot" aria-controls="release-borealis" aria-label="Slide 2 of 3"></button>      <button type="button" class="carousel-dot" aria-controls="release-cirrus" aria-label="Slide 3 of 3"></button>    </div>  </div></template>
app.js
import { carousel } from '@master/ui/behaviors/carousel.js'const releases = carousel(document.querySelector('.carousel-view'), { value: 'borealis' })releases.value // 'borealis'

#アクセシビリティ

  • 矢印キーでスクロールできるように、トラックにrole="region"、ラベル、tabindex="0"を指定します。
  • スタイルシートは生成したボタンに「Previous slide」(前のスライド)と「Next slide」(次のスライド)という名前を付けます。ページの言語で名前を付けるには、--carousel-previous-labelと--carousel-next-labelに引用符で囲んだ文字列を指定します。カルーセル自体か、ルートなどの祖先要素に設定します。各ドットには、対応するスライドのコンテンツの名前が付きます。
  • カルーセルは自動で切り替わりません。タイマーでコンテンツを切り替える場合は、スクリプトで一時停止用のコントロールを実装する必要があります。
  • 動きを抑える設定では、スライド間を滑らかにスクロールせず、次の位置に即座に移動します。
  • 強制カラーモードでは、ボタンの輪郭はシステムのボタン色で描画されます。ドットもシステム色を使い、表示中のスライドのドットにはハイライト色を使います。
  • carousel-viewでは、ボタンとドットにページの言語で名前を付けます。ドットの名前では、全体の何枚目のスライドに移動するかを示します。

#React、Svelte、Vue

carouselコンポーネントは、項目からスライドを描画し、その横にビューのボタンとドットを置きます。各項目には、項目が変わっても同じままのvalueを指定します。コンポーネントの値は現在のスライドです。npx master-ui add carouselでプロジェクトにコピーできます。構成と使い方はReact、Svelte、Vueを参照してください。表示中のスライドを読み取る必要がないカルーセルなら、コンポーネントは不要で、上記のマークアップだけで十分です。

ui/carousel.tsx
'use client'import { useId, useState, type ComponentProps, type ReactNode } from 'react'import { carousel, type CarouselOptions } from '@master/ui/behaviors/carousel.js'import { useBehavior } from './use-behavior'export interface CarouselItem {  /** What the slide stands for: the carousel's value while it is current. Keep it the same when items move. */  value: string}export interface CarouselProps<T extends CarouselItem> extends Omit<ComponentProps<'div'>, 'children' | 'defaultValue' | 'ref'> {  items: T[]  /** The current slide, as its item's value. The first slide by default. */  value?: string  defaultValue?: string  onValueChange?: (value: string) => void  /** What a slide shows. */  children: (item: T) => ReactNode  /** The accessible name of the track. */  label: string  /** A column that scrolls up and down. */  vertical?: boolean  /** Classes of the track, such as `carousel-2` or `carousel-center`. */  trackClassName?: string  /** Classes of each slide. */  slideClassName?: string  previousLabel?: string  nextLabel?: string  /** The accessible name of a dot. `Slide 2 of 5` by default. */  dotLabel?: (index: number, count: number) => string}const chevron = (turn?: number) => (  <svg className="icon" viewBox="0 0 16 16" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true"><path d="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform={turn ? `rotate(${turn} 8 8)` : undefined} /></svg>)/** * A carousel whose slide in view the application can read and set. React writes the slides from the items * and the controls beside them; the carousel behavior follows the scroll, marks the current dot and moves the * track through the platform when a control is pressed. */export function Carousel<T extends CarouselItem>({ items, value, defaultValue, onValueChange, children, label, vertical, trackClassName, slideClassName, previousLabel = 'Previous slide', nextLabel = 'Next slide', dotLabel = (index, count) => `Slide ${index + 1} of ${count}`, className, ...root }: CarouselProps<T>) {  const id = useId()  // The first value is drawn into the first render; after that the behavior marks what is current.  const [first] = useState(() => value ?? defaultValue ?? items[0]?.value)  // Drawn into the first render only; after that the behavior marks the buttons, and a render with other items  // does not draw the first state over the one it gave.  const [at] = useState(() => items.findIndex(item => item.value === first))  // The behavior's value is null only for a carousel with no slides, which never reports a change.  const ref = useBehavior<HTMLDivElement, CarouselOptions, string | null>(carousel, { value: first }, { value, defaultValue, onValueChange: onValueChange as ((value: string | null) => void) | undefined })  const track = ['carousel', vertical && 'carousel-vertical', trackClassName].filter(Boolean).join(' ')  return (    <div className={className ? `carousel-view ${className}` : 'carousel-view'} ref={ref} {...root}>      <div className={track} role="region" aria-label={label} tabIndex={0}>        {items.map(item => (          <div className={slideClassName ? `carousel-slide ${slideClassName}` : 'carousel-slide'} id={`${id}${item.value}`} data-value={item.value} key={item.value}>{children(item)}</div>        ))}      </div>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-previous" aria-label={previousLabel} disabled={at <= 0}>{chevron(vertical ? -90 : 180)}</button>      <button type="button" className="btn btn-square btn-sm btn-pill carousel-next" aria-label={nextLabel} disabled={at >= items.length - 1}>{chevron(vertical ? 90 : 0)}</button>      <div className="carousel-dots">        {items.map((item, index) => (          <button type="button" className="carousel-dot" aria-controls={`${id}${item.value}`} aria-label={dotLabel(index, items.length)} aria-current={item.value === first ? 'true' : undefined} key={item.value} />        ))}      </div>    </div>  )}
ui/Carousel.svelte
<script lang="ts" module>  import type { Snippet } from 'svelte'  import type { HTMLAttributes } from 'svelte/elements'  export interface CarouselItem {    /** What the slide stands for: the carousel's value while it is current. Keep it the same when items move. */    value: string  }  export interface CarouselProps<T extends CarouselItem> extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> {    items: T[]    /** The current slide, as its item's value. Bind it, or leave it out and the first slide is current. */    value?: string    onValueChange?: (value: string) => void    /** What a slide shows. */    children: Snippet<[T]>    /** The accessible name of the track. */    label: string    /** A column that scrolls up and down. */    vertical?: boolean    /** Classes of the track, such as `carousel-2` or `carousel-center`. */    trackClass?: string    /** Classes of each slide. */    slideClass?: string    previousLabel?: string    nextLabel?: string    /** The accessible name of a dot. `Slide 2 of 5` by default. */    dotLabel?: (index: number, count: number) => string  }</script><script lang="ts" generics="T extends CarouselItem">  import { untrack } from 'svelte'  import { carousel } from '@master/ui/behaviors/carousel.js'  import { behavior } from './behavior.svelte.js'  let { items, value = $bindable(), onValueChange, children, label, vertical, trackClass, slideClass, previousLabel = 'Previous slide', nextLabel = 'Next slide', dotLabel = (index, count) => `Slide ${index + 1} of ${count}`, class: className, ...root }: CarouselProps<T> = $props()  const id = $props.id()  // The first value is drawn into the first render; after that the behavior marks what is current.  const first = untrack(() => value ?? items[0]?.value)  // Drawn into the first render only; after that the behavior marks the buttons and the dots. The first state is  // spread from a constant, so the template's own updates never draw it over the state the behavior gave.  const atStart = { disabled: untrack(() => items.findIndex(item => item.value === first) <= 0) }  const atEnd = { disabled: untrack(() => items.findIndex(item => item.value === first) >= items.length - 1) }  // A dot written later, from new items, is drawn with the slide current then, which is not read as state.  const current = { value: first }  const attach = behavior(carousel, () => ({ value: first }), {    value: () => value,    // The behavior's value is null only for a carousel with no slides, which never reports a change.    change: (next) => { if (next === null) return; current.value = next; value = next; onValueChange?.(next) },    settled: (held) => { if (held !== null) current.value = held },  })</script><!--  @component  A carousel whose slide in view the application can read and set. Svelte writes the slides from the items  and the controls beside them; the carousel behavior follows the scroll, marks the current dot and moves the  track through the platform when a control is pressed.--><div class={['carousel-view', className]} {@attach attach} {...root}>  <!-- The track is a scroll container: in the tab order, so the arrow keys scroll it. -->  <!-- svelte-ignore a11y_no_noninteractive_tabindex -->  <div class={['carousel', vertical && 'carousel-vertical', trackClass]} role="region" aria-label={label} tabindex="0">    {#each items as item (item.value)}      <div class={['carousel-slide', slideClass]} id="{id}{item.value}" data-value={item.value}>{@render children(item)}</div>    {/each}  </div>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" aria-label={previousLabel} {...atStart}>    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform={vertical ? 'rotate(-90 8 8)' : 'rotate(180 8 8)'} /></svg>  </button>  <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" aria-label={nextLabel} {...atEnd}>    <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" transform={vertical ? 'rotate(90 8 8)' : undefined} /></svg>  </button>  <div class="carousel-dots">    {#each items as item, index (item.value)}      <button type="button" class="carousel-dot" aria-controls="{id}{item.value}" aria-label={dotLabel(index, items.length)} aria-current={item.value === current.value ? 'true' : undefined}></button>    {/each}  </div></div>
ui/Carousel.vue
<script lang="ts">export interface CarouselItem {  /** What the slide stands for: the carousel's value while it is current. Keep it the same when items move. */  value: string}</script><script setup lang="ts" generic="T extends CarouselItem">import { useId, useTemplateRef } from 'vue'import { carousel } from '@master/ui/behaviors/carousel.js'import { useBehavior } from './use-behavior'defineOptions({ inheritAttrs: false })const { items, label, vertical, trackClass, slideClass, previousLabel = 'Previous slide', nextLabel = 'Next slide', dotLabel = (index: number, count: number) => `Slide ${index + 1} of ${count}` } = defineProps<{  /** The slides. An item's `value` is what the slide stands for; keep it the same when items move. */  items: T[]  /** The accessible name of the track. */  label: string  /** A column that scrolls up and down. */  vertical?: boolean  /** Classes of the track, such as `carousel-2` or `carousel-center`. */  trackClass?: string  /** Classes of each slide. */  slideClass?: string  previousLabel?: string  nextLabel?: string  /** The accessible name of a dot. `Slide 2 of 5` by default. */  dotLabel?: (index: number, count: number) => string}>()/** The current slide, as its item's value: `v-model`. */const model = defineModel<string>()defineSlots<{  /** What a slide shows. */  default: (props: { item: T }) => unknown}>()/* * A carousel whose slide in view the application can read and set. Vue writes the slides from the items and * the controls beside them; the carousel behavior follows the scroll, marks the current dot and moves the * track through the platform when a control is pressed. */const id = useId()// The first value is drawn into the first render; after that the behavior marks what is current.const first = model.value ?? items[0]?.valueconst at = items.findIndex(item => item.value === first)const root = useTemplateRef<HTMLDivElement>('root')useBehavior(root, carousel, () => ({ value: first }), model)</script><template>  <div class="carousel-view" ref="root" v-bind="$attrs">    <div class="carousel" :class="[{ 'carousel-vertical': vertical }, trackClass]" role="region" :aria-label="label" tabindex="0">      <div v-for="item in items" :key="item.value" class="carousel-slide" :class="slideClass" :id="`${id}-${item.value}`" :data-value="item.value"><slot :item="item" /></div>    </div>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-previous" :aria-label="previousLabel" :disabled="at <= 0">      <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" :transform="vertical ? 'rotate(-90 8 8)' : 'rotate(180 8 8)'" /></svg>    </button>    <button type="button" class="btn btn-square btn-sm btn-pill carousel-next" :aria-label="nextLabel" :disabled="at >= items.length - 1">      <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="M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5" :transform="vertical ? 'rotate(90 8 8)' : undefined" /></svg>    </button>    <div class="carousel-dots">      <button v-for="(item, index) in items" :key="item.value" type="button" class="carousel-dot" :aria-controls="`${id}-${item.value}`" :aria-label="dotLabel(index, items.length)" :aria-current="item.value === first ? 'true' : undefined"></button>    </div>  </div></template>

#クラス

クラス説明
carouselスクロールしてスナップするトラック。矢印キーでスクロールできるよう、role="region"、ラベル、tabindex="0"を指定します。
carousel-slideスライドはトラックの先頭にスナップし、carousel-center では中央にスナップします。幅はトラックの幅を基準に --carousel-slide で指定します。
carousel-2スライドを2枚表示carousel と併用します
carousel-3スライドを3枚表示carousel と併用します
carousel-center表示中のスライドは中央に置かれ、両隣の端が左右に見えます。最初と最後のスライドも中央まで移動します。--carousel-slideで変更しない限り、各スライドはトラックの80%を占めます。carousel と併用します
carousel-vertical高さ --carousel-viewport の縦の列で、上下方向にスクロールとスナップを行います。各スライドの高さは --carousel-slide で指定した割合で、ボタンは上端と下端に、ドットは終端側の辺に沿って置かれますcarousel と併用します
carousel-viewページ独自の前後ボタンとドットとともにカルーセルを保持し、それらを carousel ビヘイビアが制御します。この中では、生成されるコントロールは表示されません
carousel-previous現在のスライドの前のスライドに移動します。トラックの開始側の端、縦方向のビューでは上端に置かれます。最初のスライドでは無効です
carousel-next現在のスライドの次のスライドに移動します。トラックの終了側の端、縦方向のビューでは下端に置かれます。最後のスライドでは無効です
carousel-dotsトラックの下に並ぶドットの行。縦方向のカルーセルでは、その横に並ぶ列
carousel-dotaria-controls で指定したスライドに移動します。アクセシブルな名前で、全体の何枚目のスライドかを伝えます

#カスタムプロパティ

プロパティ既定値説明
--carousel-gap1remスライド同士の間隔
--carousel-slide100%スライドの幅。carousel-center では80%。スライドの一部をのぞかせる場合は任意の幅を設定
--carousel-viewport20rem縦方向のカルーセルの高さ指定先:carousel-vertical
--carousel-control2remブラウザーが描画する前へ・次へボタンのサイズ
--carousel-previous-labelブラウザーが描画する「前へ」ボタンの名前を、引用符で囲んだ文字列で指定します。未設定時は 'Previous slide' です。カルーセルまたはその祖先要素に、ページの言語で設定します。
--carousel-next-labelブラウザーが描画する「次へ」ボタンの名前を、引用符で囲んだ文字列で指定します。未設定時は 'Next slide' です。

#ソース

@master/ui/components/carousel.css
/* * Carousel: a row of slides that scrolls and snaps, built from CSS alone. * The track is a scroll container with scroll snapping, so touch, trackpad, keys and assistive technology * all work as scrolling does. Where the browser supports them, ::scroll-button() adds previous and next * buttons and ::scroll-marker adds a dot per slide that follows the scroll position; both are real, * focusable controls the browser generates, named by --carousel-previous-label and --carousel-next-label, so a * page names them in its own language. Elsewhere the carousel is a swipeable, scrollable row with a thin scrollbar. * carousel-vertical turns the row into a column --carousel-viewport tall, with its buttons above and below. * * A carousel-view holds a track and controls of the page's own: previous and next buttons and a row of dots, * which the carousel behavior drives, so a page or a framework can read and set the slide in view. Inside a * view the generated controls step aside, so there is never a second set of buttons. */@layer components.ui {  .carousel {    --carousel-gap: 1rem;    --carousel-slide: 100%;    --carousel-control: 2rem;    /* A neutral mid-grey chevron reads on moonstone and obsidian alike. */    --carousel-chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' fill='none' stroke='%23777' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 3.5L10.3586 7.8586A0.2 0.2 0 0 1 10.3586 8.1414L6 12.5'/%3E%3C/svg%3E");    position: relative;    display: flex;    gap: var(--carousel-gap);    min-width: 0;    margin: 0;    padding: 0;    overflow: auto hidden;    overscroll-behavior-x: contain;    list-style: none;    scroll-snap-type: x mandatory;    scroll-behavior: smooth;    /*     * Without generated buttons, a thin scrollbar is the way across for a mouse that cannot swipe. Where the browser     * draws the buttons and the dots, or in a view with buttons of the page's own, the scrollbar steps aside.     */    scrollbar-width: thin;    scrollbar-color: var(--color-line-strong) transparent;    .carousel-view & {      scrollbar-width: none;    }    .carousel-view &::-webkit-scrollbar {      display: none;    }    &:focus-visible {      @apply --focus-ring;    }    @media (prefers-reduced-motion: reduce) {      scroll-behavior: auto;    }    /*     * Centered: the slide in view stands in the middle and its neighbors show an edge on either side. Room before     * the first slide and after the last lets both reach the middle too. That room is two empty flex items, not     * padding: a percentage in a slide is read from the track less its padding, so padding would shrink the slide     * it is measured from. A flex item's percentage is read from the whole track, as the slide's is.     */    &.carousel-center {      --carousel-slide: 80%;      &::before, &::after {        content: '';        flex: 0 0 max(0px, calc((100% - var(--carousel-slide)) / 2 - var(--carousel-gap)));      }      .carousel-slide {        scroll-snap-align: center;      }    }    /*     * Vertical: a column of slides --carousel-viewport tall that scrolls and snaps up and down. A slide takes     * --carousel-slide of the height; the buttons stand at the middle of the top and the bottom edges, and the     * dots in a column beside the end edge.     */    &.carousel-vertical {      --carousel-viewport: 20rem;      flex-direction: column;      height: var(--carousel-viewport);      overflow: hidden auto;      overscroll-behavior: auto contain;      scroll-snap-type: y mandatory;      .carousel-slide {        min-height: 0;      }    }    @supports selector(::scroll-marker) {      scrollbar-width: none;      &::-webkit-scrollbar {        display: none;      }      /* Room under the track for the markers, which sit on the carousel's own edge. */      margin-block-end: 1.75rem;      /* The controls anchor to their own carousel: the name is scoped, so several on a page do not share it. */      anchor-name: --carousel;      anchor-scope: --carousel;      scroll-marker-group: after;      /*       * The dots: a row centered under the track. Each dot is padded into a larger target, so the row has no gap of       * its own: the padding on either side of a dot is the space between them, and the dot stays .75rem under the track.       */      &::scroll-marker-group {        position: fixed;        position-anchor: --carousel;        top: calc(anchor(bottom) + .5625rem);        left: anchor(left);        right: anchor(right);        display: flex;        justify-content: center;        /* A finger's dots are padded to 24px: the row moves up by the extra padding, so each dot stays where it was. */        @media (pointer: coarse) {          top: calc(anchor(bottom) + .1875rem);        }      }      /* Previous and next: small raised squares on the track's edges, at its middle. */      &::scroll-button(inline-start), &::scroll-button(inline-end) {        position: fixed;        position-anchor: --carousel;        top: calc(anchor(center) - var(--carousel-control) / 2);        display: grid;        place-items: center;        width: var(--carousel-control);        height: var(--carousel-control);        padding: 0;        border: 0;        border-radius: 999px;        background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-floating);        box-shadow: var(--shadow-raised);        cursor: pointer;        touch-action: manipulation;        -webkit-tap-highlight-color: transparent;        transition: box-shadow var(--duration-quick) var(--easing-glide), opacity var(--duration-quick) var(--easing-glide), visibility var(--duration-quick);      }      &::scroll-button(inline-start) {        content: '' / var(--carousel-previous-label, 'Previous slide');        left: calc(anchor(left) + .5rem);        scale: -1 1;      }      &::scroll-button(inline-end) {        content: '' / var(--carousel-next-label, 'Next slide');        right: calc(anchor(right) + .5rem);      }      /* Where slides run from the right, previous is on the right and both chevrons turn round. */      &:dir(rtl)::scroll-button(inline-start) {        right: calc(anchor(right) + .5rem);        left: auto;        scale: none;      }      &:dir(rtl)::scroll-button(inline-end) {        right: auto;        left: calc(anchor(left) + .5rem);        scale: -1 1;      }      @media (hover: hover) {        &::scroll-button(inline-start):hover, &::scroll-button(inline-end):hover {          box-shadow: var(--shadow-raised-hover);        }      }      /* Pressed, the button sinks into the well, as every raised control does. */      &::scroll-button(inline-start):active:not(:disabled), &::scroll-button(inline-end):active:not(:disabled) {        background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-inset);        box-shadow: var(--shadow-pressed);      }      &::scroll-button(inline-start):focus-visible, &::scroll-button(inline-end):focus-visible {        outline: 2px solid var(--color-focus);        outline-offset: 2px;      }      /* A column: its buttons go up and down at the middle of its top and bottom edges, its dots down its end. */      &.carousel-vertical {        margin-block-end: 0;        margin-inline-end: 1.75rem;        &::scroll-marker-group {          top: anchor(top);          bottom: anchor(bottom);          left: calc(anchor(right) + .5625rem);          right: auto;          flex-direction: column;          justify-content: center;        }        &:dir(rtl)::scroll-marker-group {          left: auto;          right: calc(anchor(left) + .5625rem);        }        /* A finger's dots are padded to 24px: the column moves in by the extra padding, so each dot stays where it was. */        @media (pointer: coarse) {          &::scroll-marker-group {            left: calc(anchor(right) + .1875rem);          }          &:dir(rtl)::scroll-marker-group {            left: auto;            right: calc(anchor(left) + .1875rem);          }        }        &::scroll-button(block-start), &::scroll-button(block-end) {          position: fixed;          position-anchor: --carousel;          left: calc(anchor(center) - var(--carousel-control) / 2);          display: grid;          width: var(--carousel-control);          height: var(--carousel-control);          padding: 0;          border: 0;          border-radius: 999px;          background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-floating);          box-shadow: var(--shadow-raised);          cursor: pointer;          touch-action: manipulation;          -webkit-tap-highlight-color: transparent;          transition: box-shadow var(--duration-quick) var(--easing-glide), opacity var(--duration-quick) var(--easing-glide), visibility var(--duration-quick);        }        &::scroll-button(block-start) {          content: '' / var(--carousel-previous-label, 'Previous slide');          top: calc(anchor(top) + .5rem);          rotate: -90deg;        }        &::scroll-button(block-end) {          content: '' / var(--carousel-next-label, 'Next slide');          top: calc(anchor(bottom) - .5rem - var(--carousel-control));          rotate: 90deg;        }        @media (hover: hover) {          &::scroll-button(block-start):hover, &::scroll-button(block-end):hover {            box-shadow: var(--shadow-raised-hover);          }        }        &::scroll-button(block-start):active:not(:disabled), &::scroll-button(block-end):active:not(:disabled) {          background: var(--carousel-chevron) center / .875rem no-repeat, var(--color-surface-inset);          box-shadow: var(--shadow-pressed);        }        &::scroll-button(block-start):focus-visible, &::scroll-button(block-end):focus-visible {          outline: 2px solid var(--color-focus);          outline-offset: 2px;        }        &::scroll-button(block-start):disabled, &::scroll-button(block-end):disabled,        &::scroll-button(inline-start), &::scroll-button(inline-end) {          opacity: 0;          visibility: hidden;          cursor: default;        }      }      /* In a view the page's own controls drive the track: the generated ones step aside. */      .carousel-view & {        margin: 0;        scroll-marker-group: none;        &::scroll-button(*) {          content: none;        }      }      /*       * At either end there is nowhere to go: the button fades out and then leaves, so it no longer covers what the       * slide under it holds.       */      &::scroll-button(inline-start):disabled, &::scroll-button(inline-end):disabled {        opacity: 0;        visibility: hidden;        cursor: default;      }    }    /* Shadows vanish in forced colors: an outline draws the buttons' edge without changing their size. */    @media (forced-colors: active) {      &::scroll-button(inline-start), &::scroll-button(inline-end), &::scroll-button(block-start), &::scroll-button(block-end) {        outline: 1px solid ButtonText;        outline-offset: -1px;      }    }  }  /*   * A view: the track, with the page's previous and next buttons over its edges and its dots under it. The   * buttons stand in the track's own cell, so they keep to its middle at any height; at either end the behavior   * disables the one that has nowhere to go, and it fades out of the way.   */  .carousel-view {    position: relative;    /* Its buttons stand over the track inside the view, not over a sticky header that scrolls past it. */    isolation: isolate;    display: grid;    grid-template: 'track' minmax(0, auto) 'dots' auto / minmax(0, 1fr);    min-width: 0;    .carousel {      grid-area: track;    }  }  .carousel-previous, .carousel-next {    z-index: 1;    grid-area: track;    align-self: center;    margin-inline: .5rem;    transition-property: box-shadow, translate, color, opacity, visibility;    &:disabled {      opacity: 0;      visibility: hidden;    }  }  .carousel-previous {    justify-self: start;  }  .carousel-next {    justify-self: end;  }  /*   * Back and on point the way the slides run: where they run from the right, previous stands on the right and the   * chevrons turn round. An up or down chevron of a vertical view is the same either way.   */  .carousel-previous, .carousel-next {    &:dir(rtl) {      scale: -1 1;    }  }  /* A vertical view: the buttons at the middle of the top and the bottom, the dots in a column at the end. */  .carousel-view:has(.carousel-vertical) {    grid-template: 'track dots' auto / minmax(0, 1fr) auto;    .carousel-previous, .carousel-next {      justify-self: center;      margin: .5rem 0;    }    .carousel-previous {      align-self: start;    }    .carousel-next {      align-self: end;    }    .carousel-dots {      flex-direction: column;      padding: 0;      padding-inline-start: .5625rem;      @media (pointer: coarse) {        padding-inline-start: .1875rem;      }    }  }  /*   * The dots: one button for each slide, centered under the track, the one in view lengthened and in ink. Each   * dot stands .75rem from the track, as the browser's own do.   */  .carousel-dots {    grid-area: dots;    display: flex;    justify-content: center;    align-items: center;    padding-block-start: .5625rem;    /* A finger's dots carry .375rem more padding: the row gives it back, so the dots stay where they were. */    @media (pointer: coarse) {      padding-block-start: .1875rem;    }  }  .carousel-dot {    display: block;    box-sizing: content-box;    width: .375rem;    height: .375rem;    margin: 0;    padding: .1875rem;    border: 0;    border-radius: 999px;    corner-shape: round;    background: var(--color-line-mark) content-box;    appearance: none;    cursor: pointer;    touch-action: manipulation;    -webkit-tap-highlight-color: transparent;    transition: width var(--duration-base) var(--easing-glide), height var(--duration-base) var(--easing-glide), background-color var(--duration-quick) var(--easing-glide);    /* Packed edge to edge, a dot is its own target: on touch it is padded to 24px, the floor, and the dot stays 6px. */    @media (pointer: coarse) {      padding: .5625rem;    }    @media (hover: hover) {      &:hover {        background-color: var(--color-text-muted);      }    }    &[aria-current="true"] {      width: 1rem;      background-color: var(--color-text-strong);      cursor: default;      .carousel-view:has(.carousel-vertical) & {        width: .375rem;        height: 1rem;      }    }    &:focus-visible {      outline: 2px solid var(--color-focus);      outline-offset: 0;    }    @media (prefers-reduced-motion: reduce) {      transition-property: background-color;    }    /* The dots keep system colors of their own, so their hover and their focus ring are system colors too. */    @media (forced-colors: active) {      forced-color-adjust: none;      background-color: GrayText;      @media (hover: hover) {        &:hover {          background-color: CanvasText;        }      }      &[aria-current="true"] {        background-color: Highlight;      }      &:focus-visible {        outline-color: Highlight;      }    }  }  /* A slide: it snaps to the start of the track and takes --carousel-slide of its width. */  .carousel-slide {    flex: 0 0 var(--carousel-slide);    min-width: 0;    scroll-snap-align: start;    @supports selector(::scroll-marker) {      /*       * A 6px dot painted in the middle of its padding, which is the target and the space beside it. The padding is       * even on every side: the round corner less the padding is what rounds the dot, so it stays a dot and a pill.       */      &::scroll-marker {        content: '';        box-sizing: content-box;        width: .375rem;        height: .375rem;        padding: .1875rem;        border-radius: 999px;        corner-shape: round;        background: var(--color-line-mark) content-box;        cursor: pointer;        -webkit-tap-highlight-color: transparent;        transition: width var(--duration-base) var(--easing-glide), height var(--duration-base) var(--easing-glide), background-color var(--duration-quick) var(--easing-glide);        /* On a touch screen the target grows to 24px, the floor for a target that cannot reach past itself; the dot does not. */        @media (pointer: coarse) {          padding: .5625rem;        }        @media (prefers-reduced-motion: reduce) {          transition-property: background-color;        }      }      @media (hover: hover) {        &::scroll-marker:hover {          background-color: var(--color-text-muted);        }      }      /* The slide in view: the dot lengthens and takes ink. */      &::scroll-marker:target-current {        width: 1rem;        background-color: var(--color-text-strong);        cursor: default;      }      /* In a column of dots the one in view lengthens down the column, as a view's own dot does. */      .carousel-vertical &::scroll-marker:target-current {        width: .375rem;        height: 1rem;      }      &::scroll-marker:focus-visible {        outline: 2px solid var(--color-focus);        outline-offset: 0;      }      /* The dots keep their system colors, and the one in view its highlight, instead of the forced canvas. */      @media (forced-colors: active) {        &::scroll-marker {          forced-color-adjust: none;          background-color: GrayText;        }        @media (hover: hover) {          &::scroll-marker:hover {            background-color: CanvasText;          }        }        &::scroll-marker:target-current {          background-color: Highlight;        }        &::scroll-marker:focus-visible {          outline-color: Highlight;        }      }    }  }  /* Several slides in view: each takes a share of the track, less the gaps between them. */  .carousel-2 {    --carousel-slide: calc((100% - var(--carousel-gap)) / 2);  }  .carousel-3 {    --carousel-slide: calc((100% - var(--carousel-gap) * 2) / 3);  }}