|
4 | 4 |
|
5 | 5 | import { PureComponent } from 'react'; |
6 | 6 | import type { ComponentProps } from 'react'; |
7 | | -import ReactDOM from 'react-dom'; |
8 | 7 | import { ContextMenu as ReactContextMenu } from '@firefox-devtools/react-contextmenu'; |
9 | 8 |
|
10 | 9 | import './ContextMenu.css'; |
11 | 10 |
|
12 | 11 | type Props = ComponentProps<typeof ReactContextMenu>; |
13 | 12 |
|
14 | 13 | export class ContextMenu extends PureComponent<Props> { |
15 | | - _contextMenu: any = null; |
16 | | - _takeContextMenuRef = (contextMenu: any) => { |
17 | | - this._contextMenu = contextMenu; |
18 | | - }; |
19 | | - |
20 | | - _mouseDownHandler(event: Event): void { |
| 14 | + _mouseDownHandler = (event: React.MouseEvent<HTMLDivElement>): void => { |
21 | 15 | // This prevents from stealing the focus from where it was. |
22 | 16 | event.preventDefault(); |
23 | | - } |
24 | | - |
25 | | - override componentDidMount() { |
26 | | - if (this._contextMenu) { |
27 | | - // The context menu component does not expose a reference to its internal |
28 | | - // DOM node so using findDOMNode is currently unavoidable. |
29 | | - // eslint-disable-next-line react/no-find-dom-node |
30 | | - const contextMenuNode = ReactDOM.findDOMNode(this._contextMenu); |
31 | | - if (contextMenuNode) { |
32 | | - // There's no need to remove this event listener since the component is |
33 | | - // never unmounted. Duplicate event listeners will also be discarded |
34 | | - // automatically so we don't need to handle that. |
35 | | - contextMenuNode.addEventListener('mousedown', this._mouseDownHandler); |
36 | | - } |
37 | | - } |
38 | | - } |
39 | | - |
40 | | - override componentWillUnmount() { |
41 | | - // eslint-disable-next-line react/no-find-dom-node |
42 | | - const contextMenuNode = ReactDOM.findDOMNode(this._contextMenu); |
43 | | - if (contextMenuNode) { |
44 | | - contextMenuNode.removeEventListener('mousedown', this._mouseDownHandler); |
45 | | - } |
46 | | - } |
| 17 | + }; |
47 | 18 |
|
48 | 19 | override render() { |
49 | 20 | return ( |
50 | | - <ReactContextMenu ref={this._takeContextMenuRef} {...this.props}> |
51 | | - {this.props.children ? this.props.children : <div />} |
52 | | - </ReactContextMenu> |
| 21 | + <div onMouseDown={this._mouseDownHandler}> |
| 22 | + <ReactContextMenu {...this.props}> |
| 23 | + {this.props.children ? this.props.children : <div />} |
| 24 | + </ReactContextMenu> |
| 25 | + </div> |
53 | 26 | ); |
54 | 27 | } |
55 | 28 | } |
0 commit comments