Skip to content

Commit 55ca76a

Browse files
committed
Components: Refactor Dropdown to use onFocusOutside
1 parent fc1fec7 commit 55ca76a

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

packages/components/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

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

18+
### Internal
19+
20+
- The `Dropdown` component has been refactored to focus changes using the `Popover` component's `onFocusOutside` prop.
21+
1822
## 7.2.0 (2019-03-20)
1923

2024
### Improvements

packages/components/src/dropdown/index.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Dropdown extends Component {
1414

1515
this.toggle = this.toggle.bind( this );
1616
this.close = this.close.bind( this );
17-
this.closeIfClickOutside = this.closeIfClickOutside.bind( this );
17+
this.closeIfFocusOutside = this.closeIfFocusOutside.bind( this );
1818

1919
this.containerRef = createRef();
2020

@@ -46,15 +46,13 @@ class Dropdown extends Component {
4646
}
4747

4848
/**
49-
* Closes the dropdown if a click occurs outside the dropdown wrapper. This
50-
* is intentionally distinct from `onClose` in that a click outside the
51-
* popover may occur in the toggling of the dropdown via its toggle button.
52-
* The correct behavior is to keep the dropdown closed.
53-
*
54-
* @param {MouseEvent} event Click event triggering `onClickOutside`.
49+
* Closes the dropdown if a focus leaves the dropdown wrapper. This is
50+
* intentionally distinct from `onClose` since focus loss from the popover
51+
* is expected to occur when using the Dropdown's toggle button, in which
52+
* case the correct behavior is to keep the dropdown closed.
5553
*/
56-
closeIfClickOutside( event ) {
57-
if ( ! this.containerRef.current.contains( event.target ) ) {
54+
closeIfFocusOutside() {
55+
if ( ! this.containerRef.current.contains( document.activeElement ) ) {
5856
this.close();
5957
}
6058
}
@@ -86,7 +84,7 @@ class Dropdown extends Component {
8684
className={ contentClassName }
8785
position={ position }
8886
onClose={ this.close }
89-
onClickOutside={ this.closeIfClickOutside }
87+
onFocusOutside={ this.closeIfFocusOutside }
9088
expandOnMobile={ expandOnMobile }
9189
headerTitle={ headerTitle }
9290
focusOnMount={ focusOnMount }

0 commit comments

Comments
 (0)