本文へ移動
日本語
「ドラッグ並べ替え」は Pro に含まれます。解除するとプレビューが表示されます。

仕組み

Rows rest as filled tiles (--ds-drag-sort-bg, surface-2 by default); a lifted row turns into a raised card (--ds-drag-sort-lift-bg) with a 1.03 scale and a large soft shadow. During a drag the DOM order and container height stay fixed and everything moves by transform. Each <li> only moves; the card inside scales and casts the shadow, so movement is strictly 1:1. Passed rows make way by the lifted row's measured height plus the gap, on a soft spring. On release, FLIP finishes the move with Web Animations from the old to the new position on the same spring. Keyboard and pointer share one offset calculation, and there is no edge auto-scroll.

向いている場面

  • Letting people set their own order: home shortcuts, favourites, display order of their items.
  • Reordering banners, featured slots, course chapters or FAQ entries in an admin screen.
  • Short lists that fit on one screen (a dozen items or fewer), where dragging is easiest.

向かない場面

  • Long lists that need scrolling while dragging: there is no edge auto-scroll, so use move up / down / to top buttons or a number field.
  • Dragging between several lists or into a tree: this only sorts within one list.
  • Grids or horizontal layouts: only a single vertical column is supported.
  • Lists whose order is set by the system (by time or popularity): do not offer a handle that does nothing.

使い方

import { useState } from 'react'
import { DragSort } from './DragSort.jsx'

const DEFAULT = [
  { id: 'warmup', name: 'Warm-up' },
  { id: 'squats', name: 'Squats' },
  { id: 'plank', name: 'Plank' },
]

export function WorkoutOrder() {
  const [items, setItems] = useState(DEFAULT)
  return (
    <>
      <DragSort
        label="Exercise order"
        items={items}
        onChange={setItems} // called after release, only if the order changed, with the new array
        renderItem={(item, { index }) => (
          <span>
            {index + 1}. {item.name}
          </span>
        )}
      />
      <button onClick={() => setItems(DEFAULT)}>Reset</button>
    </>
  )
}

プロパティ

プロパティ型既定値説明
itemsT[] (each with a unique, stable id)—List data; each item needs a unique, stable id. Controlled: this order wins, and rows slide to their new places when it changes from outside.
onChange(nextItems) => void—Called on release (or keyboard drop) when the order really changed, with the new array. Not called on cancel or when dropped in place.
renderItem(item, state) => ReactNode—Renders a row's content (without the handle). state: { index, live while dragging; dragging, this row is lifted; sorting, some row is lifted }.
handlebooleantrueDrag only by the handle. When false, the whole row drags too (mouse after 4px, touch after a 220ms long press); the handle stays for the keyboard.
handlePosition'start' | 'end''end'Whether the handle sits at the start or the end of the row.
disabledbooleanfalseTurns sorting off.
getLabel(item, index) => stringitem label / name / titleName of each row, used for the handle's accessible name and the announcements.
labelstring—Accessible name of the list (aria-label), such as “Shortcut order”.
labelsobject—Replaces built-in screen-reader text (handle, roleDescription, instructions, lifted, moved, dropped, cancelled, item). Shallow-merged over the English defaults.
...restHTMLAttributes<HTMLDivElement>—Other attributes go to the root. Gap, radius and row background use the CSS variables --ds-drag-sort-gap, --ds-drag-sort-radius and --ds-drag-sort-bg.

アクセシビリティ

  • The handle is a real <button>: Tab focuses it, Space or Enter lifts, Up and Down move one place, Home and End go to the ends, Space or Enter drops, Esc cancels. Losing focus while lifted drops in place.
  • Each step is announced through an aria-live region, such as “Lifted Tasks, position 1 of 4”; the text can be replaced with labels.
  • The handle has an aria-label, aria-roledescription, an aria-describedby pointing to the keyboard instructions, and aria-pressed while lifted.
  • The list is an <ol role="list"> whose DOM order always equals the visual order; focus returns to the handle after a drop.
  • On touch screens with a screen reader there are no arrow keys, so add move up / down buttons in the row that call the same onChange.
  • Reduced motion makes making way and landing instant and skips the scale-up (the raised colour and shadow stay).

モーション

No transition while dragging; the row follows 1:1. Lifting scales to 1.03 and fades the shadow in over 160ms. Making way is a 320ms translateY on a soft spring (cubic-bezier(0.34, 1.3, 0.64, 1)), and the FLIP landing uses the same 320ms spring. Over-drag damping caps at 28px; touch long press lifts after 220ms.

プロジェクトに追加

Pro メンバーはプロンプトをコピーし、コーディングエージェントが無料コンポーネントと同じ手順でソースを導入します。

DragSort.jsxdrag-sort.csstokens.cssREADME.md

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

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