dnd-kit-implementation
dnd-kit Implementation Guide
Overview
This skill provides patterns for implementing drag-and-drop functionality using dnd-kit library that supports both sortable containers and droppable targets simultaneously.
Core Concept
The key to combining useSortable and useDroppable is conditional logic based on drag context:
- When dragging items → containers act as drop-only targets
- When dragging containers → containers act as sortable elements
This is achieved by detecting what's currently being dragged and enabling only the appropriate functionality.
Implementation Pattern
Container Component Structure
const SortableDroppableContainer = ({ container }) => {
// useSortable for container reordering
const {
attributes,
listeners,
setNodeRef: setSortableRef,
transform,
transition,
isDragging,
} = useSortable({ id: container.id });
// useDroppable for receiving items
const { setNodeRef: setDroppableRef, isOver } = useDroppable({
id: container.id,
});
// Active element from context
const { active } = useDndContext();
// Determine behavior based on what's being dragged
const isItem = active?.id?.toString().startsWith('item-');
const isContainer = active?.id?.toString().startsWith('container-');
// Apply conditional refs
const setNodeRef = isItem ? setDroppableRef : setSortableRef;
return (
<div ref={setNodeRef} {...(isContainer ? attributes : {})} style={style}>
<div {...(isContainer ? listeners : {})}>{/* Drag handle */}</div>
{/* Container content */}
</div>
);
};
Key Implementation Points
- Dual Hook Usage: Use both
useSortableanduseDroppablein the same component - Context Detection: Use
useDndContext()to check what's being dragged - Conditional Refs: Apply the appropriate ref based on drag state
- ID Naming Convention: Use prefixes like
container-*anditem-*to distinguish types
ID Naming Convention
// Containers
const containerId = `container-${id}`;
// Items
const itemId = `item-${id}`;
This convention makes conditional logic clean and maintainable.
State Management Critical Point
When updating container items, always create new objects:
// ❌ Won't trigger re-render
containers.items.push(newItem);
// ✅ Correct - creates new reference
setContainers(prev => prev.map(c =>
c.id === targetId
? { ...c, items: [...c.items, newItem] }
: c
));
Common Use Cases
- Kanban Boards: Reorder columns and move cards between columns
- File Management: Reorganize folders and move files into folders
- Playlist Editors: Reorder playlists and add songs to playlists
- Task Managers: Reorder task lists and move tasks between lists
References
For detailed implementation examples and code patterns, see:
- implementation-patterns.md - Complete component examples with TypeScript
- state-management.md - State update patterns and common pitfalls
More from atman-33/skills
tech-article-humanizer
Transform technical article drafts or source materials into human-like, high-quality Japanese technical articles. Use this skill when the user wants to generate, rewrite, or humanize technical articles (especially about TypeScript, JavaScript, React, or frontend topics) following specific human-writing patterns and style guidelines. Triggers include requests like "記事を人間風に", "tech article を生成", "humanize this article", or providing article source materials.
4agent-memory
Use this skill when the user asks to save, remember, recall, or organize memories. Triggers on phrases like 'remember this', 'save this', 'note this', 'what did we discuss about...', 'check your notes', 'clean up memories'. Also use proactively when discovering valuable findings worth preserving.
2react-router-v7-app
Implements React Router v7 app structure, routing patterns, and component templates. Use when creating or modifying React Router v7 applications to ensure consistent folder structure, data loading patterns, and component architecture.
2pr-assistant
Analyzes git changes and assists with creating comprehensive pull requests. Use when user wants to create a PR, review changes before PR, or needs help drafting PR descriptions. Triggers on phrases like 'create PR', 'make a pull request', 'draft PR description', 'what changed in this branch', 'prepare PR'.
1openspec-bulk-archive-change
Archive multiple completed changes at once. Use when archiving several parallel changes.
1serena-skills
Standalone Serena MCP tools for code intelligence - symbol search, file ops, memory, editing, config, workflow helpers, and shell execution without MCP server
1