dnd-kit icon indicating copy to clipboard operation
dnd-kit copied to clipboard

Scroll position reset across all items, with useSortable, when dropping item

Open getnashty opened this issue 2 years ago • 1 comments

I have a horizontal list of scrollable children, but when I drop any of the items, the scroll position resets on all of them. Any ideas for how I can resolve this?

CleanShot 2023-05-01 at 12 36 23

this is the item code:

    const SortableTest = React.memo(({ id, width }: { id: string; width: string }) => {
        const { attributes, listeners, setNodeRef, transform, transition } = useSortable({ id });

        return (
            <div
                {...attributes}
                {...listeners}
                ref={setNodeRef}
                className="overflow-auto height-full"
                style={{
                    width,
                    transform: CSS.Translate.toString(transform),
                    transition,
                }}
            >
                {LIPSUM}
            </div>
        );
    });

    SortableTest.displayName = 'SortableTest';

and the container:

                        <DndContext
                            onDragEnd={onDragEnd}
                            modifiers={[restrictToHorizontalAxis, restrictToFirstScrollableAncestor]}
                        >
                            <div
                                className={classNames('flex flex-1 overflow-x-auto')}
                            >
                                <SortableContext items={items} strategy={horizontalListSortingStrategy}>
                                    {items.map((id) => {
                                        return <SortableTest key={id} id={id} width={'368px'} />;
                                    })}
                                </SortableContext>
                            </div>
                        </DndContext>

getnashty avatar May 01 '23 16:05 getnashty

useLayoutEffect custom hook for each sortable that stores the scroll position. Simple.

ehrro avatar Nov 25 '23 00:11 ehrro