Skip to content

Commit 9c92c38

Browse files
committed
Restore focused Element Before focus loop
1 parent 3716d1e commit 9c92c38

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/FocusLoop.vue

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,29 @@ export default {
5555
},
5656
5757
watch: {
58-
isVisible: 'focusFirst'
58+
isVisible (val) {
59+
this.managePrevFocusElement(val)
60+
this.focusFirst(val)
61+
}
5962
},
6063
6164
mounted () {
65+
this.managePrevFocusElement(this.isVisible)
6266
this.focusFirst(this.isVisible)
6367
},
6468
69+
beforeDestroy () {
70+
this.managePrevFocusElement(false)
71+
},
72+
6573
methods: {
74+
managePrevFocusElement (visible) {
75+
if (!visible && window.vflPrevFocusedElement) {
76+
return window.vflPrevFocusedElement.focus()
77+
}
78+
window.vflPrevFocusedElement = document.activeElement
79+
},
80+
6681
getFocusableElements () {
6782
const focusableElements = this.$refs.focusLoop.querySelectorAll(focusableElementsSelector)
6883
if (focusableElements && focusableElements.length) return focusableElements

0 commit comments

Comments
 (0)