Created
April 25, 2020 10:48
-
-
Save juliankoehn/dfbba406c20b8dd50e9912cc85d58603 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bringFocusInsideOverlay() { | |
return requestAnimationFrame(() => { | |
// Container ref may be undefined between component mounting and Portal rendering | |
// activeElement may be undefined in some rare cases in IE | |
if ( | |
this.$refs.container == null || | |
document.activeElement == null || | |
!this.isShown | |
) { | |
return | |
} | |
const isFocusOutsideModal = !this.$refs.container.contains( | |
document.activeElement | |
) | |
if (isFocusOutsideModal) { | |
// Element marked autofocus has higher priority than the other clowns | |
const autofocusElement = <HTMLElement>this.$refs.container.querySelector( | |
'[autofocus]' | |
) | |
const wrapperElement = <HTMLElement>this.$refs.container.querySelector('[tabindex]') | |
const buttonElement = <HTMLElement>this.$refs.container.querySelector('button') | |
if (autofocusElement) { | |
autofocusElement.focus() | |
} else if (wrapperElement) { | |
wrapperElement.focus() | |
} else if (buttonElement) { | |
buttonElement.focus() | |
} | |
} | |
}) | |
} | |
bringFocusBackToTarget() { | |
return requestAnimationFrame(() => { | |
if ( | |
this.$refs.container == null || | |
document.activeElement == null | |
) { | |
return | |
} | |
const isFocusInsideModal = this.$refs.container.contains(document.activeElement) | |
// Bring back focus on the target | |
if (this.previousActiveElement && (document.activeElement === document.body || isFocusInsideModal)) { | |
this.previousActiveElement.focus() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment