Skip to content

fix(dropdowns): auto-focus only once dropdown is properly positioned

Paul Gascou-Vaillancourt requested to merge dropdowns-await-compute-position into main

What does this MR do?

fix(dropdowns): auto-focus only once dropdown is properly positioned

This ensures that we wait on dropdowns to be properly positioned within the page before attempting to auto-focus some of its child elements. Without this, the auto-focus might cause the page to unexpectedly scroll back to the top.

There is a simpler approach

When I finished implementing this fix, it occurred to me that a much simpler approach might work:

diff --git a/src/components/base/new_dropdowns/base_dropdown/base_dropdown.vue b/src/components/base/new_dropdowns/base_dropdown/base_dropdown.vue
index 8688008f..2936d028 100644
--- a/src/components/base/new_dropdowns/base_dropdown/base_dropdown.vue
+++ b/src/components/base/new_dropdowns/base_dropdown/base_dropdown.vue
@@ -222,8 +222,8 @@ export default {
       return {
         'gl-display-block!': this.visible,
         [FIXED_WIDTH_CLASS]: !this.fluidWidth,
-        'gl-fixed': this.isFixed,
-        'gl-absolute': !this.isFixed,
+        'gl-fixed': this.openedYet && this.isFixed,
+        'gl-absolute': this.openedYet && !this.isFixed,
       };
     },
     isFixed() {
@@ -324,6 +324,7 @@ export default {
          * dropdown actually being visible.
          */
         await this.$nextTick();
+        this.openedYet = true;
         this.startFloating();
         this.$emit(GL_DROPDOWN_SHOWN);
       } else {

And indeed it works. Delaying the application of the position CSS property seems to ensure that the auto-focus always happens when the dropdown is close to the toggle. I did not push that approach simply because I'm not sure I fully understand the underlying timing. E.g. how are the dropdown's coordinates properly computed if it's not absolutely positioned yet? I'm concerned that the fact that this works is a mere coincidence and can't be relied on. I figured I would rather go with a slightly more complex solution that I can fully grasp.

Screenshots or screen recordings

Before After
scroll no_scroll

Integration merge requests

Creating an integration MR might be messy currently as @gitlab/ui is way outdated in GitLab. The changes can be verified by:

  1. Pulling gitlab!121070 (merged) locally and running yarn. At this point, you should be on a version that has the scrolling issue. This can be verified in http://gdk.test:3000/-/profile/notifications.
  2. Installing the dev build in the GDK:
    yarn add @gitlab/ui@https://gitlab.com/gitlab-org/gitlab-ui/-/jobs/4379481074/artifacts/raw/gitlab-ui.dropdowns-await-compute-position.tgz
    You're now on this fixed version, the page should not scroll unexpectedly anymore.

Does this MR meet the acceptance criteria?

This checklist encourages the authors, reviewers, and maintainers of merge requests (MRs) to confirm changes were analyzed for conformity with the project's guidelines, security and accessibility.

Toggle the acceptance checklist

Conformity

  • Code review guidelines.
  • GitLab UI's contributing guidelines.
  • If it changes a Pajamas-compliant component's look & feel, the MR has been reviewed by a UX designer.
  • If it changes GitLab UI's documentation guidelines, the MR has been reviewed by a Technical Writer.
  • If the MR changes a component's API, integration MR(s) have been opened (see integration merge requests above).
  • Added the ~"component:*" label(s) if applicable.

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • Security reports checked/validated by a reviewer from the AppSec team

Accessibility

If this MR adds or modifies a component, take a few moments to review the following:

  • All actions and functionality can be done with a keyboard.
  • Links, buttons, and controls have a visible focus state.
  • All content is presented in text or with a text equivalent. For example, alt text for SVG, or aria-label for icons that have meaning or perform actions.
  • Changes in a component’s state are announced by a screen reader. For example, changing aria-expanded="false" to aria-expanded="true" when an accordion is expanded.
  • Color combinations have sufficient contrast.
Edited by Paul Gascou-Vaillancourt

Merge request reports

Loading