Skip to content

Commit

Permalink
Add experiemental support for kinetic scroll super speeding
Browse files Browse the repository at this point in the history
  • Loading branch information
jassmith committed Nov 27, 2023
1 parent 2c2e01c commit 2e69c48
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "root",
"version": "5.99.9-alpha1",
"version": "5.99.9-alpha2",
"scripts": {
"start": "npm run storybook",
"version": "./update-version.sh",
Expand Down
4 changes: 2 additions & 2 deletions packages/cells/package.json
@@ -1,6 +1,6 @@
{
"name": "@glideapps/glide-data-grid-cells",
"version": "5.99.9-alpha1",
"version": "5.99.9-alpha2",
"description": "Extra cells for glide-data-grid",
"sideEffects": [
"**/*.css"
Expand Down Expand Up @@ -50,7 +50,7 @@
"canvas"
],
"dependencies": {
"@glideapps/glide-data-grid": "5.99.9-alpha1",
"@glideapps/glide-data-grid": "5.99.9-alpha2",
"@linaria/react": "^4.5.3",
"@toast-ui/editor": "3.1.10",
"@toast-ui/react-editor": "3.1.10",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
@@ -1,6 +1,6 @@
{
"name": "@glideapps/glide-data-grid",
"version": "5.99.9-alpha1",
"version": "5.99.9-alpha2",
"description": "React data grid for beautifully displaying and editing large amounts of data with amazing performance.",
"sideEffects": [
"**/*.css"
Expand Down
Expand Up @@ -4,6 +4,7 @@ import * as React from "react";
import { useResizeDetector } from "../../common/resize-detector.js";
import { browserIsSafari } from "../../common/browser-detect.js";
import { useEventListener } from "../../common/utils.js";
import useKineticScroll from "./use-kinetic-scroll.js";

interface Props {
readonly className?: string;
Expand Down Expand Up @@ -227,6 +228,8 @@ export const InfiniteScroller: React.FC<Props> = p => {
});
}, [paddingBottom, paddingRight, scrollHeight, update, preventDiagonalScrolling, hasTouches]);

useKineticScroll(browserIsSafari.value, onScroll, scroller);

const onScrollRef = React.useRef(onScroll);
onScrollRef.current = onScroll;

Expand Down
@@ -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;
4 changes: 2 additions & 2 deletions packages/source/package.json
@@ -1,6 +1,6 @@
{
"name": "@glideapps/glide-data-grid-source",
"version": "5.99.9-alpha1",
"version": "5.99.9-alpha2",
"description": "Useful data source hooks for Glide Data Grid",
"sideEffects": false,
"type": "module",
Expand Down Expand Up @@ -42,7 +42,7 @@
"canvas"
],
"dependencies": {
"@glideapps/glide-data-grid": "5.99.9-alpha1"
"@glideapps/glide-data-grid": "5.99.9-alpha2"
},
"peerDependencies": {
"lodash": "^4.17.19",
Expand Down

0 comments on commit 2e69c48

Please sign in to comment.