From 194874b3b2281b066b288591665185313b75af5d Mon Sep 17 00:00:00 2001 From: Leopoldthecoder Date: Wed, 1 Nov 2017 17:33:51 +0800 Subject: [PATCH] Message: restore focus only when target in viewport --- packages/message/src/main.vue | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/message/src/main.vue b/packages/message/src/main.vue index a29d05e52..882b76f4d 100644 --- a/packages/message/src/main.vue +++ b/packages/message/src/main.vue @@ -87,7 +87,18 @@ if (typeof this.onClose === 'function') { this.onClose(this); } - this.originFocus && this.originFocus.focus(); // 键盘焦点回归 + if (!this.originFocus || !this.originFocus.getBoundingClientRect) return; + + // restore keyboard focus + const { top, left, bottom, right } = this.originFocus.getBoundingClientRect(); + const viewportHeight = window.innerHeight || document.documentElement.clientHeight; + const viewportWidth = window.innerWidth || document.documentElement.clientWidth; + if (top >= 0 && + left >= 0 && + bottom <= viewportHeight && + right <= viewportWidth) { + this.originFocus.focus(); + } }, clearTimer() {