Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Drag & Drop APIのDataTransferを使ってみる
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
mame_daifuku
February 20, 2025
0
2
Drag & Drop APIのDataTransferを使ってみる
サンプルアプリ:
https://dnd-sample-two.vercel.app/
リポジトリ:
https://github.com/hiroshi-kato/dnd-sample
mame_daifuku
February 20, 2025
Tweet
Share
Featured
See All Featured
Unsuck your backbone
ammeep
672
58k
Faster Mobile Websites
deanohume
310
31k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.3k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.3k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
140
The Mindset for Success: Future Career Progression
greggifford
PRO
0
270
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
2.8k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
380
How GitHub (no longer) Works
holman
316
140k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.1k
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
74
Transcript
Drag & Drop APIのDataTransferを使ってみる 発表者: Saul
1. 動機 react-dndって何してるんだろう const [{ opacity }, dragRef] = useDrag(
() => ({ type: ItemTypes.CARD, item: { text }, collect: (monitor) => ({ opacity: monitor.isDragging() ? 0.5 : 1 }) }),[]) const [, drop] = useDrop( () => ({ accept: ItemTypes.KNIGHT, drop: () => moveKnight(x, y) }),[x, y])
2. Drag & Drop APIおさらい
D&Dの歴史 古代: JavaScriptで独自実装されていた 現代(2014年): HTML5のAPIとして標準化 仕様書はこちら
属性 draggable <p id="p1" draggable="true">この要素はドラッグできます。</p>
イベント
インターフェース
3. dataTransfer に触れてみる ケース1:ドロップできるアイテムを制限する ケース2:ドラッグ中のアイテムを変更する サンプルアプリ
ケース1:ドロップできるアイテムを制限する // ドラッグ開始時に Todo の情報を dataTransfer にセットする const handleDragStart =
(e: DragEvent<HTMLLIElement>, todo: Todo) => { e.dataTransfer.setData('application/json', JSON.stringify(todo)); e.dataTransfer.effectAllowed = 'move'; }; // ドロップ時に、対象リストと Todo の list プロパティが一致するかチェック const handleDrop = (e: DragEvent<HTMLDivElement>, targetList: 'A' | 'B') => { e.preventDefault(); const data = e.dataTransfer.getData('application/json'); if (data) { const todo: Todo = JSON.parse(data); if (todo.list === targetList) { if (targetList === 'A') { setDroppedA(prev => [...prev, todo]); } else { setDroppedB(prev => [...prev, todo]); } } else { alert(`この Todo アイテムは ${todo.list} 用です。こちらのリストにはドロップできません。`); } } };
ケース2:ドラッグ中のアイテムを変更する const handleDragStart = (e: DragEvent<HTMLLIElement>, todo: Todo) => {
e.dataTransfer.setData('application/json', JSON.stringify(todo)); e.dataTransfer.effectAllowed = 'move'; // カスタムのドラッグイメージを作成 const dragImage = document.createElement('div'); dragImage.className = 'p-2 bg-black text-white rounded text-sm w-32 text-center'; dragImage.innerText = `Dragging: ${todo.text}`; // 一時的にドキュメントに追加してドラッグイメージに設定 document.body.appendChild(dragImage); e.dataTransfer.setDragImage(dragImage, dragImage.offsetWidth / 2, dragImage.offsetHeight / 2); // ドラッグ開始直後に削除 setTimeout(() => document.body.removeChild(dragImage), 0); };
まとめ react-dndって何してるんだろう わかる気がする!!! const [{ opacity }, dragRef] = useDrag(
() => ({ type: ItemTypes.CARD, item: { text }, collect: (monitor) => ({ opacity: monitor.isDragging() ? 0.5 : 1 }) }),[]) const [, drop] = useDrop( () => ({ accept: ItemTypes.KNIGHT, drop: () => moveKnight(x, y) }),[x, y])
おしまい
参考文献 MDN Web Docs: Drag and Drop API W3C: HTML5
仕様書 react-dnd