react-dnd / react-dnd-html5-backend

HTML5 backend for React DnD [Legacy Repo]
MIT License
146 stars 97 forks source link

Allow listening to frames. #9

Closed akorchev closed 8 years ago

akorchev commented 8 years ago

Extracted two methods from setup and teardown that will allow me to attach the backend to an iframe. I need a DropTarget to live inside an iframe. Here is how I would do it

class Frame extends React.Component {
  static contextTypes = {
    dragDropManager: PropTypes.object.isRequired
  }
  render() {
     return <iframe></iframe>
  }
  componentDidMount() {
      const { contentDocument } = findDOMNode(this)
      this.context.dragDropManager.backend.addEventListeners(contentDocument.defaultView)
  }
  componentWillUnmount() {
      const { contentDocument } = findDOMNode(this)
      this.context.dragDropManager.backend.removeEventListeners(contentDocument.defaultView)
  }
}
nelix commented 8 years ago

That is much simpler than I was expecting. Do you think you could add some tests and maybe an example?

akorchev commented 8 years ago

I can definitely create an example. Not sure how to create tests though. This is only half of the solution - the other half is to get the dragDropManager to the iframe which I will show in the example.

akorchev commented 8 years ago

Here is the example: https://github.com/akorchev/react-dnd-cross-frame

gaearon commented 8 years ago

Thanks. I don't promise to support or test this in the future but I see no harm in adding it. :+1:

gaearon commented 8 years ago

Note that I still reference window in a couple of places so this won't cover all cases. There's a couple of places where I add a mousemove listener on window, and it's important to do this lazily for better perf. I'm open to merging more PRs handling this but I won't work on it myself.