Skip to content

[@visx/drag] Enhancement to limit dx and dy #1866

Description

@agoldis

Trying to enhance the library to limit dx and dy values for useDrag, motivation being limit the allowed distance for drag operation.

For example, without dx restrictions:

currents-2024-08-05-16.05.56.mp4

After the suggested change:

currents-2024-08-05-16.04.55.mp4

I wanted to confirm my naive implementation before opening a PR:

restrict?: {
    xMin?: number;
    xMax?: number;
    yMin?: number;
    yMax?: number;
    dxMax?: number; // new 
    dyMax?: number; // new 
    dxMin?: number; // new 
    dyMin?: number; // new 
};
let dx = dragPoint.x - x;
let dy= dragPoint.y - y;
if ( typeof restrict.dxMax === "number" && _dx > restrict.dxMax) {
  dx =  restrict.dxMax;
}
if (typeof restrict.dyMax === "number" && _dy > restrict.dyMax) {
  dy = restrict.dyMax;
}
if (typeof restrict.dxMin === "number" && _dx < restrict.dxMin) {
  dx =  restrict.dxMin;
}
if (typeof restrict.dyMin === "number" && _dy < restrict.dyMin) {
  dy =  restrict.dyMin;
}

return {
  ...currState,
  dx,
  dy,
};

Would appreciate weighing in on this approach.

Activity

  1. joeldodich commented on Nov 15, 2024

    @joeldodich

    This would be really nice to have.

  2. agoldis commented on Nov 15, 2024

    @agoldis
    Author
  3. williaster commented on Nov 15, 2024

    @williaster
    Collaborator

    sorry for the slow reply, this looks reasonable to me! happy to review a PR if you're still interested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions