Parcourir la source

Message: restore focus only when target in viewport

Leopoldthecoder il y a 7 ans
Parent
commit
194874b3b2
1 fichiers modifiés avec 12 ajouts et 1 suppressions
  1. 12 1
      packages/message/src/main.vue

+ 12 - 1
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() {