Avoid excessive re-render of room list and member list (#31131)
* fix(list view): avoid re-create `onFocus` function at each render of the child items * fix(room list): update `onFocus` signature * fix(member list): update `onFocus` signature * fix(room list): avoid re-render at the beginning and end of the scroll * test(room list): remove scrolling test and props * test(member list): update member tile view tests * test(room list): update `ListView` focus test * test(member list): add `onFocus` test for member list tile
This commit is contained in:
@@ -42,7 +42,7 @@ export interface IListViewProps<Item, Context>
|
||||
index: number,
|
||||
item: Item,
|
||||
context: ListContext<Context>,
|
||||
onFocus: (e: React.FocusEvent) => void,
|
||||
onFocus: (item: Item, e: React.FocusEvent) => void,
|
||||
) => JSX.Element;
|
||||
|
||||
/**
|
||||
@@ -230,19 +230,26 @@ export function ListView<Item, Context = any>(props: IListViewProps<Item, Contex
|
||||
virtuosoDomRef.current = element;
|
||||
}, []);
|
||||
|
||||
const getItemComponentInternal = useCallback(
|
||||
(index: number, item: Item, context: ListContext<Context>): JSX.Element => {
|
||||
const onFocus = (e: React.FocusEvent): void => {
|
||||
// If one of the item components has been focused directly, set the focused and tabIndex state
|
||||
// and stop propagation so the ListViews onFocus doesn't also handle it.
|
||||
const key = getItemKey(item);
|
||||
setIsFocused(true);
|
||||
setTabIndexKey(key);
|
||||
e.stopPropagation();
|
||||
};
|
||||
return getItemComponent(index, item, context, onFocus);
|
||||
/**
|
||||
* Focus handler passed to each item component.
|
||||
* Don't declare inside getItemComponent to avoid re-creating on each render.
|
||||
*/
|
||||
const onFocusForGetItemComponent = useCallback(
|
||||
(item: Item, e: React.FocusEvent) => {
|
||||
// If one of the item components has been focused directly, set the focused and tabIndex state
|
||||
// and stop propagation so the ListViews onFocus doesn't also handle it.
|
||||
const key = getItemKey(item);
|
||||
setIsFocused(true);
|
||||
setTabIndexKey(key);
|
||||
e.stopPropagation();
|
||||
},
|
||||
[getItemComponent, getItemKey],
|
||||
[getItemKey],
|
||||
);
|
||||
|
||||
const getItemComponentInternal = useCallback(
|
||||
(index: number, item: Item, context: ListContext<Context>): JSX.Element =>
|
||||
getItemComponent(index, item, context, onFocusForGetItemComponent),
|
||||
[getItemComponent, onFocusForGetItemComponent],
|
||||
);
|
||||
/**
|
||||
* Handles focus events on the list.
|
||||
|
||||
@@ -45,7 +45,7 @@ const MemberListView: React.FC<IProps> = (props: IProps) => {
|
||||
index: number,
|
||||
item: MemberWithSeparator,
|
||||
context: ListContext<any>,
|
||||
onFocus: (e: React.FocusEvent) => void,
|
||||
onFocus: (item: MemberWithSeparator, e: React.FocusEvent) => void,
|
||||
): JSX.Element => {
|
||||
const itemKey = getItemKey(item);
|
||||
const isRovingItem = itemKey === context.tabIndexKey;
|
||||
@@ -55,6 +55,7 @@ const MemberListView: React.FC<IProps> = (props: IProps) => {
|
||||
} else if (item.member) {
|
||||
return (
|
||||
<RoomMemberTileView
|
||||
item={item}
|
||||
member={item.member}
|
||||
showPresence={isPresenceEnabled}
|
||||
focused={focused}
|
||||
@@ -67,6 +68,7 @@ const MemberListView: React.FC<IProps> = (props: IProps) => {
|
||||
} else {
|
||||
return (
|
||||
<ThreePidInviteTileView
|
||||
item={item}
|
||||
threePidInvite={item.threePidInvite}
|
||||
focused={focused}
|
||||
tabIndex={isRovingItem ? 0 : -1}
|
||||
|
||||
@@ -16,15 +16,20 @@ import BaseAvatar from "../../../avatars/BaseAvatar";
|
||||
import { _t } from "../../../../../languageHandler";
|
||||
import { MemberTileView } from "./common/MemberTileView";
|
||||
import { InvitedIconView } from "./common/InvitedIconView";
|
||||
import { type MemberWithSeparator } from "../../../../viewmodels/memberlist/MemberListViewModel";
|
||||
|
||||
interface IProps {
|
||||
/**
|
||||
* Needed for `onFocus`
|
||||
*/
|
||||
item: MemberWithSeparator;
|
||||
member: RoomMember;
|
||||
index: number;
|
||||
memberCount: number;
|
||||
showPresence?: boolean;
|
||||
focused?: boolean;
|
||||
tabIndex?: number;
|
||||
onFocus: (e: React.FocusEvent) => void;
|
||||
onFocus: (item: MemberWithSeparator, e: React.FocusEvent) => void;
|
||||
}
|
||||
|
||||
export function RoomMemberTileView(props: IProps): JSX.Element {
|
||||
@@ -60,7 +65,7 @@ export function RoomMemberTileView(props: IProps): JSX.Element {
|
||||
return (
|
||||
<MemberTileView
|
||||
onClick={vm.onClick}
|
||||
onFocus={props.onFocus}
|
||||
onFocus={(e) => props.onFocus(props.item, e)}
|
||||
avatarJsx={av}
|
||||
presenceJsx={presenceJSX}
|
||||
nameJsx={nameJSX}
|
||||
|
||||
@@ -12,14 +12,19 @@ import { type ThreePIDInvite } from "../../../../../models/rooms/ThreePIDInvite"
|
||||
import BaseAvatar from "../../../avatars/BaseAvatar";
|
||||
import { MemberTileView } from "./common/MemberTileView";
|
||||
import { InvitedIconView } from "./common/InvitedIconView";
|
||||
import { type MemberWithSeparator } from "../../../../viewmodels/memberlist/MemberListViewModel";
|
||||
|
||||
interface Props {
|
||||
/**
|
||||
* Needed for `onFocus`
|
||||
*/
|
||||
item: MemberWithSeparator;
|
||||
threePidInvite: ThreePIDInvite;
|
||||
memberIndex: number;
|
||||
memberCount: number;
|
||||
focused?: boolean;
|
||||
tabIndex?: number;
|
||||
onFocus: (e: React.FocusEvent) => void;
|
||||
onFocus: (item: MemberWithSeparator, e: React.FocusEvent) => void;
|
||||
}
|
||||
|
||||
export function ThreePidInviteTileView(props: Props): JSX.Element {
|
||||
@@ -40,7 +45,7 @@ export function ThreePidInviteTileView(props: Props): JSX.Element {
|
||||
iconJsx={iconJsx}
|
||||
focused={props.focused}
|
||||
tabIndex={props.tabIndex}
|
||||
onFocus={props.onFocus}
|
||||
onFocus={(e) => props.onFocus(props.item, e)}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* Please see LICENSE files in the repository root for full details.
|
||||
*/
|
||||
|
||||
import React, { useCallback, useRef, useState, type JSX } from "react";
|
||||
import React, { useCallback, useRef, type JSX } from "react";
|
||||
import { type Room } from "matrix-js-sdk/src/matrix";
|
||||
import { type ScrollIntoViewLocation } from "react-virtuoso";
|
||||
import { isEqual } from "lodash";
|
||||
@@ -44,7 +44,6 @@ export function RoomList({ vm: { roomsResult, activeIndex } }: RoomListProps): J
|
||||
const lastSpaceId = useRef<string | undefined>(undefined);
|
||||
const lastFilterKeys = useRef<FilterKey[] | undefined>(undefined);
|
||||
const roomCount = roomsResult.rooms.length;
|
||||
const [isScrolling, setIsScrolling] = useState(false);
|
||||
const getItemComponent = useCallback(
|
||||
(
|
||||
index: number,
|
||||
@@ -53,7 +52,7 @@ export function RoomList({ vm: { roomsResult, activeIndex } }: RoomListProps): J
|
||||
spaceId: string;
|
||||
filterKeys: FilterKey[] | undefined;
|
||||
}>,
|
||||
onFocus: (e: React.FocusEvent) => void,
|
||||
onFocus: (item: Room, e: React.FocusEvent) => void,
|
||||
): JSX.Element => {
|
||||
const itemKey = item.roomId;
|
||||
const isRovingItem = itemKey === context.tabIndexKey;
|
||||
@@ -69,11 +68,10 @@ export function RoomList({ vm: { roomsResult, activeIndex } }: RoomListProps): J
|
||||
roomIndex={index}
|
||||
roomCount={roomCount}
|
||||
onFocus={onFocus}
|
||||
listIsScrolling={isScrolling}
|
||||
/>
|
||||
);
|
||||
},
|
||||
[activeIndex, roomCount, isScrolling],
|
||||
[activeIndex, roomCount],
|
||||
);
|
||||
|
||||
const getItemKey = useCallback((item: Room): string => {
|
||||
@@ -129,7 +127,6 @@ export function RoomList({ vm: { roomsResult, activeIndex } }: RoomListProps): J
|
||||
getItemKey={getItemKey}
|
||||
isItemFocusable={() => true}
|
||||
onKeyDown={keyDownCallback}
|
||||
isScrolling={setIsScrolling}
|
||||
increaseViewportBy={{
|
||||
bottom: EXTENDED_VIEWPORT_HEIGHT,
|
||||
top: EXTENDED_VIEWPORT_HEIGHT,
|
||||
|
||||
@@ -16,7 +16,7 @@ import { NotificationDecoration } from "../NotificationDecoration";
|
||||
import { RoomAvatarView } from "../../avatars/RoomAvatarView";
|
||||
import { RoomListItemContextMenuView } from "./RoomListItemContextMenuView";
|
||||
|
||||
interface RoomListItemViewProps extends React.HTMLAttributes<HTMLButtonElement> {
|
||||
interface RoomListItemViewProps extends Omit<React.HTMLAttributes<HTMLButtonElement>, "onFocus"> {
|
||||
/**
|
||||
* The room to display
|
||||
*/
|
||||
@@ -32,7 +32,7 @@ interface RoomListItemViewProps extends React.HTMLAttributes<HTMLButtonElement>
|
||||
/**
|
||||
* A callback that indicates the item has received focus
|
||||
*/
|
||||
onFocus: (e: React.FocusEvent) => void;
|
||||
onFocus: (room: Room, e: React.FocusEvent) => void;
|
||||
/**
|
||||
* The index of the room in the list
|
||||
*/
|
||||
@@ -41,10 +41,6 @@ interface RoomListItemViewProps extends React.HTMLAttributes<HTMLButtonElement>
|
||||
* The total number of rooms in the list
|
||||
*/
|
||||
roomCount: number;
|
||||
/**
|
||||
* Whether the list is currently scrolling
|
||||
*/
|
||||
listIsScrolling: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,7 +53,6 @@ export const RoomListItemView = memo(function RoomListItemView({
|
||||
onFocus,
|
||||
roomIndex: index,
|
||||
roomCount: count,
|
||||
listIsScrolling,
|
||||
...props
|
||||
}: RoomListItemViewProps): JSX.Element {
|
||||
const ref = useRef<HTMLButtonElement>(null);
|
||||
@@ -100,7 +95,7 @@ export const RoomListItemView = memo(function RoomListItemView({
|
||||
aria-selected={isSelected}
|
||||
aria-label={vm.a11yLabel}
|
||||
onClick={() => vm.openRoom()}
|
||||
onFocus={onFocus}
|
||||
onFocus={(e: React.FocusEvent<HTMLButtonElement>) => onFocus(room, e)}
|
||||
onMouseOver={() => setHover(true)}
|
||||
onMouseOut={() => setHover(false)}
|
||||
onBlur={() => setHover(false)}
|
||||
@@ -148,9 +143,7 @@ export const RoomListItemView = memo(function RoomListItemView({
|
||||
|
||||
// Rendering multiple context menus can causes crashes in radix upstream,
|
||||
// See https://github.com/radix-ui/primitives/issues/2717.
|
||||
// We also don't need the context menu while scrolling so can improve scroll performance
|
||||
// by not rendering it.
|
||||
if (!vm.showContextMenu || listIsScrolling) return content;
|
||||
if (!vm.showContextMenu) return content;
|
||||
|
||||
return (
|
||||
<RoomListItemContextMenuView
|
||||
|
||||
Reference in New Issue
Block a user