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
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.3k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
60
Ruling the World: When Life Gets Gamed
codingconduct
0
130
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
9.5k
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.1k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
0
250
Between Models and Reality
mayunak
1
180
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
How Software Deployment tools have changed in the past 20 years
geshan
0
31k
How to train your dragon (web standard)
notwaldorf
97
6.5k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
55
49k
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