[Glitch] Fix and refactor keyboard navigation in dropdown menus

Port 80182eda62 to glitch-soc

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
master
ThibG 5 years ago committed by Thibaut Girka
parent 8b2823b7b6
commit ce33822e2c
  1. 29
      app/javascript/flavours/glitch/components/dropdown_menu.js
  2. 31
      app/javascript/flavours/glitch/features/compose/components/dropdown_menu.js

@ -68,20 +68,14 @@ class DropdownMenu extends React.PureComponent {
handleKeyDown = e => { handleKeyDown = e => {
const items = Array.from(this.node.getElementsByTagName('a')); const items = Array.from(this.node.getElementsByTagName('a'));
const index = items.indexOf(document.activeElement); const index = items.indexOf(document.activeElement);
let element; let element = null;
switch(e.key) { switch(e.key) {
case 'ArrowDown': case 'ArrowDown':
element = items[index+1]; element = items[index+1] || items[0];
if (element) {
element.focus();
}
break; break;
case 'ArrowUp': case 'ArrowUp':
element = items[index-1]; element = items[index-1] || items[items.length-1];
if (element) {
element.focus();
}
break; break;
case 'Tab': case 'Tab':
if (e.shiftKey) { if (e.shiftKey) {
@ -89,28 +83,23 @@ class DropdownMenu extends React.PureComponent {
} else { } else {
element = items[index+1] || items[0]; element = items[index+1] || items[0];
} }
if (element) {
element.focus();
e.preventDefault();
e.stopPropagation();
}
break; break;
case 'Home': case 'Home':
element = items[0]; element = items[0];
if (element) {
element.focus();
}
break; break;
case 'End': case 'End':
element = items[items.length-1]; element = items[items.length-1];
if (element) {
element.focus();
}
break; break;
case 'Escape': case 'Escape':
this.props.onClose(); this.props.onClose();
break; break;
} }
if (element) {
element.focus();
e.preventDefault();
e.stopPropagation();
}
} }
handleItemKeyPress = e => { handleItemKeyPress = e => {

@ -106,7 +106,7 @@ export default class ComposerOptionsDropdownContent extends React.PureComponent
const index = items.findIndex(item => { const index = items.findIndex(item => {
return (item.name === name); return (item.name === name);
}); });
let element; let element = null;
switch(e.key) { switch(e.key) {
case 'Escape': case 'Escape':
@ -117,18 +117,10 @@ export default class ComposerOptionsDropdownContent extends React.PureComponent
this.handleClick(e); this.handleClick(e);
break; break;
case 'ArrowDown': case 'ArrowDown':
element = this.node.childNodes[index + 1]; element = this.node.childNodes[index + 1] || this.node.firstChild;
if (element) {
element.focus();
this.handleChange(element.getAttribute('data-index'));
}
break; break;
case 'ArrowUp': case 'ArrowUp':
element = this.node.childNodes[index - 1]; element = this.node.childNodes[index - 1] || this.node.lastChild;
if (element) {
element.focus();
this.handleChange(element.getAttribute('data-index'));
}
break; break;
case 'Tab': case 'Tab':
if (e.shiftKey) { if (e.shiftKey) {
@ -136,27 +128,20 @@ export default class ComposerOptionsDropdownContent extends React.PureComponent
} else { } else {
element = this.node.childNodes[index + 1] || this.node.firstChild; element = this.node.childNodes[index + 1] || this.node.firstChild;
} }
if (element) {
element.focus();
this.handleChange(element.getAttribute('data-index'));
e.preventDefault();
e.stopPropagation();
}
break; break;
case 'Home': case 'Home':
element = this.node.firstChild; element = this.node.firstChild;
if (element) {
element.focus();
this.handleChange(element.getAttribute('data-index'));
}
break; break;
case 'End': case 'End':
element = this.node.lastChild; element = this.node.lastChild;
break;
}
if (element) { if (element) {
element.focus(); element.focus();
this.handleChange(element.getAttribute('data-index')); this.handleChange(element.getAttribute('data-index'));
} e.preventDefault();
break; e.stopPropagation();
} }
} }

Loading…
Cancel
Save