SnippetUI

Smooth Reveal Accordion

シームレスな高さのアニメーションと、回転するアイコンを持つクリーンなアコーディオンです。

HTMLCSSJavaScript

プレビュー (Preview)

洗練されたデザインと、スムーズに展開するアニメーションが特徴のコンポーネントです。モダンなCSS技術を使用し、固定の高さやJavaScriptによる複雑な計算に頼ることなく、コンテンツの自然な高さに合わせてシームレスに開閉します。

CSS Gridを利用しています。grid-template-rowsの値を0frから1frへとトランジションさせることで、モダンブラウザ全体で滑らかで信頼性の高い高さのアニメーションを実現しています。

はい、対応しています。ボタンには適切なaria-expanded属性が使用されており、スクリーンリーダーなどの支援技術を使用するユーザーにも、各アコーディオンパネルの開閉状態が正しく伝わります。

ユースケースと特徴 (Use Cases & Features)

  • FAQセクション: よくある質問とその回答をすっきりとまとめるのに最適です。
  • 設定メニュー: 複雑な設定項目をカテゴリごとに整理し、必要な部分だけを展開させることができます。
  • モバイルナビゲーション: 限られた画面スペースを有効活用し、多階層のメニューを表現するのに役立ちます。
  • 特徴: CSS Grid を用いた高さアニメーションにより、内容の量に関わらず滑らかな展開・折りたたみを実現しています。他を閉じると1つだけ開く排他的な動作を標準で備えています。

デザインとUXのポイント (Design & UX)

  • 滑らかなトランジション: grid-template-rowscubic-bezier イージングを組み合わせることで、思わず触りたくなるような心地よい開閉アニメーションを実現しています。
  • 視覚的なフィードバック: 開閉状態に応じてボーダーカラー、タイトルの色、アイコンの回転・色が変化し、ユーザーに現在の状態を直感的に伝えます。
  • ホバーエフェクト: 項目にホバーした際に微妙なシャドウとボーダーカラーの変更を行い、クリッカブルであることを示唆します。
  • アクセシビリティ対応: aria-expanded 属性を正しくトグルすることで、スクリーンリーダーでも状態の変更を認識できるようにしています。

カスタマイズ方法 (Customization Guide)

  • 複数同時の開閉: スクリプト内の「他のアイテムを閉じる(document.querySelectorAll... のループ部分)」処理を削除またはコメントアウトすることで、複数のアコーディオンを同時に開けるように変更できます。
  • テーマカラーの変更: .accordion-item.active.accordion-title#8b5cf6 (紫) を、ブランドのアクセントカラーに変更してください。
  • アイコンの変更: SVGを別のアイコンセット(例: Heroicons, Lucide)のプラス/マイナスアイコンやシェブロンに変更できます。

実装コード (Implementation)

<div class="accordion-container">
<div class="accordion-item">
  <button class="accordion-header" aria-expanded="false">
    <span class="accordion-title">このアコーディオンの特徴は?</span>
    <span class="accordion-icon">
      <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="6 9 12 15 18 9"></polyline></svg>
    </span>
  </button>
  <div class="accordion-content-wrapper">
    <div class="accordion-content">
      洗練されたデザインと、スムーズに展開するアニメーションが特徴のコンポーネントです。モダンなCSS技術を使用し、固定の高さやJavaScriptによる複雑な計算に頼ることなく、コンテンツの自然な高さに合わせてシームレスに開閉します。
    </div>
  </div>
</div>
<!-- 他のアコーディオンアイテムをここに追加 -->
</div>

<style>
.accordion-container {
max-width: 600px;
width: 100%;
display: flex;
flex-direction: column;
gap: 0.75rem;
font-family: system-ui, -apple-system, sans-serif;
}

.accordion-item {
border: 1px solid #e5e7eb;
border-radius: 12px;
overflow: hidden;
background-color: #ffffff;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.02);
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.accordion-item:hover {
border-color: #d1d5db;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.accordion-item.active {
border-color: #8b5cf6;
box-shadow: 0 4px 12px rgba(139, 92, 246, 0.1);
}

.accordion-header {
width: 100%;
padding: 1.25rem 1.5rem;
display: flex;
align-items: center;
justify-content: space-between;
background: none;
border: none;
cursor: pointer;
text-align: left;
color: #1f2937;
}

.accordion-title {
font-weight: 600;
font-size: 1.05rem;
transition: color 0.3s ease;
}

.accordion-icon {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
color: #6b7280;
transition: transform 0.4s cubic-bezier(0.4, 0, 0.2, 1), color 0.3s ease;
}

.accordion-item.active .accordion-icon {
transform: rotate(180deg);
color: #8b5cf6;
}

.accordion-item.active .accordion-title {
color: #8b5cf6;
}

.accordion-content-wrapper {
display: grid;
grid-template-rows: 0fr;
transition: grid-template-rows 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.accordion-item.active .accordion-content-wrapper {
grid-template-rows: 1fr;
}

.accordion-content {
overflow: hidden;
padding: 0 1.5rem;
color: #4b5563;
line-height: 1.6;
font-size: 0.95rem;
opacity: 0;
transform: translateY(-8px);
transition: opacity 0.4s ease, transform 0.4s ease, padding 0.4s ease;
}

.accordion-item.active .accordion-content {
padding-bottom: 1.25rem;
opacity: 1;
transform: translateY(0);
}
</style>

<script>
document.querySelectorAll('.accordion-header').forEach(button => {
button.addEventListener('click', () => {
  const item = button.closest('.accordion-item');
  const isExpanded = button.getAttribute('aria-expanded') === 'true';
  
  document.querySelectorAll('.accordion-item').forEach(otherItem => {
    if (otherItem !== item) {
      otherItem.classList.remove('active');
      otherItem.querySelector('.accordion-header').setAttribute('aria-expanded', 'false');
    }
  });

  if (!isExpanded) {
    item.classList.add('active');
    button.setAttribute('aria-expanded', 'true');
  } else {
    item.classList.remove('active');
    button.setAttribute('aria-expanded', 'false');
  }
});
});
</script>

ブラウザ対応状況 (Browser Support)

  • Chrome: 対応
  • Firefox: 対応
  • Safari: 対応 (Safari 16+ にて Grid Animation をフルサポート)
  • Edge: 対応

※ 古いブラウザで `grid-template-rows` のトランジションがサポートされていない場合、高さのアニメーションは再生されず即座に開閉が行われますが、機能的には問題なく利用できます。