Skip to content

Commit b844770

Browse files
ClearlyClairehiyuki2578
authored andcommitted
Improve focus handling with dropdown menus (mastodon#11511)
- Focus first item when activated via keyboard - When the dropdown menu closes, give back the focus to the actual element which was focused prior to opening the menu
1 parent b17ddb6 commit b844770

2 files changed

Lines changed: 44 additions & 7 deletions

File tree

app/javascript/mastodon/components/dropdown_menu.js

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class DropdownMenu extends React.PureComponent {
4545
document.addEventListener('click', this.handleDocumentClick, false);
4646
document.addEventListener('keydown', this.handleKeyDown, false);
4747
document.addEventListener('touchend', this.handleDocumentClick, listenerOptions);
48-
this.activeElement = document.activeElement;
4948
if (this.focusedItem && this.props.openedViaKeyboard) {
5049
this.focusedItem.focus();
5150
}
@@ -56,9 +55,6 @@ class DropdownMenu extends React.PureComponent {
5655
document.removeEventListener('click', this.handleDocumentClick, false);
5756
document.removeEventListener('keydown', this.handleKeyDown, false);
5857
document.removeEventListener('touchend', this.handleDocumentClick, listenerOptions);
59-
if (this.activeElement) {
60-
this.activeElement.focus();
61-
}
6258
}
6359

6460
setRef = c => {
@@ -117,7 +113,7 @@ class DropdownMenu extends React.PureComponent {
117113
}
118114
}
119115

120-
handleItemKeyUp = e => {
116+
handleItemKeyPress = e => {
121117
if (e.key === 'Enter' || e.key === ' ') {
122118
this.handleClick(e);
123119
}
@@ -147,7 +143,7 @@ class DropdownMenu extends React.PureComponent {
147143

148144
return (
149145
<li className='dropdown-menu__item' key={`${text}-${i}`}>
150-
<a href={href} target={target} data-method={method} rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyUp={this.handleItemKeyUp} data-index={i}>
146+
<a href={href} target={target} data-method={method} rel='noopener' role='button' tabIndex='0' ref={i === 0 ? this.setFocusRef : null} onClick={this.handleClick} onKeyPress={this.handleItemKeyPress} data-index={i}>
151147
{text}
152148
</a>
153149
</li>
@@ -214,15 +210,44 @@ export default class Dropdown extends React.PureComponent {
214210
} else {
215211
const { top } = target.getBoundingClientRect();
216212
const placement = top * 2 < innerHeight ? 'bottom' : 'top';
217-
218213
this.props.onOpen(this.state.id, this.handleItemClick, placement, type !== 'click');
219214
}
220215
}
221216

222217
handleClose = () => {
218+
if (this.activeElement) {
219+
this.activeElement.focus();
220+
this.activeElement = null;
221+
}
223222
this.props.onClose(this.state.id);
224223
}
225224

225+
handleMouseDown = () => {
226+
if (!this.state.open) {
227+
this.activeElement = document.activeElement;
228+
}
229+
}
230+
231+
handleButtonKeyDown = (e) => {
232+
switch(e.key) {
233+
case ' ':
234+
case 'Enter':
235+
this.handleMouseDown();
236+
break;
237+
}
238+
}
239+
240+
handleKeyPress = (e) => {
241+
switch(e.key) {
242+
case ' ':
243+
case 'Enter':
244+
this.handleClick(e);
245+
e.stopPropagation();
246+
e.preventDefault();
247+
break;
248+
}
249+
}
250+
226251
handleItemClick = e => {
227252
const i = Number(e.currentTarget.getAttribute('data-index'));
228253
const { action, to } = this.props.items[i];
@@ -266,6 +291,9 @@ export default class Dropdown extends React.PureComponent {
266291
size={size}
267292
ref={this.setTargetRef}
268293
onClick={this.handleClick}
294+
onMouseDown={this.handleMouseDown}
295+
onKeyDown={this.handleButtonKeyDown}
296+
onKeyPress={this.handleKeyPress}
269297
/>
270298

271299
<Overlay show={open} placement={dropdownPlacement} target={this.findTarget}>

app/javascript/mastodon/components/icon_button.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class IconButton extends React.PureComponent {
1414
onClick: PropTypes.func,
1515
onMouseDown: PropTypes.func,
1616
onKeyDown: PropTypes.func,
17+
onKeyPress: PropTypes.func,
1718
size: PropTypes.number,
1819
active: PropTypes.bool,
1920
pressed: PropTypes.bool,
@@ -44,6 +45,12 @@ export default class IconButton extends React.PureComponent {
4445
}
4546
}
4647

48+
handleKeyPress = (e) => {
49+
if (this.props.onKeyPress && !this.props.disabled) {
50+
this.props.onKeyPress(e);
51+
}
52+
}
53+
4754
handleMouseDown = (e) => {
4855
if (!this.props.disabled && this.props.onMouseDown) {
4956
this.props.onMouseDown(e);
@@ -100,6 +107,7 @@ export default class IconButton extends React.PureComponent {
100107
onClick={this.handleClick}
101108
onMouseDown={this.handleMouseDown}
102109
onKeyDown={this.handleKeyDown}
110+
onKeyPress={this.handleKeyPress}
103111
style={style}
104112
tabIndex={tabIndex}
105113
disabled={disabled}
@@ -121,6 +129,7 @@ export default class IconButton extends React.PureComponent {
121129
onClick={this.handleClick}
122130
onMouseDown={this.handleMouseDown}
123131
onKeyDown={this.handleKeyDown}
132+
onKeyPress={this.handleKeyPress}
124133
style={style}
125134
tabIndex={tabIndex}
126135
disabled={disabled}

0 commit comments

Comments
 (0)