仕組み
Every card sits at the top centre of the stage and is placed by one transform: its slot, set from its depth in the pile (each card below peeks out 12px lower, 6% smaller, with its own small tilt; three show) or its cell in the grid, plus the drag offset and a small hover lift. Because the function list never changes, the pile, the grid, a throw and a return all interpolate cleanly. Dragging writes the offset straight to the card's style and turns it up to 18 degrees with the sideways movement. On release, faster than 0.45px/ms or further than 45% of the card's width is a throw: the card flies on along its direction of travel, turning 16 degrees more and fading, in 360ms; the next card rises on a 560ms spring. Otherwise the card springs back. The thrown card lands at the bottom of the pile without animating that jump. Bringing a card back slides it in from the left. Switching to the grid deals the cards from the top of the pile, 32ms apart, over 560ms.
向いている場面
- A short set of cards to go through one at a time: workout steps, flashcards, featured projects.
- A playful portfolio or product showcase where browsing should feel physical.
- When people sometimes want the overview: the grid shows the whole set at once.
向かない場面
- Decisions such as like or dismiss: a throw here only moves on; use swipe actions with clear outcomes.
- Long or growing lists: the pile hides all but three cards; use a list or a carousel.
- Cards that need their own buttons or links: in the grid each card is a single button.
使い方
import { ThrowDeck } from './ThrowDeck.jsx'
export function ProjectPile({ projects, onOpen }) {
return (
<ThrowDeck
items={projects}
getLabel={(p) => p.title}
onSelect={(p) => onOpen(p.id)}
cardWidth={240}
renderCard={(p) => (
<div style={{ display: 'grid', height: '100%', gridTemplateRows: '1fr auto' }}>
<img src={p.cover} alt="" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
<strong style={{ padding: 12 }}>{p.title}</strong>
</div>
)}
labels={{ deck: 'Projects' }}
/>
)
}プロパティ
| プロパティ | 型 | 既定値 | 説明 |
|---|---|---|---|
items | { id, ... }[] | — | The cards, in order. Each needs a stable id. Four to twelve work best. |
renderCard | (item, { index, top, view }) => ReactNode | — | Draws one card's face. Keep it free of links and buttons: in the grid each card is itself a button. |
getLabel | (item, index) => string | — | A short text name for a card, used by screen readers and announcements. |
index | number | — | Which card is on top (controlled). |
defaultIndex | number | 0 | The card on top at first when uncontrolled. |
onIndexChange | (index: number) => void | — | Called when a throw, a return or a choice in the grid changes the top card. |
view | 'pile' | 'grid' | — | Pile or grid (controlled). |
defaultView | 'pile' | 'grid' | 'pile' | The view at first when uncontrolled. |
onViewChange | (view) => void | — | Called when the view button or a choice in the grid switches the view. |
onSelect | (item, index) => void | — | Called when the top card is tapped without dragging, or Enter is pressed on the deck. Without it, a tap throws the card. |
cardWidth | number | 210 | Card width in the pile, in pixels. |
cardRatio | number | 0.72 | Card height as a share of its width. |
gridColumns | number | — | Columns in the grid. By default it fits cards about 128px wide, two to four columns. |
gap | number | 10 | Space between cards in the grid, in pixels. |
controls | boolean | true | Show the buttons under the deck. If you hide them, give people another way to move through the cards. |
labels | { deck, hint, previous, next, grid, pile, position(i, n), count(n) } | — | Replace the built-in text, for example to localize. |
...rest | HTMLAttributes | — | Other attributes go to the root element. |
アクセシビリティ
- The pile is one focusable group, described by a hint: Right throws the top card, Left brings the last one back, Enter opens it when onSelect is set.
- Under the deck, buttons named "Bring back the last card", "Throw the top card" and "Show all cards" or "Back to the pile" do the same, so no gesture is needed.
- Only the top card is exposed to screen readers in the pile; the others are hidden and inert. A status line announces the new top card, as in "2 of 6: Push-ups".
- In the grid each card is a button named by getLabel; choosing one returns to the pile with it on top and moves focus to the pile.
- Reduced motion moves cards straight to their slots: a drag still follows the pointer, but nothing flies, springs or deals.
モーション
Throw: 360ms ease-out flight with a fade. Next card rises on a 560ms spring; a released card springs back the same way. Grid and pile: 560ms ease-out, 32ms apart. Hover lifts the top card 4px and turns it 1.5 degrees.
プロジェクトに追加
Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。
ThrowDeck.jsxthrow-deck.csstokens.cssREADME.md
2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。







