Coder Social home page Coder Social logo

jackdeadman / use-mouse-drag Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 534 KB

A react hook for creating interaction by dragging the mouse e.g., moving elements and drawing boxes

Home Page: https://jackdeadman.github.io/use-mouse-drag/

HTML 26.81% JavaScript 70.18% CSS 3.01%
react reactjs react-hooks mouse dragging

use-mouse-drag's Introduction

use-mouse-drag

Examples

A react hook for creating interaction by dragging the mouse e.g., moving elements and drawing boxes

NPM JavaScript Style Guide

Install

npm install --save use-mouse-drag

Basic Usage

const { mousePosition, dragging } = useMouseDrag(element, constraints)

The mousePosition variable changes whenever the mouse moves. The object contains a start attribute and an end attribute indicating the location of the mouse at the start and the end of dragging. The dragging variable indicates whether or not the mouse is currently down. The mousePosition.start is an undefined value when not being dragged, use mousePosition.end to get current mousePosition.

Both the parameters passed into the hook are optional, The element parameter is the element that will listen for drags and will default to the entire page. The constraints parameter is an array of functions, the function gets passed a mousePosition and if any of the functions are false a drag will not begin.

See the Example below to see how it all fits together.

Example

import React, { useRef, useCallback, useState, useEffect } from 'react'
import { useMouseDrag, useChange } from 'use-mouse-drag'

const App = () => {
  const [ allowDragging, setAllowDragging ] = useState(true);
  const canvasRef = useRef();
  const allowDraggingRef = useRef(allowDragging);

  const { mousePosition, dragging } = useMouseDrag(canvasRef.current, [
    mousePosition => allowDraggingRef.current
  ]);

  useEffect(() => {
    allowDraggingRef.current = allowDragging;
  }, [ allowDragging ]);

  useChange(() => {
    // Drag ended
    if (!dragging) {
      alert('Dragged: ' + JSON.stringify(mousePosition))
    }
  }, [ dragging ]);

  return (
    <React.Fragment>
      <div ref={canvasRef} className="canvas"
      style={
        { opacity: allowDragging ? 1: 0.4 }
      }>
      </div>

      <div>Dragging: { dragging.toString() }</div>
      <div>Position: { JSON.stringify(mousePosition) }</div>
      <button onClick={() => setAllowDragging(!allowDragging)}>
        Turn dragging { allowDragging ? 'Off' : 'On' }
      </button>
    </React.Fragment>
  )
}
export default App

License

MIT © jackdeadman


This hook is created using create-react-hook.

use-mouse-drag's People

Contributors

jackdeadman avatar

Watchers

 avatar  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.