Focus the active item in the thread

This commit is contained in:
Alex Gleason 2022-05-13 20:23:03 -05:00
parent f3ce519fce
commit 1307b436b1
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 24 additions and 18 deletions

View file

@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { Virtuoso, Components } 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';
import { useSettings } from 'soapbox/hooks'; import { useSettings } from 'soapbox/hooks';
@ -25,7 +25,7 @@ const List: Components<Context>['List'] = React.forwardRef((props, ref) => {
return <div ref={ref} className={context?.listClassName} {...rest} />; return <div ref={ref} className={context?.listClassName} {...rest} />;
}); });
interface IScrollableList { interface IScrollableList extends VirtuosoProps<any, any> {
scrollKey?: string, scrollKey?: string,
onLoadMore?: () => void, onLoadMore?: () => void,
isLoading?: boolean, isLoading?: boolean,
@ -45,7 +45,7 @@ interface IScrollableList {
} }
/** Legacy ScrollableList with Virtuoso for backwards-compatibility */ /** Legacy ScrollableList with Virtuoso for backwards-compatibility */
const ScrollableList: React.FC<IScrollableList> = ({ const ScrollableList = React.forwardRef<VirtuosoHandle, IScrollableList>(({
prepend = null, prepend = null,
alwaysPrepend, alwaysPrepend,
children, children,
@ -61,7 +61,9 @@ const ScrollableList: React.FC<IScrollableList> = ({
hasMore, hasMore,
placeholderComponent: Placeholder, placeholderComponent: Placeholder,
placeholderCount = 0, placeholderCount = 0,
}) => { initialTopMostItemIndex = 0,
scrollerRef,
}, ref) => {
const settings = useSettings(); const settings = useSettings();
const autoloadMore = settings.get('autoloadMore'); const autoloadMore = settings.get('autoloadMore');
@ -126,6 +128,7 @@ const ScrollableList: React.FC<IScrollableList> = ({
/** Render the actual Virtuoso list */ /** Render the actual Virtuoso list */
const renderFeed = (): JSX.Element => ( const renderFeed = (): JSX.Element => (
<Virtuoso <Virtuoso
ref={ref}
useWindowScroll useWindowScroll
className={className} className={className}
data={data} data={data}
@ -133,6 +136,7 @@ const ScrollableList: React.FC<IScrollableList> = ({
endReached={handleEndReached} endReached={handleEndReached}
isScrolling={isScrolling => isScrolling && onScroll && onScroll()} isScrolling={isScrolling => isScrolling && onScroll && onScroll()}
itemContent={renderItem} itemContent={renderItem}
initialTopMostItemIndex={showLoading ? 0 : initialTopMostItemIndex}
context={{ context={{
listClassName: className, listClassName: className,
itemClassName, itemClassName,
@ -145,6 +149,7 @@ const ScrollableList: React.FC<IScrollableList> = ({
Item, Item,
Footer: loadMore, Footer: loadMore,
}} }}
scrollerRef={scrollerRef}
/> />
); );
@ -162,6 +167,6 @@ const ScrollableList: React.FC<IScrollableList> = ({
{renderBody()} {renderBody()}
</PullToRefresh> </PullToRefresh>
); );
}; });
export default ScrollableList; export default ScrollableList;

View file

@ -65,6 +65,7 @@ import ThreadStatus from './components/thread-status';
import type { AxiosError } from 'axios'; import type { AxiosError } from 'axios';
import type { History } from 'history'; import type { History } from 'history';
import type { VirtuosoHandle } from 'react-virtuoso';
import type { AnyAction } from 'redux'; import type { AnyAction } from 'redux';
import type { ThunkDispatch } from 'redux-thunk'; import type { ThunkDispatch } from 'redux-thunk';
import type { RootState } from 'soapbox/store'; import type { RootState } from 'soapbox/store';
@ -210,6 +211,7 @@ class Status extends ImmutablePureComponent<IStatus, IStatusState> {
node: HTMLDivElement | null = null; node: HTMLDivElement | null = null;
status: HTMLDivElement | null = null; status: HTMLDivElement | null = null;
scroller: VirtuosoHandle | null = null;
_scrolledIntoView: boolean = false; _scrolledIntoView: boolean = false;
fetchData = async() => { fetchData = async() => {
@ -615,11 +617,10 @@ class Status extends ImmutablePureComponent<IStatus, IStatusState> {
} }
componentDidUpdate(prevProps: IStatus, prevState: IStatusState) { componentDidUpdate(prevProps: IStatus, prevState: IStatusState) {
const { params, status, displayMedia } = this.props; const { params, status, displayMedia, ancestorsIds } = this.props;
const { ancestorsIds } = prevProps; const { isLoaded } = this.state;
if (params.statusId !== prevProps.params.statusId) { if (params.statusId !== prevProps.params.statusId) {
this._scrolledIntoView = false;
this.fetchData(); this.fetchData();
} }
@ -627,17 +628,11 @@ class Status extends ImmutablePureComponent<IStatus, IStatusState> {
this.setState({ showMedia: defaultMediaVisibility(status, displayMedia), loadedStatusId: status.id }); this.setState({ showMedia: defaultMediaVisibility(status, displayMedia), loadedStatusId: status.id });
} }
if (this._scrolledIntoView) { if (params.statusId !== prevProps.params.statusId || status?.id !== prevProps.status?.id || ancestorsIds.size > prevProps.ancestorsIds.size || isLoaded !== prevState.isLoaded) {
return; this.scroller?.scrollToIndex({
} index: this.props.ancestorsIds.size,
offset: -80,
if (prevProps.status && ancestorsIds && ancestorsIds.size > 0 && this.node) {
const element = this.node.querySelector('.detailed-status');
window.requestAnimationFrame(() => {
element?.scrollIntoView(true);
}); });
this._scrolledIntoView = true;
} }
} }
@ -671,6 +666,10 @@ class Status extends ImmutablePureComponent<IStatus, IStatusState> {
})); }));
} }
setScrollerRef = (c: VirtuosoHandle) => {
this.scroller = c;
}
render() { render() {
const { me, status, ancestorsIds, descendantsIds, intl } = this.props; const { me, status, ancestorsIds, descendantsIds, intl } = this.props;
@ -788,10 +787,12 @@ class Status extends ImmutablePureComponent<IStatus, IStatusState> {
<Stack space={2}> <Stack space={2}>
<div ref={this.setRef} className='thread'> <div ref={this.setRef} className='thread'>
<ScrollableList <ScrollableList
ref={this.setScrollerRef}
onRefresh={this.handleRefresh} onRefresh={this.handleRefresh}
hasMore={!!this.state.next} hasMore={!!this.state.next}
onLoadMore={this.handleLoadMore} onLoadMore={this.handleLoadMore}
placeholderComponent={() => <PlaceholderStatus thread />} placeholderComponent={() => <PlaceholderStatus thread />}
initialTopMostItemIndex={ancestorsIds.size}
> >
{children} {children}
</ScrollableList> </ScrollableList>