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

Page scroll to the bottom of page when closing the modal on Safari #1260

Open
daolf opened this issue Jan 12, 2022 · 18 comments
Open

Page scroll to the bottom of page when closing the modal on Safari #1260

daolf opened this issue Jan 12, 2022 · 18 comments
Labels
Needs investigation Investigation is planned but not started yet

Comments

@daolf
Copy link

daolf commented Jan 12, 2022

Description

On Safari, when opening the search modal and then closing it without having made any search, the page scroll to the bottom of the page.

When searching something, it looks like the search box behave nicely.

Safari version: 14.1
Docsearch version: 3.0.0-alpha.42

Enregistrement.de.l.ecran.2022-01-12.a.23.47.27.mov

Steps to reproduce

  1. Open Safari 14.1
  2. Go to [https://www.scrapingbee.com/documentation]
  3. Type cmd + K to open the modal
  4. Press esc to close it
  5. Window scroll to the bottom of the page

Live reproduction:

https://www.scrapingbee.com/documentation

Expected behavior

It should not scroll to the bottom of the page

Environment

  • OS: macOS 11.3.1
  • Browser: Safari 14.1
  • DocSearch version: 3.0.0-alpha.42
@shortcuts
Copy link
Member

Hey, are you able to reproduce on our documentation? docsearch.algolia.com

(Sorry I'm not on a computer rn)

@daolf
Copy link
Author

daolf commented Jan 12, 2022

No, I was not, but it looks like at least one other project had the same issue:

I'm instantiating the search in the simplest way:

<script src="https://cdn.jsdelivr.net/npm/@docsearch/js@alpha"></script>
<div class="mb-19 pr-15">
    <div id="search" placeholder="Search">
    </div>
</div>
<script>
    window.docsearch({
        // Your Search API Key
        apiKey: '<apiKey>',
        // The index populated by the DocSearch scraper
        indexName: 'scrapingbee_docs',
        // Your Algolia Application ID
        appId: '<APP-ID>',
        // Replace inputSelector with a CSS selector
        // matching your search input
        container: '#search',
        // Set debug to true to inspect the dropdown
        debug: false,
    });
</script>

@daolf
Copy link
Author

daolf commented Jan 13, 2022

Ok so I found out how to fix this.

After noticing that this bug didn't happen on Netlify deploy preview I wanted to understand why.

On Deploy preview mode, Netlify adds a "feedback" overlay. That overlay seemed to fix the bug.

Conclusion

Adding an empty input inside a fixed div fixes it.

Here is the updated structure of the HTML of our doc page:

<!DOCTYPE html>
<html lang="en">
    {{ partial "head" . }}
    <body>
        <div id="wrapper">
            <div id="content">
            <!--SearchBar-->
            <!--Content-->
            </div>
        </div>
        <!--Magic Fix-->
        <div class="fixed">
            <input type="text">
        </div>
    </body>
</html>

Looking at other issues it probably has to do with tabindex, like it looks like Safari doesn't like when you close the popup and the browser doesn't have any other input / "tabindexable" element to focus. The Netlify preview feature didn't add an input, but an iframe.

But replacing the iframe by the empty input fixes it 🤷‍♂️

@shortcuts
Copy link
Member

Adding an empty input inside a fixed div fixes it.
The Netlify preview feature didn't add an input, but an iframe.

So anything that fills the DOM along with the DocSearch component should fix this issue?

This is good to know, thanks for investigating, I'll try to reproduce in multiple cases to see if it's something we can handle

@daolf
Copy link
Author

daolf commented Jan 26, 2022

I don't know if it is anything or a particlular kind element that needs to be focusable by the browser.

@jackmcdade
Copy link

jackmcdade commented Jan 26, 2022

We're having this issue on https://statamic.dev and haven't been able to find a workaround yet.

Our open issue: statamic/docs#707

@daolf
Copy link
Author

daolf commented Jan 26, 2022

Hi there,

I was able to fix this on your dom by adding this just before the end of the body

image

@daolf
Copy link
Author

daolf commented Jan 26, 2022

Still have no idea why this works though

@jackmcdade
Copy link

jackmcdade commented Jan 26, 2022

Hi there,

I was able to fix this on your dom by adding this just before the end of the body

image

I tried that but it didn't work for me. Ah yes, i had it near the end, but not the very last thing. It worked!

@daolf
Copy link
Author

daolf commented Jan 26, 2022

Great!

@shortcuts shortcuts added the Needs investigation Investigation is planned but not started yet label Feb 9, 2022
@mdo
Copy link

mdo commented Apr 14, 2022

I've just encountered this issue as well in trying to update Bootstrap's docs to DocSearch 3. See twbs/bootstrap#35736. The problem can be seen in our staging deploy for that PR: https://deploy-preview-35736--twbs-bootstrap.netlify.app/docs/5.1/getting-started/introduction/.

Anything else we can do to help debug?

@shortcuts
Copy link
Member

shortcuts commented Apr 14, 2022

Hey, thanks for coming back to this @mdo.

Are you also testing with Safari 14? I'm not able to reproduce with Safari 15.x anymore

edit: managed to reproduce, you need to scroll a bit before hitting cmd k

@mdo
Copy link

mdo commented Apr 14, 2022

Yup, still have it in latest Safari 15. I see it whenever I click into it or Cmd+K.

@shortcuts
Copy link
Member

shortcuts commented Apr 14, 2022

It seems that this part is triggered on escape 🤔 I'll try to investigate on the Autocomplete side. (cc @sarahdayan @francoischalifour if you want to double check)

@sarahdayan
Copy link
Member

Likely related: algolia/autocomplete#592

@mdo
Copy link

mdo commented Apr 29, 2022

@shortcuts Were you able to investigate this further? Let me know if I can be of help at all :).

@DanielSGaspar
Copy link

Ok so I found out how to fix this.

After noticing that this bug didn't happen on Netlify deploy preview I wanted to understand why.

On Deploy preview mode, Netlify adds a "feedback" overlay. That overlay seemed to fix the bug.

Conclusion

Adding an empty input inside a fixed div fixes it.

Here is the updated structure of the HTML of our doc page:

<!DOCTYPE html>
<html lang="en">
    {{ partial "head" . }}
    <body>
        <div id="wrapper">
            <div id="content">
            <!--SearchBar-->
            <!--Content-->
            </div>
        </div>
        <!--Magic Fix-->
        <div class="fixed">
            <input type="text">
        </div>
    </body>
</html>

Looking at other issues it probably has to do with tabindex, like it looks like Safari doesn't like when you close the popup and the browser doesn't have any other input / "tabindexable" element to focus. The Netlify preview feature didn't add an input, but an iframe.

But replacing the iframe by the empty input fixes it 🤷‍♂️

As you mention here, the issue is with the focus. So another fix would be to blur the input before closing the modal. This way safari will not try to search for another input to focus and this will also fix the issue.

@ryanleichty
Copy link

This issue also exists on https://remix.run/. When you're at the top of the page in Safari (v16.6) and open and close the modal with esc, it scrolls to the bottom of the page.

RobinMalfait added a commit to tailwindlabs/tailwindcss.com that referenced this issue Jan 28, 2025
#### Reference Issues / PRs

closes #2004

#### What does this implement/fix?

Fixes the quick jump to bottom of the page in safari

This is an existing bug in docsearch:
algolia/docsearch#1260

#### Any other comments?

No

---------

Co-authored-by: Robin Malfait <malfait.robin@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs investigation Investigation is planned but not started yet
Projects
None yet
Development

No branches or pull requests

7 participants