about summary refs log tree commit diff
path: root/app/javascript/mastodon/components/dropdown_menu.js
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2020-04-21 15:13:26 +0200
committerGitHub <noreply@github.com>2020-04-21 15:13:26 +0200
commit80182eda622e1317bffb6729259b8a81d84251a2 (patch)
treedcce603b89f6a8867f6696d86382cbf940d12d16 /app/javascript/mastodon/components/dropdown_menu.js
parentff32a25ee3d5032122b8b7af31e6e51304e1e703 (diff)
Fix and refactor keyboard navigation in dropdown menus (#13528)
Fixes #13527

- Fixes caught keyboard events being needlessly propagated
- Let up/down arrows wrap around like the tab key does
- Refactor common code
Diffstat (limited to 'app/javascript/mastodon/components/dropdown_menu.js')
-rw-r--r--app/javascript/mastodon/components/dropdown_menu.js29
1 files changed, 9 insertions, 20 deletions
diff --git a/app/javascript/mastodon/components/dropdown_menu.js b/app/javascript/mastodon/components/dropdown_menu.js
index a4f262285..31c02d735 100644
--- a/app/javascript/mastodon/components/dropdown_menu.js
+++ b/app/javascript/mastodon/components/dropdown_menu.js
@@ -68,20 +68,14 @@ class DropdownMenu extends React.PureComponent {
   handleKeyDown = e => {
     const items = Array.from(this.node.getElementsByTagName('a'));
     const index = items.indexOf(document.activeElement);
-    let element;
+    let element = null;
 
     switch(e.key) {
     case 'ArrowDown':
-      element = items[index+1];
-      if (element) {
-        element.focus();
-      }
+      element = items[index+1] || items[0];
       break;
     case 'ArrowUp':
-      element = items[index-1];
-      if (element) {
-        element.focus();
-      }
+      element = items[index-1] || items[items.length-1];
       break;
     case 'Tab':
       if (e.shiftKey) {
@@ -89,28 +83,23 @@ class DropdownMenu extends React.PureComponent {
       } else {
         element = items[index+1] || items[0];
       }
-      if (element) {
-        element.focus();
-        e.preventDefault();
-        e.stopPropagation();
-      }
       break;
     case 'Home':
       element = items[0];
-      if (element) {
-        element.focus();
-      }
       break;
     case 'End':
       element = items[items.length-1];
-      if (element) {
-        element.focus();
-      }
       break;
     case 'Escape':
       this.props.onClose();
       break;
     }
+
+    if (element) {
+      element.focus();
+      e.preventDefault();
+      e.stopPropagation();
+    }
   }
 
   handleItemKeyPress = e => {