FIX: We can't use `?.` yet (#13168)

FIX: We can’t use ?. yet (#13168)

diff --git a/app/assets/javascripts/discourse/app/widgets/header.js b/app/assets/javascripts/discourse/app/widgets/header.js
index 784d526..11d5d56 100644
--- a/app/assets/javascripts/discourse/app/widgets/header.js
+++ b/app/assets/javascripts/discourse/app/widgets/header.js
@@ -508,9 +508,12 @@ export default createWidget("header", {
     this.toggleBodyScrolling(this.state.userVisible);
 
     // auto focus on first button in dropdown
-    schedule("afterRender", () =>
-      document.querySelector(".user-menu button")?.focus()
-    );
+    schedule("afterRender", () => {
+      let elem = document.querySelector(".user-menu button");
+      if (elem) {
+        elem.focus();
+      }
+    });
   },
 
   toggleHamburger() {
@@ -519,7 +522,10 @@ export default createWidget("header", {
 
     // auto focus on first link in dropdown
     schedule("afterRender", () => {
-      document.querySelector(".hamburger-panel .menu-links a")?.focus();
+      let elem = document.querySelector(".hamburger-panel .menu-links a");
+      if (elem) {
+        elem.focus();
+      }
     });
   },
 

GitHub sha: 96fd202d

This commit appears in #13168 which was approved by markvanlan. It was merged by eviltrout.

Out of interest, why can’t we use it? Should we add an eslint rule to warn us?

@davidtaylorhq see DEV: Enable optional chaining in all contexts by pmusaraj · Pull Request #13180 · discourse/discourse · GitHub :slightly_smiling_face:

This commit has been mentioned on Discourse Meta. There might be relevant details there: