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.

A way to drag outside to iframe

See original GitHub issue

Is your feature request related to a problem? Please describe. React limits that drag-and-drop within a single HTML document. Somehow we need drag something outside to iframe inside.

Describe the solution you’d like I find that dnd HTML5backend just trigger drag callbacks in window. So if we can make the components inside iframe trigger those callback.And React.createPortal make components inside iframe have same DndProvider with outside.It works, luckily. But i worry about may some problem i didn’t notice.And the code is not so convenient with HTML5Backend.

import React, {
  useRef,
  useEffect,
  useContext,
  forwardRef,
} from 'react';
import classnames from 'classnames';
import { DndContext } from 'react-dnd';

// Frame base on createPortal inside iframe dom.
import Frame from 'react-frame-component';

const events = [
  ['dragstart', 'handleTopDragStart', false],
  ['dragstart', 'handleTopDragStartCapture', true],
  ['dragend', 'handleTopDragEndCapture', true],
  ['dragenter', 'handleTopDragEnter', false],
  ['dragenter', 'handleTopDragEnterCapture', true],
  ['dragleave', 'handleTopDragLeaveCapture', true],
  ['dragover', 'handleTopDragOver', false],
  ['dragover', 'handleTopDragOverCapture', true],
  ['drop', 'handleTopDrop', false],
  ['drop', 'handleTopDropCapture', true],
];

export const DndFrame = forwardRef((props = {}, ref) => {
  const container = useRef(null);
  const dndValue = useContext(DndContext) || {};

  const { dragDropManager: { backend = {} } = {} } = dndValue;
  const { children, className, containerProps = {}, ...others } = props;

  const cls = classnames({
    'dnd-frame': true,
    [className]: !!className,
  });

  useEffect(() => {
    const { current } = container;

    if (!current) {
      return;
    }

    // this make callback run in outside window
    events.forEach((eventArgs = []) => {
      const [name, callbackName, capture = false] = eventArgs;

      const callback = backend[callbackName] && backend[callbackName].bind(backend);

      current.addEventListener(name, (...args) => {
        callback && callback(...args);
      }, capture);
    });
  }, [container.current]);

  return (
    <Frame className={cls} ref={ref} {...others}>
      <div className="dnd-frame-container" ref={container} {...containerProps}>
        { children }
      </div>
    </Frame>
  );
});

Describe alternatives you’ve considered May you can export some options with HTML5Backend.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:5
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

32reactions
HsuTingcommented, Aug 7, 2019

I found an easier solution to simplify this code, but I am not sure if this one is a better solution. Hope to help you to fix this issue.

import React, { useContext, useEffect } from 'react';
import DndProvider, { DndContext } from 'react-dnd';
import HTML5Backend from 'react-dnd-html5-backend';
import Frame, { FrameContext } from 'react-frame-component';

const DndFrame = ({ children }) => {
  const { dragDropManager } = useContext(DndContext);
  const { window } = useContext(FrameContext);

  useEffect(() => {
    dragDropManager.getBackend().addEventListeners(window);
  });

  return children;
};

const Example = () => (
  <DndProvider backend={HTML5Backend}>
    <Frame>
       <DndFrame>
          <div>...</div>
       </DndFrame>
    </Frame>
  </DndProvider>
);
3reactions
monkeyphysicscommented, Sep 10, 2021

For future visitors, there’s a simple solution: https://react-dnd.github.io/react-dnd/examples/dustbin/iframe

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to move / drag element from outside to iframe
One possible solution would be to try extending the Selenium ActionChains API and implement new method(s) that would allow you to start the...
Read more >
jQuery : How to move / drag element from outside to iframe
jQuery : How to move / drag element from outside to iframe [ Beautify Your Computer : https://www.hows.tech/p/recommended.html ] jQuery ...
Read more >
Drag'n'Drop with mouse events - The Modern JavaScript Tutorial
Then on mousemove move it by changing left/top with position:absolute . On mouseup – perform all actions related to finishing the drag'n'drop.
Read more >
Using the HTML5 Drag and Drop API - web.dev
It's worth noting that in most browsers, text selections, images, and links are draggable by default. For example, if you drag a link...
Read more >
Drag Between Two Iframes - CodePen
<iframe class="dragFrame dragDrop" srcdoc='</html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script><script ...
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