Skip to content

Commit

Permalink
Factor in scroll container offset
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Jun 25, 2020
1 parent 1017e34 commit 2ad9d77
Showing 1 changed file with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,36 @@ export default function useScrollWhenDragging( blockElementId ) {

const scrollOnDragOver = useCallback( ( event ) => {
const scrollParentHeight = scrollParentY.current.offsetHeight;
const offsetDragStartPosition =
dragStartY.current - scrollParentY.current.offsetTop;
const offsetDragPosition =
event.clientY - scrollParentY.current.offsetTop;

if ( event.clientY > dragStartY.current ) {
if ( event.clientY > offsetDragStartPosition ) {
// User is dragging downwards.
const moveableDistance = Math.max(
scrollParentHeight -
dragStartY.current -
offsetDragStartPosition -
SCROLL_INACTIVE_DISTANCE_PX,
0
);
const dragDistance = Math.max(
event.clientY -
dragStartY.current -
offsetDragPosition -
offsetDragStartPosition -
SCROLL_INACTIVE_DISTANCE_PX,
0
);
const distancePercentage = dragDistance / moveableDistance;
velocityY.current = VELOCITY_MULTIPLIER * distancePercentage;
} else if ( event.clientY < dragStartY.current ) {
} else if ( event.clientY < offsetDragStartPosition ) {
// User is dragging upwards.
const moveableDistance = Math.max(
dragStartY.current - SCROLL_INACTIVE_DISTANCE_PX,
offsetDragStartPosition - SCROLL_INACTIVE_DISTANCE_PX,
0
);
const dragDistance = Math.max(
dragStartY.current -
event.clientY -
offsetDragStartPosition -
offsetDragPosition -
SCROLL_INACTIVE_DISTANCE_PX,
0
);
Expand Down

0 comments on commit 2ad9d77

Please sign in to comment.