Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

make custom draggable in react

var Draggable = React.createClass({
  getDefaultProps: function () {
    return {
      // allow the initial position to be passed in as a prop
      initialPos: {x: 0, y: 0}
    }
  },
  getInitialState: function () {
    return {
      pos: this.props.initialPos,
      dragging: false,
      rel: null // position relative to the cursor
    }
  },
  // we could get away with not having this (and just having the listeners on
  // our div), but then the experience would be possibly be janky. If there's
  // anything w/ a higher z-index that gets in the way, then you're toast,
  // etc.
  componentDidUpdate: function (props, state) {
    if (this.state.dragging && !state.dragging) {
      document.addEventListener('mousemove', this.onMouseMove)
      document.addEventListener('mouseup', this.onMouseUp)
    } else if (!this.state.dragging && state.dragging) {
      document.removeEventListener('mousemove', this.onMouseMove)
      document.removeEventListener('mouseup', this.onMouseUp)
    }
  },

  // calculate relative position to the mouse and set dragging=true
  onMouseDown: function (e) {
    // only left mouse button
    if (e.button !== 0) return
    var pos = $(this.getDOMNode()).offset()
    this.setState({
      dragging: true,
      rel: {
        x: e.pageX - pos.left,
        y: e.pageY - pos.top
      }
    })
    e.stopPropagation()
    e.preventDefault()
  },
  onMouseUp: function (e) {
    this.setState({dragging: false})
    e.stopPropagation()
    e.preventDefault()
  },
  onMouseMove: function (e) {
    if (!this.state.dragging) return
    this.setState({
      pos: {
        x: e.pageX - this.state.rel.x,
        y: e.pageY - this.state.rel.y
      }
    })
    e.stopPropagation()
    e.preventDefault()
  },
  render: function () {
    // transferPropsTo will merge style & other props passed into our
    // component to also be on the child DIV.
    return this.transferPropsTo(React.DOM.div({
      onMouseDown: this.onMouseDown,
      style: {
        left: this.state.pos.x + 'px',
        top: this.state.pos.y + 'px'
      }
    }, this.props.children))
  }
})
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to access response headers in javascript fetch api 
Javascript :: import an image in react 
Javascript :: import math javascript 
Javascript :: node js classes 
Javascript :: CHECKING TYPE OF ARRAY 
Javascript :: search array for property js 
Javascript :: js spread parameters 
Javascript :: react date range picker 
Javascript :: how to make a function in javascript 
Javascript :: date-fns 
Javascript :: error: Unknown dialect undefined 
Javascript :: js string to num 
Javascript :: debug bar laravel unninstall 
Javascript :: javascript object get subset 
Javascript :: define function 
Javascript :: JavaScript timer set Interval js ClearInterval 
Javascript :: all jquery selectors 
Javascript :: wow.js 
Javascript :: time difference in javascript 
Javascript :: Get the Timezone 
Javascript :: javascript object/function which you want to proxy 
Javascript :: javascript document 
Javascript :: javascript if equal infinity 
Javascript :: javascript strin literal 
Javascript :: crone expression in spring boot 
Javascript :: javascript loading animation on button click 
Javascript :: sequelize change item 
Javascript :: create file object node js 
Javascript :: javascript map() method 
Javascript :: instantiate js 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =