mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2025-02-06 14:14:23 +01:00
Close search modal on anchor click
This commit is contained in:
parent
9f8ef33545
commit
9edf730873
File diff suppressed because one or more lines are too long
@ -113,10 +113,18 @@ export default class Result {
|
|||||||
const result = this.index_.search(ev.target.value)
|
const result = this.index_.search(ev.target.value)
|
||||||
result.forEach(item => {
|
result.forEach(item => {
|
||||||
const doc = this.data_[item.ref]
|
const doc = this.data_[item.ref]
|
||||||
|
|
||||||
|
/* Check if it's a anchor link on the current page */
|
||||||
|
let [pathname] = doc.location.split("#")
|
||||||
|
pathname = pathname.replace(/^(\/?\.{2})+/g, "")
|
||||||
|
|
||||||
|
/* Append search result */
|
||||||
this.list_.appendChild(
|
this.list_.appendChild(
|
||||||
<li class="md-search-result__item">
|
<li class="md-search-result__item">
|
||||||
<a href={doc.location} title={doc.title}
|
<a href={doc.location} title={doc.title}
|
||||||
class="md-search-result__link">
|
class="md-search-result__link" data-md-rel={
|
||||||
|
pathname === document.location.pathname
|
||||||
|
? "anchor" : ""}>
|
||||||
<article class="md-search-result__article">
|
<article class="md-search-result__article">
|
||||||
<h1 class="md-search-result__title">
|
<h1 class="md-search-result__title">
|
||||||
{doc.title}
|
{doc.title}
|
||||||
@ -130,6 +138,25 @@ export default class Result {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
/* Bind click handlers for anchors */
|
||||||
|
const anchors = this.list_.querySelectorAll("[data-md-rel=anchor]")
|
||||||
|
Array.prototype.forEach.call(anchors, anchor => {
|
||||||
|
anchor.addEventListener("click", ev2 => {
|
||||||
|
const toggle = document.querySelector("[data-md-toggle=search]")
|
||||||
|
if (toggle.checked) {
|
||||||
|
toggle.checked = false
|
||||||
|
toggle.dispatchEvent(new CustomEvent("change"))
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Hack: prevent default, as the navigation needs to be delayed due
|
||||||
|
to the search body lock on mobile */
|
||||||
|
ev2.preventDefault()
|
||||||
|
setTimeout(() => {
|
||||||
|
document.location.href = anchor.href
|
||||||
|
}, 100)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
/* Update search metadata */
|
/* Update search metadata */
|
||||||
this.meta_.textContent =
|
this.meta_.textContent =
|
||||||
`${result.length} search result${result.length !== 1 ? "s" : ""}`
|
`${result.length} search result${result.length !== 1 ? "s" : ""}`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user