Socket
Book a DemoInstallSign in
Socket

use-drags

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-drags

Useful way to handle drag gestures. Covers whole drag and drop lifecycle.

2.2.0
latest
Source
npmnpm
Version published
Weekly downloads
5
25%
Maintainers
1
Weekly downloads
 
Created
Source

Ci Status Npm version React version

use-drags

Handle drag events without overhead by only 1 callback. React hook for manage drag and drop lifecycle without extra business or view logic. Useful as base for custom UI components with draggable elements.

Demo

Package includes its TypeScript Definitions

Install

npm install use-drags

Usage

import React, { useRef, useState } = 'react';
import useDrags from 'use-drags';

function DraggableBlock() {
  const ref = useRef(null);
  const [position, setPosition] = useState(null);

  useDrags(ref, ({
    el,
    first,
    last,
    deltaX,
    deltaY,
    offsetX,
    offsetY,
    clientX,
    clientY,
  }) => {
    if (first) {
      el.style.opacity = 0.5;
    }

    if (last) {
      el.style.opacity = null;
      el.style.transform = null;
      setPosition(null);

      return;
    }

    setPosition({ clientX, clientY });

    el.style.transform = `translate3d(${offsetX}px, ${offsetY}px, 0)`;
  });

  return (
    <div ref={ref}>
      <span>Drag me!</span>
      {position !== null &&
        <span>X: {position.clientX }, Y: {position.clientY}</span>
      }
    </div>
  );
}

Development

npm run lint # linting
npm run test # testing

Active maintenance with care and ❤️.

Feel free to send a PR.

Keywords

drag

FAQs

Package last updated on 02 Oct 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.