Add prevent select helper to stop highlighting UI elements on drag with touch devices

This commit is contained in:
Mitchell McCaffrey 2021-06-09 23:25:28 +10:00
parent 8b0ee7075d
commit 21986231fa
4 changed files with 48 additions and 6 deletions

View File

@ -5,7 +5,8 @@ import SimpleBar from "simplebar-react";
import { import {
DragOverlay, DragOverlay,
DndContext, DndContext,
PointerSensor, MouseSensor,
TouchSensor,
KeyboardSensor, KeyboardSensor,
useSensor, useSensor,
useSensors, useSensors,
@ -18,6 +19,7 @@ import SelectTokensButton from "./SelectTokensButton";
import Draggable from "../drag/Draggable"; import Draggable from "../drag/Draggable";
import useSetting from "../../hooks/useSetting"; import useSetting from "../../hooks/useSetting";
import usePreventSelect from "../../hooks/usePreventSelect";
import { useTokenData } from "../../contexts/TokenDataContext"; import { useTokenData } from "../../contexts/TokenDataContext";
import { useAuth } from "../../contexts/AuthContext"; import { useAuth } from "../../contexts/AuthContext";
@ -43,14 +45,20 @@ function TokenBar({ onMapTokensStateCreate }) {
// https://github.com/clauderic/dnd-kit/issues/238 // https://github.com/clauderic/dnd-kit/issues/238
const dragOverlayRef = useRef(); const dragOverlayRef = useRef();
const pointerSensor = useSensor(PointerSensor, { const mouseSensor = useSensor(MouseSensor, {
activationConstraint: { distance: 5 },
});
const touchSensor = useSensor(TouchSensor, {
activationConstraint: { distance: 5 }, activationConstraint: { distance: 5 },
}); });
const keyboardSensor = useSensor(KeyboardSensor); const keyboardSensor = useSensor(KeyboardSensor);
const sensors = useSensors(pointerSensor, keyboardSensor); const sensors = useSensors(mouseSensor, touchSensor, keyboardSensor);
const [preventSelect, resumeSelect] = usePreventSelect();
function handleDragStart({ active }) { function handleDragStart({ active }) {
setDragId(active.id); setDragId(active.id);
preventSelect();
} }
function handleDragEnd({ active }) { function handleDragEnd({ active }) {
@ -93,10 +101,13 @@ function TokenBar({ onMapTokensStateCreate }) {
} }
} }
} }
resumeSelect();
} }
function handleDragCancel() { function handleDragCancel() {
setDragId(null); setDragId(null);
resumeSelect();
} }
function renderToken(group, draggable = true) { function renderToken(group, draggable = true) {

View File

@ -1,7 +1,8 @@
import React, { useState, useContext } from "react"; import React, { useState, useContext } from "react";
import { import {
DndContext, DndContext,
PointerSensor, MouseSensor,
TouchSensor,
KeyboardSensor, KeyboardSensor,
useSensor, useSensor,
useSensors, useSensors,
@ -12,6 +13,8 @@ import { useGroup } from "./GroupContext";
import { moveGroupsInto, moveGroups, ungroup } from "../helpers/group"; import { moveGroupsInto, moveGroups, ungroup } from "../helpers/group";
import usePreventSelect from "../hooks/usePreventSelect";
const TileDragContext = React.createContext(); const TileDragContext = React.createContext();
export const BASE_SORTABLE_ID = "__base__"; export const BASE_SORTABLE_ID = "__base__";
@ -56,17 +59,22 @@ export function TileDragProvider({
filter, filter,
} = useGroup(); } = useGroup();
const pointerSensor = useSensor(PointerSensor, { const mouseSensor = useSensor(MouseSensor, {
activationConstraint: { delay: 250, tolerance: 5 },
});
const touchSensor = useSensor(TouchSensor, {
activationConstraint: { delay: 250, tolerance: 5 }, activationConstraint: { delay: 250, tolerance: 5 },
}); });
const keyboardSensor = useSensor(KeyboardSensor); const keyboardSensor = useSensor(KeyboardSensor);
const sensors = useSensors(pointerSensor, keyboardSensor); const sensors = useSensors(mouseSensor, touchSensor, keyboardSensor);
const [dragId, setDragId] = useState(); const [dragId, setDragId] = useState();
const [overId, setOverId] = useState(); const [overId, setOverId] = useState();
const [dragCursor, setDragCursor] = useState("pointer"); const [dragCursor, setDragCursor] = useState("pointer");
const [preventSelect, resumeSelect] = usePreventSelect();
function handleDragStart(event) { function handleDragStart(event) {
const { active, over } = event; const { active, over } = event;
setDragId(active.id); setDragId(active.id);
@ -77,6 +85,8 @@ export function TileDragProvider({
setDragCursor("grabbing"); setDragCursor("grabbing");
onDragStart && onDragStart(event); onDragStart && onDragStart(event);
preventSelect();
} }
function handleDragOver(event) { function handleDragOver(event) {
@ -146,6 +156,8 @@ export function TileDragProvider({
} }
} }
resumeSelect();
onDragEnd && onDragEnd(event); onDragEnd && onDragEnd(event);
} }
@ -154,6 +166,8 @@ export function TileDragProvider({
setOverId(); setOverId();
setDragCursor("pointer"); setDragCursor("pointer");
resumeSelect();
onDragCancel && onDragCancel(event); onDragCancel && onDragCancel(event);
} }

View File

@ -0,0 +1,13 @@
function usePreventSelect() {
function preventSelect() {
document.body.classList.add("no-select");
}
function resumeSelect() {
document.body.classList.remove("no-select");
}
return [preventSelect, resumeSelect];
}
export default usePreventSelect;

View File

@ -13,3 +13,7 @@ html {
input[type="checkbox"]:disabled ~ svg { input[type="checkbox"]:disabled ~ svg {
opacity: 0.1; opacity: 0.1;
} }
.no-select div {
user-select: none;
}