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

Support variable sized sortables

Open martinpengellyphillips opened this issue 3 years ago • 5 comments

The current (0.6.0) Sortable algorithm requires items to be the same dimensions for correct visual sorting. Otherwise, gaps and overlays occur. The previous algorithm attempted to handle this (for specific vertical orientation), but still had issues (e.g. #9) .

Try a holistic approach to this instead.

martinpengellyphillips avatar May 02 '22 08:05 martinpengellyphillips

For reference, this was the previous 'vertical' algorithm:

  const sortedTransform = (): Transform => {
    const delta = noopTransform();
    const resolvedInitialIndex = initialIndex();
    const resolvedCurrentIndex = currentIndex();

    if (
      !anyDraggableActive() ||
      resolvedCurrentIndex === resolvedInitialIndex
    ) {
      return delta;
    }

    const draggableId = dndState.active.draggable!;
    const draggableInitialIndex = sortableState.initialIds.indexOf(draggableId);
    const draggableLayout = layoutById(draggableId)!;

    if (draggable.isActiveDraggable) {
      const droppableId = dndState.active.droppable!;
      const droppableLayout = layoutById(droppableId)!;
      if (resolvedCurrentIndex > resolvedInitialIndex) {
        delta.y = droppableLayout.bottom - draggableLayout.bottom;
      } else {
        delta.y = droppableLayout.top - draggableLayout.top;
      }
    } else {
      if (resolvedCurrentIndex > resolvedInitialIndex) {
        const leadingId = sortableState.initialIds[draggableInitialIndex - 1];
        const leadingLayout = layoutById(leadingId)!;
        const leadingGap = draggableLayout.top - leadingLayout.bottom;
        delta.y += draggableLayout.height + leadingGap;
      } else {
        const trailingId = sortableState.initialIds[draggableInitialIndex + 1];
        const trailingLayout = layoutById(trailingId)!;
        const trailingGap = trailingLayout.top - draggableLayout.bottom;
        delta.y -= draggableLayout.height + trailingGap;
      }
    }

    return delta;
  };

martinpengellyphillips avatar May 02 '22 15:05 martinpengellyphillips

Very much looking forward to this!

Currently, it's the only thing that seems to be not working for me. Here is how dragging looks for the variable-sized sortables with the flex flex-row items-center gap-1 Tailwind styles for the container.

https://user-images.githubusercontent.com/2759152/192066488-3ba9f74f-cbc4-43f9-b3c8-72f0d593b4b4.mov

alllex avatar Sep 23 '22 22:09 alllex

Same here. Everything else is working great, but this makes things look quite ugly at times. Haven't looked into this much, but could we steal some logic from here https://master--5fc05e08a4a65d0021ae0bf2.chromatic.com/?path=/story/presets-sortable-vertical--variable-heights maybe?

Would love to see this "fixed" soon. Thank you for making and maintaining this library.

JoleMile avatar Nov 12 '22 12:11 JoleMile

Thanks for the kind words.

The logic you see in dnd-kit is similar to what I had before (see the example code at the top of this issue). However, I encountered some issues with it and will rethink the approach. Hopefully the overall improvements to the library I've made since will make this easier to solve :)

martinpengellyphillips avatar Nov 19 '22 17:11 martinpengellyphillips

Hey. Lib is wonderful, but this issue is the first thing that catches your eye. Has anyone found any solution?

cheechqt avatar Jul 20 '23 12:07 cheechqt