element-plus/packages/hooks/use-restore-active/index.ts
三咲智子 55348b30b6
style: use prettier (#3228)
* style: use prettier

* style: just prettier format, no code changes

* style: eslint fix
object-shorthand, prefer-const

* style: fix no-void

* style: no-console
2021-09-04 19:29:28 +08:00

28 lines
725 B
TypeScript

import { isRef, watch } from 'vue'
import type { Ref } from 'vue'
/**
* This method provides dialogable components the ability to restore previously activated element before
* the dialog gets opened
*/
export default (toggle: Ref<boolean>, initialFocus?: Ref<HTMLElement>) => {
let previousActive: HTMLElement
watch(
() => toggle.value,
(val) => {
if (val) {
previousActive = document.activeElement as HTMLElement
if (isRef(initialFocus)) {
initialFocus.value.focus?.()
}
} else {
if (process.env.NODE_ENV === 'testing') {
previousActive.focus.call(previousActive)
} else {
previousActive.focus()
}
}
}
)
}