kopia lustrzana https://gitlab.com/soapbox-pub/soapbox
ScrollableList: save and restore scroll position
rodzic
c749738e4b
commit
ba0b4851f6
|
@ -1,4 +1,4 @@
|
||||||
import React from 'react';
|
import React, { useEffect, useRef } from 'react';
|
||||||
import { Virtuoso, Components, VirtuosoProps, VirtuosoHandle } from 'react-virtuoso';
|
import { Virtuoso, Components, VirtuosoProps, VirtuosoHandle } from 'react-virtuoso';
|
||||||
|
|
||||||
import PullToRefresh from 'soapbox/components/pull-to-refresh';
|
import PullToRefresh from 'soapbox/components/pull-to-refresh';
|
||||||
|
@ -62,11 +62,16 @@ const ScrollableList = React.forwardRef<VirtuosoHandle, IScrollableList>(({
|
||||||
placeholderComponent: Placeholder,
|
placeholderComponent: Placeholder,
|
||||||
placeholderCount = 0,
|
placeholderCount = 0,
|
||||||
initialTopMostItemIndex = 0,
|
initialTopMostItemIndex = 0,
|
||||||
scrollerRef,
|
|
||||||
}, ref) => {
|
}, ref) => {
|
||||||
const settings = useSettings();
|
const settings = useSettings();
|
||||||
const autoloadMore = settings.get('autoloadMore');
|
const autoloadMore = settings.get('autoloadMore');
|
||||||
|
|
||||||
|
// Preserve scroll position
|
||||||
|
const scrollPositionKey = `soapbox:scrollPosition:${location.pathname}`;
|
||||||
|
const scrollPosition = Number(sessionStorage.getItem(scrollPositionKey));
|
||||||
|
const initialScrollTop = useRef(scrollPosition);
|
||||||
|
const scroller = useRef<Window | HTMLElement | null>(null);
|
||||||
|
|
||||||
/** Normalized children */
|
/** Normalized children */
|
||||||
const elements = Array.from(children || []);
|
const elements = Array.from(children || []);
|
||||||
|
|
||||||
|
@ -85,6 +90,15 @@ const ScrollableList = React.forwardRef<VirtuosoHandle, IScrollableList>(({
|
||||||
data.push(<Spinner />);
|
data.push(<Spinner />);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
sessionStorage.removeItem(scrollPositionKey);
|
||||||
|
|
||||||
|
// On unmount, save the scroll position.
|
||||||
|
return () => {
|
||||||
|
sessionStorage.setItem(scrollPositionKey, String(document.documentElement.scrollTop));
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
/* Render an empty state instead of the scrollable list */
|
/* Render an empty state instead of the scrollable list */
|
||||||
const renderEmpty = (): JSX.Element => {
|
const renderEmpty = (): JSX.Element => {
|
||||||
return (
|
return (
|
||||||
|
@ -130,6 +144,7 @@ const ScrollableList = React.forwardRef<VirtuosoHandle, IScrollableList>(({
|
||||||
<Virtuoso
|
<Virtuoso
|
||||||
ref={ref}
|
ref={ref}
|
||||||
useWindowScroll
|
useWindowScroll
|
||||||
|
initialScrollTop={initialScrollTop.current}
|
||||||
className={className}
|
className={className}
|
||||||
data={data}
|
data={data}
|
||||||
startReached={onScrollToTop}
|
startReached={onScrollToTop}
|
||||||
|
@ -149,7 +164,7 @@ const ScrollableList = React.forwardRef<VirtuosoHandle, IScrollableList>(({
|
||||||
Item,
|
Item,
|
||||||
Footer: loadMore,
|
Footer: loadMore,
|
||||||
}}
|
}}
|
||||||
scrollerRef={scrollerRef}
|
scrollerRef={c => scroller.current = c}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
Ładowanie…
Reference in New Issue