Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support variable sized sortables #32

Open
martinpengellyphillips opened this issue May 2, 2022 · 5 comments
Open

Support variable sized sortables #32

martinpengellyphillips opened this issue May 2, 2022 · 5 comments

Comments

@martinpengellyphillips
Copy link
Contributor

martinpengellyphillips commented May 2, 2022

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
Copy link
Contributor Author

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;
  };

@alllex
Copy link
Contributor

alllex commented Sep 23, 2022

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.

Screen.Recording.2022-09-24.at.00.45.30.mov

@JoleMile
Copy link

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.

@martinpengellyphillips
Copy link
Contributor Author

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 :)

@cheechgodx
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants