-
Notifications
You must be signed in to change notification settings - Fork 331
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just added a missing period from my last PR :) |
||
* | ||
* @return {Boolean} | ||
*/ | ||
export function isMacOS () { | ||
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform) | ||
} | ||
|
||
/** | ||
* Return `true` if the client's device is touch-enabled. | ||
* | ||
* @return {Boolean} | ||
*/ | ||
export function isTouchDevice () { | ||
return (('ontouchstart' in window) || | ||
(navigator.maxTouchPoints > 0) || | ||
(navigator.msMaxTouchPoints > 0)) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,11 @@ import { | |
AUTOCOMPLETE_CONTAINER_SELECTOR, | ||
AUTOCOMPLETE_SUGGESTION_SELECTOR | ||
} from './autocomplete/autocomplete-list' | ||
import { isMacOS, qs } from './helpers' | ||
import { isMacOS, isTouchDevice, qs } from './helpers' | ||
|
||
const SEARCH_INPUT_SELECTOR = 'form.search-bar input' | ||
const SEARCH_CLOSE_BUTTON_SELECTOR = 'form.search-bar .search-close-button' | ||
const TOP_SEARCH_SELECTOR = '.top-search' | ||
|
||
/** | ||
* Initializes the sidebar search box. | ||
|
@@ -34,6 +35,16 @@ export function setSearchInputValue (value) { | |
*/ | ||
export function focusSearchInput () { | ||
const searchInput = qs(SEARCH_INPUT_SELECTOR) | ||
|
||
if (!isTouchDevice()) { | ||
qs(TOP_SEARCH_SELECTOR).classList.add('sticky') | ||
if (window.scrollY === 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above. |
||
qs('.sidebar-button').classList.add('fixed-top') | ||
} else { | ||
qs('.sidebar-button').classList.add('fixed') | ||
} | ||
} | ||
|
||
searchInput.focus() | ||
} | ||
|
||
|
@@ -139,38 +150,47 @@ function hideAutocomplete () { | |
} | ||
|
||
let lastScrollTop = window.scrollY | ||
const topSearch = document.querySelector('.top-search') | ||
const topSearch = document.querySelector(TOP_SEARCH_SELECTOR) | ||
const sidebarMenu = document.getElementById('sidebar-menu') | ||
const backgroundLayer = document.querySelector('.background-layer') | ||
const scrollThreshold = 70 // Set a threshold for scroll, adjust as needed | ||
const searchInput = qs(SEARCH_INPUT_SELECTOR) | ||
const sidebarButton = qs('.sidebar-button') | ||
|
||
window.addEventListener('scroll', function () { | ||
const currentScroll = window.scrollY | ||
|
||
// Add 'fixed' class when not at the top | ||
if (currentScroll > scrollThreshold * 2) { | ||
topSearch.classList.add('sm-fixed') | ||
sidebarMenu.classList.add('sm-fixed') | ||
backgroundLayer.classList.add('sm-fixed') | ||
} | ||
if (isTouchDevice()) { | ||
// Add 'fixed' class when not at the top | ||
if (currentScroll > scrollThreshold * 2) { | ||
topSearch.classList.add('sm-fixed') | ||
sidebarMenu.classList.add('sm-fixed') | ||
backgroundLayer.classList.add('sm-fixed') | ||
} | ||
|
||
if (currentScroll === 0) { | ||
// Remove 'fixed' class when at the top | ||
topSearch.classList.remove('sm-fixed') | ||
sidebarMenu.classList.remove('sm-fixed') | ||
backgroundLayer.classList.remove('sm-fixed') | ||
} | ||
if (currentScroll === 0) { | ||
// Remove 'fixed' class when at the top | ||
topSearch.classList.remove('sm-fixed') | ||
sidebarMenu.classList.remove('sm-fixed') | ||
backgroundLayer.classList.remove('sm-fixed') | ||
} | ||
|
||
if (currentScroll > lastScrollTop && currentScroll > scrollThreshold) { | ||
// Scrolling down and past the threshold | ||
topSearch.classList.add('sm-hidden') | ||
sidebarMenu.classList.add('sm-hidden') | ||
backgroundLayer.classList.add('sm-hidden') | ||
} else { | ||
// Scrolling up or at the top of the page | ||
topSearch.classList.remove('sm-hidden') | ||
sidebarMenu.classList.remove('sm-hidden') | ||
backgroundLayer.classList.remove('sm-hidden') | ||
if (currentScroll > lastScrollTop && currentScroll > scrollThreshold) { | ||
// Scrolling down and past the threshold | ||
topSearch.classList.add('sm-hidden') | ||
sidebarMenu.classList.add('sm-hidden') | ||
backgroundLayer.classList.add('sm-hidden') | ||
} else { | ||
// Scrolling up or at the top of the page | ||
topSearch.classList.remove('sm-hidden') | ||
sidebarMenu.classList.remove('sm-hidden') | ||
backgroundLayer.classList.remove('sm-hidden') | ||
} | ||
} else if (currentScroll !== lastScrollTop) { | ||
topSearch.classList.remove('sticky') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to extract these into a |
||
sidebarButton.classList.remove('fixed') | ||
sidebarButton.classList.remove('fixed-top') | ||
searchInput.blur() | ||
} | ||
|
||
lastScrollTop = currentScroll <= 0 ? 0 : currentScroll | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,5 +91,4 @@ | |
<span class="sr-only">Settings</span> | ||
</button> | ||
</div> | ||
|
||
</div> |
There was a problem hiding this comment.
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.