Skip to content

Commit

Permalink
Components: Refactor Dropdown to use onFocusOutside
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Apr 5, 2019
1 parent fc1fec7 commit 55ca76a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

- The `Popover` component `onClickOutside` prop has been deprecated. Use `onFocusOutside` instead.

### Internal

- The `Dropdown` component has been refactored to focus changes using the `Popover` component's `onFocusOutside` prop.

## 7.2.0 (2019-03-20)

### Improvements
Expand Down
18 changes: 8 additions & 10 deletions packages/components/src/dropdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Dropdown extends Component {

this.toggle = this.toggle.bind( this );
this.close = this.close.bind( this );
this.closeIfClickOutside = this.closeIfClickOutside.bind( this );
this.closeIfFocusOutside = this.closeIfFocusOutside.bind( this );

this.containerRef = createRef();

Expand Down Expand Up @@ -46,15 +46,13 @@ class Dropdown extends Component {
}

/**
* Closes the dropdown if a click occurs outside the dropdown wrapper. This
* is intentionally distinct from `onClose` in that a click outside the
* popover may occur in the toggling of the dropdown via its toggle button.
* The correct behavior is to keep the dropdown closed.
*
* @param {MouseEvent} event Click event triggering `onClickOutside`.
* Closes the dropdown if a focus leaves the dropdown wrapper. This is
* intentionally distinct from `onClose` since focus loss from the popover
* is expected to occur when using the Dropdown's toggle button, in which
* case the correct behavior is to keep the dropdown closed.
*/
closeIfClickOutside( event ) {
if ( ! this.containerRef.current.contains( event.target ) ) {
closeIfFocusOutside() {
if ( ! this.containerRef.current.contains( document.activeElement ) ) {
this.close();
}
}
Expand Down Expand Up @@ -86,7 +84,7 @@ class Dropdown extends Component {
className={ contentClassName }
position={ position }
onClose={ this.close }
onClickOutside={ this.closeIfClickOutside }
onFocusOutside={ this.closeIfFocusOutside }
expandOnMobile={ expandOnMobile }
headerTitle={ headerTitle }
focusOnMount={ focusOnMount }
Expand Down

0 comments on commit 55ca76a

Please sign in to comment.