横スクロールの実績カルーセル|スクロールスナップ+層視差(React・CSS)
React+Tailwind v4・ライブラリなし・画像素材なし。ブラウザ標準のスクロールスナップで横に送る実績カルーセルです。各カードは「現場の絵(背景)」「橙の形(中景)」「納品した画面(前景)」の3層のSVGで、案件ごとに色面と構図を変えています。スクロール位置から求めたCSS変数 --p を3層が別々の係数(−40px・−16px・+14px)で読み、送ると奥と手前が違う速さで動きます。前後ボタン・位置の点は360msのease-outで送り、番号は押した時点で行き先に合わせます。自動送りは4.6秒ごとで、再生ボタンの輪が次の送りまでの残りを示し、「自動送り中・一時停止中・停止中」を文字でも出します。スライドにフォーカスが入ると停止して自動では再開せず、端では巻き戻さずに向きを反転します。動きを減らす設定では視差と自動送りを止め、送りは即時に切り替えます。プレビュー下の「見比べる」で通常・視差なし・自動送りなしを切り替え、それぞれの props とコードの該当箇所を確認できます。HTML欄は JavaScript なしの最小版です。事例は見本用の架空データです。
プレビュー
BLK-32 — Works / Carousel
現場で決まった、その日の一枚。
6件の実績を、納品した画面と現場の絵で。横に送ると、奥と手前が違う速さで動きます。
表示中の事例: 01/06
自動送り中
見比べる
<MomentsGalleryCarousel />measure() が各スライドに --p を書き、背景の絵(×−40px)・橙の形(×−16px)・納品画面(×+14px)が別々に動く。送りは 360ms。
該当コードはコード欄の measure()・goTo()・自動送りの useEffect。HTML欄は JavaScript なしの最小版(スクロールスナップだけ・視差と自動送りなし)。
掲載の事例・業種・日付・画面は見本用の架空データです。
出典: 自動送りに停止の操作を置く根拠 — W3C「Understanding SC 2.2.2: Pause, Stop, Hide」(WCAG 2.2・ページの最終更新 2026-10-04)。フォーカスが入ったら止めて自動では再開しない・再生中は現在位置を読み上げない — W3C WAI-ARIA APG「Carousel Pattern」。いずれも 2026-11-08 確認。
説明の全文を読む
React+Tailwind v4・ライブラリなし・画像素材なし。ブラウザ標準のスクロールスナップで横に送る実績カルーセルです。各カードは「現場の絵(背景)」「橙の形(中景)」「納品した画面(前景)」の3層のSVGで、案件ごとに色面と構図を変えています。スクロール位置から求めたCSS変数 --p を3層が別々の係数(−40px・−16px・+14px)で読み、送ると奥と手前が違う速さで動きます。前後ボタン・位置の点は360msのease-outで送り、番号は押した時点で行き先に合わせます。自動送りは4.6秒ごとで、再生ボタンの輪が次の送りまでの残りを示し、「自動送り中・一時停止中・停止中」を文字でも出します。スライドにフォーカスが入ると停止して自動では再開せず、端では巻き戻さずに向きを反転します。動きを減らす設定では視差と自動送りを止め、送りは即時に切り替えます。プレビュー下の「見比べる」で通常・視差なし・自動送りなしを切り替え、それぞれの props とコードの該当箇所を確認できます。HTML欄は JavaScript なしの最小版です。事例は見本用の架空データです。
- 追加日:
- 2026-08-20
- 更新日:
- 2026-11-08
- 依存:
- なし
- tags
- #block #works #case-study #carousel #horizontal-scroll #scroll-snap #parallax #autoplay #pause-button #container-query #svg-illustration #stretched-link #no-image #no-dependency #react #tailwind #html-css
"use client";
import { useCallback, useEffect, useRef, useState, type ReactNode } from "react";
/**
* 横スクロールの実績カルーセル(BLK-32)
*
* 使い方
* <MomentsGalleryCarousel /> 通常(層視差あり・自動送りあり)
* <MomentsGalleryCarousel parallax={false} /> 視差なし(3層が一体で動く)
* <MomentsGalleryCarousel autoplay={false} /> 自動送りなし(再生ボタンも出さない)
* showGuide={false} で、下の見比べ切り替えと注記を外す(転用するときはこちら)
*
* 汎用技法メモ
*
* 1. 進行はライブラリではなくスクロール位置そのものに持たせる
* `overflow-x-auto` + `snap-x snap-mandatory` にすると、ドラッグ・スワイプ・ホイール・
* キーボードのフォーカス送りはブラウザが持つ。JS が担うのは「今どこにいるか読む」ことと
* ボタンで送ることの2つだけ。
*
* 2. ボタンの送りは 360ms の自前の補間にする
* scroll-behavior: smooth の速さはブラウザ任せで、短い・長いが環境で変わり、視差の途中が
* 読めないことがある。送りの間だけ scroll-snap-type を外して scrollLeft を rAF で
* ease-out に動かし、着いたら戻す。動きを減らす設定では補間せずに即時に切り替える。
* 番号は押した時点で行き先に合わせる(移動の途中で数字が揺れない)。
*
* 3. 層の視差は「1フレーム1回、CSS変数を1つ書く」だけで作る
* 各スライドの中心とトラックの中心の差をトラック幅で正規化した値 --p(≒ -1〜1)を書き、
* 背景の絵・橙の形・納品画面の3層が `calc(var(--p,0) * Npx)` を別々の係数で読む。
* 背景は逆向きに大きく(遠い)、納品画面は順向きに小さく(近い)動かす。
* React の state は経由しない。--p を書かなければ var の既定値 0 に畳まれて視差は消える。
*
* 4. 自動送りは巻き戻さず、端で向きを反転する
* 複数枚が同時に見える幅では、最後のスライドは左端まで来られない。届く最後の位置
* (lastReachable)を測っておき、それより先は「末尾に着いた」として番号を最後に合わせる。
*
* 5. 自動送りの止め方(W3C WAI-ARIA APG の Carousel Pattern に沿う)
* 停止ボタン(再生中の状態を輪の進み具合で見せる)/ ポインタが上にある間の一時停止 /
* スライドにフォーカスが入ったら停止して自動では再開しない / 画面外 / タブ非表示 /
* 動きを減らす設定。再生中は現在位置の aria-live を off にし、止まっている間だけ読ませる。
*/
type Moment = {
no: string;
kind: string;
industry: string;
title: string;
note: string;
scene: string;
date: string;
/** プレートの地の色(背景の絵の下に敷く) */
field: string;
/** 橙の形(中景)の位置と形 */
accent: string;
/** 背景の絵(最奥・幅を広げて描く viewBox 0 0 440 300) */
back: ReactNode;
/** 納品した画面(最前・viewBox 0 0 400 300) */
front: ReactNode;
};
/* ---------- 図版の部品(すべて aria-hidden の装飾。文字は画面の中の短いラベルだけ) ---------- */
const AMBER = "#d97706";
const INK = "#111827";
function Phone({ x, y, children }: { x: number; y: number; children: ReactNode }) {
// 幅 112・高さ 236 のスマホ枠。中身は (0,0)〜(100,212) の座標で描く
return (
<g transform={`translate(${x} ${y})`}>
<rect width="112" height="236" rx="18" fill={INK} />
<rect x="6" y="12" width="100" height="212" rx="6" fill="#fff" />
<rect x="44" y="4" width="24" height="4" rx="2" fill="#374151" />
<g transform="translate(6 12)">{children}</g>
</g>
);
}
function Browser({ x, y, w, h, children }: { x: number; y: number; w: number; h: number; children: ReactNode }) {
// ブラウザ枠。中身は (0,0)〜(w, h-18) の座標で描く
return (
<g transform={`translate(${x} ${y})`}>
<rect width={w} height={h} rx="8" fill="#fff" stroke="rgba(17,24,39,0.18)" />
<path d={`M0 8a8 8 0 0 1 8-8h${w - 16}a8 8 0 0 1 8 8v10H0z`} fill="#f3f4f6" />
<circle cx="10" cy="9" r="2.5" fill="#d1d5db" />
<circle cx="18" cy="9" r="2.5" fill="#d1d5db" />
<circle cx="26" cy="9" r="2.5" fill="#d1d5db" />
<rect x={w * 0.3} y="5" width={w * 0.4} height="8" rx="4" fill="#fff" />
<g transform="translate(0 18)">{children}</g>
</g>
);
}
function Lines({ x, y, w, n, gap = 9, color = "#e5e7eb" }: { x: number; y: number; w: number; n: number; gap?: number; color?: string }) {
return (
<g fill={color}>
{Array.from({ length: n }, (_, i) => (
<rect key={i} x={x} y={y + i * gap} width={i === n - 1 ? w * 0.6 : w} height="4" rx="2" />
))}
</g>
);
}
const label = { fontFamily: "inherit", fontWeight: 700 } as const;
/* ---------- 6件の事例(見本用の架空データ) ---------- */
const moments: Moment[] = [
{
no: "01",
kind: "SITE",
industry: "建築金物",
title: "見積依頼を、写真1枚から始められるように",
note: "「品番が分からない」で止まっていた問い合わせを、写真を添えて送るだけの1ステップに置き換えました。",
scene: "公開の翌朝、最初の1件は現場から届いた。",
date: "2026.05",
field: "bg-[#1f2a37]",
accent: "top-[10%] left-[44%] size-[34%] rounded-full bg-amber-500",
back: (
<g>
<g stroke="rgba(255,255,255,0.07)">
{Array.from({ length: 23 }, (_, i) => (
<line key={`v${i}`} x1={i * 20} y1="0" x2={i * 20} y2="300" />
))}
{Array.from({ length: 16 }, (_, i) => (
<line key={`h${i}`} x1="0" y1={i * 20} x2="440" y2={i * 20} />
))}
</g>
<g fill="none" stroke="rgba(255,255,255,0.32)" strokeWidth="3">
<path d="M40 60h110v26H66v130H40z" />
<path d="M60 236h150v-22H86V128H60z" opacity="0.6" />
<circle cx="53" cy="74" r="6" />
<circle cx="53" cy="200" r="6" />
<circle cx="128" cy="73" r="6" />
</g>
<g stroke="rgba(251,191,36,0.55)" strokeWidth="2">
<line x1="60" y1="262" x2="210" y2="262" />
<line x1="60" y1="256" x2="60" y2="268" />
<line x1="210" y1="256" x2="210" y2="268" />
</g>
<text x="122" y="284" fill="rgba(251,191,36,0.8)" fontSize="11" style={label}>
150
</text>
</g>
),
front: (
<Phone x={238} y={40}>
<rect width="100" height="22" fill="#f9fafb" />
<rect x="8" y="8" width="38" height="6" rx="3" fill={INK} />
<text x="8" y="40" fontSize="9" fill={INK} style={label}>
写真で見積依頼
</text>
<rect x="8" y="48" width="84" height="70" rx="4" fill="#e5e7eb" />
<path d="M30 70h30v10H40v26H30z" fill="none" stroke="#6b7280" strokeWidth="3" />
<circle cx="74" cy="104" r="9" fill="#fff" stroke="#9ca3af" strokeWidth="2" />
<Lines x={8} y={128} w={84} n={2} />
<rect x="8" y="160" width="84" height="24" rx="12" fill={AMBER} />
<text x="50" y="176" fontSize="9" fill="#fff" textAnchor="middle" style={label}>
写真を添えて送る
</text>
</Phone>
),
},
{
no: "02",
kind: "LP",
industry: "歯科",
title: "はじめての人にだけ、道順を長く見せる",
note: "初診と再診で入口を分け、迷いやすい人にだけ地図と持ち物の案内を厚く出す構成にしました。",
scene: "撮影日、待合室の光がいちばん柔らかい時間を待った。",
date: "2026.03",
field: "bg-[#dcefe9]",
accent: "bottom-[8%] left-[6%] h-[40%] w-[52%] rounded-[10px] bg-amber-400/80",
back: (
<g>
<g fill="#c4e2d9">
<rect x="0" y="0" width="120" height="90" rx="6" />
<rect x="150" y="0" width="140" height="90" rx="6" />
<rect x="320" y="0" width="120" height="130" rx="6" />
<rect x="0" y="120" width="120" height="180" rx="6" />
<rect x="150" y="120" width="140" height="70" rx="6" />
<rect x="150" y="220" width="140" height="80" rx="6" />
<rect x="320" y="160" width="120" height="140" rx="6" />
</g>
<path
d="M135 300V105h170v-60"
fill="none"
stroke="#0f766e"
strokeWidth="4"
strokeDasharray="2 9"
strokeLinecap="round"
/>
<g transform="translate(305 34)">
<path d="M0 0c-11 0-18 8-18 17 0 13 18 29 18 29s18-16 18-29C18 8 11 0 0 0z" fill="#0f766e" />
<circle cx="0" cy="17" r="6" fill="#dcefe9" />
</g>
</g>
),
front: (
<Browser x={34} y={62} w={226} h={176}>
<rect x="14" y="12" width="70" height="6" rx="3" fill={INK} />
<rect x="14" y="30" width="94" height="58" rx="6" fill={AMBER} />
<text x="61" y="64" fontSize="13" fill="#fff" textAnchor="middle" style={label}>
初診の方
</text>
<rect x="118" y="30" width="94" height="58" rx="6" fill="#fff" stroke="#d1d5db" strokeWidth="1.5" />
<text x="165" y="64" fontSize="13" fill={INK} textAnchor="middle" style={label}>
再診の方
</text>
<rect x="14" y="100" width="64" height="48" rx="4" fill="#dcefe9" />
<path d="M22 140l18-20 14 10 18-22" fill="none" stroke="#0f766e" strokeWidth="2.5" strokeDasharray="2 4" />
<Lines x={88} y={104} w={124} n={4} gap={11} />
</Browser>
),
},
{
no: "03",
kind: "SHOP",
industry: "焙煎所",
title: "焙煎の周期に、売り場のほうを合わせる",
note: "入荷日を入れておけば棚が自動で入れ替わる仕組みに。毎月の差し替え作業がなくなりました。",
scene: "試し焼きの香りの中で、豆の並び順を決めた。",
date: "2026.01",
field: "bg-[#3a2a20]",
accent: "top-[22%] -left-[6%] h-[16%] w-[70%] -rotate-6 bg-amber-500",
back: (
<g>
<path
d="M0 250C60 245 90 200 140 160S240 70 300 60s110 0 140 4"
fill="none"
stroke="rgba(251,191,36,0.45)"
strokeWidth="3"
/>
{[
[40, 50, 20],
[110, 90, -30],
[360, 200, 40],
[400, 110, -10],
[60, 190, 60],
[250, 30, 15],
[300, 250, -45],
[180, 260, 30],
].map(([x, y, r]) => (
<g key={`${x}-${y}`} transform={`translate(${x} ${y}) rotate(${r})`}>
<ellipse rx="22" ry="15" fill="#6b4630" />
<path d="M-18 2c10-6 26 2 36-4" fill="none" stroke="#2a1d15" strokeWidth="3" />
</g>
))}
</g>
),
front: (
<Browser x={86} y={96} w={252} h={190}>
<rect x="14" y="12" width="60" height="6" rx="3" fill={INK} />
<rect x="170" y="9" width="68" height="13" rx="6.5" fill="#fef3c7" />
<text x="204" y="19" fontSize="8.5" fill="#92400e" textAnchor="middle" style={label}>
入荷 11/04
</text>
{[0, 1, 2].map((i) => (
<g key={i} transform={`translate(${14 + i * 76} 32)`}>
<rect width="68" height="76" rx="4" fill={i === 0 ? "#fde68a" : "#f3f4f6"} />
<path d="M20 18h28l4 50H16z" fill={i === 0 ? "#92400e" : "#6b4630"} />
<rect x="24" y="34" width="20" height="12" rx="2" fill="#fff" opacity="0.85" />
<rect y="84" width="52" height="5" rx="2.5" fill="#e5e7eb" />
<rect y="94" width="30" height="5" rx="2.5" fill={i === 0 ? AMBER : "#d1d5db"} />
</g>
))}
</Browser>
),
},
{
no: "04",
kind: "FORM",
industry: "社会保険労務士",
title: "相談前の不安を、3つの質問に畳む",
note: "長い問い合わせフォームを3問に分け、送信の前に相談内容の要約を見せてから確定する形へ。",
scene: "初稿を見せた日、担当者が最初に触ったのは戻るボタンだった。",
date: "2025.11",
field: "bg-[#f1ebdf]",
accent: "-right-[8%] -bottom-[18%] size-[58%] rounded-full bg-amber-400/90",
back: (
<g>
{[
[30, 40, -8],
[70, 70, 4],
[300, 30, 10],
].map(([x, y, r], i) => (
<g key={i} transform={`translate(${x} ${y}) rotate(${r})`}>
<rect width="120" height="160" fill="#fff" stroke="rgba(17,24,39,0.12)" />
<Lines x={14} y={20} w={92} n={9} gap={14} color="#e7e1d4" />
</g>
))}
<circle cx="120" cy="220" r="22" fill="none" stroke="rgba(185,28,28,0.45)" strokeWidth="3" />
</g>
),
front: (
<Phone x={150} y={34}>
<g>
{[0, 1, 2].map((i) => (
<g key={i}>
<circle cx={22 + i * 28} cy="16" r="8" fill={i === 0 ? AMBER : "#e5e7eb"} />
<text x={22 + i * 28} y="19.5" fontSize="9" fill={i === 0 ? "#fff" : "#6b7280"} textAnchor="middle" style={label}>
{i + 1}
</text>
</g>
))}
<rect x="30" y="15" width="12" height="2" fill="#e5e7eb" />
<rect x="58" y="15" width="12" height="2" fill="#e5e7eb" />
</g>
<text x="8" y="46" fontSize="9" fill={INK} style={label}>
ご相談の内容は?
</text>
{["採用・入社", "給与・社保", "労務トラブル"].map((t, i) => (
<g key={t} transform={`translate(8 ${58 + i * 30})`}>
<rect width="84" height="24" rx="4" fill={i === 1 ? "#fffbeb" : "#fff"} stroke={i === 1 ? AMBER : "#d1d5db"} strokeWidth="1.5" />
<circle cx="12" cy="12" r="4.5" fill={i === 1 ? AMBER : "#fff"} stroke={i === 1 ? AMBER : "#9ca3af"} strokeWidth="1.5" />
<text x="22" y="15.5" fontSize="8.5" fill={INK}>
{t}
</text>
</g>
))}
<rect x="8" y="160" width="84" height="24" rx="12" fill={INK} />
<text x="50" y="176" fontSize="9" fill="#fff" textAnchor="middle" style={label}>
次へ(2/3)
</text>
</Phone>
),
},
{
no: "05",
kind: "SITE",
industry: "造園",
title: "四季を、更新作業ではなく設定にする",
note: "季節ごとの主役写真を日付で切り替え、現場が触る画面を1つだけに絞りました。",
scene: "剪定の音が止んだ夕方に、庭の写真の順番が決まった。",
date: "2025.09",
field: "bg-[#2f4a37]",
accent: "top-[8%] left-[8%] size-[26%] rounded-full bg-amber-400",
back: (
<g>
<path d="M0 210C80 170 150 180 220 200s150 0 220-30v130H0z" fill="#3d5f45" />
<path d="M0 250c90-30 170-20 250 0s130 10 190-10v60H0z" fill="#26402e" />
{[
[60, 196, 30],
[120, 186, 22],
[330, 176, 34],
[395, 168, 24],
[250, 208, 18],
].map(([x, y, r]) => (
<g key={x}>
<rect x={x - 3} y={y} width="6" height={r} fill="#1c3022" />
<circle cx={x} cy={y - r * 0.4} r={r} fill="#4f7a57" />
</g>
))}
</g>
),
front: (
<Browser x={150} y={50} w={228} h={176}>
<rect x="0" y="0" width="228" height="96" fill="#d9e8d4" />
<path d="M0 96c40-26 80-30 120-14s80 6 108-6v20H0z" fill="#9cbf91" />
<circle cx="170" cy="56" r="20" fill="#e3a453" />
<rect x="166" y="70" width="8" height="20" fill="#6b4a2b" />
{["春", "夏", "秋", "冬"].map((s, i) => (
<g key={s} transform={`translate(${14 + i * 50} 108)`}>
<rect width="44" height="22" rx="11" fill={i === 2 ? AMBER : "#fff"} stroke={i === 2 ? AMBER : "#d1d5db"} strokeWidth="1.5" />
<text x="22" y="15" fontSize="10" fill={i === 2 ? "#fff" : INK} textAnchor="middle" style={label}>
{s}
</text>
</g>
))}
<Lines x={14} y={140} w={200} n={2} />
</Browser>
),
},
{
no: "06",
kind: "EC",
industry: "器・工芸",
title: "一点ものを、待たせずに見せ切る",
note: "再入荷を待つ人の受け口を商品ページの中に置き、次の窯出しの予定をその場で伝える形にしました。",
scene: "窯出しの日、割れた1枚も含めて記録に残した。",
date: "2025.07",
field: "bg-[#c69a72]",
accent: "top-[14%] right-[6%] h-[64%] w-[40%] rounded-t-full border-[10px] border-amber-600 border-b-0",
back: (
<g>
<g fill="#a87b55">
<rect x="0" y="120" width="440" height="8" />
<rect x="0" y="230" width="440" height="8" />
</g>
{[30, 110, 200, 290, 370].map((x, i) => (
<path
key={x}
d={`M${x} ${i % 2 ? 120 : 230}c0-30 14-44 30-44s30 14 30 44z`}
fill={i % 2 ? "#8a5e3c" : "#e8d6c0"}
transform={`translate(0 ${i % 2 ? 0 : 0})`}
/>
))}
{[70, 250, 330].map((x) => (
<path key={x} d={`M${x} 120h46l-8 -30h-30z`} fill="#6e4a30" />
))}
</g>
),
front: (
<Phone x={44} y={46}>
<rect width="100" height="92" fill="#f3ebe1" />
<path d="M22 70c0 14 12 18 28 18s28-4 28-18z" fill="#1e3a5f" />
<path d="M26 70c4 6 8 2 12 8s10-2 14 4 10-2 14 2" fill="none" stroke="#7aa1c9" strokeWidth="2" />
<text x="8" y="108" fontSize="9" fill={INK} style={label}>
藍釉の鉢 一点もの
</text>
<rect x="8" y="116" width="40" height="5" rx="2.5" fill="#e5e7eb" />
<rect x="8" y="132" width="84" height="22" rx="4" fill="#fffbeb" stroke={AMBER} strokeWidth="1" />
<text x="50" y="146" fontSize="8" fill="#92400e" textAnchor="middle" style={label}>
次の窯出し 11月下旬
</text>
<rect x="8" y="162" width="84" height="24" rx="12" fill={AMBER} />
<text x="50" y="178" fontSize="8.5" fill="#fff" textAnchor="middle" style={label}>
入荷を知らせる
</text>
</Phone>
),
},
];
const AUTOPLAY_MS = 4600;
const SLIDE_MS = 360;
const RING = 2 * Math.PI * 16; // 再生ボタンの進み具合の輪(r=16)の周長
type Props = {
headingTag?: "h1" | "h2" | "p";
parallax?: boolean;
autoplay?: boolean;
showGuide?: boolean;
};
const MODES = [
{
key: "full",
name: "通常",
flags: { parallax: true, autoplay: true },
code: "<MomentsGalleryCarousel />",
body: "measure() が各スライドに --p を書き、背景の絵(×−40px)・橙の形(×−16px)・納品画面(×+14px)が別々に動く。送りは 360ms。",
},
{
key: "flat",
name: "視差なし",
flags: { parallax: false, autoplay: true },
code: "<MomentsGalleryCarousel parallax={false} />",
body: "--p を書かない。var(--p,0) が 0 に畳まれ、3層は1枚の絵として一緒に動く。送りの 360ms と自動送りは残る。",
},
{
key: "manual",
name: "自動送りなし",
flags: { parallax: true, autoplay: false },
code: "<MomentsGalleryCarousel autoplay={false} />",
body: "自動送りの useEffect が最初の return で抜け、再生ボタンと状態の表示も出さない。視差と送りの動きは残る。",
},
] as const;
export default function MomentsGalleryCarousel({
headingTag = "h2",
parallax: parallaxProp = true,
autoplay: autoplayProp = true,
showGuide = true,
}: Props) {
const Heading = headingTag;
const rootRef = useRef<HTMLElement>(null);
const trackRef = useRef<HTMLUListElement>(null);
const ringRef = useRef<SVGCircleElement>(null);
const raf = useRef(0);
const tween = useRef(0);
const animating = useRef(false); // ボタンの送りの途中は、測った位置で番号を上書きしない
const reduced = useRef(false);
const parallaxRef = useRef(parallaxProp);
const lastReachable = useRef(0); // 左端に揃えられる最後のスライド
const direction = useRef(1); // 自動送りの進行方向(端で反転する)
const [useParallax, setUseParallax] = useState(parallaxProp);
const [useAutoplay, setUseAutoplay] = useState(autoplayProp);
const [index, setIndex] = useState(0);
const [bounds, setBounds] = useState({ start: true, end: false });
const [playing, setPlaying] = useState(true);
const [hovering, setHovering] = useState(false);
const [inView, setInView] = useState(false);
const [revealed, setRevealed] = useState(false);
const [motionOk, setMotionOk] = useState(true);
const [tabHidden, setTabHidden] = useState(false);
const total = moments.length;
// 1フレームに1回だけ、スクロール位置を読んで CSS 変数と現在位置へ反映する
const measure = useCallback(() => {
raf.current = 0;
const track = trackRef.current;
const root = rootRef.current;
if (!track || !root) return;
const max = track.scrollWidth - track.clientWidth;
root.style.setProperty("--progress", max > 0 ? (track.scrollLeft / max).toFixed(4) : "1");
const viewport = track.clientWidth || 1;
const center = track.scrollLeft + viewport / 2;
const base = (track.children[0] as HTMLElement | undefined)?.offsetLeft ?? 0;
let nearest = 0;
let min = Infinity;
let reachable = 0;
for (let i = 0; i < track.children.length; i++) {
const el = track.children[i] as HTMLElement;
const left = el.offsetLeft - base;
if (left <= max + 2) reachable = i;
// 先頭に来ているスライド=現在位置(snap-start なので左端との距離で決まる)
const gap = Math.abs(left - track.scrollLeft);
if (gap < min) {
min = gap;
nearest = i;
}
if (parallaxRef.current) {
const p = ((el.offsetLeft + el.offsetWidth / 2 - center) / viewport) * 2;
el.style.setProperty("--p", Math.max(-1.4, Math.min(1.4, p)).toFixed(3));
}
}
lastReachable.current = reachable;
const atStart = track.scrollLeft <= 2;
const atEnd = track.scrollLeft >= max - 2;
// 末尾に着いたら、左端に来られない残りのスライドも「見えている」ので最後を現在位置にする
if (atEnd && max > 0) nearest = track.children.length - 1;
if (!animating.current) setIndex((prev) => (prev === nearest ? prev : nearest));
setBounds((prev) => (prev.start === atStart && prev.end === atEnd ? prev : { start: atStart, end: atEnd }));
}, []);
useEffect(() => {
const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
const sync = () => {
reduced.current = mq.matches;
setMotionOk(!mq.matches);
};
sync();
mq.addEventListener("change", sync);
return () => mq.removeEventListener("change", sync);
}, []);
// 視差の切り替え。止めるときは --p を消し、var の既定値 0 に戻す
useEffect(() => {
parallaxRef.current = useParallax && motionOk;
const track = trackRef.current;
if (!track) return;
if (!parallaxRef.current) {
for (const el of Array.from(track.children)) (el as HTMLElement).style.removeProperty("--p");
}
measure();
}, [useParallax, motionOk, measure]);
const stopTween = useCallback(() => {
if (!tween.current) return;
cancelAnimationFrame(tween.current);
tween.current = 0;
animating.current = false;
const track = trackRef.current;
if (track) track.style.scrollSnapType = "";
}, []);
useEffect(() => {
const track = trackRef.current;
if (!track) return;
const schedule = () => {
if (!raf.current) raf.current = requestAnimationFrame(measure);
};
// 送りの途中で指やホイールが入ったら、補間をやめて手の操作に渡す
const interrupt = () => {
if (!tween.current) return;
stopTween();
schedule();
};
track.addEventListener("scroll", schedule, { passive: true });
track.addEventListener("pointerdown", interrupt, { passive: true });
track.addEventListener("wheel", interrupt, { passive: true });
window.addEventListener("resize", schedule);
measure();
return () => {
track.removeEventListener("scroll", schedule);
track.removeEventListener("pointerdown", interrupt);
track.removeEventListener("wheel", interrupt);
window.removeEventListener("resize", schedule);
cancelAnimationFrame(raf.current);
raf.current = 0;
stopTween();
};
}, [measure, stopTween]);
// リビールと「画面内か」を1つの Observer で見る
useEffect(() => {
const root = rootRef.current;
if (!root) return;
if (typeof IntersectionObserver === "undefined") {
const id = requestAnimationFrame(() => {
setRevealed(true);
setInView(true);
});
return () => cancelAnimationFrame(id);
}
const io = new IntersectionObserver(
(entries) => {
for (const entry of entries) {
if (entry.isIntersecting) setRevealed(true);
setInView(entry.intersectionRatio >= 0.3);
}
},
{ threshold: [0.05, 0.3, 0.6] },
);
io.observe(root);
return () => io.disconnect();
}, []);
const goTo = useCallback(
(i: number) => {
const track = trackRef.current;
if (!track) return;
const last = track.children.length - 1;
let target = Math.max(0, Math.min(last, i));
const el = track.children[target] as HTMLElement | undefined;
const first = track.children[0] as HTMLElement | undefined;
if (!el || !first) return;
const max = track.scrollWidth - track.clientWidth;
let left = el.offsetLeft - first.offsetLeft;
// 左端に来られないスライドは「末尾の位置」へ送り、番号は最後に合わせる
if (left >= max - 2) {
left = max;
target = last;
}
stopTween();
setIndex(target); // 番号は押した時点で行き先に合わせる
const from = track.scrollLeft;
if (reduced.current || Math.abs(left - from) < 1) {
track.scrollLeft = left;
return;
}
animating.current = true;
track.style.scrollSnapType = "none";
const t0 = performance.now();
const step = (now: number) => {
const t = Math.min(1, (now - t0) / SLIDE_MS);
const eased = 1 - Math.pow(1 - t, 3);
track.scrollLeft = from + (left - from) * eased;
if (t < 1) {
tween.current = requestAnimationFrame(step);
} else {
tween.current = 0;
animating.current = false;
track.style.scrollSnapType = "";
measure();
}
};
tween.current = requestAnimationFrame(step);
},
[measure, stopTween],
);
// 戻るときは、末尾から「左端に揃えられる最後のスライド」へ戻す
const prev = useCallback(() => goTo(Math.min(index - 1, lastReachable.current)), [goTo, index]);
const next = useCallback(() => goTo(index + 1), [goTo, index]);
// タブが裏に回ったら止め、戻ったら輪とタイマーを最初から張り直す
useEffect(() => {
const onVis = () => setTabHidden(document.hidden);
document.addEventListener("visibilitychange", onVis);
return () => document.removeEventListener("visibilitychange", onVis);
}, []);
const running = useAutoplay && playing && inView && !hovering && motionOk && !tabHidden;
// 自動送り: 1枚ごとにタイマーを張り直し、再生ボタンの輪を同じ長さで進める
useEffect(() => {
if (!running) return;
const ring = ringRef.current;
const anim = ring?.animate?.([{ strokeDashoffset: RING }, { strokeDashoffset: 0 }], {
duration: AUTOPLAY_MS,
easing: "linear",
fill: "forwards",
});
const id = window.setTimeout(() => {
const last = total - 1;
if (index >= last) direction.current = -1;
if (index <= 0) direction.current = 1;
if (direction.current > 0) goTo(index + 1);
else goTo(Math.min(index - 1, lastReachable.current));
}, AUTOPLAY_MS);
return () => {
window.clearTimeout(id);
anim?.cancel();
};
}, [running, index, goTo, total]);
const status = !useAutoplay
? null
: !motionOk
? "停止中"
: !playing
? "停止中"
: hovering
? "一時停止中"
: "自動送り中";
const reveal =
"transition-[opacity,transform] duration-700 ease-out motion-reduce:transition-none " +
(revealed
? "translate-y-0 opacity-100"
: "translate-y-6 opacity-0 motion-reduce:translate-y-0 motion-reduce:opacity-100");
const delay = (ms: number) => ({ transitionDelay: `${ms}ms` });
const roundBtn =
"grid size-10 place-items-center rounded-full border border-gray-300 bg-white text-[14px] text-gray-800 transition-[color,background-color,border-color,transform] duration-150 hover:border-gray-900 hover:text-gray-900 active:scale-90 active:bg-gray-100 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600 disabled:border-gray-200 disabled:text-gray-300 disabled:active:scale-100 motion-reduce:transition-none motion-reduce:active:scale-100";
const activeMode = MODES.find((m) => m.flags.parallax === useParallax && m.flags.autoplay === useAutoplay);
return (
<section
ref={rootRef}
aria-labelledby="moments-heading"
aria-roledescription="カルーセル"
className="@container w-full max-w-5xl bg-white"
>
{/* 見出し+操作 */}
<div className="flex flex-col gap-4 px-1 @2xl:flex-row @2xl:items-end @2xl:justify-between @2xl:gap-6">
<div className="max-w-xl">
<p
style={delay(0)}
className={`font-display text-[11px] font-bold tracking-[0.25em] text-amber-600 uppercase ${reveal}`}
>
BLK-32 — Works / Carousel
</p>
<Heading
id="moments-heading"
style={delay(80)}
className={`mt-2 text-[24px] leading-[1.35] font-bold tracking-tight text-gray-900 @2xl:mt-3 @2xl:text-[32px] ${reveal}`}
>
現場で決まった、
<span className="whitespace-nowrap">
<span className="relative inline-block">
<span className="relative z-10">その日の一枚</span>
<span aria-hidden="true" className="absolute -bottom-0.5 left-0 h-[3px] w-full bg-amber-500/70" />
</span>
。
</span>
</Heading>
<p
style={delay(160)}
className={`mt-2 text-[14px] leading-[1.75] font-medium text-gray-600 @2xl:mt-3 @2xl:text-[15px] ${reveal}`}
>
6件の実績を、納品した画面と現場の絵で。横に送ると、奥と手前が違う速さで動きます。
</p>
</div>
{/* 進行制御。自動送りの停止ボタンを最初に置く(APG: 回転の操作をスライドより前に) */}
<div style={delay(240)} className={`flex items-center gap-3 ${reveal}`}>
{useAutoplay && (
<button
type="button"
onClick={() => setPlaying((v) => !v)}
aria-pressed={!playing}
aria-label={playing ? "自動送りを停止" : "自動送りを再開"}
disabled={!motionOk}
className={`relative grid size-10 place-items-center rounded-full text-[11px] transition-[color,background-color,transform] duration-150 active:scale-90 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600 disabled:bg-gray-100 disabled:text-gray-400 motion-reduce:transition-none motion-reduce:active:scale-100 ${
playing && motionOk ? "bg-white text-gray-900" : "bg-gray-900 text-white"
}`}
>
{/* 再生中は輪が 4.6 秒かけて一周し、次の送りまでの残りを見せる */}
<svg aria-hidden="true" viewBox="0 0 40 40" className="absolute inset-0 size-full -rotate-90">
<circle cx="20" cy="20" r="16" fill="none" stroke={playing && motionOk ? "#e5e7eb" : "transparent"} strokeWidth="2.5" />
<circle
ref={ringRef}
cx="20"
cy="20"
r="16"
fill="none"
stroke={AMBER}
strokeWidth="2.5"
strokeDasharray={RING}
strokeDashoffset={RING}
strokeLinecap="round"
className={running ? "opacity-100" : "opacity-0"}
/>
</svg>
<span aria-hidden="true" className="relative">
{playing && motionOk ? "❙❙" : "▶"}
</span>
</button>
)}
<div className="flex items-center gap-1.5">
<button type="button" onClick={() => { setPlaying(false); prev(); }} disabled={bounds.start} aria-label="前の事例へ" className={roundBtn}>
<span aria-hidden="true">←</span>
</button>
<button type="button" onClick={() => { setPlaying(false); next(); }} disabled={bounds.end} aria-label="次の事例へ" className={roundBtn}>
<span aria-hidden="true">→</span>
</button>
</div>
<div className="flex flex-col leading-tight">
<p
className="font-display text-[13px] font-semibold tracking-[0.1em] text-gray-400 tabular-nums"
aria-live={running ? "off" : "polite"}
>
<span className="sr-only">表示中の事例: </span>
<span className="text-gray-900">{String(index + 1).padStart(2, "0")}</span>
<span className="mx-1 text-gray-300">/</span>
{String(total).padStart(2, "0")}
</p>
{status && (
<p className={`mt-0.5 text-[11px] font-semibold ${running ? "text-amber-700" : "text-gray-500"}`}>
{status}
{!motionOk && <span className="sr-only">(動きを減らす設定のため)</span>}
</p>
)}
</div>
</div>
</div>
{/* トラック。スナップ・ドラッグ・キーボード送りはブラウザ側の機能 */}
<div
style={delay(320)}
className={`mt-4 border-y border-gray-200 py-4 @2xl:mt-7 @2xl:py-6 ${reveal}`}
onPointerEnter={(e) => e.pointerType === "mouse" && setHovering(true)}
onPointerLeave={() => setHovering(false)}
onFocus={() => setPlaying(false)}
>
<ul
ref={trackRef}
className="relative flex snap-x snap-mandatory gap-4 overflow-x-auto scroll-pl-1 px-1 pb-1 [-ms-overflow-style:none] [scrollbar-width:none] @2xl:gap-5 [&::-webkit-scrollbar]:hidden"
>
{moments.map((m, i) => (
<li
key={m.no}
aria-roledescription="スライド"
aria-label={`${i + 1} / ${total}`}
data-active={String(i === index)}
className="group relative flex w-[86%] shrink-0 snap-start flex-col @md:w-[58%] @2xl:w-[44%]"
>
{/* plate。overflow-hidden が層のはみ出しを切ることで、移動量の差が奥行きに見える */}
<div className={`relative aspect-[4/3] overflow-hidden rounded-[4px] ${m.field}`}>
{/* 最奥: 現場の絵(幅を広げて描き、逆向きに大きく動かす) */}
<svg
aria-hidden="true"
viewBox="0 0 440 300"
preserveAspectRatio="xMidYMid slice"
className="absolute inset-y-0 -left-[10%] h-full w-[120%] [transform:translate3d(calc(var(--p,0)*-40px),0,0)]"
>
{m.back}
</svg>
{/* 中景: 橙の形 */}
<span
aria-hidden="true"
className={`absolute [transform:translate3d(calc(var(--p,0)*-16px),0,0)] ${m.accent}`}
/>
{/* 最前: 納品した画面(順向きに小さく動かす=手前に見える) */}
<svg
aria-hidden="true"
viewBox="0 0 400 300"
preserveAspectRatio="xMidYMid slice"
className="absolute inset-0 size-full drop-shadow-[0_14px_18px_rgba(0,0,0,0.28)] [transform:translate3d(calc(var(--p,0)*14px),0,0)]"
>
{m.front}
</svg>
{/* 種別のチップ */}
<span className="font-display absolute top-2.5 left-2.5 rounded-full bg-white/95 px-2.5 py-1 text-[10px] font-bold tracking-[0.16em] text-gray-800 tabular-nums">
{m.no} · {m.kind}
</span>
{/* 現在位置の合図 */}
<span
aria-hidden="true"
className="absolute bottom-0 left-0 h-[4px] w-full origin-left scale-x-0 bg-amber-500 transition-transform duration-300 ease-out group-data-[active=true]:scale-x-100 motion-reduce:transition-none"
/>
</div>
<div className="mt-3 @2xl:mt-4">
<p className="flex items-center gap-2 text-[11px] font-semibold text-gray-500">
{m.industry}
<span aria-hidden="true" className="text-gray-300">
/
</span>
<span className="font-display text-gray-400 tabular-nums">{m.date}</span>
</p>
<h3 className="mt-1.5 text-[15px] leading-[1.6] font-bold text-gray-900 @2xl:text-base">
{/* セル全面をクリック領域にする stretched link(<a> はスライドに1本だけ) */}
<a
href="#"
className="after:absolute after:inset-0 after:content-[''] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-amber-600"
>
{m.title}
</a>
</h3>
<p className="mt-2 text-[13px] leading-[1.8] text-gray-500">{m.note}</p>
<p className="mt-3 border-l-2 border-amber-500/60 pl-3 text-[12px] leading-[1.8] text-gray-600">{m.scene}</p>
</div>
</li>
))}
</ul>
</div>
{/* 位置の点(押すとその事例へ)+進捗レール */}
<div style={delay(420)} className={`mt-4 flex flex-wrap items-center justify-between gap-4 px-1 ${reveal}`}>
<div className="flex items-center gap-4">
<div className="flex items-center">
{moments.map((m, i) => (
<button
key={m.no}
type="button"
onClick={() => {
setPlaying(false);
goTo(i);
}}
aria-label={`事例 ${i + 1} へ`}
aria-current={i === index ? "true" : undefined}
className="group/dot grid h-8 w-6 place-items-center rounded focus-visible:outline-2 focus-visible:outline-amber-600"
>
<span
aria-hidden="true"
className={`block h-1.5 rounded-full transition-[width,background-color] duration-300 ease-out motion-reduce:transition-none ${
i === index ? "w-5 bg-amber-600" : "w-1.5 bg-gray-300 group-hover/dot:bg-gray-500"
}`}
/>
</button>
))}
</div>
<span aria-hidden="true" className="relative hidden h-px w-28 bg-gray-200 @md:block">
<span className="absolute inset-0 h-px origin-left bg-amber-600 [transform:scaleX(var(--progress,0))]" />
</span>
</div>
<a
href="#"
className="group relative inline-flex items-center gap-2 text-sm font-semibold text-gray-900 focus-visible:outline-2 focus-visible:outline-offset-4 focus-visible:outline-amber-600"
>
<span className="relative">
実績をすべて見る
<span
aria-hidden="true"
className="absolute -bottom-1 left-0 h-px w-full origin-left scale-x-100 bg-gray-900 transition-transform duration-300 ease-out group-hover:scale-x-0 motion-reduce:transition-none"
/>
<span
aria-hidden="true"
className="absolute -bottom-1 left-0 h-px w-full origin-right scale-x-0 bg-amber-600 transition-transform delay-150 duration-300 ease-out group-hover:origin-left group-hover:scale-x-100 motion-reduce:transition-none"
/>
</span>
<span aria-hidden="true" className="transition-transform duration-300 ease-out group-hover:translate-x-1 motion-reduce:transition-none">
→
</span>
</a>
</div>
{showGuide && (
<div className="mt-6 border-t border-gray-200 px-1 pt-4">
<div className="flex flex-wrap items-center gap-x-3 gap-y-2">
<p className="text-[12px] font-bold text-gray-900">見比べる</p>
<div role="group" aria-label="動きの見比べ" className="flex rounded-full bg-gray-100 p-1">
{MODES.map((m) => (
<button
key={m.key}
type="button"
aria-pressed={activeMode?.key === m.key}
onClick={() => {
setUseParallax(m.flags.parallax);
setUseAutoplay(m.flags.autoplay);
setPlaying(true);
}}
className="min-h-9 rounded-full px-3 text-[12px] font-semibold text-gray-600 hover:text-gray-900 focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-amber-600 aria-pressed:bg-white aria-pressed:text-gray-900 aria-pressed:shadow-[0_1px_2px_rgba(17,24,39,0.14)]"
>
{m.name}
</button>
))}
</div>
</div>
{activeMode && (
<div className="mt-3 grid gap-2 @2xl:grid-cols-[auto_1fr] @2xl:items-baseline @2xl:gap-4">
<code className="w-fit rounded bg-gray-900 px-2 py-1 font-mono text-[12px] text-amber-200">{activeMode.code}</code>
<p className="text-[12px] leading-[1.8] text-gray-600">{activeMode.body}</p>
</div>
)}
<p className="mt-3 text-[11px] leading-[1.8] text-gray-500">
該当コードはコード欄の measure()・goTo()・自動送りの useEffect。HTML欄は JavaScript なしの最小版(スクロールスナップだけ・視差と自動送りなし)。
</p>
<div className="mt-3 text-[11px] leading-[1.8] text-gray-500">
<p>掲載の事例・業種・日付・画面は見本用の架空データです。</p>
<p className="mt-1">
出典: 自動送りに停止の操作を置く根拠 —{" "}
<a className="underline underline-offset-2 hover:text-gray-900" href="https://www.w3.org/WAI/WCAG22/Understanding/pause-stop-hide.html">
W3C「Understanding SC 2.2.2: Pause, Stop, Hide」
</a>
(WCAG 2.2・ページの最終更新 2026-10-04)。フォーカスが入ったら止めて自動では再開しない・再生中は現在位置を読み上げない —{" "}
<a className="underline underline-offset-2 hover:text-gray-900" href="https://www.w3.org/WAI/ARIA/apg/patterns/carousel/">
W3C WAI-ARIA APG「Carousel Pattern」
</a>
。いずれも 2026-11-08 確認。
</p>
</div>
</div>
)}
</section>
);
}
shadcn CLI でプロジェクトに追加
npx shadcn@latest add https://designs.first-ch.com/r/moments-gallery-carousel.json