Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

drag and drop pic using hooks pure js

import React, {
   Dispatch,
   MouseEvent,
   RefObject,
   SetStateAction,
   useEffect,
   useRef,
   useState
 } from "react";

 const useDebounce = (callback: any, delay: number) => {
   const latestCallback = useRef(callback);
   const latestTimeout = useRef(1);

   useEffect(() => {
     latestCallback.current = callback;
   }, [callback]);

   return (obj: any) => {
     const { event, args } = obj;
     event.persist();
     if (latestTimeout.current) {
       clearTimeout(latestTimeout.current);
     }

     latestTimeout.current = window.setTimeout(
       () => latestCallback.current(event, ...args),
       delay
     );
   };
 };

 const setPosition = (
   event: any,
   setter: any
 ) => {
     const rect = event.target.getBoundingClientRect();
     const clientX: number = event.pageX;
     console.log('clientX: ', clientX)
     // console.log(rect.left)
     setter(clientX - rect.left)
 };

 const Slider: React.FC = () => {
   const [x, setX] = useState(null);
   const handleOnDrag = useDebounce(setPosition, 100)

   return (
     <div style={{ position: "absolute", margin: '10px' }}>
       <div
         style={{ position: "absolute", left: "0", top: "0" }}
         onDragOver={e => e.preventDefault()}
       >
         <svg width="300" height="10">
           <rect
             y="5"
             width="300"
             height="2"
             rx="10"
             ry="10"
             style={{ fill: "rgb(96,125,139)" }}
           />
         </svg>
       </div>
       <div
         draggable={true}
         onDrag={event => handleOnDrag({event, args: [setX]})}
         // onDrag={event => setPosition(event, setX)}
         style={{
           position: "absolute",
           left: (x || 0).toString() + "px",
           top: "5px",
           width: "10px",
           height: "10px",
           padding: "0px",
         }}
       >
         <svg width="10" height="10" style={{display:"block"}}>
           <circle cx="5" cy="5" r="4" />
         </svg>
       </div>
     </div>
   );
 };
Comment

PREVIOUS NEXT
Code Example
Javascript :: TextInputEditText click event 
Javascript :: javascript activate file input 
Javascript :: how to detech my cosle errors in angualr 
Javascript :: sentry not working in frontend 
Javascript :: resource loads fastest 
Javascript :: Finding Attribute value with playwright in node.js 
Javascript :: cache blogposts for 24 hours react native 
Javascript :: how to add a key to every html tag in a list react 
Javascript :: nodejs post req accept form data 
Javascript :: typeorm clear cache 
Javascript :: react js exoirt examoek 
Javascript :: req.parms en react js 
Javascript :: material ui refresh icon 
Javascript :: get all keys of nested object json data javascript 
Javascript :: javascript array table append loop 
Javascript :: Example code of using inner blocks in Wordpress with ES5 
Javascript :: JS get dropdown setting 
Javascript :: image opacity reduce js 
Javascript :: javascript python like for loop 
Javascript :: maxscript saveMaxFile 
Javascript :: for loop display numbers 1 to 10 javascript 
Javascript :: react native delay input 
Javascript :: js tabbed data to array 
Javascript :: browserslist 
Javascript :: how to read from asset in angular 
Javascript :: change the input feild name when the div contaoining that field is cloned using jquery 
Javascript :: update mongoose 
Javascript :: nice password generator 
Javascript :: solana solana-Web3.js change for devnet lamports to production transaction 
Javascript :: toast duplicate angular 
ADD CONTENT
Topic
Content
Source link
Name
5+9 =