SCALE — Build Lab
UI部品 · REACT COMPONENT

アコーディオン部品

CATEGORYUI部品 TYPEReact Component EFFORT30〜90分 DIFFICULTY
PRIMARY CODE
tsx
import { ReactNode } from 'react';

export function Accordion({ items, exclusive = false }: {
  items: { title: string; content: ReactNode }[];
  exclusive?: boolean;  // true=同時に1つだけ
}) {
  return (
    <div>
      {items.map((it, i) => (
        <details key={i} name={exclusive ? 'accordion' : undefined}
          style={{ borderBottom: '1px solid rgba(255,255,255,.07)', padding: '.85rem 0' }}>
          <summary style={{
            cursor: 'pointer',
            fontFamily: 'serif',
            fontSize: '.95rem',
            color: '#fff',
            listStyle: 'none',
            display: 'flex',
            justifyContent: 'space-between',
            alignItems: 'center',
          }}>
            {it.title}
            <span style={{ color: '#a5b4fc', transition: 'transform .25s' }}>+</span>
          </summary>
          <div style={{ marginTop: '.65rem', color: 'rgba(255,255,255,.55)', fontSize: '.8rem', lineHeight: 1.85 }}>
            {it.content}
          </div>
        </details>
      ))}
    </div>
  );
}
前提条件
Tailwind CSS v4TypeScript 5
USE CASES
  • 任意のダッシュボードに組み込み

アコーディオン部品

:LiTarget: 用途

利用の純HTML軽量アコーディオン。アニメーション + 単一展開モード対応。

:LiSparkle: 特徴

  • details/summary 利用
  • 滑らかアニメーション
  • 単一/複数展開
  • a11y 対応

:LiCode: コード(コピペ用)

import { ReactNode } from 'react';

export function Accordion({ items, exclusive = false }: {
  items: { title: string; content: ReactNode }[];
  exclusive?: boolean;  // true=同時に1つだけ
}) {
  return (
    <div>
      {items.map((it, i) => (
        <details key={i} name={exclusive ? 'accordion' : undefined}
          style={{ borderBottom: '1px solid rgba(255,255,255,.07)', padding: '.85rem 0' }}>
          <summary style={{
            cursor: 'pointer',
            fontFamily: 'serif',
            fontSize: '.95rem',
            color: '#fff',
            listStyle: 'none',
            display: 'flex',
            justifyContent: 'space-between',
            alignItems: 'center',
          }}>
            {it.title}
            <span style={{ color: '#a5b4fc', transition: 'transform .25s' }}>+</span>
          </summary>
          <div style={{ marginTop: '.65rem', color: 'rgba(255,255,255,.55)', fontSize: '.8rem', lineHeight: 1.85 }}>
            {it.content}
          </div>
        </details>
      ))}
    </div>
  );
}

:LiHandPointer: 使い方

対象プロジェクトに該当ファイルをコピーして、props を流し込むだけ。

:LiAlertCircle: 注意事項

  • 依存パッケージを忘れず追加