@@ -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 } >
0 commit comments