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

Cancel drag with escape key using useDragDropManager? #3558

Open
stevenkkim opened this issue Apr 9, 2023 · 0 comments
Open

Cancel drag with escape key using useDragDropManager? #3558

stevenkkim opened this issue Apr 9, 2023 · 0 comments

Comments

@stevenkkim
Copy link

I'm using the touch backend because the html5 backend has ugly cursors which cannot be changed (see: #325) and I'm trying to use the escape key to cancel a drag operation (which was discussed here but never resolved: #600).

I just discovered useDragDropManager but the documentation is a bit sparse (https://react-dnd.github.io/react-dnd/docs/api/use-drag-drop-manager)

Would useDragDropManager be an appropriate way to implement this? Here is my code, which is a component with a mousetrap listener for the escape key that calls dndManager.getActions().endDrag() .

import { useDragDropManager } from 'react-dnd';
import { useMousetrap } from '../components/shortcuts/useMousetrap';

export function EscapeToCancelDrag() {
  const dndManager = useDragDropManager()
  useMousetrap('esc', () => {
    if (dndManager.getMonitor().isDragging()) {
      dndManager.getActions().endDrag();
    }
  });
  return null;
}

It seems to work, but not sure whether this is an appropriate use of useDragDropManager, if there are problems/risks/drawbacks to this approach, or if there are simply better ways to do this. Feedback is appreciated. Thanks!

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

1 participant