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

Fix: Long press on image doesn't open the context menu #8018

Merged
merged 8 commits into from
Mar 15, 2022
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
47 changes: 35 additions & 12 deletions src/components/PressableWithSecondaryInteraction/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';
import React, {Component} from 'react';
import {Pressable} from 'react-native';
import {LongPressGestureHandler, State} from 'react-native-gesture-handler';
import SelectionScraper from '../../libs/SelectionScraper';
import * as pressableWithSecondaryInteractionPropTypes from './pressableWithSecondaryInteractionPropTypes';
import styles from '../../styles/styles';
Expand All @@ -11,7 +12,7 @@ import styles from '../../styles/styles';
class PressableWithSecondaryInteraction extends Component {
constructor(props) {
super(props);

this.callSecondaryInteractionWithMappedEvent = this.callSecondaryInteractionWithMappedEvent.bind(this);
this.executeSecondaryInteractionOnContextMenu = this.executeSecondaryInteractionOnContextMenu.bind(this);
}

Expand All @@ -26,6 +27,27 @@ class PressableWithSecondaryInteraction extends Component {
this.pressableRef.removeEventListener('contextmenu', this.executeSecondaryInteractionOnContextMenu);
}

/**
* @param {Object} e
*/
callSecondaryInteractionWithMappedEvent(e) {
if (e.nativeEvent.state !== State.ACTIVE) {
return;
}

// Map gesture event to normal Responder event
const {
absoluteX, absoluteY, locationX, locationY,
} = e.nativeEvent;
ahmdshrif marked this conversation as resolved.
Show resolved Hide resolved
const mapEvent = {
...e,
nativeEvent: {
...e.nativeEvent, pageX: absoluteX, pageY: absoluteY, x: locationX, y: locationY,
},
};
this.props.onSecondaryInteraction(mapEvent);
}

/**
* @param {contextmenu} e - A right-click MouseEvent.
* https://developer.mozilla.org/en-US/docs/Web/API/Element/contextmenu_event
Expand All @@ -44,18 +66,19 @@ class PressableWithSecondaryInteraction extends Component {

// On Web, Text does not support LongPress events thus manage inline mode with styling instead of using Text.
return (
<Pressable
style={this.props.inline && styles.dInline}
onPressIn={this.props.onPressIn}
onLongPress={this.props.onSecondaryInteraction}
onPressOut={this.props.onPressOut}
onPress={this.props.onPress}
ref={el => this.pressableRef = el}
<LongPressGestureHandler onHandlerStateChange={this.callSecondaryInteractionWithMappedEvent}>
<Pressable
style={this.props.inline && styles.dInline}
onPressIn={this.props.onPressIn}
onPressOut={this.props.onPressOut}
onPress={this.props.onPress}
ref={el => this.pressableRef = el}
// eslint-disable-next-line react/jsx-props-no-spreading
{...defaultPressableProps}
>
{this.props.children}
</Pressable>
{...defaultPressableProps}
>
{this.props.children}
</Pressable>
</LongPressGestureHandler>
);
}
}
Expand Down
28 changes: 18 additions & 10 deletions src/components/PressableWithSecondaryInteraction/index.native.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import _ from 'underscore';
import React, {forwardRef} from 'react';
import {Pressable} from 'react-native';
import {LongPressGestureHandler, State} from 'react-native-gesture-handler';
ahmdshrif marked this conversation as resolved.
Show resolved Hide resolved
import * as pressableWithSecondaryInteractionPropTypes from './pressableWithSecondaryInteractionPropTypes';
import Text from '../Text';
import HapticFeedback from '../../libs/HapticFeedback';
Expand All @@ -15,21 +16,28 @@ const PressableWithSecondaryInteraction = (props) => {
// Use Text node for inline mode to prevent content overflow.
const Node = props.inline ? Text : Pressable;
return (
<Node
ref={props.forwardedRef}
onPress={props.onPress}
onPressIn={props.onPressIn}
onLongPress={(e) => {
<LongPressGestureHandler
onHandlerStateChange={(e) => {
if (e.nativeEvent.state !== State.ACTIVE) {
return;
}
e.preventDefault();
HapticFeedback.trigger();
props.onSecondaryInteraction(e);
}}
onPressOut={props.onPressOut}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(_.omit(props, 'onLongPress'))}
>
{props.children}
</Node>
<Node
ref={props.forwardedRef}
onPress={props.onPress}
onPressIn={props.onPressIn}
onPressOut={props.onPressOut}
// eslint-disable-next-line react/jsx-props-no-spreading
{...(_.omit(props, 'onLongPress'))}
>
{props.children}
</Node>
</LongPressGestureHandler>

);
};

Expand Down
4 changes: 3 additions & 1 deletion src/pages/home/report/ContextMenu/ContextMenuActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ export default [
icon: Expensicons.Clipboard,
successTextTranslateKey: 'reportActionContextMenu.copied',
successIcon: Expensicons.Checkmark,
shouldShow: (type, reportAction) => (type === CONTEXT_MENU_TYPES.REPORT_ACTION && reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU),
shouldShow: (type, reportAction) => (type === CONTEXT_MENU_TYPES.REPORT_ACTION
&& reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU
&& !ReportUtils.isReportMessageAttachment(lodashGet(reportAction, ['message', 0, 'text'], ''))),

// If return value is true, we switch the `text` and `icon` on
// `ContextMenuItem` with `successText` and `successIcon` which will fallback to
Expand Down