Skip to content

Commit 44adda3

Browse files
committed
Add right-click context menu to GroupInviteTile.js
Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
1 parent 5c92f8a commit 44adda3

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/components/views/groups/GroupInviteTile.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
1616
limitations under the License.
1717
*/
1818

19-
import React, {createRef} from 'react';
19+
import React from 'react';
2020
import PropTypes from 'prop-types';
2121
import createReactClass from 'create-react-class';
2222
import { MatrixClient } from 'matrix-js-sdk';
@@ -27,6 +27,7 @@ import classNames from 'classnames';
2727
import MatrixClientPeg from "../../../MatrixClientPeg";
2828
import {ContextMenu, ContextMenuButton, toRightOf} from "../../structures/ContextMenu";
2929

30+
// XXX this class copies a lot from RoomTile.js
3031
export default createReactClass({
3132
displayName: 'GroupInviteTile',
3233

@@ -47,10 +48,6 @@ export default createReactClass({
4748
});
4849
},
4950

50-
componentDidMount: function() {
51-
this._contextMenuButton = createRef();
52-
},
53-
5451
onClick: function(e) {
5552
dis.dispatch({
5653
action: 'view_group',
@@ -74,16 +71,12 @@ export default createReactClass({
7471
});
7572
},
7673

77-
openMenu: function(e) {
74+
_showContextMenu: function(boundingClientRect) {
7875
// Only allow non-guests to access the context menu
7976
if (MatrixClientPeg.get().isGuest()) return;
8077

81-
// Prevent the GroupInviteTile onClick event firing as well
82-
e.stopPropagation();
83-
e.preventDefault();
84-
8578
const state = {
86-
menuDisplayed: true,
79+
contextMenuPosition: boundingClientRect,
8780
};
8881

8982
// If the badge is clicked, then no longer show tooltip
@@ -94,9 +87,28 @@ export default createReactClass({
9487
this.setState(state);
9588
},
9689

90+
onContextMenuButtonClick: function(e) {
91+
// Prevent the RoomTile onClick event firing as well
92+
e.stopPropagation();
93+
e.preventDefault();
94+
95+
this._showContextMenu(e.target.getBoundingClientRect());
96+
},
97+
98+
onContextMenu: function(e) {
99+
// Prevent the native context menu
100+
e.preventDefault();
101+
102+
this._showContextMenu({
103+
right: e.clientX,
104+
top: e.clientY,
105+
height: 0,
106+
});
107+
},
108+
97109
closeMenu: function() {
98110
this.setState({
99-
menuDisplayed: false,
111+
contextMenuPosition: null,
100112
});
101113
},
102114

@@ -110,15 +122,16 @@ export default createReactClass({
110122

111123
const av = <BaseAvatar name={groupName} width={24} height={24} url={httpAvatarUrl} />;
112124

125+
const isMenuDisplayed = Boolean(this.state.contextMenuPosition);
113126
const nameClasses = classNames('mx_RoomTile_name mx_RoomTile_invite mx_RoomTile_badgeShown', {
114-
'mx_RoomTile_badgeShown': this.state.badgeHover || this.state.menuDisplayed,
127+
'mx_RoomTile_badgeShown': this.state.badgeHover || isMenuDisplayed,
115128
});
116129

117130
const label = <div title={this.props.group.groupId} className={nameClasses} dir="auto">
118131
{ groupName }
119132
</div>;
120133

121-
const badgeEllipsis = this.state.badgeHover || this.state.menuDisplayed;
134+
const badgeEllipsis = this.state.badgeHover || isMenuDisplayed;
122135
const badgeClasses = classNames('mx_RoomTile_badge mx_RoomTile_highlight', {
123136
'mx_RoomTile_badgeButton': badgeEllipsis,
124137
});
@@ -127,10 +140,9 @@ export default createReactClass({
127140
const badge = (
128141
<ContextMenuButton
129142
className={badgeClasses}
130-
inputRef={this._contextMenuButton}
131-
onClick={this.openMenu}
143+
onClick={this.onContextMenuButtonClick}
132144
label={_t("Options")}
133-
isExpanded={this.state.menuDisplayed}
145+
isExpanded={isMenuDisplayed}
134146
>
135147
{ badgeContent }
136148
</ContextMenuButton>
@@ -143,17 +155,16 @@ export default createReactClass({
143155
}
144156

145157
const classes = classNames('mx_RoomTile mx_RoomTile_highlight', {
146-
'mx_RoomTile_menuDisplayed': this.state.menuDisplayed,
158+
'mx_RoomTile_menuDisplayed': isMenuDisplayed,
147159
'mx_RoomTile_selected': this.state.selected,
148160
'mx_GroupInviteTile': true,
149161
});
150162

151163
let contextMenu;
152-
if (this.state.menuDisplayed) {
153-
const elementRect = this._contextMenuButton.current.getBoundingClientRect();
164+
if (isMenuDisplayed) {
154165
const GroupInviteTileContextMenu = sdk.getComponent('context_menus.GroupInviteTileContextMenu');
155166
contextMenu = (
156-
<ContextMenu {...toRightOf(elementRect)} onFinished={this.closeMenu}>
167+
<ContextMenu {...toRightOf(this.state.contextMenuPosition)} onFinished={this.closeMenu}>
157168
<GroupInviteTileContextMenu group={this.props.group} onFinished={this.closeMenu} />
158169
</ContextMenu>
159170
);

0 commit comments

Comments
 (0)