Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(): Introduce search bar for 404 page #129

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

Merged
merged 1 commit into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import dropdownMenu from './dropdown-menu'
import anchorsIndicator from './anchors-indicator'
import codeExample from './code-example'
import popover from './popover'
import searchBar404 from './search-bar-404'

feedback()
accordion()
Expand All @@ -21,3 +22,4 @@ dropdownMenu()
anchorsIndicator()
codeExample()
popover()
searchBar404()
38 changes: 20 additions & 18 deletions js/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,32 @@ const COMPONENT_SELECTOR = `[${COMPONENT_NAME}]`
const BODY_SELECTOR = 'data-popover-body'
const TRIGGER_SELECTOR = '[data-popover-target]'

const components = document.querySelectorAll(COMPONENT_SELECTOR)
export default function () {
const components = document.querySelectorAll(COMPONENT_SELECTOR)

for (let i = 0; i < components.length; i++) {
const triggers = components[i].querySelectorAll(TRIGGER_SELECTOR)
for (let i = 0; i < components.length; i++) {
const triggers = components[i].querySelectorAll(TRIGGER_SELECTOR)

for (let i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', e => {
e.preventDefault()
for (let i = 0; i < triggers.length; i++) {
triggers[i].addEventListener('click', e => {
e.preventDefault()

const targetBody = document.querySelector(`[${BODY_SELECTOR}=${e.target.dataset.popoverTarget}]`)
const targetBody = document.querySelector(`[${BODY_SELECTOR}=${e.target.dataset.popoverTarget}]`)

components[i].classList.toggle(components[i].dataset.activeClass)
})
}
components[i].classList.toggle(components[i].dataset.activeClass)
})
}

document.addEventListener('click', e => {
if (components[i].classList.contains(components[i].dataset.activeClass)) {
const popoverBody = components[i].querySelector(`[${BODY_SELECTOR}]`)
document.addEventListener('click', e => {
if (components[i].classList.contains(components[i].dataset.activeClass)) {
const popoverBody = components[i].querySelector(`[${BODY_SELECTOR}]`)

for (let i = 0; i < triggers.length; i++) {
if (!popoverBody.contains(e.target) && e.target !== triggers[i]) {
components[i].classList.remove(components[i].dataset.activeClass)
for (let i = 0; i < triggers.length; i++) {
if (!popoverBody.contains(e.target) && e.target !== triggers[i]) {
components[i].classList.remove(components[i].dataset.activeClass)
}
}
}
}
})
})
}
}
13 changes: 13 additions & 0 deletions js/search-bar-404/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const COMPONENT_NAME = 'data-search'
const COMPONENT_SELECTOR = `[${COMPONENT_NAME}]`

export default function () {
const search = document.querySelectorAll(COMPONENT_SELECTOR)
const pathParts = window.location.pathname.split("/")
const searchWord = pathParts[pathParts.length - 1]

for (let i = 0; i < search.length; i++) {
const input = search[i].querySelector('input')
input.value = searchWord
}
}
2 changes: 1 addition & 1 deletion src/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h1>The page you were looking for doesn’t exist.</h1>
<div class="flex__column--10 markdown">
<p>You may have mistyped the address or the page may have moved. Double-check the URL and try again or search the term.</p>

<div class="search">
<div class="search" data-search>
{% include navbar/search.html placeholder="Enter a search term" %}
</div>
</div>
Expand Down