Skip to content
This repository was archived by the owner on Mar 4, 2020. It is now read-only.

Commit fc0d558

Browse files
authored
fix(Popup): Focus the last focused element outside Popup on ESC (#861)
* fix(Popup): Focus the last focused element outside Popup on ESC * Update changelog * Update packages/react/src/components/Popup/Popup.tsx Co-Authored-By: sophieH29 <[email protected]>
1 parent d4ca3af commit fc0d558

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2626
- Add `black` and `white` options for the `color` prop of the `Label` component @mnajdova ([#855](https://github.com/stardust-ui/react/pull/855))
2727
- Add `Flex` component @kuzhelov ([#802](https://github.com/stardust-ui/react/pull/802))
2828

29+
### Fixes
30+
- Focus the last focused element which triggered `Popup` on ESC @sophieH29 ([#861](https://github.com/stardust-ui/react/pull/861))
31+
2932
<!--------------------------------[ v0.20.0 ]------------------------------- -->
3033
## [v0.20.0](https://github.com/stardust-ui/react/tree/v0.20.0) (2019-02-06)
3134
[Compare changes](https://github.com/stardust-ui/react/compare/v0.19.2...v0.20.0)

packages/react/src/components/Popup/Popup.tsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,15 @@ export default class Popup extends AutoControlledComponent<ReactProps<PopupProps
166166
private outsideClickSubscription = EventStack.noSubscription
167167

168168
private triggerDomElement = null
169+
// focusable element which has triggered Popup, can be either triggerDomElement or the element inside it
170+
private triggerFocusableDomElement = null
169171
private popupDomElement = null
170172

171173
private closeTimeoutId
172174

173175
protected actionHandlers: AccessibilityActionHandlers = {
174176
closeAndFocusTrigger: e => {
175-
this.close(e, () => _.invoke(this.triggerDomElement, 'focus'))
177+
this.close(e, () => _.invoke(this.triggerFocusableDomElement, 'focus'))
176178
e.stopPropagation()
177179
},
178180
close: e => this.close(e),
@@ -474,6 +476,10 @@ export default class Popup extends AutoControlledComponent<ReactProps<PopupProps
474476
}
475477

476478
private trySetOpen(newValue: boolean, eventArgs: any) {
479+
// when new state 'open' === 'true', save the last focused element
480+
if (newValue) {
481+
this.updateTriggerFocusableDomElement()
482+
}
477483
this.trySetState({ open: newValue })
478484
_.invoke(this.props, 'onOpenChange', eventArgs, { ...this.props, ...{ open: newValue } })
479485
}
@@ -497,4 +503,14 @@ export default class Popup extends AutoControlledComponent<ReactProps<PopupProps
497503
onClose && onClose()
498504
}
499505
}
506+
507+
/**
508+
* Save DOM element which had focus before Popup opens.
509+
* Can be either trigger DOM element itself or the element inside it.
510+
*/
511+
private updateTriggerFocusableDomElement() {
512+
this.triggerFocusableDomElement = this.triggerDomElement.contains(document.activeElement)
513+
? document.activeElement
514+
: this.triggerDomElement
515+
}
500516
}

0 commit comments

Comments
 (0)