Coder Social home page Coder Social logo

kasperekt / react-draggable-ie9-react14 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adampacanowski/react-draggable-ie9-react14

0.0 0.0 0.0 1.95 MB

React draggable component

License: MIT License

Makefile 4.21% HTML 12.72% JavaScript 81.94% Shell 1.13%

react-draggable-ie9-react14's Introduction

react-draggable

A simple component for making elements draggable.

This version is prepared to Internet Explorer 9 (and other better browsers) and React 0.14.

Based on 0.8.5 version.

Installing

$ npm install react-draggable

If you aren't using browserify/webpack, a UMD version of react-draggable is available. It is updated per-release only.

If you want a UMD version of the latest master revision, you can generate it yourself from master by cloning this repository and running $ make. This will create umd dist files in the dist/ folder.

Details

A <Draggable> element wraps an existing element and extends it with new event handlers and styles. It does not create a wrapper element in the DOM.

Draggable items are moved using CSS Transforms. This allows items to be dragged regardless of their current positioning (relative, absolute, or static). Elements can also be moved between drags without incident.

If the item you are dragging already has a CSS Transform applied, it will be overwritten by <Draggable>. Use an intermediate wrapper (<Draggable><span>...</span></Draggable>) in this case.

API

The <Draggable/> component transparently adds draggable to whatever element is supplied as this.props.children. Note: Only a single element is allowed or an Error will be thrown.

For the <Draggable/> component to correctly attach itself to its child, the child element must provide support for the following props:

  • style is used to give the transform css to the child.
  • className is used to apply the proper classes to the object being dragged.
  • onMouseDown is used along with onMouseUp to keep track of dragging state.
  • onMouseUp is used along with onMouseDown to keep track of dragging state.
  • onTouchStart is used along with onTouchEnd to keep track of dragging state.
  • onTouchEnd is used along with onTouchStart to keep track of dragging state.

React.DOM elements support the above six properties by default, so you may use those elements as children without any changes. If you wish to use a React component you created, you might find this React page helpful.

Props:

axis: determines which axis the draggable can move. Accepted values:

  • both allows movement horizontally and vertically (default).
  • x limits movement to horizontal axis.
  • y limits movement to vertical axis.

handle: specifies a selector to be used as the handle that initiates drag.

cancel: specifies a selector to be used to prevent drag initialization.

grid: specifies the x and y that dragging should snap to.

bounds: specifies movement boundaries. Accepted values:

  • parent restricts movement within the node's offsetParent (nearest node with position relative or absolute), or
  • An object with left, top, right, and bottom properties. These indicate how far in each direction the draggable can be moved. See example/index.html for more on this.

start: specifies the x and y that the dragged item should start at. This is generally not necessary to use (you can use absolute or relative positioning of the child directly), but can be helpful for uniformity in your callbacks and with css transforms.

moveOnStartChange: if true (it defaults false), will move the element if there is a change in start. It won't work if the start is changed in the middle of a drag. We set this by default to false because it can cause unwanted effects if you are not aware of it.

zIndex: specifies the zIndex to use while dragging.

onStart: called when dragging starts.

onDrag: called while dragging.

onStop: called when dragging stops.

Example usage

/** @jsx React.DOM */
var React = require('react'),
	Draggable = require('react-draggable');

var App = React.createClass({
	handleStart: function (event, ui) {
		console.log('Event: ', event);
		console.log('Position: ', ui.position);
	},

	handleDrag: function (event, ui) {
		console.log('Event: ', event);
        console.log('Position: ', ui.position);
	},

	handleStop: function (event, ui) {
		console.log('Event: ', event);
        console.log('Position: ', ui.position);
	},

	render: function () {
		return (
			<Draggable
				axis="x"
				handle=".handle"
				start={{x: 0, y: 0}}
				moveOnStartChange={false}
				grid={[25, 25]}
				zIndex={100}
				onStart={this.handleStart}
				onDrag={this.handleDrag}
				onStop={this.handleStop}>
				<div>
					<div className="handle">Drag from here</div>
					<div>This readme is really dragging on...</div>
				</div>
			</Draggable>
		);
	}
});

React.renderComponent(<App/>, document.body);

State Problems?

<Draggable> is a stateful component. This means that it is storing its current drag offsets in its internal state. This can cause problems with certain integrations. For example, if you change the position of the element manually, <Draggable> can get into trouble as it assumes a translation in the DOM. If you see an element jump around the page when you click it, this is affecting you.

This is an unfortunate side-effect of dragging, which is inherently stateful.

If you move the element manually, you have two options:

  1. Feed the <Draggable> an x and y parameter in the start param, and change it as you go while setting moveOnStartChange to true, or,
  2. When moving the <Draggable>, ref the element and call resetState().

You can't update the start position during a drag. See #94.

Dragging images

The HTML5 Drag & Drop API can interact strangely with <Draggable>, especially on <img> tags.

If you want an <img> to be draggable, set <img draggable="false" />. See the corresponding issue.

Contributing

  • Fork the project
  • Run the project in development mode: $ make dev
  • Make changes.
  • Add appropriate tests
  • $ make test
  • If tests don't pass, make them pass.
  • Update README with appropriate docs.
  • Commit and PR

Release checklist

  • Update CHANGELOG
  • make release-patch, make release-minor, or make-release-major
  • make publish

License

MIT

react-draggable-ie9-react14's People

Contributors

adampacanowski avatar andrewraycode avatar brianfay avatar cjblomqvist avatar davidwells avatar jamwise avatar jmuerle avatar joelrbrandt avatar markthethomas avatar martinross avatar matiss avatar meikidd avatar mzabriskie avatar strml avatar victor-homyakov avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.