本文へ移動
日本語

仕組み

Unlike CSS columns, which fill top to bottom, each card goes to the top of the shortest column in turn, so the first cards stay at the top. Cards keep their DOM order and are placed with transforms from measured heights; a ResizeObserver remeasures when images load or text wraps. Before measuring (on the server and in the first frame) the grid falls back to a plain auto-fill grid, so it never renders empty.

向いている場面

  • Galleries, moodboards and pins with mixed aspect ratios.
  • Cards whose text lengths vary a lot, such as notes or reviews.

向かない場面

  • Items that must line up in rows for comparison: use a regular grid or table.
  • Thousands of items: pair it with paging, or use a virtual list.

使い方

import { MasonryGrid } from './MasonryGrid.jsx'

export function Gallery({ photos }) {
  return (
    <MasonryGrid
      label="Photos"
      items={photos}
      getKey={(p) => p.id}
      minColumnWidth={180}
      maxColumns={4}
      renderItem={(p) => <img src={p.src} alt={p.alt} width={p.width} height={p.height} style={{ width: '100%', height: 'auto' }} />}
    />
  )
}

プロパティ

プロパティ型既定値説明
itemsT[]—The data, in reading order.
renderItem(item: T, index) => ReactNode—Renders one card at the column width; its height can be anything.
getKey(item: T, index) => Keyitem.id ?? indexA stable key, so a card keeps its measured height and glides instead of reappearing.
minColumnWidthnumber180Columns are added while each stays at least this wide.
maxColumnsnumber4Upper limit on columns.
gapnumber12Space between cards in px, both ways.
labelstring—Accessible name of the list.
...restHTMLAttributes<HTMLDivElement>—Passed to the root.

アクセシビリティ

  • The root is role="list" and each card a listitem; DOM order equals data order, so screen readers and Tab follow the reading order, not the columns.
  • Focusable content inside cards keeps its own focus styles; the grid adds no keyboard traps.
  • Reduced motion removes the glide between layouts and shortens the fade-in to 160ms.

モーション

Cards fade in 40ms apart (up to twelve). When columns or heights change, cards move on a 560ms decelerating curve (cubic-bezier(0.2, 0.9, 0.25, 1)) and the grid height follows.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

MasonryGrid.jsxmasonry-grid.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

リスト
「アーチ型カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。
「円柱カルーセル」は Pro に含まれます。解除するとプレビューが表示されます。