npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@tradelunch/usedragndrop

v2.0.7

Published

useDragNDrop to create Draggable Component. By wrapping a component with Draggable, you can create drag and drop component

Downloads

55

Readme

Simple drag and drop React hook and draggable component

This is a simple react hook to create a draggable component.

Please let me know anything if you need. I will try to work on this Thank you for downloading!!

Example

use cra to create test app

npx create-react-app test-hook-example --template typescript

install hook and use vscode

# move to example folder
cd test-hook-example

npm i @tradelunch/usedragndrop

# open folder with vscode
code .

App.tsx

This code is simple file that I created for example. you can see how to use useDragNDrop hook briefly.

App.module.css

.Box {
    background: #b6b6e5;
    height: 100vh;
    width: 100%;
}

.wrapper:hover {
    will-change: transform, top, left;
}

.Content {
    width: 100px;
    height: 100px;
    background-color: grey;
    display: flex;
    flex: 1;
    justify-content: center;
    align-items: center;
    margin: 0px;
    padding: 0px;
    font-weight: bold;
    border: 2px solid darkgrey;
}

App.jsx with useDragNDrop

import React, { useRef } from "react";
import { useDragNDrop } from "@tradelunch/usedragndrop";
import styles from "App.module.css";

function App() {
    // take event functions, positions, ref and etc.
    const bounds = useRef(null); // you can set bounds inside which a draggable component can move 
    const { onMouseDown, onDragStart, onMouseUp, style, dragRef } =
        useDragNDrop({
            position: "absolute",
            bounds
        });

    return (
        <div
            style={styles.Box}
            bounds={bounds}
        >
            {/* apply them into a dom to make it draggable */}
            <div
                style={styles.Wrapper}
                ref={dragRef}
                // draggable
                onDragStart={onDragStart}
                onMouseDown={onMouseDown}
                onMouseUp={onMouseUp}
            >
                <div style={styles.Content} >
                    <span>Hello Drag!!</span>
                </div>
            </div>
        </div>
    );
}

export default App;

You can use style or top/left value to adjust position what you want

export declare function useDragNDrop(props?: TProps): {
    onMouseDown: (e: any) => void;
    onDragStart: () => false;
    onMouseUp: () => void;
    style: CSSProperties;
    dragRef: import("react").RefObject<HTMLDivElement>;
    top: number;
    left: number;
    isDragging: boolean;
    setIsDragging: import("react").Dispatch<
        import("react").SetStateAction<boolean>
    >;
    position: TPositions;
    shiftX: number;
    shiftY: number;
};

With useDragNDropRxJs

App.module.css

.Box {
    background: #b6b6e5;
    height: 100vh;
	  overflow: scroll;
    width: 100%;
}

height: 100vh; and overflow: scroll; added

App.jsx

import React, { useRef } from "react";
import { useDragNDrop } from "@tradelunch/usedragndrop";
import styles from "App.module.css";

function App() {
    // take event functions, positions, ref and etc.
    const bounds = useRef(null); // you can set bounds inside which a draggable component can move 
    const { ref: dragRef, bounds, pos } = useDragNDropRxJs({ overflow: 'scroll' });

    return (
        <div
            style={styles.Box}
            bounds={bounds}
        >
            {/* apply them into a dom to make it draggable */}
            <div
                style={styles.Wrapper}
                ref={dragRef}
                // draggable
                onDragStart={onDragStart}
                onMouseDown={onMouseDown}
                onMouseUp={onMouseUp}
            >
                <div style={styles.Content} >
                    <span>Hello Drag!!</span>
                </div>
            </div>
        </div>
    );
}

export default App;

with css added above, we need to pass overflow with scroll value.