summaryrefslogtreecommitdiffhomepage
path: root/gui/src/renderer/components
diff options
context:
space:
mode:
authorOskar Nyberg <oskar@mullvad.net>2020-11-02 16:55:50 +0100
committerOskar Nyberg <oskar@mullvad.net>2020-11-02 16:55:50 +0100
commitc2f07e2533153a0a40c0901fe89901c658196514 (patch)
tree3cbe881c250b287820f9729f809381765ca6f855 /gui/src/renderer/components
parent6d2461477b5b34c30a59abf84f43407552a69c11 (diff)
parentac13e1d54d9a7cacf6eeb4909d030ea4d47f2b18 (diff)
downloadmullvadvpn-c2f07e2533153a0a40c0901fe89901c658196514.tar.xz
mullvadvpn-c2f07e2533153a0a40c0901fe89901c658196514.zip
Merge branch 'add-escape-hatch'
Diffstat (limited to 'gui/src/renderer/components')
-rw-r--r--gui/src/renderer/components/KeyboardNavigation.tsx27
-rw-r--r--gui/src/renderer/components/Modal.tsx7
2 files changed, 32 insertions, 2 deletions
diff --git a/gui/src/renderer/components/KeyboardNavigation.tsx b/gui/src/renderer/components/KeyboardNavigation.tsx
new file mode 100644
index 0000000000..3aefd7c684
--- /dev/null
+++ b/gui/src/renderer/components/KeyboardNavigation.tsx
@@ -0,0 +1,27 @@
+import React, { useCallback, useEffect } from 'react';
+import { useHistory } from 'react-router';
+import History from '../lib/history';
+
+interface IKeyboardNavigationProps {
+ children: React.ReactElement;
+}
+
+export default function KeyboardNavigation(props: IKeyboardNavigationProps) {
+ const history = useHistory() as History;
+
+ const handleKeyDown = useCallback(
+ (event: KeyboardEvent) => {
+ if (event.key === 'Escape') {
+ history.reset();
+ }
+ },
+ [history.reset],
+ );
+
+ useEffect(() => {
+ document.addEventListener('keydown', handleKeyDown);
+ return () => document.removeEventListener('keydown', handleKeyDown);
+ }, [handleKeyDown]);
+
+ return props.children;
+}
diff --git a/gui/src/renderer/components/Modal.tsx b/gui/src/renderer/components/Modal.tsx
index 4c449e8cf2..858c0be066 100644
--- a/gui/src/renderer/components/Modal.tsx
+++ b/gui/src/renderer/components/Modal.tsx
@@ -152,7 +152,9 @@ class ModalAlertWithContext extends React.Component<IModalAlertProps & IModalCon
public componentDidMount() {
this.props.setActiveModal(true);
- document.addEventListener('keydown', this.handleKeyPress);
+ // The `true` argument specifies that the event should be dispatched in the capture phase. This
+ // makes sure that this component catches the event before the escape hatch.
+ document.addEventListener('keydown', this.handleKeyPress, true);
const modalContainer = this.props.modalContainerRef.current;
if (modalContainer) {
@@ -170,7 +172,7 @@ class ModalAlertWithContext extends React.Component<IModalAlertProps & IModalCon
public componentWillUnmount() {
this.props.setActiveModal(false);
- document.removeEventListener('keydown', this.handleKeyPress);
+ document.removeEventListener('keydown', this.handleKeyPress, true);
this.appendScheduler.cancel();
this.props.modalContainerRef.current?.removeChild(this.element);
@@ -219,6 +221,7 @@ class ModalAlertWithContext extends React.Component<IModalAlertProps & IModalCon
private handleKeyPress = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
+ event.stopPropagation();
this.props.close?.();
}
};