Skip to content

Commit 7d00c80

Browse files
committed
feat(search): add dynamic slash icon to search-container
1 parent 8b2d308 commit 7d00c80

4 files changed

Lines changed: 83 additions & 18 deletions

File tree

sass/parts/_search.scss

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
1+
/* Container for search elements */
2+
.search-container {
3+
position: relative;
4+
flex-grow: 1;
5+
max-width: 400px;
6+
}
7+
18
/* Style the magnifying glass SVG */
29
.search-icon {
310
position: absolute;
411
left: 10px;
12+
top: 50%;
13+
transform: translateY(-50%);
514
width: 24px;
615
height: 24px;
716
/* default gray */
817
stroke: var(--text-color, #757575);
918
/* Adjust icon color */
1019
pointer-events: none;
11-
/* Ensure the SVG doesn't block input interaction */
12-
padding: 2.5px 0px;
20+
}
21+
22+
/* Style the slash-square icon */
23+
.slash-icon {
24+
position: absolute;
25+
right: 10px;
26+
top: 50%;
27+
transform: translateY(-50%);
28+
width: 18px;
29+
height: 18px;
30+
opacity: 0.6;
31+
pointer-events: none;
32+
stroke: var(--text-color, #757575);
33+
}
34+
35+
.slash-icon.hidden {
36+
opacity: 0;
37+
visibility: hidden;
1338
}
1439

1540
/* Input field styling */

static/icons/slash-square.svg

Lines changed: 8 additions & 0 deletions
Loading

static/js/search.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ function initSearch() {
196196
var $searchInput = document.getElementById("search");
197197
var $searchResults = document.querySelector(".search-results");
198198
var $searchResultsItems = document.querySelector(".search-results__items");
199+
var $slashIcon = document.getElementById("slash-icon");
199200
var MAX_ITEMS = 10;
200201

201202
var options = {
@@ -250,6 +251,27 @@ function initSearch() {
250251
return index;
251252
};
252253

254+
// Function to toggle slash icon visibility
255+
function updateSlashIconVisibility() {
256+
if ($slashIcon) {
257+
// Hide if field is focused OR has text, show only when unfocused AND empty
258+
if (document.activeElement === $searchInput || $searchInput.value.trim() !== "") {
259+
$slashIcon.classList.add("hidden");
260+
} else {
261+
$slashIcon.classList.remove("hidden");
262+
}
263+
}
264+
}
265+
266+
// Update slash icon visibility on input
267+
$searchInput.addEventListener("input", updateSlashIconVisibility);
268+
269+
// Hide slash icon when search field is focused
270+
$searchInput.addEventListener("focus", updateSlashIconVisibility);
271+
272+
// Show slash icon when search field loses focus (if empty)
273+
$searchInput.addEventListener("blur", updateSlashIconVisibility);
274+
253275
$searchInput.addEventListener(
254276
"keyup",
255277
debounce(async function () {
@@ -293,6 +315,8 @@ function initSearch() {
293315
// clear search query to go back to placeholder
294316
$searchInput.value = "";
295317
$searchInput.blur();
318+
// Show slash icon again since input is now empty
319+
updateSlashIconVisibility();
296320
}
297321
});
298322

templates/macros/index_macros.html

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,31 @@
2323
</nav>
2424
<aside class="user-actions-container">
2525
{% if config.extra.enable_search %}
26-
<svg
27-
xmlns="http://www.w3.org/2000/svg"
28-
fill="none"
29-
viewBox="0 0 24 24"
30-
stroke-width="1.5"
31-
class="search-icon"
32-
>
33-
<path
34-
stroke-linecap="round"
35-
stroke-linejoin="round"
36-
d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z"
26+
<div class="search-container">
27+
<svg
28+
xmlns="http://www.w3.org/2000/svg"
29+
fill="none"
30+
viewBox="0 0 24 24"
31+
stroke-width="1.5"
32+
class="search-icon"
33+
>
34+
<path
35+
stroke-linecap="round"
36+
stroke-linejoin="round"
37+
d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z"
38+
/>
39+
</svg>
40+
<input type="text" id="search" placeholder="Search..." />
41+
<img
42+
src="{{ get_url(path='icons/slash-square.svg') }}"
43+
id="slash-icon"
44+
class="slash-icon"
45+
alt="Press / to search"
3746
/>
38-
</svg>
39-
<input type="text" id="search" placeholder="Search..." />
40-
<section class="search-results" aria-live="polite">
41-
<article class="search-results__items" role="list"></article>
42-
</section>
47+
<section class="search-results" aria-live="polite">
48+
<article class="search-results__items" role="list"></article>
49+
</section>
50+
</div>
4351
{% endif %} {% if config.extra.theme | default(value="toggle") ==
4452
"toggle" %}
4553
<a id="dark-mode-toggle" href="#">

0 commit comments

Comments
 (0)