Skip to content

perf: disable hover when scrolling on web and desktop #27236

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

Merged
merged 14 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import Config from 'react-native-config';
import * as KeyCommand from 'react-native-key-command';
import { Platform } from 'react-native';
import * as Url from './libs/Url';
import SCREENS from './SCREENS';

Expand Down Expand Up @@ -2642,6 +2643,10 @@ const CONST = {
HTTPS: 'https',
PUSHER: 'pusher',
},
EVENTS: {
SCROLLING: 'scrolling',
},
IS_DESKTOP_AND_WEB: Platform.OS === 'web' || Platform.OS === 'macos' || Platform.OS === 'windows',
} as const;

export default CONST;
41 changes: 41 additions & 0 deletions src/components/Hoverable/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import _ from 'underscore';
import React, {Component} from 'react';
import { DeviceEventEmitter } from 'react-native';
import {propTypes, defaultProps} from './hoverablePropTypes';
import * as DeviceCapabilities from '../../libs/DeviceCapabilities';
import CONST from '../../CONST';

/**
* It is necessary to create a Hoverable component instead of relying solely on Pressable support for hover state,
Expand All @@ -19,12 +21,37 @@ class Hoverable extends Component {
isHovered: false,
};

this.isHoveredRef = false;
this.isScrollingRef = false;
this.wrapperView = null;
}

componentDidMount() {
document.addEventListener('visibilitychange', this.handleVisibilityChange);
document.addEventListener('mouseover', this.checkHover);

/**
* Only add the scrolling listener if the shouldHandleScroll prop is true
* and the scrollingListener is not already set.
*/
if (!this.scrollingListener && this.props.shouldHandleScroll) {
this.scrollingListener = DeviceEventEmitter.addListener(CONST.EVENTS.SCROLLING, (scrolling) => {
/**
* If user has stopped scrolling and the isHoveredRef is true, then we should update the hover state.
*/
if (!scrolling && this.isHoveredRef) {
this.setState({isHovered: this.isHoveredRef}, this.props.onHoverIn);
} else if (scrolling && this.isHoveredRef) {
/**
* If the user has started scrolling and the isHoveredRef is true, then we should set the hover state to false.
* This is to hide the existing hover and reaction bar.
*/
this.isHoveredRef = false;
this.setState({isHovered: false}, this.props.onHoverOut);
}
this.isScrollingRef = scrolling;
});
}
}

componentDidUpdate(prevProps) {
Expand All @@ -40,6 +67,9 @@ class Hoverable extends Component {
componentWillUnmount() {
document.removeEventListener('visibilitychange', this.handleVisibilityChange);
document.removeEventListener('mouseover', this.checkHover);
if (this.scrollingListener) {
this.scrollingListener.remove();
}
}

/**
Expand All @@ -52,6 +82,17 @@ class Hoverable extends Component {
return;
}

/**
* Capture whther or not the user is hovering over the component.
* We will use this to determine if we should update the hover state when the user has stopped scrolling.
*/
this.isHoveredRef = isHovered;

/**
* If the isScrollingRef is true, then the user is scrolling and we should not update the hover state.
*/
if (this.isScrollingRef && this.props.shouldHandleScroll && !this.state.isHovered) return

if (isHovered !== this.state.isHovered) {
this.setState({isHovered}, isHovered ? this.props.onHoverIn : this.props.onHoverOut);
}
Expand Down
87 changes: 85 additions & 2 deletions src/components/InvertedFlatList/BaseInvertedFlatList.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import _ from 'underscore';
import React, {forwardRef, Component} from 'react';
import PropTypes from 'prop-types';
import {View, FlatList as NativeFlatlist} from 'react-native';
import {View, FlatList as NativeFlatlist, DeviceEventEmitter} from 'react-native';
import * as CollectionUtils from '../../libs/CollectionUtils';
import FlatList from '../FlatList';
import CONST from '../../CONST';

const propTypes = {
/** Same as FlatList can be any array of anything */
Expand All @@ -22,11 +23,15 @@ const propTypes = {

/** Should we measure these items and call getItemLayout? */
shouldMeasureItems: PropTypes.bool,

/** Same as for FlatList */
onScroll: PropTypes.func,
};

const defaultProps = {
data: [],
shouldMeasureItems: false,
onScroll: () => {},
};

class BaseInvertedFlatList extends Component {
Expand All @@ -35,12 +40,53 @@ class BaseInvertedFlatList extends Component {

this.renderItem = this.renderItem.bind(this);
this.getItemLayout = this.getItemLayout.bind(this);
this.handleScroll = this.handleScroll.bind(this);
this.onScroll = this.onScroll.bind(this);
this.onScrollEnd = this.onScrollEnd.bind(this);

// Stores each item's computed height after it renders
// once and is then referenced for the life of this component.
// This is essential to getting FlatList inverted to work on web
// and also enables more predictable scrolling on native platforms.
this.sizeMap = {};

this.lastScrollEvent = null;
this.scrollEndTimeout = null;
this.updateInProgress = false;
this.eventHandler = null;
}

componentWillUnmount() {
if (this.scrollEndTimeout) {
clearTimeout(this.scrollEndTimeout);
}

if (this.eventHandler) {
this.eventHandler.remove();
}
}

/**
* Emits when the scrolling is in progress. Also,
* invokes the onScroll callback function from props.
*
* @param {Event} event - The onScroll event from the FlatList
*/
onScroll(event) {
this.props.onScroll(event);

if (!this.updateInProgress) {
this.updateInProgress = true;
this.eventHandler = DeviceEventEmitter.emit(CONST.EVENTS.SCROLLING, true);
}
}

/**
* Emits when the scrolling has ended.
*/
onScrollEnd() {
this.eventHandler = DeviceEventEmitter.emit(CONST.EVENTS.SCROLLING, false);
this.updateInProgress = false;
}

/**
Expand Down Expand Up @@ -80,6 +126,43 @@ class BaseInvertedFlatList extends Component {
};
}

/**
* Decides whether the scrolling has ended or not. If it has ended,
* then it calls the onScrollEnd function. Otherwise, it calls the
* onScroll function and pass the event to it.
*
* This is a temporary work around, since react-native-web doesn't
* support onScrollBeginDrag and onScrollEndDrag props for FlatList.
* More info:
* https://github.com/necolas/react-native-web/pull/1305
*
* This workaround is taken from below and refactored to fit our needs:
* https://github.com/necolas/react-native-web/issues/1021#issuecomment-984151185
*
* @param {Event} event - The onScroll event from the FlatList
*/
handleScroll(event) {
this.onScroll(event);
const timestamp = Date.now();

if (this.scrollEndTimeout) {
clearTimeout(this.scrollEndTimeout);
}

if (this.lastScrollEvent) {
this.scrollEndTimeout = setTimeout(() => {
if (this.lastScrollEvent !== timestamp) {
return;
}
// Scroll has ended
this.lastScrollEvent = null;
this.onScrollEnd();
}, 250);
}

this.lastScrollEvent = timestamp;
};

/**
* Measure item and cache the returned length (a.k.a. height)
*
Expand Down Expand Up @@ -139,7 +222,7 @@ class BaseInvertedFlatList extends Component {
// We keep this property very low so that chat switching remains fast
maxToRenderPerBatch={1}
windowSize={15}

onScroll={CONST.IS_DESKTOP_AND_WEB ? this.handleScroll : this.props.onScroll}
// Commenting the line below as it breaks the unread indicator test
// we will look at fixing/reusing this after RN v0.72
// maintainVisibleContentPosition={{minIndexForVisible: 0, autoscrollToTopThreshold: 0}}
Expand Down
1 change: 1 addition & 0 deletions src/components/Tooltip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function Tooltip(props) {
<Hoverable
onHoverIn={showTooltip}
onHoverOut={hideTooltip}
shouldHandleScroll={props.shouldHandleScroll}
>
{children}
</Hoverable>
Expand Down
1 change: 1 addition & 0 deletions src/components/UserDetailsTooltip/index.web.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ function UserDetailsTooltip(props) {
shiftHorizontal={props.shiftHorizontal}
renderTooltipContent={renderTooltipContent}
renderTooltipContentKey={[userDisplayName, userLogin]}
shouldHandleScroll
>
{props.children}
</Tooltip>
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ function ReportActionItem(props) {
withoutFocusOnSecondaryInteraction
accessibilityLabel={props.translate('accessibilityHints.chatMessage')}
>
<Hoverable disabled={Boolean(props.draftMessage)}>
<Hoverable shouldHandleScroll={CONST.IS_DESKTOP_AND_WEB} disabled={Boolean(props.draftMessage)}>
{(hovered) => (
<View>
{props.shouldDisplayNewMarker && <UnreadActionIndicator reportActionID={props.action.reportActionID} />}
Expand Down