moveable icon indicating copy to clipboard operation
moveable copied to clipboard

Moveable drag box is not updating after a redux update

Open grantspilsbury opened this issue 3 years ago • 1 comments

Environments

  • react 18.1.8
  • react-moveable 0.38.4

Description

I am trying to reset a moveable box with a reset button but after the button is clicked the drag box doesn't reset to the new position.

Screen Shot 2022-09-29 at 3 22 45 pm

After reset Screen Shot 2022-09-29 at 3 22 54 pm

import { useRef } from 'react'
import Moveable from 'react-moveable'

const TextPanel = ({ reset }) => {
  return (
    <button
      onClick={() => {
        reset({
          translate: [screenWidth / 2, screenHeight / 2],
          rotate: 0,
          scale: [1, 1],
        })
      }}>
      Reset
    </button>
  )
}

const App = () => {
  const moveableRef = useRef(null)

  const handleResetClick = () => {
    dispatch({
      type: 'UPDATE',
      text: {
        translate: [500, 500],
        rotate: 0,
        scale: [1, 1],
      },
    })
    moveableRef.current.updateRect()
  }

  return (
    <>
      <TextPanel reset={handleResetClick} />
      <Moveable
        ref={moveableRef}
        target={target}
        snappable={true}
        draggable={true}
        rotatable={true}
        throttleDrag={0}
        startDragRotate={0}
        throttleDragRotate={0}
        scalable={true}
        keepRatio={false}
        edge={false}
        zoom={1}
        origin={true}
        bounds={bounds}
        onDrag={(e) => {
          updateValue({ translate: e.beforeTranslate })
        }}
        onRotate={(e) => {
          updateValue({ rotate: e.beforeRotation })
        }}
        onScale={(e) => {
          updateValue({ scale: e.scale })
        }}
      />
    </>
  )
}

export default App

I have tried to add a useEffect hook just in case it is a timing issue without success

  useEffect(() => {
    moveableRef.current.updateRect()
  }, [moveableRef, selectedText, target])

grantspilsbury avatar Sep 29 '22 08:09 grantspilsbury

hi. moveable doesn't support async yet.

It will be reset properly after an update.

Check it with requestAnimationFrame or setTimeout.

daybrush avatar Sep 29 '22 16:09 daybrush