From b2ab4200f783b891e713c650a1c07ecaa656c5d5 Mon Sep 17 00:00:00 2001 From: Saurav Kumar Sinha <94630933+sauravsinhadev@users.noreply.github.com> Date: Thu, 3 Dec 2026 16:52:35 +0000 Subject: [PATCH] Improve search UI and UX --- CHANGELOG.md | 37 ++++ assets/js/search.js | 361 +++++++++++++++++++++++++++++++++-- assets/theme-css/search.css | 78 ++++++++ layouts/partials/navbar.html | 13 +- pyproject.toml | 2 +- 5 files changed, 472 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c692128..d289ad7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,40 @@ +# scientific-python-hugo-theme 0.24rc0 + +We're happy to announce the release of scientific-python-hugo-theme 0.24rc0! + +## Bug Fixes + +- Update hugo-atom-layout to restore `utm_source` links ([#719](https://github.com/scientific-python/scientific-python-hugo-theme/pull/719)). +- Update hugo-atom-layout to limit the home feed to main sections ([#720](https://github.com/scientific-python/scientific-python-hugo-theme/pull/720)). +- Only add discourse link when configured ([#722](https://github.com/scientific-python/scientific-python-hugo-theme/pull/722)). + +## Documentation + +- Document cloning submodules recursively ([#717](https://github.com/scientific-python/scientific-python-hugo-theme/pull/717)). + +## Maintenance + +- Remove learn.scientific-python.org from Netlify preview build ([#727](https://github.com/scientific-python/scientific-python-hugo-theme/pull/727)). +- Upgrade Mermaid to 12.2.0 ([#731](https://github.com/scientific-python/scientific-python-hugo-theme/pull/731)). + +## Contributors + +4 authors added to this release (alphabetically): + +- Agriya Khetarpal ([@agriyakhetarpal](https://github.com/agriyakhetarpal)) +- Inessa Pawson ([@InessaPawson](https://github.com/InessaPawson)) +- Jarrod Millman ([@jarrodmillman](https://github.com/jarrodmillman)) +- Stefan van der Walt ([@stefanv](https://github.com/stefanv)) + +3 reviewers added to this release (alphabetically): + +- Agriya Khetarpal ([@agriyakhetarpal](https://github.com/agriyakhetarpal)) +- Jarrod Millman ([@jarrodmillman](https://github.com/jarrodmillman)) +- Stefan van der Walt ([@stefanv](https://github.com/stefanv)) + +_These lists are automatically generated, and may not be complete or may contain +duplicates._ + # scientific-python-hugo-theme 0.23 We're happy to announce the release of scientific-python-hugo-theme 0.23! diff --git a/assets/js/search.js b/assets/js/search.js index 459df837..bbe0ce3c 100644 --- a/assets/js/search.js +++ b/assets/js/search.js @@ -1,8 +1,10 @@ "use strict"; window.addEventListener("DOMContentLoaded", () => { - let searchDialog = document.querySelector(".search-dialog"); - let searchButton = document.getElementById("search-button"); + const searchDialog = document.querySelector(".search-dialog"); + const searchButton = + document.getElementById("search-button") || + document.querySelector(".search-button"); // Do nothing here if search is not enabled. if (!searchDialog || !searchButton) return; @@ -17,8 +19,338 @@ window.addEventListener("DOMContentLoaded", () => { showSubResults: true, }); - let showSearch = () => searchDialog.showModal(); - let hideSearch = () => searchDialog.close(); + // Cache links once at load for zero-query quick jumps + const cachedLinks = Array.from(document.querySelectorAll("a[href]")) + .map((a) => ({ + label: (a.innerText || a.textContent || "").trim(), + href: a.getAttribute("href"), + })) + .filter( + (item) => + item.label && + item.label.length > 1 && + item.label.length < 36 && + item.href && + !item.href.startsWith("#") && + !item.href.startsWith("javascript:") + ) + .slice(0, 8); + + let uiInitialized = false; + let renderQuickJumps = () => {}; + + const applyCleanLightPalette = () => { + if (!document.getElementById("sp-clean-search-style")) { + const style = document.createElement("style"); + style.id = "sp-clean-search-style"; + style.textContent = ` + dialog.search-dialog { + background: #ffffff !important; + color: #1f2328 !important; + border: 1px solid #d0d7de !important; + border-radius: 12px !important; + box-shadow: 0 12px 28px rgba(27, 31, 36, 0.12) !important; + padding: 0 !important; + margin: auto !important; + outline: none !important; + max-width: min(720px, calc(100vw - 32px)) !important; + max-height: calc(100vh - 48px) !important; + width: 100% !important; + display: flex !important; + flex-direction: column !important; + overflow: hidden !important; + font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans", Helvetica, Arial, sans-serif !important; + } + + dialog.search-dialog:not([open]) { + display: none !important; + } + + dialog.search-dialog::backdrop { + background: rgba(27, 31, 36, 0.22) !important; + } + + dialog.search-dialog .pagefind-ui, + dialog.search-dialog .pagefind-ui__form { + background: #ffffff !important; + color: #1f2328 !important; + border: none !important; + box-shadow: none !important; + outline: none !important; + display: flex !important; + flex-direction: column !important; + min-height: 0 !important; + flex: 1 1 auto !important; + } + + dialog.search-dialog .pagefind-ui__form { + position: relative !important; + flex: 0 0 auto !important; + } + + dialog.search-dialog input[type="search"], + dialog.search-dialog .pagefind-ui__search-input { + width: 100% !important; + height: 52px !important; + box-sizing: border-box !important; + background: #ffffff !important; + color: #1f2328 !important; + border: none !important; + border-bottom: 1px solid #d8dee4 !important; + border-radius: 0 !important; + padding: 0 96px 0 42px !important; + font-size: 15px !important; + font-weight: 400 !important; + line-height: 52px !important; + outline: none !important; + box-shadow: none !important; + caret-color: #1f2328 !important; + } + + dialog.search-dialog input[type="search"]::placeholder, + dialog.search-dialog .pagefind-ui__search-input::placeholder { + color: #656d76 !important; + font-family: inherit !important; + font-size: 14px !important; + opacity: 1 !important; + } + + /* Compact height (24px) & vertically centered inside the 52px input */ + dialog.search-dialog button[aria-label*="Clear" i], + dialog.search-dialog .pagefind-ui__search-clear, + .sp-clear-pill { + position: absolute !important; + right: 14px !important; + top: 26px !important; + transform: translateY(-50%) !important; + height: 24px !important; + box-sizing: border-box !important; + display: inline-flex !important; + align-items: center !important; + justify-content: center !important; + gap: 5px !important; + background: #ffffff !important; + color: #57606a !important; + border: 1px solid #d0d7de !important; + border-radius: 6px !important; + padding: 0 7px !important; + font-size: 11px !important; + font-weight: 500 !important; + line-height: 1 !important; + cursor: pointer !important; + box-shadow: none !important; + outline: none !important; + z-index: 3 !important; + } + + dialog.search-dialog button[aria-label*="Clear" i]:hover, + dialog.search-dialog .pagefind-ui__search-clear:hover, + .sp-clear-pill:hover { + background: #f6f8fa !important; + color: #1f2328 !important; + border-color: #afb8c1 !important; + } + + .sp-esc-tag { + display: inline-flex; + align-items: center; + height: 16px; + padding: 0 4px; + font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; + font-size: 10px; + line-height: 1; + color: #57606a; + background: #f6f8fa; + border: 1px solid #d8dee4; + border-radius: 3px; + } + + .sp-quick-jumps { + padding: 10px 14px 14px 14px; + background: #ffffff; + overflow-y: auto; + overscroll-behavior: contain; + } + + .sp-quick-heading { + padding: 6px 4px 8px 4px; + font-size: 12px; + font-weight: 600; + color: #57606a; + line-height: 16px; + } + + .sp-quick-row { + display: flex; + align-items: center; + gap: 12px; + padding: 9px 10px; + margin-bottom: 4px; + border-radius: 8px; + font-size: 14px; + line-height: 20px; + color: #1f2328; + text-decoration: none; + background: #ffffff; + } + + .sp-quick-row:hover { + background: #f6f8fa; + } + + .sp-quick-row svg { + width: 16px; + height: 16px; + flex-shrink: 0; + color: #57606a; + stroke: currentColor; + fill: none; + stroke-width: 1.6; + } + + /* Scrollable results container with clean outer & inner spacing */ + dialog.search-dialog .pagefind-ui__drawer { + background: #ffffff !important; + border: none !important; + box-shadow: none !important; + overflow: hidden !important; + display: flex !important; + flex-direction: column !important; + min-height: 0 !important; + flex: 1 1 auto !important; + } + + dialog.search-dialog .pagefind-ui__results-area { + margin: 12px 14px 16px 14px !important; + padding: 0 4px 0 0 !important; + background: #ffffff !important; + border: none !important; + box-shadow: none !important; + max-height: min(60vh, 480px) !important; + overflow-y: auto !important; + overscroll-behavior: contain !important; + -webkit-overflow-scrolling: touch !important; + } + + dialog.search-dialog .pagefind-ui__message { + margin: 0 4px 12px 4px !important; + color: #57606a !important; + font-size: 13px !important; + } + + dialog.search-dialog .pagefind-ui__result { + background: #ffffff !important; + border: 1px solid #e6ebf1 !important; + border-radius: 10px !important; + box-shadow: none !important; + padding: 14px 16px !important; + margin: 0 0 10px 0 !important; + } + + dialog.search-dialog .pagefind-ui__result:hover { + background: #f6f8fa !important; + border-color: #d0d7de !important; + } + + dialog.search-dialog mark { + background: #fff8c5 !important; + color: #1f2328 !important; + border-radius: 2px !important; + padding: 0 2px !important; + } + `; + document.head.appendChild(style); + } + + if (uiInitialized) return; + + const input = searchDialog.querySelector( + 'input[type="search"], .pagefind-ui__search-input' + ); + const formEl = + input?.closest("form, .pagefind-ui__form") || input?.parentElement; + if (!input || !formEl) return; + + uiInitialized = true; + input.placeholder = "Search docs..."; + + const handleClearOrClose = (evt) => { + evt.stopPropagation(); + if (input.value && input.value.trim().length > 0) { + evt.preventDefault(); + input.value = ""; + input.dispatchEvent(new Event("input", { bubbles: true })); + input.focus(); + } else { + evt.preventDefault(); + searchDialog.close(); + } + }; + + const existingClear = formEl.querySelector( + 'button[aria-label*="Clear" i], .pagefind-ui__search-clear' + ); + if (existingClear) { + existingClear.classList.add("sp-clear-pill"); + existingClear.innerHTML = `ClearEsc`; + existingClear.addEventListener("click", handleClearOrClose); + } else { + const clearBtn = document.createElement("button"); + clearBtn.type = "button"; + clearBtn.className = "sp-clear-pill"; + clearBtn.innerHTML = `ClearEsc`; + clearBtn.addEventListener("click", handleClearOrClose); + formEl.appendChild(clearBtn); + } + + const quickBox = document.createElement("div"); + quickBox.className = "sp-quick-jumps"; + formEl.insertAdjacentElement("afterend", quickBox); + + const docIcon = ``; + const quickItems = [ + { label: "User Guide", href: cachedLinks[0]?.href || "#" }, + { label: "Shortcodes", href: cachedLinks[1]?.href || "#" }, + { label: "Examples", href: cachedLinks[2]?.href || "#" }, + { label: "Repository", href: cachedLinks[3]?.href || "#" }, + ]; + + renderQuickJumps = (query = "") => { + const hasText = query.trim().length > 0; + quickBox.style.display = hasText ? "none" : "block"; + if (hasText) return; + + quickBox.innerHTML = + `
Quick Jump
` + + quickItems + .map( + (item) => ` + + ${docIcon} + ${item.label} + + ` + ) + .join(""); + }; + + renderQuickJumps(input.value); + input.addEventListener("input", (evt) => renderQuickJumps(evt.target.value)); + }; + + let showSearch = () => { + if (!searchDialog.open) { + searchDialog.showModal(); + } + applyCleanLightPalette(); + renderQuickJumps(searchDialog.querySelector("input")?.value || ""); + }; + + let hideSearch = () => { + if (searchDialog.open) { + searchDialog.close(); + } + }; let toggleSearch = () => { if (!searchDialog.open) { @@ -38,12 +370,10 @@ window.addEventListener("DOMContentLoaded", () => { ); }; - // Close the search dialog if user clicks outside of it when it is open. - // This feels like functionality that should really be natively supported - // by the dialog element already. - // https://blog.webdevsimplified.com/2023-04/html-dialog/ + // Only close when clicking genuinely outside the dialog bounds searchDialog.addEventListener("click", (evt) => { - if (searchDialog.open && isClickOutside(searchDialog, evt)) { + if (!searchDialog.open) return; + if (evt.target === searchDialog && isClickOutside(searchDialog, evt)) { hideSearch(); } }); @@ -51,14 +381,15 @@ window.addEventListener("DOMContentLoaded", () => { window.addEventListener("keydown", (evt) => { if ( ((isMac && evt.metaKey) || (!isMac && evt.ctrlKey)) && - evt.key === "k" + evt.key.toLowerCase() === "k" ) { - evt.preventDefault(); // prevents default browser behaviour + evt.preventDefault(); toggleSearch(); } }); - document - .querySelector(".search-button") - .addEventListener("click", showSearch); -}); + searchButton.addEventListener("click", (evt) => { + evt.stopPropagation(); + showSearch(); + }); +}); \ No newline at end of file diff --git a/assets/theme-css/search.css b/assets/theme-css/search.css index 9e66566b..5229f795 100644 --- a/assets/theme-css/search.css +++ b/assets/theme-css/search.css @@ -24,3 +24,81 @@ .pagefind-ui__result-title > .pagefind-ui__result-link { color: unset !important; } + +button#search-button, +button[aria-label*="Search"], +.search-button, +.navbar-search, +#search-btn, +nav button:has(svg) { + display: inline-flex; + align-items: center; + gap: 10px; + width: auto; + height: auto; + min-width: 0; + padding: 6px 14px; + position: relative; + word-wrap: break-word; + + background-clip: border-box; + background-color: var(--sd-color-card-background, #0d1117); + border: 1px solid var(--sd-color-card-border, #30363d); + border-radius: 8px; + color: var(--sd-color-card-text, #c9d1d9); + + font-family: 'Fira Code', 'JetBrains Mono', monospace; + font-size: 13px; + cursor: pointer; + transition: border-color 0.2s ease, box-shadow 0.2s ease, color 0.2s ease; +} + +button#search-button:hover, +button[aria-label*="Search"]:hover, +.search-button:hover, +.navbar-search:hover, +#search-btn:hover, +nav button:has(svg):hover { + border-color: #58a6ff; + box-shadow: 0 0 12px rgba(88, 166, 255, 0.25); +} + +button#search-button svg { + color: #58a6ff; + flex-shrink: 0; +} + +.dev-search-label { + color: var(--sd-color-card-text, #8b949e); + user-select: none; +} + +.dev-search-kbds { + border: 1px solid var(--sd-color-card-border, #30363d); + border-bottom-width: 2px; + border-radius: 4px; + padding: 1px 6px; + font-size: 11px; + color: #58a6ff; +} + +/* Optional: matching search input styling */ +input[type="search"], +input[placeholder*="Search"], +.search-input { + background-color: var(--sd-color-card-background, #0d1117); + color: var(--sd-color-card-text, #e6edf3); + border: 1px solid var(--sd-color-card-border, #30363d); + border-radius: 8px; + padding: 10px 14px; + font-family: 'Fira Code', 'JetBrains Mono', monospace; + outline: none; + transition: border-color 0.2s ease, box-shadow 0.2s ease; +} + +input[type="search"]:focus, +input[placeholder*="Search"]:focus, +.search-input:focus { + border-color: #58a6ff; + box-shadow: 0 0 12px rgba(88, 166, 255, 0.25); +} diff --git a/layouts/partials/navbar.html b/layouts/partials/navbar.html index 1dca9be1..a88f9b37 100644 --- a/layouts/partials/navbar.html +++ b/layouts/partials/navbar.html @@ -26,9 +26,16 @@