Configs2026年7月23日·1 分钟阅读

Dragula — Drag and Drop So Simple It Hurts

A framework-agnostic JavaScript library that provides simple, no-configuration drag-and-drop for DOM elements.

Agent 就绪

先审查再安装

这个资产需要先审查。复制的指令会要求 Agent dry-run、列出写入项,确认后再继续。

Needs Confirmation · 66/100策略:需确认
Agent 入口
任意 MCP/CLI Agent
类型
Skill
安装
Single
信任
信任等级:Established
入口
Dragula Overview
先审查命令
npx -y tokrepo@latest install 7d81ac9d-862d-11f1-9bc6-00163e2b0d79 --target codex

先 dry-run,确认写入项后再运行此命令。

Introduction

Dragula provides drag-and-drop functionality with zero configuration. Pass it an array of container elements, and items inside them become draggable and reorderable across containers immediately. No data attributes, no class names, no complex setup.

What Dragula Does

  • Enables drag-and-drop between multiple containers with one function call
  • Reorders items within a single container or moves them across containers
  • Provides visual feedback with a mirror element that follows the cursor
  • Emits events for drag, drop, cancel, remove, and shadow positioning
  • Works with any DOM structure without requiring specific markup

Architecture Overview

Dragula listens for mousedown and touchstart events on registered containers. When a drag starts, it creates a mirror element (a visual copy) that tracks the pointer. As the pointer moves over valid containers, Dragula calculates the insertion point using element geometry. On release, the original element is moved in the DOM to the drop position. The entire operation is DOM-based with no virtual model.

Self-Hosting & Configuration

  • Install via npm or include the standalone script and CSS
  • Call dragula(containers, options) to initialize
  • Set moves callback to control which items are draggable
  • Set accepts callback to restrict which containers accept drops
  • Use copy: true to duplicate items instead of moving them

Key Features

  • Zero-configuration default that works out of the box
  • Framework-agnostic: works with vanilla JS, React, Vue, Angular, or any DOM
  • Touch device support without additional plugins
  • Spilling: configurable behavior when items are dropped outside containers
  • Lightweight at ~4 KB gzipped with no dependencies

Comparison with Similar Tools

  • SortableJS — more features (multi-drag, nested groups, animations) but larger; Dragula favors simplicity
  • react-beautiful-dnd — React-specific with accessibility focus; Dragula is framework-agnostic
  • dnd-kit — modern React toolkit with sensors and modifiers; heavier abstraction than Dragula
  • HTML5 Drag and Drop API — native but inconsistent across browsers and limited on mobile

FAQ

Q: Can I restrict which items are draggable? A: Yes. Pass a moves(el, source, handle, sibling) function that returns true for draggable items.

Q: Does Dragula work with React? A: Yes. Initialize in a useEffect hook, passing ref containers. Clean up with drake.destroy() on unmount.

Q: Can I animate the drop? A: Dragula does not include animation. Apply CSS transitions on the container children for smooth reordering.

Q: How do I handle drop events? A: Listen with drake.on('drop', (el, target, source, sibling) => { ... }) to update your data model.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产