Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add experiemental support for kinetic scroll super speeding
- Loading branch information
Showing
6 changed files
with
72 additions
and
6 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
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
63 changes: 63 additions & 0 deletions
63
packages/core/src/internal/scrolling-data-grid/use-kinetic-scroll.ts
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useRef } from "react"; | ||
|
||
const useKineticScroll = ( | ||
isEnabled: boolean, | ||
callback: () => void, | ||
targetScroller: React.MutableRefObject<HTMLDivElement | null> | ||
) => { | ||
const rafId = useRef<number | null>(null); | ||
const isTouching = useRef(false); | ||
const lastScrollPosition = useRef<number | null>(null); | ||
|
||
useEffect(() => { | ||
const handleScroll = () => { | ||
if (!isTouching.current) { | ||
const currentScrollPosition = targetScroller.current?.scrollTop ?? 0; | ||
if (lastScrollPosition.current === currentScrollPosition) { | ||
// Scroll position hasn't changed, stop the animation frame | ||
lastScrollPosition.current = null; | ||
return; | ||
} | ||
|
||
lastScrollPosition.current = currentScrollPosition; | ||
callback(); | ||
rafId.current = requestAnimationFrame(handleScroll); | ||
} | ||
}; | ||
|
||
const startTouch = () => { | ||
isTouching.current = true; | ||
lastScrollPosition.current = null; // Reset last scroll position on touch start | ||
if (rafId.current !== null) { | ||
cancelAnimationFrame(rafId.current); | ||
rafId.current = null; | ||
} | ||
}; | ||
|
||
const endTouch = (event: TouchEvent) => { | ||
if (event.touches.length === 0) { | ||
// All touches have ended | ||
isTouching.current = false; | ||
rafId.current = requestAnimationFrame(handleScroll); | ||
} | ||
}; | ||
|
||
if (isEnabled && targetScroller.current !== null) { | ||
const element = targetScroller.current; | ||
element.addEventListener("touchstart", startTouch); | ||
element.addEventListener("touchend", endTouch); | ||
element.addEventListener("scroll", handleScroll, { passive: true }); | ||
|
||
return () => { | ||
element.removeEventListener("touchstart", startTouch); | ||
element.removeEventListener("touchend", endTouch); | ||
element.removeEventListener("scroll", handleScroll); | ||
if (rafId.current !== null) { | ||
cancelAnimationFrame(rafId.current); | ||
} | ||
}; | ||
} | ||
}, [isEnabled, targetScroller, callback]); | ||
}; | ||
|
||
export default useKineticScroll; |
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