-
Notifications
You must be signed in to change notification settings - Fork 3k
/
index.js
145 lines (126 loc) · 5.11 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import PropTypes from 'prop-types';
import React, {useRef, useState} from 'react';
import {View} from 'react-native';
import _ from 'underscore';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import PopoverMenu from '@components/PopoverMenu';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import Tooltip from '@components/Tooltip/PopoverAnchorTooltip';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as Browser from '@libs/Browser';
import CONST from '@src/CONST';
import ThreeDotsMenuItemPropTypes from './ThreeDotsMenuItemPropTypes';
const propTypes = {
/** Tooltip for the popup icon */
iconTooltip: PropTypes.string,
/** icon for the popup trigger */
icon: PropTypes.oneOfType([PropTypes.elementType, PropTypes.string]),
/** Any additional styles to pass to the icon container. */
// eslint-disable-next-line react/forbid-prop-types
iconStyles: PropTypes.arrayOf(PropTypes.object),
/** The fill color to pass into the icon. */
iconFill: PropTypes.string,
/** Function to call on icon press */
onIconPress: PropTypes.func,
/** menuItems that'll show up on toggle of the popup menu */
menuItems: ThreeDotsMenuItemPropTypes.isRequired,
/** The anchor position of the menu */
anchorPosition: PropTypes.shape({
top: PropTypes.number,
right: PropTypes.number,
bottom: PropTypes.number,
left: PropTypes.number,
}).isRequired,
/** The anchor alignment of the menu */
anchorAlignment: PropTypes.shape({
horizontal: PropTypes.oneOf(_.values(CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL)),
vertical: PropTypes.oneOf(_.values(CONST.MODAL.ANCHOR_ORIGIN_VERTICAL)),
}),
/** Whether the popover menu should overlay the current view */
shouldOverlay: PropTypes.bool,
/** Whether the menu is disabled */
disabled: PropTypes.bool,
/** Should we announce the Modal visibility changes? */
shouldSetModalVisibility: PropTypes.bool,
};
const defaultProps = {
iconTooltip: 'common.more',
disabled: false,
iconFill: undefined,
iconStyles: [],
icon: Expensicons.ThreeDots,
onIconPress: () => {},
anchorAlignment: {
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.TOP, // we assume that popover menu opens below the button, anchor is at TOP
},
shouldOverlay: false,
shouldSetModalVisibility: true,
};
function ThreeDotsMenu({iconTooltip, icon, iconFill, iconStyles, onIconPress, menuItems, anchorPosition, anchorAlignment, shouldOverlay, shouldSetModalVisibility, disabled}) {
const styles = useThemeStyles();
const [isPopupMenuVisible, setPopupMenuVisible] = useState(false);
const buttonRef = useRef(null);
const {translate} = useLocalize();
const showPopoverMenu = () => {
setPopupMenuVisible(true);
};
const hidePopoverMenu = () => {
setPopupMenuVisible(false);
};
return (
<>
<View>
<Tooltip text={translate(iconTooltip)}>
<PressableWithoutFeedback
onPress={() => {
if (isPopupMenuVisible) {
hidePopoverMenu();
return;
}
showPopoverMenu();
if (onIconPress) {
onIconPress();
}
}}
disabled={disabled}
onMouseDown={(e) => {
/* Keep the focus state on mWeb like we did on the native apps. */
if (!Browser.isMobile()) {
return;
}
e.preventDefault();
}}
ref={buttonRef}
style={[styles.touchableButtonImage, ...iconStyles]}
role={CONST.ROLE.BUTTON}
accessibilityLabel={translate(iconTooltip)}
>
<Icon
src={icon}
fill={iconFill}
/>
</PressableWithoutFeedback>
</Tooltip>
</View>
<PopoverMenu
onClose={hidePopoverMenu}
isVisible={isPopupMenuVisible}
anchorPosition={anchorPosition}
anchorAlignment={anchorAlignment}
onItemSelected={hidePopoverMenu}
menuItems={menuItems}
withoutOverlay={!shouldOverlay}
shouldSetModalVisibility={shouldSetModalVisibility}
anchorRef={buttonRef}
/>
</>
);
}
ThreeDotsMenu.propTypes = propTypes;
ThreeDotsMenu.defaultProps = defaultProps;
ThreeDotsMenu.displayName = 'ThreeDotsMenu';
export default ThreeDotsMenu;
export {ThreeDotsMenuItemPropTypes};