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

feat: scrolling uses smooth transition effects by default #3277

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions src/DataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type DefaultColumnOptions<R, SR> = Pick<

export interface DataGridHandle {
element: HTMLDivElement | null;
scrollToCell: (position: PartialPosition) => void;
scrollToCell: (position: PartialPosition, scrollIntoViewOptions?: boolean | ScrollIntoViewOptions) => void;
selectCell: (position: Position, enableEditor?: Maybe<boolean>) => void;
}

Expand Down Expand Up @@ -293,6 +293,7 @@ function DataGrid<R, SR, K extends Key>(
const lastSelectedRowIdx = useRef(-1);
const focusSinkRef = useRef<HTMLDivElement>(null);
const shouldFocusCellRef = useRef(false);
const scrollIntoViewOptions = useRef<boolean | ScrollIntoViewOptions>();

/**
* computed values
Expand Down Expand Up @@ -441,13 +442,14 @@ function DataGrid<R, SR, K extends Key>(

useImperativeHandle(ref, () => ({
element: gridRef.current,
scrollToCell({ idx, rowIdx }) {
scrollToCell({ idx, rowIdx }, options) {
const scrollToIdx =
idx !== undefined && idx > lastFrozenColumnIndex && idx < columns.length ? idx : undefined;
const scrollToRowIdx =
rowIdx !== undefined && isRowIdxWithinViewportBounds(rowIdx) ? rowIdx : undefined;

if (scrollToIdx !== undefined || scrollToRowIdx !== undefined) {
scrollIntoViewOptions.current = options;
setScrollToPosition({ idx: scrollToIdx, rowIdx: scrollToRowIdx });
}
},
Expand Down Expand Up @@ -678,7 +680,11 @@ function DataGrid<R, SR, K extends Key>(
);
}

function selectCell(position: Position, enableEditor?: Maybe<boolean>): void {
function selectCell(
position: Position,
enableEditor?: Maybe<boolean>,
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions
): void {
if (!isCellWithinSelectionBounds(position)) return;
commitEditorChanges();

Expand All @@ -687,7 +693,7 @@ function DataGrid<R, SR, K extends Key>(
setSelectedPosition({ ...position, mode: 'EDIT', row, originalRow: row });
} else if (isSamePosition(selectedPosition, position)) {
// Avoid re-renders if the selected cell state is the same
scrollIntoView(getCellToScroll(gridRef.current!));
scrollIntoView(getCellToScroll(gridRef.current!), scrollIntoViewOptions);
} else {
shouldFocusCellRef.current = true;
setSelectedPosition({ ...position, mode: 'SELECT' });
Expand Down Expand Up @@ -1125,6 +1131,7 @@ function DataGrid<R, SR, K extends Key>(
scrollToPosition={scrollToPosition}
setScrollToCellPosition={setScrollToPosition}
gridElement={gridRef.current!}
scrollIntoViewOptions={scrollIntoViewOptions.current}
/>
)}
</div>
Expand Down
6 changes: 4 additions & 2 deletions src/ScrollToCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ export interface PartialPosition {
export default function ScrollToCell({
scrollToPosition: { idx, rowIdx },
gridElement,
setScrollToCellPosition
setScrollToCellPosition,
scrollIntoViewOptions
}: {
scrollToPosition: PartialPosition;
gridElement: HTMLDivElement;
setScrollToCellPosition: (cell: null) => void;
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions | undefined;
}) {
const ref = useRef<HTMLDivElement>(null);

useLayoutEffect(() => {
// scroll until the cell is completely visible
// this is needed if the grid has auto-sized columns
scrollIntoView(ref.current);
scrollIntoView(ref.current, scrollIntoViewOptions);
});

useLayoutEffect(() => {
Expand Down
15 changes: 13 additions & 2 deletions src/utils/domUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ export function stopPropagation(event: React.SyntheticEvent) {
event.stopPropagation();
}

export function scrollIntoView(element: Maybe<Element>) {
element?.scrollIntoView({ inline: 'nearest', block: 'nearest' });
export function scrollIntoView(
element: Maybe<Element>,
coverOptions?: boolean | ScrollIntoViewOptions
) {
if(element?.scrollIntoView) {
let options: typeof coverOptions = { inline: 'nearest', block: 'nearest' };
if (typeof coverOptions === 'boolean') {
options = coverOptions;
} else if(typeof coverOptions === 'object') {
options = Object.assign(options, coverOptions);
}
element.scrollIntoView(options);
}
}
2 changes: 1 addition & 1 deletion website/demos/ScrollToCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function ScrollToCell({ direction }: Props) {
}

function scrollToCell() {
gridRef.current!.scrollToCell({ idx, rowIdx });
gridRef.current!.scrollToCell({ idx, rowIdx }, { behavior: 'smooth' }); // smooth transition
}

return (
Expand Down
Loading