Skip to content
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

Refactor tooltip #14048

Merged
merged 3 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 0 additions & 4 deletions src/components/Hoverable/hoverablePropTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ const propTypes = {

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

// If the mouse clicks outside, should we dismiss hover?
resetsOnClickOutside: PropTypes.bool,
};

const defaultProps = {
absolute: false,
containerStyles: [],
onHoverIn: () => {},
onHoverOut: () => {},
resetsOnClickOutside: false,
};

export {
Expand Down
24 changes: 18 additions & 6 deletions src/components/Hoverable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class Hoverable extends Component {
if (!this.state.isHovered) {
return;
}
if (this.props.resetsOnClickOutside) {
this.setIsHovered(false);
return;
}
if (this.wrapperView && !this.wrapperView.contains(event.target)) {
this.setIsHovered(false);
}
Expand All @@ -93,8 +89,24 @@ class Hoverable extends Component {
ref(el);
}
},
onMouseEnter: () => this.setIsHovered(true),
onMouseLeave: () => this.setIsHovered(false),
onMouseEnter: (el) => {
this.setIsHovered(true);

// Call the original onMouseEnter, if any
const {onMouseEnter} = this.props.children;
if (_.isFunction(onMouseEnter)) {
onMouseEnter(el);
}
},
onMouseLeave: (el) => {
this.setIsHovered(false);

// Call the original onMouseLeave, if any
const {onMouseLeave} = this.props.children;
if (_.isFunction(onMouseLeave)) {
onMouseLeave(el);
}
},
});
}
return (
Expand Down
2 changes: 2 additions & 0 deletions src/components/Modal/BaseModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as StyleUtils from '../../styles/StyleUtils';
import themeColors from '../../styles/themes/default';
import {propTypes as modalPropTypes, defaultProps as modalDefaultProps} from './modalPropTypes';
import * as Modal from '../../libs/actions/Modal';
import DomUtils from '../../libs/DomUtils';
import getModalStyles from '../../styles/getModalStyles';
import variables from '../../styles/variables';

Expand Down Expand Up @@ -90,6 +91,7 @@ class BaseModal extends PureComponent {
// Note: Escape key on web/desktop will trigger onBackButtonPress callback
// eslint-disable-next-line react/jsx-props-no-multi-spaces
onBackButtonPress={this.props.onClose}
onModalWillShow={DomUtils.blurActiveElement}
onModalShow={() => {
if (this.props.shouldSetModalVisibility) {
Modal.setModalVisibility(true);
Expand Down
25 changes: 21 additions & 4 deletions src/components/Tooltip/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import withWindowDimensions from '../withWindowDimensions';
import {propTypes, defaultProps} from './tooltipPropTypes';
import TooltipSense from './TooltipSense';
import makeCancellablePromise from '../../libs/MakeCancellablePromise';
import * as Browser from '../../libs/Browser';

class Tooltip extends PureComponent {
constructor(props) {
Expand Down Expand Up @@ -63,7 +64,7 @@ class Tooltip extends PureComponent {
getWrapperPosition() {
return new Promise(((resolve) => {
// Make sure the wrapper is mounted before attempting to measure it.
if (this.wrapperView) {
if (this.wrapperView && _.isFunction(this.wrapperView.measureInWindow)) {
this.wrapperView.measureInWindow((x, y, width, height) => resolve({
x, y, width, height,
}));
Expand All @@ -79,6 +80,12 @@ class Tooltip extends PureComponent {
* Display the tooltip in an animation.
*/
showTooltip() {
// On mWeb we do not show Tooltips as there are no way to hide them besides blurring.
// That's due to that fact that on mWeb there is no MouseLeave events.
if (Browser.isMobile()) {
return;
}

if (!this.state.isRendered) {
this.setState({isRendered: true});
}
Expand Down Expand Up @@ -148,6 +155,8 @@ class Tooltip extends PureComponent {
<View
ref={el => this.wrapperView = el}
style={this.props.containerStyles}
onBlur={this.hideTooltip}
focusable
>
{this.props.children}
</View>
Expand All @@ -156,15 +165,24 @@ class Tooltip extends PureComponent {
if (this.props.absolute && React.isValidElement(this.props.children)) {
child = React.cloneElement(React.Children.only(this.props.children), {
ref: (el) => {
// Keep your own reference
this.wrapperView = el;

// Call the original ref, if any
const {ref} = this.props.children;
if (typeof ref === 'function') {
if (_.isFunction(ref)) {
ref(el);
}
},
onBlur: (el) => {
this.hideTooltip();

// Call the original onBlur, if any
const {onBlur} = this.props.children;
if (_.isFunction(onBlur)) {
onBlur(el);
}
},
focusable: true,
});
}
return (
Expand All @@ -189,7 +207,6 @@ class Tooltip extends PureComponent {
containerStyles={this.props.containerStyles}
onHoverIn={this.showTooltip}
onHoverOut={this.hideTooltip}
resetsOnClickOutside
>
{child}
</Hoverable>
Expand Down
7 changes: 7 additions & 0 deletions src/libs/DomUtils/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function blurActiveElement() {
document.activeElement.blur();
}

export default {
blurActiveElement,
};
5 changes: 5 additions & 0 deletions src/libs/DomUtils/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function blurActiveElement() {}

export default {
blurActiveElement,
};
7 changes: 7 additions & 0 deletions src/libs/Navigation/NavigationRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import AppNavigator from './AppNavigator';
import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndicator';
import themeColors from '../../styles/themes/default';
import styles from '../../styles/styles';
import DomUtils from '../DomUtils';
import Log from '../Log';

// https://reactnavigation.org/docs/themes
Expand Down Expand Up @@ -45,6 +46,12 @@ function parseAndLogRoute(state) {
Log.info('Navigating to route', false, {path: currentPath});
}

// Clicking a button that does navigation will stay active even if it's out of view
// and it's tooltip will stay visible.
// We blur the element manually to fix that (especially for Safari).
// More info: https://github.com/Expensify/App/issues/13146
DomUtils.blurActiveElement();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be a problem with this logic. For instance when the page is not changed on navigation. Normally, the navigation is handled by the framework, and clicking on links only changes the URL in the address bar without reloading the page.

This creates a possibility for an implementer to update the content of the page without changing the page.

If that happens, it will interfere with natural behavior. Thus this is dangerous. I always suggest restricting the impact of logic to a sufficient context. stateChange fires literally every change in state which could be anything(not only navigation) and thus this action might cause regressions.

Copy link
Member

@parasharrajat parasharrajat Mar 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this change outdates the function name parseAndLogRoute. We should update that to reflect the new logic.

cc: C+

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are referencing to an outdated code, this has been moved to a better place #15249

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just saw one regression from it #14048 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or does the new place also have the same effects? LMK I will check

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you are referencing to an outdated code,

Oops, I see.


Navigation.setIsNavigationReady();
}

Expand Down