@@ -101,6 +101,7 @@ type SortableProps<T> = DndContextProps & {
101
101
collisionDetection ?: DndContextProps [ "collisionDetection" ] ;
102
102
modifiers ?: DndContextProps [ "modifiers" ] ;
103
103
sensors ?: DndContextProps [ "sensors" ] ;
104
+ strategy ?: SortableContextProps [ "strategy" ] ;
104
105
orientation ?: "vertical" | "horizontal" | "mixed" ;
105
106
flatCursor ?: boolean ;
106
107
} & ( T extends object
@@ -115,6 +116,7 @@ function Sortable<T>(props: SortableProps<T>) {
115
116
collisionDetection,
116
117
modifiers,
117
118
sensors : sensorsProp ,
119
+ strategy,
118
120
onMove,
119
121
orientation = "vertical" ,
120
122
flatCursor = false ,
@@ -152,7 +154,7 @@ function Sortable<T>(props: SortableProps<T>) {
152
154
id,
153
155
items : value ,
154
156
modifiers : modifiers ?? config . modifiers ,
155
- strategy : config . strategy ,
157
+ strategy : strategy ?? config . strategy ,
156
158
activeId,
157
159
setActiveId,
158
160
getItemValue,
@@ -162,6 +164,7 @@ function Sortable<T>(props: SortableProps<T>) {
162
164
id ,
163
165
value ,
164
166
modifiers ,
167
+ strategy ,
165
168
config . modifiers ,
166
169
config . strategy ,
167
170
activeId ,
@@ -256,12 +259,14 @@ const SortableContent = React.forwardRef<HTMLDivElement, SortableContentProps>(
256
259
257
260
const ContentSlot = asChild ? Slot : "div" ;
258
261
262
+ const items = React . useMemo ( ( ) => {
263
+ return context . items . map ( ( item ) => context . getItemValue ( item ) ) ;
264
+ } , [ context . items , context . getItemValue ] ) ;
265
+
259
266
return (
260
267
< SortableContentContext . Provider value = { true } >
261
268
< SortableContext
262
- items = { context . items . map ( ( item ) => ( {
263
- id : context . getItemValue ( item ) ,
264
- } ) ) }
269
+ items = { items }
265
270
strategy = { strategyProp ?? context . strategy }
266
271
>
267
272
< ContentSlot { ...contentProps } ref = { forwardedRef } />
@@ -367,37 +372,34 @@ const SortableItem = React.forwardRef<HTMLDivElement, SortableItemProps>(
367
372
const { value, style, asGrip, asChild, className, ...itemProps } = props ;
368
373
const context = useSortableContext ( "item" ) ;
369
374
const id = React . useId ( ) ;
370
- const sortableContext = useSortable ( { id : value } ) ;
375
+ const {
376
+ attributes,
377
+ listeners,
378
+ setNodeRef,
379
+ transform,
380
+ transition,
381
+ isDragging,
382
+ } = useSortable ( { id : value } ) ;
371
383
372
384
const composedStyle = React . useMemo < React . CSSProperties > ( ( ) => {
373
385
return {
374
- opacity : sortableContext . isDragging ? 0.5 : 1 ,
375
- transform : CSS . Translate . toString ( sortableContext . transform ) ,
376
- transition : sortableContext . transition ,
386
+ opacity : isDragging ? 0.5 : 1 ,
387
+ transform : CSS . Translate . toString ( transform ) ,
388
+ transition,
377
389
...style ,
378
390
} ;
379
- } , [
380
- sortableContext . isDragging ,
381
- sortableContext . transform ,
382
- sortableContext . transition ,
383
- style ,
384
- ] ) ;
391
+ } , [ isDragging , transform , transition , style ] ) ;
385
392
386
393
const ItemSlot = asChild ? Slot : "div" ;
387
394
388
395
const itemContext = React . useMemo < SortableItemContextValue > (
389
396
( ) => ( {
390
397
id,
391
- attributes : sortableContext . attributes ,
392
- listeners : sortableContext . listeners ,
393
- isDragging : sortableContext . isDragging ,
398
+ attributes,
399
+ listeners,
400
+ isDragging,
394
401
} ) ,
395
- [
396
- id ,
397
- sortableContext . attributes ,
398
- sortableContext . listeners ,
399
- sortableContext . isDragging ,
400
- ] ,
402
+ [ id , attributes , listeners , isDragging ] ,
401
403
) ;
402
404
403
405
if ( value === "" ) {
@@ -408,25 +410,22 @@ const SortableItem = React.forwardRef<HTMLDivElement, SortableItemProps>(
408
410
< SortableItemContext . Provider value = { itemContext } >
409
411
< ItemSlot
410
412
id = { id }
411
- data-dragging = { sortableContext . isDragging ? "" : undefined }
413
+ data-dragging = { isDragging ? "" : undefined }
412
414
{ ...itemProps }
413
- ref = { composeRefs ( forwardedRef , ( node ) =>
414
- sortableContext . setNodeRef ( node ) ,
415
- ) }
415
+ ref = { composeRefs ( forwardedRef , ( node ) => setNodeRef ( node ) ) }
416
416
style = { composedStyle }
417
417
className = { cn (
418
418
"data-[dragging]:focus-visible:outline-none data-[dragging]:focus-visible:ring-1 data-[dragging]:focus-visible:ring-ring data-[dragging]:focus-visible:ring-offset-1" ,
419
419
{
420
420
"touch-none select-none" : asGrip ,
421
421
"cursor-default" : context . flatCursor ,
422
422
"data-[dragging]:cursor-grabbing" : ! context . flatCursor ,
423
- "cursor-grab" :
424
- ! sortableContext . isDragging && asGrip && ! context . flatCursor ,
423
+ "cursor-grab" : ! isDragging && asGrip && ! context . flatCursor ,
425
424
} ,
426
425
className ,
427
426
) }
428
- { ...( asGrip ? sortableContext . attributes : { } ) }
429
- { ...( asGrip ? sortableContext . listeners : { } ) }
427
+ { ...( asGrip ? attributes : { } ) }
428
+ { ...( asGrip ? listeners : { } ) }
430
429
/>
431
430
</ SortableItemContext . Provider >
432
431
) ;
0 commit comments