コンテンツにスキップ

@dnd-kit

@dnd-kit は React で使えるドラッグアンドドロップのライブラリである。

https://docs.dndkit.com

タッチスクリーンでドラッグアンドドロップが上手く動作しない

Draggablestyletouch-action: noneを設定してみる。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function Draggable(props: { children: React.ReactNode }) {
  const { attributes, listeners, setNodeRef, transform } = useDraggable({
    id: "draggable",
  });
  const style = {
    transform: transform
      ? `translate3d(${transform.x}px, ${transform.y}px, 0)`
      : undefined,
    touchAction: "none",
  };

  return (
    <button ref={setNodeRef} style={style} {...listeners} {...attributes}>
      {props.children}
    </button>
  );
}

ドラッグした要素が画面外に移動しないようにしたい

DndContextmodifiersを追加する。

Modifiers | @dnd-kit – Documentation

1
$ npm i @dnd-kit/modifiers
1
2
3
4
import { DndContext, DragEndEvent, DragStartEvent } from "@dnd-kit/core";
import { restrictToWindowEdges } from "@dnd-kit/modifiers";

return <DndContext modifiers={[restrictToWindowEdges]}>()</DndContext>;