Communitygithub.com

拖放 (Drag and Drop)

可排序列表、看板、檔案拖放區域,支援 dnd-kit、觸控及無障礙功能。

拖放 (Drag and Drop) 是什麼?

此技能專注於使用現代、可無障礙存取且支援觸控的拖放函式庫 `@dnd-kit` 來實作拖放功能。它涵蓋三種核心使用情境:可排序列表、看板以及檔案拖放區域。對於可排序列表,它使用 `<SortableContext>` 包裹列表,並讓每個項目透過 `useSortable()` 鉤子獲得拖放能力。看板則透過多個 `<SortableContext>` 容器(每個欄位一個)搭配單一 `<DndContext>` 來實現跨欄拖放。檔案拖放區域建議使用 `react-dropzone` 或原生的 `onDragOver`/`onDrop` 事件處理器。技能強調開箱即用的觸控支援(`TouchSensor`、`PointerSensor`)與鍵盤無障礙(`KeyboardSensor` 搭配 `sortableKeyboardCoordinates`)。效能方面,建議採用樂觀更新策略:拖放完成後立即更新狀態,背景同步至伺服器,出錯時回滾。視覺回饋則透過 `<DragOverlay>` 元件提供拖動項目的浮動預覽。該技能支援所有主流 React 專案,適用於 Claude Code、CURSOR、Codex 等 AI 代理平台。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/YepAPI/skills/tree/HEAD/skills/drag-and-drop

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

拖放

規則

  • 使用 @dnd-kit/core + @dnd-kit/sortable — 現代、可無障礙存取、支援觸控的拖放函式庫
  • 可排序列表:包裹在 <SortableContext> 中,每個項目使用 useSortable() 鉤子
  • 看板:使用 <DndContext> 搭配多個 <SortableContext> 容器 — 每個欄位一個
  • 檔案拖放區域:使用 react-dropzone 或原生的 onDragOver/onDrop 搭配 e.dataTransfer.files
  • 觸控支援:dnd-kit 原生處理觸控 — 使用 TouchSensor 和 PointerSensor 並設定啟用條件
  • 無障礙功能:dnd-kit 開箱即用提供鍵盤拖放 — 使用 KeyboardSensor 搭配 sortableKeyboardCoordinates
  • 樂觀重新排序:放下時立即更新狀態,背景同步至伺服器,出錯時回滾
  • 拖動疊加層:使用 <DragOverlay> 提供拖動項目的浮動預覽 — 更好的視覺回饋

範例

import { DndContext, closestCenter, DragOverlay } from "@dnd-kit/core";
import { SortableContext, verticalListSortingStrategy, useSortable } from "@dnd-kit/sortable";
import { CSS } from "@dnd-kit/utilities";

function SortableItem({ id, children }: { id: string; children: React.ReactNode }) {
  const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id });
  const style = { transform: CSS.Transform.toString(transform), transition };
  return (
    <div ref={setNodeRef} style={style} {...attributes} {...listeners}>
      {children}
    </div>
  );
}

function SortableList({ items }: { items: Item[] }) {
  const [activeId, setActiveId] = useState<string | null>(null);
  return (
    <DndContext
      collisionDetection={closestCenter}
      onDragStart={({ active }) => setActiveId(active.id)}
      onDragEnd={handleDragEnd}
    >
      <SortableContext items={items.map((i) => i.id)} strategy={verticalListSortingStrategy}>
        {items.map((item) => (
          <SortableItem key={item.id} id={item.id}>{item.name}</SortableItem>
        ))}
      </SortableContext>
      <DragOverlay>{activeId ? <ItemPreview id={activeId} /> : null}</DragOverlay>
    </DndContext>
  );
}

避免

  • 直接使用 HTML5 拖放 API — 觸控/行動/無障礙支援不佳
  • 忘記鍵盤無障礙功能 — 務必加入 KeyboardSensor
  • 在拖動過程中變更狀態 — 僅在 onDragEnd 時更新
  • 缺少 DragOverlay — 沒有它,原始項目在拖動時會消失,失去視覺回饋

Individual skills in this repo

This repo contains 20 individual skills — each has its own dedicated page.

YepAPI/skills

WCAG 2.1 AA — semantic HTML, keyboard navigation, screen readers.

YepAPI/skills

Experiment setup, variant splitting, statistical significance, and feature flag integration.

YepAPI/skills

CRUD generators, data tables, user management, role-based access, and bulk operations.

YepAPI/skills

Monitor what ChatGPT and Gemini say about your brand using YepAPI.

YepAPI/skills

Queue workers with BullMQ/Inngest/Trigger.dev, job retries, dead letter queues, concurrency.

YepAPI/skills

Commander.js, interactive prompts with Clack, spinners, colors, config files, exit codes, and npm publishing.

YepAPI/skills

cmdk integration, Cmd+K trigger, fuzzy search, grouped actions, keyboard navigation, and dynamic action registration.

YepAPI/skills

Content planning with trend and sentiment data from YepAPI.

YepAPI/skills

Content calendar, keyword-driven planning, and content briefs.

YepAPI/skills

Theme switching with CSS variables, system preference detection, next-themes, and persistence.

YepAPI/skills

Threaded comments, @mentions, reactions, moderation queue, optimistic UI, and cursor pagination.

YepAPI/skills

Competitive analysis between domains using YepAPI.

YepAPI/skills

Layout grids, KPI cards, data tables with sorting/filtering, sidebar navigation, and responsive breakpoints.

YepAPI/skills

Data encryption — TLS everywhere, AES-256-GCM for PII, bcrypt passwords, encrypted backups.

YepAPI/skills

Chart patterns with Recharts/Nivo/D3, responsive containers, color scales, and accessibility.

YepAPI/skills

Drizzle schema definition, push vs migrate, type-safe queries, relations, transactions, and prepared statements.

YepAPI/skills

Upload handling, resize/compress, CDN delivery, blur placeholders, and next/image patterns.

YepAPI/skills

Local business and Google Maps research using YepAPI.

YepAPI/skills

Cursor-based pagination, IntersectionObserver, virtualized lists with @tanstack/react-virtual, and scroll position restoration.

YepAPI/skills

Chat UI components, streaming responses with AI SDK, context window management, and RAG patterns.

相關技能