question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot focus inputs inside of draggable items

See original GitHub issue

Hey @clauderic, love the project and the API. Great work.

We have an application where we are rendering layers in a sortable list that can each open a popup with some inputs. While integrating dnd-kit, I ran into an issue where I couldn’t focus any inputs in a portaled popup. I also noticed that I can still drag the popup due to the pointer events being bubbled through the synthetic tree.

We’ve dealt with similar issues in our app, and have solved it by checking if event.currentTarget.contains(event.target) before calling preventDefault and doing our behavior.

I was wondering if you’ve got a good way to solve this?

Here’s a Codesandbox with an example.

Edit. Just wanted to clarify that I am setting activationContraint: { distance: 5 } to enable click behavior.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

5reactions
ranbenacommented, Feb 23, 2021

I’m having a similar issue. My draggable items have an <input> inside, which won’t be allowed to focus on user click. @clauderic it could be great if you could add a config to disable this line (perhaps as a DndContext prop?).

In the meantime, I did this dirty hack in my code:

// make my own PointerSensor
class PointerSensorWithoutPreventDefault extends PointerSensor {
  static activators = [
    {
      eventName: "onPointerDown" as const,
      handler: ({ nativeEvent }: React.PointerEvent) => {
        if (!nativeEvent.isPrimary || nativeEvent.button !== 0) {
          return false;
        }

        // HACK: comment out prevention 🙀
        // nativeEvent.preventDefault();

        return true;
      },
    },
  ];
}

const sensors = useSensors(
  useSensor(PointerSensorWithoutPreventDefault), // 🎯
);

return (
  <DndContext sensors={sensors}>
    ...
  </DndContext>
);
0reactions
horprogscommented, Jul 1, 2021

onActivate fixed the issue when you are trying to focus, but if you are trying to select text in the input, draggable items are moving. You could try to select text in the input here (focusing works fine).

image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't focus input element inside Draggable element with click.
Click on input inside Draggable element make the input focused. Actual behaviour. Click on input element is ignored, but I can still focus...
Read more >
Draggable prevents focusing text inputs with mouse - Telerik
Hi -- there seems to be a bug with the Draggable widget, where text <input> elements inside the draggable element cannot be focused...
Read more >
HTML5 Drag not working with Chrome if there is a focused ...
We haven't found a proper fix for this so we implemented a simple workaround by setting the focus on any non-password input field...
Read more >
HTML attribute: readonly - HTML: HyperText Markup Language
The Boolean readonly attribute, when present, makes the element not mutable, meaning the user can not edit the control.
Read more >
The autofocus attribute - HTML Standard - WhatWG
All HTML elements may have the hidden content attribute set. The hidden attribute is an ... Inert nodes generally cannot be focused.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found