Skip to content

Commit 2f6464d

Browse files
committed
Bring search bar into view on desktop
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. Closes #1860
1 parent 7e83e8c commit 2f6464d

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

assets/css/layout.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ body.sidebar-closed .content {
122122
left: 0;
123123
}
124124

125-
@media screen and (max-width: 768px) {
125+
@media screen and (hover: none) {
126126
.content,
127127
body.sidebar-opening .content {
128128
left: 0;

assets/css/search-bar.css

+13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,21 @@
11
.top-search {
2+
position: relative;
3+
top: 0;
4+
z-index: 101;
25
margin-top: 10px;
36
background-color: var(--background);
47
}
58

9+
@media only screen and (hover: hover) {
10+
.top-search {
11+
padding: 0.8rem 0;
12+
}
13+
}
14+
15+
.top-search.sticky {
16+
position: sticky;
17+
}
18+
619
.search-settings {
720
display: flex;
821
column-gap: 12px;

assets/js/helpers.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,21 @@ export function getProjectNameAndVersion () {
161161
}
162162

163163
/**
164-
* Return `true` if the client's OS is MacOS
164+
* Return `true` if the client's OS is MacOS.
165165
*
166166
* @return {Boolean}
167167
*/
168168
export function isMacOS () {
169169
return /(Mac|iPhone|iPod|iPad)/i.test(navigator.platform)
170170
}
171+
172+
/**
173+
* Return `true` if the client's device is touch-enabled.
174+
*
175+
* @return {Boolean}
176+
*/
177+
export function isTouchDevice () {
178+
return (('ontouchstart' in window) ||
179+
(navigator.maxTouchPoints > 0) ||
180+
(navigator.msMaxTouchPoints > 0))
181+
}

assets/js/search-bar.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ import {
77
AUTOCOMPLETE_CONTAINER_SELECTOR,
88
AUTOCOMPLETE_SUGGESTION_SELECTOR
99
} from './autocomplete/autocomplete-list'
10-
import { isMacOS, qs } from './helpers'
10+
import { isMacOS, isTouchDevice, qs } from './helpers'
1111

1212
const SEARCH_INPUT_SELECTOR = 'form.search-bar input'
1313
const SEARCH_CLOSE_BUTTON_SELECTOR = 'form.search-bar .search-close-button'
14+
const TOP_SEARCH_SELECTOR = '.top-search'
1415

1516
/**
1617
* Initializes the sidebar search box.
@@ -34,6 +35,12 @@ export function setSearchInputValue (value) {
3435
*/
3536
export function focusSearchInput () {
3637
const searchInput = qs(SEARCH_INPUT_SELECTOR)
38+
39+
if (!isTouchDevice()) {
40+
const topSearch = qs(TOP_SEARCH_SELECTOR)
41+
topSearch.classList.add('sticky')
42+
}
43+
3744
searchInput.focus()
3845
}
3946

@@ -136,15 +143,18 @@ function clearSearch () {
136143
function hideAutocomplete () {
137144
document.body.classList.remove('search-focused')
138145
hideAutocompleteList()
146+
qs(TOP_SEARCH_SELECTOR).classList.remove('sticky')
139147
}
140148

141149
let lastScrollTop = window.scrollY
142-
const topSearch = document.querySelector('.top-search')
150+
const topSearch = document.querySelector(TOP_SEARCH_SELECTOR)
143151
const sidebarMenu = document.getElementById('sidebar-menu')
144152
const backgroundLayer = document.querySelector('.background-layer')
145153
const scrollThreshold = 70 // Set a threshold for scroll, adjust as needed
146154

147155
window.addEventListener('scroll', function () {
156+
if (!isTouchDevice()) { return }
157+
148158
const currentScroll = window.scrollY
149159

150160
// Add 'fixed' class when not at the top

0 commit comments

Comments
 (0)