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

Draggable does not work inside block editor #12635

Open
espenjanson opened this issue Dec 6, 2018 · 11 comments
Open

Draggable does not work inside block editor #12635

espenjanson opened this issue Dec 6, 2018 · 11 comments
Labels
[Feature] Drag and Drop Drag and drop functionality when working with blocks [Priority] Low Used to indicate that the issue at hand isn't a top priority to address and can be handled later [Type] Bug An existing feature does not function as intended

Comments

@espenjanson
Copy link

espenjanson commented Dec 6, 2018

Describe the bug
Draggable prop does not work on elements within the editor block, ex: I am build a hero-block where the user should be able to move the text around to his / her pleasing. Nothing happens when I try to drag.

To Reproduce
Use the draggable prop on any element in a project that is not Gutenberg-based and you will be able to drag that element around. Do the same thing in a Gutenberg block editor and it will not work.

Expected behavior
Should be able to drag content around inside Gutenberg block editor.

@designsimply
Copy link
Member

Would it be possible for you to provide some sample code for testing with?

@designsimply designsimply added the Needs Technical Feedback Needs testing from a developer perspective. label Dec 6, 2018
@espenjanson
Copy link
Author

espenjanson commented Dec 6, 2018

Absolutely!

In newly started project with create-react-app:

class App extends Component {
  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />

          // This <p> is not draggable by default, but now drags as expected

          <p draggable>
            Edit <code>src/App.js</code> and save to reload.
          </p>
          <a
            className="App-link"
            href="https://reactjs.org"
            target="_blank"
            rel="noopener noreferrer"
          >
            Learn React
          </a>
        </header>
      </div>
    );
  }
}

Drags as expected (I have no handler for the drag but the items is draggable).

In my gutenberg block:

export default registerBlockType("mona/monas-draggable", {
  attributes: {
    blockAlignment: {
      type: "string",
      default: "wide"
    }
  },
  category: "common",
  getEditWrapperProps({ blockAlignment }) {
    return { "data-align": blockAlignment };
  },
  icon: "slides",
  title: "Mona's draggable",
  edit() {
    return (
      <div style={{ backgroundColor: "yellow", height: "300px" }}>

       // This p should be draggable - it is not

        <p draggable>hello from me</p>
      </div>
    );
  },
  save() {
    return null;
  }
});

@espenjanson
Copy link
Author

espenjanson commented Dec 6, 2018

I think we are talking about different things here. The draggable prop is applicable to any element in JSX (and plain HTML as draggable="true" since it is a feature of HTML5) and just makes the item draggable, so it should work in all cases. Anyway, for what it's worth I already tried the Draggable component and it does not work either. Did you get it to work with the Draggable component?

See https://www.w3schools.com/tags/att_global_draggable.asp

@youknowriad youknowriad added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Feb 6, 2019
@gziolo gziolo added the [Feature] Drag and Drop Drag and drop functionality when working with blocks label May 24, 2019
@youknowriad youknowriad added [Priority] Low Used to indicate that the issue at hand isn't a top priority to address and can be handled later and removed Needs Technical Feedback Needs testing from a developer perspective. [Type] Help Request Help with setup, implementation, or "How do I?" questions. labels Jun 13, 2019
@youknowriad
Copy link
Contributor

I think the drag events are probably prevented in the upper tree (block wrapper). That said, I feel you should be able to attach a custom onDragStart/onDragEnd events like the Draggable component does in order to make it work.

@brentjett
Copy link

@youknowriad unfortunately no, it appears that currently (7.8.1) onDragStart handler will fire, but onDrag and onDragEnd do not. Can we find a better way to do whatever is happening in the block wrapper that doesn't disable the HTML5 drag and drop API?

@mikethicke
Copy link

mikethicke commented May 24, 2020

The documentation in the Block Editor Handbook should probably state that this doesn't work in the editor. The example given works in <InspectorControls> but not in the editor. I spent some time banging my head against the wall on this one today.

@SourceNova
Copy link

Wow this just cost me a lot of time, this should really throw some JS errors or even better, it should be fixed, so that DND works... Has someone found a workaround for this? At the moment i am trying to use the touch backend of react-dnd with click events enabled because it does not use html5 dnd API, but this does only work partially. Any Help would be apreciated, thx.

@w33zy
Copy link

w33zy commented Mar 29, 2022

It's 2022 and this is still a thing

@loopmasta
Copy link

It's 2022 and this is still a thing

I did run into the same problem today. HTML5 D&D API does not work within the Gutenberg Block editor. Is this a bug or by design?

@youknowriad youknowriad added the [Type] Bug An existing feature does not function as intended label Apr 14, 2022
@arnaudbroes
Copy link

We're having this issue as well with a block that we are developing. Has anyone figured out the root cause of this yet?

@renatho
Copy link
Contributor

renatho commented Aug 8, 2022

It happens because of this event: https://github.com/WordPress/gutenberg/blob/v13.8.1/packages/block-editor/src/components/block-list/use-block-props/use-selected-block-event-handlers.js#L85

Based on the hook documentation, I think it's on purpose. Maybe it's there to avoid some issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Drag and Drop Drag and drop functionality when working with blocks [Priority] Low Used to indicate that the issue at hand isn't a top priority to address and can be handled later [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

No branches or pull requests