How to track the position of the Draggable component?

Open
#385 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
javascript, react
Domain
frontend

Research direction

Begin with the Draggable and DraggableCore APIs, focusing on the controlled position prop and the onStop callback shown in the issue. Determine whether the requested parent-state binding is an existing usage or documentation gap or requires a new API; completion should define the supported behavior and include tests if behavior changes.

Written by the indexing model from the issue text.

Description

I want to track the (x, y) coordinates of the Draggable component.

I can't use the position prop like this, because then I cannot drag the component anymore:

const { posX, posY } = this.state;
return (
  ...
  <Draggable position={{x: posX, y: posY}}>
    <div style={{backgroundColor: "green", height: "30px", width: "30px"}}></div>
  </Draggable>
  ...
);

I've looked a bit at the DraggableCore API but I don't see how it could help me.

The only solution I found out yet is to set an onStop handler to the Draggable component like so:

handleOnStop(evt, data) {
  const { x, y } = data;
  this.setState({posX: x, posY: y});
}

Is there a simpler solution to bind directly the position coordinates in the parent state (posX, posY) to the props of the component?

Dominant language
JavaScript
Stars
9.3k
Forks
1k
Avg merge
3d 8h
Merged PRs (30d)
4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from react-grid-layout/react-draggable

All issues in react-grid-layout/react-draggable

Similar issues

More JavaScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.