-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(web): refactor story dnd (#723)
- Loading branch information
1 parent
8bc811e
commit da57ac4
Showing
4 changed files
with
54 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,29 @@ | ||
import { useEffect, useState, useRef, RefObject } from "react"; | ||
|
||
export interface IUseScroll { | ||
position: number; | ||
isScrollAllowed: boolean; | ||
} | ||
|
||
const BOUND_HEIGHT = 70; | ||
|
||
function getScrollDirection({ | ||
position, | ||
upperBounds = Infinity, | ||
lowerBounds = -Infinity, | ||
}: { | ||
position: number | undefined; | ||
upperBounds: number | undefined; | ||
lowerBounds: number | undefined; | ||
}): "top" | "bottom" | "stable" { | ||
if (position === undefined) { | ||
return "stable"; | ||
} | ||
if (position > lowerBounds - BOUND_HEIGHT) { | ||
return "bottom"; | ||
} | ||
if (position > upperBounds + BOUND_HEIGHT) { | ||
return "top"; | ||
} | ||
return "stable"; | ||
} | ||
|
||
export const useScroll = (ref: RefObject<HTMLElement | null>) => { | ||
const [config, setConfig] = useState<Partial<IUseScroll>>({ | ||
position: 0, | ||
isScrollAllowed: false, | ||
}); | ||
|
||
const scrollTimer = useRef<null | NodeJS.Timeout>(null); | ||
|
||
const scrollSpeed = 1; | ||
const { position, isScrollAllowed } = config; | ||
|
||
const bounds = ref.current?.getBoundingClientRect(); | ||
const direction = getScrollDirection({ | ||
position, | ||
upperBounds: bounds?.top, | ||
lowerBounds: bounds?.bottom, | ||
}); | ||
import { useCallback, useEffect, useRef } from "react"; | ||
|
||
export const useScroll = () => { | ||
const scrollContainerRef = useRef<null | HTMLDivElement>(null); | ||
|
||
const handleMouseMove = useCallback( | ||
(e: MouseEvent) => { | ||
const scrollContainer = scrollContainerRef.current; | ||
if (!scrollContainer) return; | ||
|
||
const mouseY = e.clientY; | ||
const scrollThreshold = scrollContainer.offsetHeight * 0.4; | ||
mouseY < scrollThreshold | ||
? (scrollContainer.scrollTop -= 10) | ||
: mouseY > scrollContainer.offsetHeight - scrollThreshold && | ||
(scrollContainer.scrollTop += 10); | ||
}, | ||
[scrollContainerRef], | ||
); | ||
|
||
useEffect(() => { | ||
if (direction !== "stable" && isScrollAllowed) { | ||
scrollTimer.current = setInterval(() => { | ||
ref.current?.scrollBy(0, scrollSpeed * (direction === "top" ? -1 : 1)); | ||
}, 1); | ||
} | ||
document.addEventListener("mousemove", handleMouseMove); | ||
return () => { | ||
if (scrollTimer.current) { | ||
clearInterval(scrollTimer.current); | ||
} | ||
document.removeEventListener("mousemove", handleMouseMove); | ||
}; | ||
}, [isScrollAllowed, direction, ref, scrollSpeed]); | ||
}, [handleMouseMove]); | ||
|
||
return { updatePosition: setConfig } as const; | ||
return { scrollContainerRef } as const; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters