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 13 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
3 changes: 3 additions & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2643,6 +2643,9 @@ const CONST = {
HTTPS: 'https',
PUSHER: 'pusher',
},
EVENTS: {
SCROLLING: 'scrolling',
},
} as const;

export default CONST;
4 changes: 4 additions & 0 deletions src/components/Hoverable/hoverablePropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ const propTypes = {

/** Function that executes when the mouse leaves the children. */
onHoverOut: PropTypes.func,

/** decides whether to handle the scroll behaviour to show hover once the scroll ends */
shouldHandleScroll: PropTypes.bool,
};

const defaultProps = {
disabled: false,
onHoverIn: () => {},
onHoverOut: () => {},
shouldHandleScroll: false,
};

export {propTypes, defaultProps};
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
85 changes: 83 additions & 2 deletions src/components/InvertedFlatList/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, {forwardRef, useEffect} from 'react';
import React, {forwardRef, useEffect, useRef} from 'react';
import PropTypes from 'prop-types';
import {FlatList, StyleSheet} from 'react-native';
import {DeviceEventEmitter, FlatList, StyleSheet} from 'react-native';
import _ from 'underscore';
import BaseInvertedFlatList from './BaseInvertedFlatList';
import styles from '../../styles/styles';
import CONST from '../../CONST';

const propTypes = {
/** Passed via forwardRef so we can access the FlatList ref */
Expand All @@ -14,6 +15,9 @@ const propTypes = {
/** Any additional styles to apply */
// eslint-disable-next-line react/forbid-prop-types
contentContainerStyle: PropTypes.any,

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

// This is adapted from https://codesandbox.io/s/react-native-dsyse
Expand All @@ -22,15 +26,90 @@ function InvertedFlatList(props) {
const {innerRef, contentContainerStyle} = props;
const listRef = React.createRef();

const lastScrollEvent = useRef(null);
const scrollEndTimeout = useRef(null);
const updateInProgress = useRef(false);
const eventHandler = useRef(null);

useEffect(() => {
if (!_.isFunction(innerRef)) {
// eslint-disable-next-line no-param-reassign
innerRef.current = listRef.current;
} else {
innerRef(listRef);
}

return () => {
if (scrollEndTimeout.current) {
clearTimeout(scrollEndTimeout.current);
}

if (eventHandler.current) {
eventHandler.current.remove();
}
};
}, [innerRef, listRef]);

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

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

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

/**
* 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
*/
const handleScroll = (event) => {
onScroll(event);
const timestamp = Date.now();

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

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

lastScrollEvent.current = timestamp;
};

return (
<BaseInvertedFlatList
// eslint-disable-next-line react/jsx-props-no-spreading
Expand All @@ -39,13 +118,15 @@ function InvertedFlatList(props) {
ref={listRef}
shouldMeasureItems
contentContainerStyle={StyleSheet.compose(contentContainerStyle, styles.justifyContentEnd)}
onScroll={handleScroll}
/>
);
}

InvertedFlatList.propTypes = propTypes;
InvertedFlatList.defaultProps = {
contentContainerStyle: {},
onScroll: () => {},
};

export default forwardRef((props, ref) => (
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
4 changes: 4 additions & 0 deletions src/components/Tooltip/tooltipPropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const propTypes = {

/** Unique key of renderTooltipContent to rerender the tooltip when one of the key changes */
renderTooltipContentKey: PropTypes.arrayOf(PropTypes.string),

/** passes this down to Hoverable component to decide whether to handle the scroll behaviour to show hover once the scroll ends */
shouldHandleScroll: PropTypes.bool,
};

const defaultProps = {
Expand All @@ -38,6 +41,7 @@ const defaultProps = {
numberOfLines: CONST.TOOLTIP_MAX_LINES,
renderTooltipContent: undefined,
renderTooltipContentKey: [],
shouldHandleScroll: false,
};

export {propTypes, defaultProps};
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
5 changes: 4 additions & 1 deletion src/pages/home/report/ReportActionItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,10 @@ function ReportActionItem(props) {
withoutFocusOnSecondaryInteraction
accessibilityLabel={props.translate('accessibilityHints.chatMessage')}
>
<Hoverable disabled={Boolean(props.draftMessage)}>
<Hoverable
shouldHandleScroll
disabled={Boolean(props.draftMessage)}
>
{(hovered) => (
<View>
{props.shouldDisplayNewMarker && <UnreadActionIndicator reportActionID={props.action.reportActionID} />}
Expand Down