仕組み
Each item is a filled, rounded row with no outlines or separators; its round plus turns 45° into a close mark and fills with ink while the item is open. Each panel is a one-row grid whose grid-template-rows goes from 0fr to 1fr (the content's natural height), with an inner min-height:0 + overflow:hidden layer clipping it. Nothing is measured in JS, so late images or rewrapping text cannot misalign it. The content also slides down 8px and fades in, slightly after the height starts. Closing is about 80% as long, and visibility turns hidden only after the height has closed.
向いている場面
- FAQs in a help center or at the bottom of a checkout page.
- Folding low-use advanced options on a settings page.
- Long explanations such as shipping or return rules, collapsed by default to save space.
向かない場面
- Key information every visitor must see: folded means hidden, so lay it out flat.
- A single item: use one disclosure such as “Show more” instead of an accordion.
- Switching between sibling views, one at a time: tabs are clearer.
使い方
import { SmoothAccordion } from './SmoothAccordion.jsx'
export function Faq() {
return (
<SmoothAccordion
defaultValue={['ship']}
items={[
{ value: 'ship', title: 'How long does delivery take?', content: 'Usually 2 to 4 business days.' },
{ value: 'return', title: 'Can I return an item?', content: 'Yes, within 30 days if it is unused.' },
]}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
items | { value, title, content, disabled? }[] | — | The items. Each has a unique value, a title, content, and an optional disabled flag. |
allowMultiple | boolean | false | Allows several items open at once. When off, opening one closes the others; turning it off at runtime keeps only the last opened item. |
value | string[] | — | Controlled: the list of open values. |
defaultValue | string[] | [] | Uncontrolled: the values open at first. |
onChange | (value: string[]) => void | — | Called when the open items change. |
duration | number | 240 | Opening time in ms; closing takes 0.8 times as long. When omitted it follows --ds-duration-base. |
headingLevel | 2 | 3 | 4 | 5 | 6 | 3 | Heading level wrapped around each title button; choose it to fit the page outline. |
...rest | HTMLAttributes<HTMLDivElement> | — | Other attributes go to the root <div>. |
アクセシビリティ
- Each title is a <button> with aria-expanded and aria-controls, wrapped in a heading (h3 by default) to keep the page outline.
- Panels are role="region" labelled by their title.
- Closed panels are visibility: hidden, so their links and buttons cannot be tabbed to or read.
- Enter and Space toggle; Up and Down move between headings; Home and End jump to the first and last.
- Titles are at least 48px high with a :focus-visible ring; reduced motion opens and closes without a transition.
モーション
Opening: height 240ms ease-out, content fades in over 240ms ease-out (36ms delay) and slides down 8px, the plus turns 45° on a 240ms spring while its disc fills with ink. Closing: height 192ms standard, content fades out in 120ms ease-in. A pressed plus scales to 0.9.
プロジェクトに追加
プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。
SmoothAccordion.jsxsmooth-accordion.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。