about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/components/dropdown_menu.js
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-08-07 13:58:53 +0200
committerThibaut Girka <thib@sitedethib.com>2019-08-07 15:11:59 +0200
commite8e980cdac9d1c3a81d9f30412f6de4cd021c225 (patch)
tree31dac124decac2cee9c49f5b8e039fa62d0c0d82 /app/javascript/flavours/glitch/components/dropdown_menu.js
parent95fc39a1e653a118671026890fde241987ca3f53 (diff)
[Glitch] Improve focus handling with dropdown menus
Port 396b8cdd0f071fab85b09855f882b19c07cccd44 to glitch-soc
Diffstat (limited to 'app/javascript/flavours/glitch/components/dropdown_menu.js')
-rw-r--r--app/javascript/flavours/glitch/components/dropdown_menu.js42
1 files changed, 35 insertions, 7 deletions
diff --git a/app/javascript/flavours/glitch/components/dropdown_menu.js b/app/javascript/flavours/glitch/components/dropdown_menu.js
index f29b824d5..39d7ba50c 100644
--- a/app/javascript/flavours/glitch/components/dropdown_menu.js
+++ b/app/javascript/flavours/glitch/components/dropdown_menu.js
@@ -45,7 +45,6 @@ class DropdownMenu extends React.PureComponent {
     document.addEventListener('click', this.handleDocumentClick, false);
     document.addEventListener('keydown', this.handleKeyDown, false);
     document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
-    this.activeElement = document.activeElement;
     if (this.focusedItem && this.props.openedViaKeyboard) {
       this.focusedItem.focus();
     }
@@ -56,9 +55,6 @@ class DropdownMenu extends React.PureComponent {
     document.removeEventListener('click', this.handleDocumentClick, false);
     document.removeEventListener('keydown', this.handleKeyDown, false);
     document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
-    if (this.activeElement) {
-      this.activeElement.focus();
-    }
   }
 
   setRef = c => {
@@ -117,7 +113,7 @@ class DropdownMenu extends React.PureComponent {
     }
   }
 
-  handleItemKeyUp = e => {
+  handleItemKeyPress = e => {
     if (e.key === 'Enter' || e.key === ' ') {
       this.handleClick(e);
     }
@@ -147,7 +143,7 @@ class DropdownMenu extends React.PureComponent {
 
     return (
       <li className='dropdown-menu__item' key={`${text}-${i}`}>
-        <a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyUp={this.handleItemKeyUp} data-index={i}>
+        <a href={href} target='_blank' rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyPress={this.handleItemKeyPress} data-index={i}>
           {text}
         </a>
       </li>
@@ -214,15 +210,44 @@ export default class Dropdown extends React.PureComponent {
     } else {
       const { top } = target.getBoundingClientRect();
       const placement = top * 2 < innerHeight ? 'bottom' : 'top';
-
       this.props.onOpen(this.state.id, this.handleItemClick, placement, type !== 'click');
     }
   }
 
   handleClose = () => {
+    if (this.activeElement) {
+      this.activeElement.focus();
+      this.activeElement = null;
+    }
     this.props.onClose(this.state.id);
   }
 
+  handleMouseDown = () => {
+    if (!this.state.open) {
+      this.activeElement = document.activeElement;
+    }
+  }
+
+  handleButtonKeyDown = (e) => {
+    switch(e.key) {
+    case ' ':
+    case 'Enter':
+      this.handleMouseDown();
+      break;
+    }
+  }
+
+  handleKeyPress = (e) => {
+    switch(e.key) {
+    case ' ':
+    case 'Enter':
+      this.handleClick(e);
+      e.stopPropagation();
+      e.preventDefault();
+      break;
+    }
+  }
+
   handleItemClick = (i, e) => {
     const { action, to } = this.props.items[i];
 
@@ -265,6 +290,9 @@ export default class Dropdown extends React.PureComponent {
           size={size}
           ref={this.setTargetRef}
           onClick={this.handleClick}
+          onMouseDown={this.handleMouseDown}
+          onKeyDown={this.handleButtonKeyDown}
+          onKeyPress={this.handleKeyPress}
         />
 
         <Overlay show={open} placement={dropdownPlacement} target={this.findTarget}>