fix: search async ordering, scroll offset

This commit is contained in:
Jacky Zhao 2024-02-02 01:36:17 -08:00
parent a0b927da4a
commit 3231ce6e79
2 changed files with 9 additions and 8 deletions

View File

@ -224,6 +224,7 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
if (currentHover) { if (currentHover) {
currentHover.classList.remove("focus") currentHover.classList.remove("focus")
currentHover.blur()
} }
// If search is active, then we will render the first result and display accordingly // If search is active, then we will render the first result and display accordingly
@ -250,9 +251,9 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
: (document.activeElement as HTMLInputElement | null) : (document.activeElement as HTMLInputElement | null)
const prevResult = currentResult?.previousElementSibling as HTMLInputElement | null const prevResult = currentResult?.previousElementSibling as HTMLInputElement | null
currentResult?.classList.remove("focus") currentResult?.classList.remove("focus")
await displayPreview(prevResult)
prevResult?.focus() prevResult?.focus()
currentHover = prevResult currentHover = prevResult
await displayPreview(prevResult)
} }
} else if (e.key === "ArrowDown" || e.key === "Tab") { } else if (e.key === "ArrowDown" || e.key === "Tab") {
e.preventDefault() e.preventDefault()
@ -264,9 +265,9 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
: (document.getElementsByClassName("result-card")[0] as HTMLInputElement | null) : (document.getElementsByClassName("result-card")[0] as HTMLInputElement | null)
const secondResult = firstResult?.nextElementSibling as HTMLInputElement | null const secondResult = firstResult?.nextElementSibling as HTMLInputElement | null
firstResult?.classList.remove("focus") firstResult?.classList.remove("focus")
await displayPreview(secondResult)
secondResult?.focus() secondResult?.focus()
currentHover = secondResult currentHover = secondResult
await displayPreview(secondResult)
} else { } else {
// If an element in results-container already has focus, focus next one // If an element in results-container already has focus, focus next one
const active = currentHover const active = currentHover
@ -274,9 +275,9 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
: (document.activeElement as HTMLInputElement | null) : (document.activeElement as HTMLInputElement | null)
active?.classList.remove("focus") active?.classList.remove("focus")
const nextResult = active?.nextElementSibling as HTMLInputElement | null const nextResult = active?.nextElementSibling as HTMLInputElement | null
await displayPreview(nextResult)
nextResult?.focus() nextResult?.focus()
currentHover = nextResult currentHover = nextResult
await displayPreview(nextResult)
} }
} }
} }
@ -325,9 +326,9 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
currentHover?.classList.remove("focus") currentHover?.classList.remove("focus")
currentHover?.blur() currentHover?.blur()
const target = ev.target as HTMLInputElement const target = ev.target as HTMLInputElement
await displayPreview(target)
currentHover = target currentHover = target
currentHover.classList.add("focus") currentHover.classList.add("focus")
await displayPreview(target)
} }
async function onMouseLeave(ev: MouseEvent) { async function onMouseLeave(ev: MouseEvent) {
@ -405,12 +406,11 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
async function displayPreview(el: HTMLElement | null) { async function displayPreview(el: HTMLElement | null) {
if (!searchLayout || !enablePreview || !el || !preview) return if (!searchLayout || !enablePreview || !el || !preview) return
const slug = el.id as FullSlug const slug = el.id as FullSlug
el.classList.add("focus")
previewInner = document.createElement("div")
previewInner.classList.add("preview-inner")
const innerDiv = await fetchContent(slug).then((contents) => const innerDiv = await fetchContent(slug).then((contents) =>
contents.flatMap((el) => [...highlightHTML(currentSearchTerm, el as HTMLElement).children]), contents.flatMap((el) => [...highlightHTML(currentSearchTerm, el as HTMLElement).children]),
) )
previewInner = document.createElement("div")
previewInner.classList.add("preview-inner")
previewInner.append(...innerDiv) previewInner.append(...innerDiv)
preview.replaceChildren(previewInner) preview.replaceChildren(previewInner)
@ -418,7 +418,7 @@ document.addEventListener("nav", async (e: CustomEventMap["nav"]) => {
const highlights = [...preview.querySelectorAll(".highlight")].sort( const highlights = [...preview.querySelectorAll(".highlight")].sort(
(a, b) => b.innerHTML.length - a.innerHTML.length, (a, b) => b.innerHTML.length - a.innerHTML.length,
) )
highlights[0]?.scrollIntoView() highlights[0]?.scrollIntoView({ block: "start" })
} }
async function onType(e: HTMLElementEventMap["input"]) { async function onType(e: HTMLElementEventMap["input"]) {

View File

@ -131,6 +131,7 @@
& .highlight { & .highlight {
background: color-mix(in srgb, var(--tertiary) 60%, transparent); background: color-mix(in srgb, var(--tertiary) 60%, transparent);
border-radius: 5px; border-radius: 5px;
scroll-margin-top: 2rem;
} }
& > #preview-container { & > #preview-container {