]> cat aescling's git repositories - mastodon.git/commitdiff
[Glitch] Fix lost focus when modals open/close
authorThibG <thib@sitedethib.com>
Sat, 30 Nov 2019 17:19:47 +0000 (18:19 +0100)
committerThibaut Girka <thib@sitedethib.com>
Sun, 1 Dec 2019 11:19:06 +0000 (12:19 +0100)
Port 35b142a7ad19821483f900e81e915a7925fd4eaf to glitch-soc

Signed-off-by: Thibaut Girka <thib@sitedethib.com>
app/javascript/flavours/glitch/components/modal_root.js

index e73ef8d125cd6d8ea6b62189741850aa42885e2e..f9877d5eadff74fe45a6ad477a2873d946e13f68 100644 (file)
@@ -62,15 +62,22 @@ export default class ModalRoot extends React.PureComponent {
     } else if (!nextProps.children) {
       this.setState({ revealed: false });
     }
-    if (!nextProps.children && !!this.props.children) {
-      this.activeElement.focus({ preventScroll: true });
-      this.activeElement = null;
-    }
   }
 
   componentDidUpdate (prevProps) {
     if (!this.props.children && !!prevProps.children) {
       this.getSiblings().forEach(sibling => sibling.removeAttribute('inert'));
+
+      // Because of the wicg-inert polyfill, the activeElement may not be
+      // immediately selectable, we have to wait for observers to run, as
+      // described in https://github.com/WICG/inert#performance-and-gotchas
+      Promise.resolve().then(() => {
+        this.activeElement.focus({ preventScroll: true });
+        this.activeElement = null;
+      }).catch((error) => {
+        console.error(error);
+      });
+
       this.handleModalClose();
     }
     if (this.props.children) {