Skip to content

Commit bf7b3a2

Browse files
Don't trigger a search unless we have three characters (#1018)
1 parent 868ef2d commit bf7b3a2

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

js/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import navigation from './navigation'
1616
import relativeLinks from './relative-links'
1717
import sideMenu from './side-menu'
1818
import tracking from './tracking'
19+
import searchBar from './search-bar'
1920
feedback()
2021
accordion()
2122
accordionGroup()
@@ -33,4 +34,5 @@ backScrolling()
3334
navigation()
3435
relativeLinks()
3536
sideMenu()
36-
tracking()
37+
tracking()
38+
searchBar()

js/search-bar/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const ELEMENT_NAME = '.DocsNav-search-input'
2+
3+
export default function() {
4+
const el = document.querySelectorAll(ELEMENT_NAME)[0]
5+
if (el) {
6+
el.addEventListener('input', function(e) {
7+
// SwiftType currently trigger based on an `input` event being fired.
8+
// However, we only want this to occur if there are more than 2 characters typed
9+
// NOTE: If the user types 0 charactes i.e. removes and cancels a search we will still trigger SwiftType so we can hide the search UX.
10+
11+
if (e.target.value.length > 0 && e.target.value.length < 3) {
12+
e.stopImmediatePropagation()
13+
}
14+
})
15+
}
16+
}

js/side-menu/index.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
1-
export default function () {
2-
1+
export default function() {
32
// highlight active link slug in side menu
43
const pathname = document.location.pathname
54
// find all links associated with active catalog page
6-
const links = document.querySelectorAll(`[data-class-active="menu-item--active"] > ul > li > a[href="${pathname}"]`)
5+
const links = document.querySelectorAll(
6+
`[data-class-active="menu-item--active"] > ul > li > a[href="${pathname}"]`
7+
)
78
// only open the first 'category' that contains the active integrations
8-
links[0].parentElement.parentElement.parentElement.className += ' menu-item--active'
9-
for (let i = 0; i < links.length; i++) {
10-
let link = links[i]
11-
if (link.classList.contains('menu-item__link')) {
12-
// set link to be active
13-
link.parentElement.className += ' menu-item--active menu-item--indicator'
14-
link.className += ' menu-item__link--indicator'
9+
if (!!links.length) {
10+
links[0].parentElement.parentElement.parentElement.className +=
11+
' menu-item--active'
12+
for (let i = 0; i < links.length; i++) {
13+
let link = links[i]
14+
if (link.classList.contains('menu-item__link')) {
15+
// set link to be active
16+
link.parentElement.className +=
17+
' menu-item--active menu-item--indicator'
18+
link.className += ' menu-item__link--indicator'
19+
}
1520
}
1621
}
17-
}
22+
}

0 commit comments

Comments
 (0)