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

Bring search bar into view on desktop #1872

Merged
merged 1 commit into from
Mar 12, 2024

Conversation

sodapopcan
Copy link
Contributor

@sodapopcan sodapopcan commented Feb 24, 2024

This commit changes how the search bar is brought into view on desktop as brought up in #1860: when using one of the keyboard shortcuts to focus the input, the entire page jumps to the top making you lose your scroll position. On mobile, technically smaller screens, this is remedied by causing the input to slide into view on scroll up, but I don't believe this is a desirable solution for desktop.

The following changes are introduced:

  • Using one of the keyboard shortcuts will focus the search input causing it to stick to the top.
  • It will continue to stick to the top so long as it is focused allowing you to scroll with it open.
  • The slide-on-scroll effect has been changed to only fire on touch-enabled devices as opposed to just smaller screens. This is allows us to make the hexdocs window very small and still use keyboard shortcuts--Useful on laptops.

PR notes

This turned out to be a little hairier than I imagined due to thinking there was more that needed to be done than there was. Turns out it's a fairly small PR! I did have to make the following concessions but overall I feel the UX is pretty solid.

  • When the search bar is brought into view, it needed a little bit of padding. Rather than get into branching code that checks if the search bar is visible in the viewport or not, I've just permanently added the padding (only on desktop). It's very slight.
  • I haven't added a slide-in/out effect as that is a bit more ceremony for potentially not amazing gains. I can certainly add it if you like! It's not that bad or anything, but needs to know the static search bar height (which probably isn't a problem).
  • Closing the search bar has been hooked into closing autocomplete. I realized this JustWorks after quite a while of futzing with a conditional blur event as well as contemplating bring in a click-away event. I haven't done any function renaming as it sort of gets weird between touch and desktop. Again I'm happy to put some time in a refactor there!
  • If you are going to be testing using devtools to switch between desktop and a touch device be aware things get a little wonky when you switch so you need to reload. I put no time into fixing this since devices don't switch between being touch enabled and not in real scenarios.

@@ -161,10 +161,21 @@ export function getProjectNameAndVersion () {
}

/**
* Return `true` if the client's OS is MacOS
* Return `true` if the client's OS is MacOS.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added a missing period from my last PR :)

@sodapopcan sodapopcan marked this pull request as draft February 25, 2024 01:34
@sodapopcan
Copy link
Contributor Author

I noticed a problem with this PR I thought I had taken care of so I've converted it to a draft until I fix it. It looks like I may need to bring in click-away since click the clear button or gear causes the whole search bar to close.

@josevalim
Copy link
Member

I haven't added a slide-in/out effect as that is a bit more ceremony for potentially not amazing gains. I can certainly add it if you like! It's not that bad or anything, but needs to know the static search bar height (which probably isn't a problem).

Could we just naturally make it so scrolling down makes it disappear again?

If we scroll up, it continues at the top, if we scroll down, it goes away. The idea is that it behaves like in mobile, except that scrolling up on large screens does not bring it down, only pressing "/".

@josevalim
Copy link
Member

And thank you on the PR and the work so far!

@sodapopcan
Copy link
Contributor Author

Could we just naturally make it so scrolling down makes it disappear again?

Absolutely! It certainly crossed my mind in the several ways I realized this could work so I'll go with that as it will also solve my other problem.

@sodapopcan
Copy link
Contributor Author

Just an update: I'm very sorry I have not gotten back to this. I will in the next day or so! I just had some stuff happen that is making me not want to write code and be social, even though for today that has manifested in talking about code a whole lot of Elixir Forum 🙃

padding: 16px 12px 18px 19px;
}

body.sidebar-closed .sidebar-button.fixed-top {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The burger menu has to be moved downward to be properly aligned with the searchbox when we are scrolled down the page. When we are at the top of the page, the top-search div is padded differently so we have to compensate in this situation.

This commit changes how the search bar is brought into view on desktop as
brought up in elixir-lang#1860: when using one of the keyboard shortcuts to focus
the input, the entire page jumps to the top making you lose your scroll
position.  On mobile, technically smaller screens, this is remedied by
causing the input to slide into view on scroll up, but I don't believe
this is a desirable solution for desktop.

The following changes are introduced:

  * Using one of the keyboard shortcuts will focus the search input
    causing it to stick to the top.
  * It will continue to stick to the top so long as it is focused
    allowing you to scroll with it open.
  * The slide-on-scroll effect has been changed to only fire on
    touch-enabled devices as opposed to just smaller screens.  This is
    allows us to make the hexdocs window very small and still use
    keyboard shortcuts--Useful on laptops.

Closes elixir-lang#1860

if (!isTouchDevice()) {
qs(TOP_SEARCH_SELECTOR).classList.add('sticky')
if (window.scrollY === 0) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

backgroundLayer.classList.remove('sm-hidden')
}
} else if (currentScroll !== lastScrollTop) {
topSearch.classList.remove('sticky')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to extract these into a closeSearchbar function but it's only called once and it would require passing in the DOM elements as dependencies otherwise we'd running three DOM queries in a scroll callback which would be worse than this run-on sentence :)

@sodapopcan sodapopcan marked this pull request as ready for review March 1, 2024 23:24
@sodapopcan
Copy link
Contributor Author

sodapopcan commented Mar 1, 2024

This is ready for review!

This was honestly more of a slog than I imagined it to be 😰 Certainly not the worst, but there are more states to consider than I was thinking. I only mention that as I'm a bit burned out on it so I never brought in the animation. That said, if for some reason you want to reject this don't worry, I won't take it poorly (I know you're probably used to this I always just like to reassure for open source contributions). I added some review comments and otherwise I think it's best to just try it out as opposed to me trying to describe it, but here is a summary anyway:

Desktop

  • s & friends focus the search bar as normal when scrollY is 0.
  • In any other scroll position it will bring down the searchbar in an overlay at the top.
  • Scrolling in either direction will hide it
  • With the sidebar closed and the viewport small enough, the burger menu will come down with the searchbar

Mobile

  • Nothing has changed other than, as mentioned, I've switched it to only fire on touch-enabled devices as opposed to screen size.

As per that last point, all behaviour related to showing the searchbar is dependent on being on a touch-enabled device or not and nothing to do with viewport size.

@josevalim josevalim merged commit ac40c44 into elixir-lang:main Mar 12, 2024
4 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

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

Successfully merging this pull request may close these issues.

2 participants