Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocomplete box doesn't work with fixed positioning #1199

Open
andrewmkhoury opened this issue Oct 6, 2023 · 1 comment
Open

Autocomplete box doesn't work with fixed positioning #1199

andrewmkhoury opened this issue Oct 6, 2023 · 1 comment

Comments

@andrewmkhoury
Copy link

andrewmkhoury commented Oct 6, 2023

Description

When the search field has fixed positioning on the page and you scroll while the autocomplete box is open the box doesn't follow scroll. The box scrolls out of view.
Related code is here:

const top = scrollTop + containerRect.top + containerRect.height;

The problem is that autocomplete uses absolute positioning. To fix this, I suggest to have a configuration to allow it to use fixed positioning instead.

In my project, I was able to work around this issue by doing the following:

  • CSS
:root {
    --position-autocomplete-panel-top: 52px;
}

.aa-Panel {
    position: fixed;
    top: var(--position-autocomplete-panel-top) !important;
    z-index: 1000;
}
  • JS
    const fixPosition = () => {
      const rect = document.querySelector('.search-field').getBoundingClientRect();
      // Set css variable to be below the search box in case the search box moved when the window was resized
      setCSSVar('--position-autocomplete-panel-top', `${rect.bottom}px`);
    };

    document.querySelector('.aa-Input').addEventListener('focus', fixPosition);
    document.querySelector('.aa-Input').addEventListener('blur', fixPosition);
    window.addEventListener('resize', fixPosition);

Reproduction

My Sandbox: https://codesandbox.io/s/youthful-microservice-6km8p4?file=/style.css:843-880

Steps

  1. Add this change to the css of the sandbox:
#autocomplete {
  position: fixed;
}
  1. Now see that the search field follows you while you scroll in the frame
  2. When you click in the search field then scroll you will see weird behavior. See screenshot below:
    image

Expected behavior

It should be possible to make fixed positioning work without a CSS hack using !important.

Environment

  • OS: macOS
  • Browser: Chrome
  • Autocomplete version: 1.11.1
linonetwo added a commit to tiddly-gittly/tw-command-palette that referenced this issue May 20, 2024
linonetwo added a commit to tiddly-gittly/tw-command-palette that referenced this issue May 20, 2024
@linonetwo
Copy link

Thanks for the hack!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants