react-beautiful-dnd#FluidDragActions TypeScript Examples

The following examples show how to use react-beautiful-dnd#FluidDragActions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useAutoMoveSensor.ts    From wikitrivia with MIT License 6 votes vote down vote up
function moveStepByStep(
  drag: FluidDragActions,
  transformValues: number[],
  scrollValues: number[]
) {
  requestAnimationFrame(() => {
    const bottom = document.getElementById("bottom");

    if (bottom === null) {
      throw new Error("Can't find #bottom");
    }

    const newPosition = transformValues.shift();
    const newScroll = scrollValues.shift();

    if (newPosition === undefined || newScroll === undefined) {
      drag.drop();
    } else {
      bottom.scrollLeft = newScroll;
      drag.move({ x: newPosition, y: 0 });
      moveStepByStep(drag, transformValues, scrollValues);
    }
  });
}
Example #2
Source File: dndUtil.ts    From clearflask with Apache License 2.0 5 votes vote down vote up
dndDrag = async (api: SensorAPI, draggableId: string, droppableId: string): Promise<boolean> => {
  if (windowIso.isSsr) return false;

  var preDrag: PreDragActions | null | undefined;
  var drag: FluidDragActions | undefined;
  try {
    preDrag = api.tryGetLock(draggableId);
    if (!preDrag) return false;

    const draggableEl = dndFindElement('draggable', draggableId);
    const droppableEl = dndFindElement('droppable', droppableId);
    if (!draggableEl || !droppableEl) {
      preDrag.abort();
      return false;
    }

    const draggablePosition = draggableEl.getBoundingClientRect();
    const droppablePosition = droppableEl.getBoundingClientRect();

    const from = {
      x: draggablePosition.x + draggablePosition.width / 2,
      y: draggablePosition.y + draggablePosition.height / 2,
    }
    const to = {
      x: droppablePosition.x + droppablePosition.width / 2,
      y: droppablePosition.y + droppablePosition.height / 2,
    }

    drag = preDrag.fluidLift(from);
    var step = 0.0;
    while (step <= 1) {
      step += 0.05;

      await new Promise(resolve => setTimeout(resolve, 5));
      if (!drag.isActive()) {
        drag.cancel();
        return false;
      }

      drag.move({
        x: from.x + (to.x - from.x) * step,
        y: from.y + (to.y - from.y) * step,
      });
    }

    drag.move({
      x: droppablePosition.x + droppablePosition.width / 2 - (draggablePosition.x + draggablePosition.width / 2),
      y: droppablePosition.y + droppablePosition.height / 2 - (draggablePosition.y + draggablePosition.height / 2),
    });

    drag.drop();

    return true;

  } catch (e) {
    if (drag?.isActive()) drag.cancel();
    if (preDrag?.isActive()) preDrag.abort();

    return false;
  }
}