@@ -24,11 +24,11 @@ First, let's set up the initial setup for the columns and items. We'll be creati
24
24
25
25
26
26
<CodeSandbox height = " 560" previewHeight = " 260" files = { {
27
- " / App.js" : App ,
28
- " / Column.js" : Column ,
29
- " / Item.js" : Item ,
30
- " /Styles .css" : Styles
31
- }} />
27
+ " App.js" : App ,
28
+ " Column.js" : Column ,
29
+ " Item.js" : Item ,
30
+ " styles .css" : Styles
31
+ }} showTabs />
32
32
33
33
## Adding drag and drop functionality
34
34
@@ -37,8 +37,8 @@ Now, let's add drag and drop functionality to the items. We'll be using the [use
37
37
38
38
<CodeSandbox height = " 435" previewHeight = " 260" files = { {
39
39
... files ,
40
- " / Item.js" : {code: SortableItem , active: true },
41
- }} />
40
+ " Item.js" : {code: SortableItem , active: true },
41
+ }} showTabs />
42
42
43
43
As you can see, we've added the ` useSortable ` hook to the ` Item ` component. We've also passed the ` id ` , ` index ` , ` type ` , ` accept ` , and ` group ` props to the hook.
44
44
@@ -52,8 +52,8 @@ We'll be using the [useDroppable](/react/hooks/use-droppable) hook to create a d
52
52
53
53
<CodeSandbox height = " 455" previewHeight = " 260" files = { {
54
54
... files ,
55
- " / Column.js" : {code: DroppableColumn , active: true },
56
- }} />
55
+ " Column.js" : {code: DroppableColumn , active: true },
56
+ }} showTabs />
57
57
58
58
<Note >We're setting the ` collisionPriority ` to ` CollisionPriority.Low ` to prioritize collisions of items over collisions of columns. Learn more about [ detecting collisions] ( /concepts/droppable#detecting-collisions ) .</Note >
59
59
@@ -63,9 +63,9 @@ We'll be using the [DragDropProvider](/react/components/drag-drop-provider) comp
63
63
64
64
<CodeSandbox height = " 455" previewHeight = " 260" files = { {
65
65
... files ,
66
- " / App.js" : {code: AppDroppableColumns , active: true },
67
- " / Column.js" : {code: DroppableColumn },
68
- }} />
66
+ " App.js" : {code: AppDroppableColumns , active: true },
67
+ " Column.js" : {code: DroppableColumn },
68
+ }} showTabs />
69
69
70
70
As you can see, we've added the ` DragDropProvider ` component to the ` App ` component. We've also added an ` onDragOver ` event handler to listen for drag and drop events.
71
71
@@ -81,16 +81,16 @@ Here's how you can modify the `Column` component to make it sortable:
81
81
82
82
<CodeSandbox height = " 455" previewHeight = " 260" files = { {
83
83
... files ,
84
- " / App.js" : {code: AppSortableColumns },
85
- " / Column.js" : {code: SortableColumn , active: true },
86
- }} />
84
+ " App.js" : {code: AppSortableColumns },
85
+ " Column.js" : {code: SortableColumn , active: true },
86
+ }} showTabs />
87
87
88
88
<Info >You'll also need to pass the column ` index ` prop to the ` Column ` component in the ` App ` component.</Info >
89
89
90
90
If we want to control the state of the columns in React, we can update the ` App ` component to handle the column order in the ` onDragEnd ` callback:
91
91
92
92
``` jsx App.js
93
- export function App ({style = styles} ) {
93
+ export function App () {
94
94
const [items , setItems ] = useState ({
95
95
A : [' A0' , ' A1' , ' A2' ],
96
96
B : [' B0' , ' B1' ],
@@ -144,7 +144,7 @@ For example, here is how we would update our app to handle this case for the ord
144
144
import React , {useRef , useState } from ' react' ;
145
145
import {DragDropProvider } from ' @dnd-kit/react' ;
146
146
import {move } from ' @dnd-kit/helpers' ;
147
- import " ./Styles .css" ;
147
+ import " ./styles .css" ;
148
148
149
149
import {Column } from ' ./Column' ;
150
150
import {Item } from ' ./Item' ;
@@ -203,9 +203,9 @@ export function App({style = styles}) {
203
203
<Note>Optimistic updates performed by ` @dnd- kit` are automatically reverted when a drag operation is canceled.</Note>
204
204
205
205
export const App = ` import React , {useState } from ' react' ;
206
- import {Column } from ' ./Column' ;
207
- import {Item } from ' ./Item' ;
208
- import ' ./Styles .css' ;
206
+ import {Column } from ' ./Column.js ' ;
207
+ import {Item } from ' ./Item.js ' ;
208
+ import ' ./styles .css' ;
209
209
210
210
export default function App () {
211
211
const [items ] = useState ({
@@ -230,9 +230,9 @@ export default function App() {
230
230
export const AppDroppableColumns = ` import React , {useState } from ' react' ;
231
231
import {DragDropProvider } from ' @dnd-kit/react' ;
232
232
import {move } from ' @dnd-kit/helpers' ;
233
- import {Column } from ' ./Column' ;
234
- import {Item } from ' ./Item' ;
235
- import ' ./Styles .css' ;
233
+ import {Column } from ' ./Column.js ' ;
234
+ import {Item } from ' ./Item.js ' ;
235
+ import ' ./styles .css' ;
236
236
237
237
export default function App () {
238
238
const [items , setItems ] = useState ({
@@ -263,9 +263,9 @@ export default function App() {
263
263
export const AppSortableColumns = ` import React , {useState } from ' react' ;
264
264
import {DragDropProvider } from ' @dnd-kit/react' ;
265
265
import {move } from ' @dnd-kit/helpers' ;
266
- import {Column } from ' ./Column' ;
267
- import {Item } from ' ./Item' ;
268
- import ' ./Styles .css' ;
266
+ import {Column } from ' ./Column.js ' ;
267
+ import {Item } from ' ./Item.js ' ;
268
+ import ' ./styles .css' ;
269
269
270
270
export default function App () {
271
271
const [items , setItems ] = useState ({
@@ -361,7 +361,7 @@ export const SortableItem = `import React from 'react';
361
361
import {useSortable } from ' @dnd-kit/react/sortable' ;
362
362
363
363
export function Item ({id, index, column}) {
364
- const {ref } = useSortable ({
364
+ const {ref , isDragSource } = useSortable ({
365
365
id,
366
366
index,
367
367
type: ' item' ,
@@ -370,17 +370,13 @@ export function Item({id, index, column}) {
370
370
});
371
371
372
372
return (
373
- < button className= " Item" ref= {ref}>
373
+ < button className= " Item" ref= {ref} data - dragging = {isDragSource} >
374
374
{id}
375
375
< / button>
376
376
);
377
377
}` ;
378
378
379
- export const Styles = (() => `
380
- html {
381
- background- color: ${typeof document != ' undefined' && document .documentElement .classList .contains (' dark' ) ? ' #11131b' : ' #fafafd' };
382
- }
383
-
379
+ export const Styles = `
384
380
.Root {
385
381
display: flex;
386
382
flex- direction: row;
@@ -408,19 +404,19 @@ html {
408
404
border: none;
409
405
border- radius: 5px ;
410
406
cursor: grab;
411
- transition: transform 0 .3s ease- in , box- shadow 0 .3s ease- in ;
407
+ transition: transform 0 .2s ease, box- shadow 0 .2s ease;
412
408
transform: scale (1 );
413
409
box- shadow: inset 0px 0px 1px rgba (0 ,0 ,0 ,0.4 ), 0 0 0 calc (1px / var (-- scale- x, 1 )) rgba (63 , 63 , 68 , 0.05 ), 0px 1px calc (2px / var (-- scale- x, 1 )) 0 rgba (34 , 33 , 81 , 0.05 );
414
410
}
415
411
416
- .Item [data- dnd - dragging= " true" ] {
412
+ .Item [data- dragging= " true" ] {
417
413
transform: scale (1.02 );
418
414
box- shadow: inset 0px 0px 1px rgba (0 ,0 ,0 ,0.5 ), - 1px 0 15px 0 rgba (34 , 33 , 81 , 0.01 ), 0px 15px 15px 0 rgba (34 , 33 , 81 , 0.25 )
419
- }` )() ;
415
+ }` ;
420
416
421
417
export const files = {
422
- "/ Item.js": {code: SortableItem, hidden: true},
423
- "/ App.js": {code: App, hidden: true},
424
- "/ Column.js": {code: Column, hidden: true},
425
- "/Styles .css": {code: Styles, hidden: true}
418
+ "Item.js": {code: SortableItem, hidden: true},
419
+ "App.js": {code: App, hidden: true},
420
+ "Column.js": {code: Column, hidden: true},
421
+ "styles .css": {code: Styles, hidden: true}
426
422
};
0 commit comments