Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[data grid] Experimental touch scroll lock #16313

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ import { DataGridProcessedProps } from '../../models/props/DataGridProps';

type Position = 'vertical' | 'horizontal';
type OwnerState = DataGridProcessedProps;
type GridVirtualScrollbarProps = { position: Position };
type GridVirtualScrollbarProps = {
position: Position;
scrollPosition: React.RefObject<{
left: number;
top: number;
}>;
};

const useUtilityClasses = (ownerState: OwnerState, position: Position) => {
const { classes } = ownerState;
Expand Down Expand Up @@ -83,6 +89,7 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp

const propertyDimension = props.position === 'vertical' ? 'height' : 'width';
const propertyScroll = props.position === 'vertical' ? 'scrollTop' : 'scrollLeft';
const propertyScrollPosition = props.position === 'vertical' ? 'top' : 'left';
const hasScroll = props.position === 'vertical' ? dimensions.hasScrollX : dimensions.hasScrollY;

const contentSize =
Expand All @@ -97,31 +104,33 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp
scrollbarSize * (contentSize / dimensions.viewportOuterSize[propertyDimension]);

const onScrollerScroll = useEventCallback(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current;
const scrollPosition = props.scrollPosition.current;

if (!scrollbar) {
return;
}

if (scroller[propertyScroll] === lastPosition.current) {
if (scrollPosition[propertyScrollPosition] === lastPosition.current) {
return;
}

lastPosition.current = scroller[propertyScroll];
lastPosition.current = scrollPosition[propertyScrollPosition];

if (isLocked.current) {
isLocked.current = false;
return;
}
isLocked.current = true;

const value = scroller[propertyScroll] / contentSize;
const value = scrollPosition[propertyScrollPosition] / contentSize;
scrollbar[propertyScroll] = value * scrollbarInnerSize;
});

const onScrollbarScroll = useEventCallback(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current;

if (!scrollbar) {
return;
}
Expand All @@ -139,12 +148,12 @@ const GridVirtualScrollbar = forwardRef<HTMLDivElement, GridVirtualScrollbarProp
useOnMount(() => {
const scroller = apiRef.current.virtualScrollerRef.current!;
const scrollbar = scrollbarRef.current!;
const options = { capture: true, passive: true };
const options = { passive: true };
scroller.addEventListener('scroll', onScrollerScroll, options);
scrollbar.addEventListener('scroll', onScrollbarScroll, options);
return () => {
scroller.removeEventListener('scroll', onScrollerScroll, options);
scrollbar.removeEventListener('scroll', onScrollbarScroll, options);
scroller.removeEventListener('scroll', onScrollerScroll, options as any);
scrollbar.removeEventListener('scroll', onScrollbarScroll, options as any);
};
});

Expand Down
Loading
Loading