Skip to content

Commit

Permalink
The position of the view is at the top when opening the page (#257)
Browse files Browse the repository at this point in the history
Co-authored-by: Mailn Nifeli Snieske <[email protected]>
  • Loading branch information
malinnsnieske and Mailn Nifeli Snieske authored Sep 17, 2024
1 parent 0ac5b46 commit 2b6ce66
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Flex, Icon } from '@kvib/react';
import { Table, Updater } from '@tanstack/react-table';
import { PaginationActionButton } from './PaginationActionButton';
import { PaginationRelativeButtons } from './PaginationRelativeButtons';
import { useRef, useEffect } from 'react';
import { useRef, useEffect, useState } from 'react';

interface Props<TData> {
table: Table<TData>;
Expand All @@ -15,15 +15,18 @@ export function PaginationButtonContainer<TData>({ table }: Props<TData>) {
const numberOfRows = table.getRowCount();
const numberOfPages = Math.ceil(numberOfRows / pageSize);
const ref = useRef<HTMLDivElement>(null);
const [isInitialLoad, setIsInitialLoad] = useState(true);

useEffect(() => {
if (ref.current) {
const currentScrollPosition =
window.scrollY || document.documentElement.scrollTop;
const distanceToBottom =
document.documentElement.scrollHeight -
(currentScrollPosition + window.innerHeight);
window.scrollBy({ top: distanceToBottom, behavior: 'auto' });
if (!isInitialLoad) {
window.scrollTo({
top: document.documentElement.scrollHeight,
behavior: 'auto',
});
} else {
setIsInitialLoad(false);
}
}
}, [index]);

Expand Down

0 comments on commit 2b6ce66

Please sign in to comment.