fix: Allow clicking anywhere outside command menu to close
This commit is contained in:
@@ -76,6 +76,7 @@ class CommandMenu<T extends MenuItem> extends React.Component<Props<T>, State> {
|
||||
};
|
||||
|
||||
componentDidMount() {
|
||||
window.addEventListener("mousedown", this.handleMouseDown);
|
||||
window.addEventListener("keydown", this.handleKeyDown);
|
||||
}
|
||||
|
||||
@@ -107,9 +108,21 @@ class CommandMenu<T extends MenuItem> extends React.Component<Props<T>, State> {
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
window.removeEventListener("mousedown", this.handleMouseDown);
|
||||
window.removeEventListener("keydown", this.handleKeyDown);
|
||||
}
|
||||
|
||||
handleMouseDown = (event: MouseEvent) => {
|
||||
if (
|
||||
!this.menuRef.current ||
|
||||
this.menuRef.current.contains(event.target as Element)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.props.onClose();
|
||||
};
|
||||
|
||||
handleKeyDown = (event: KeyboardEvent) => {
|
||||
if (!this.props.isActive) {
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user