Skip to content

Commit 240d5da

Browse files
Diya-odjagdish-15github-actions[bot]
authored
Added dynamic clear button to leaderboard search (#88)
* add dynamic clear button to leaderboard search * change according to suggestion * Update clear button text to `ESC` and update visibility logic * Refactor clear search button style * style: auto-format code with Prettier (/format) --------- Co-authored-by: Jagdish Prajapati <jagadishdrp@gmail.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 7931c99 commit 240d5da

2 files changed

Lines changed: 53 additions & 3 deletions

File tree

frontend/leaderboard.html

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,17 @@ <h1 class="page-title">Leaderboard</h1>
8181
autocomplete="off"
8282
spellcheck="false"
8383
/>
84+
8485
<span class="search-result-count" id="search-result-count"></span>
8586
<kbd class="search-shortcut" id="search-shortcut">Ctrl+K</kbd>
87+
<button
88+
type="button"
89+
id="clear-search"
90+
class="clear-search-btn"
91+
aria-label="Clear search"
92+
>
93+
ESC
94+
</button>
8695
</div>
8796
</div>
8897

@@ -134,19 +143,33 @@ <h1 class="page-title">Leaderboard</h1>
134143

135144
const searchInput = document.getElementById("leaderboard-search");
136145
const shortcutBadge = document.getElementById("search-shortcut");
146+
const clearBtn = document.getElementById("clear-search");
137147

138148
searchInput.addEventListener("input", (e) => {
139149
currentSearchTerm = e.target.value.toLowerCase().trim();
150+
151+
clearBtn.style.display =
152+
e.target.value.trim() !== "" ? "flex" : "none";
153+
154+
applyFiltersAndRender();
155+
});
156+
clearBtn.addEventListener("click", () => {
157+
searchInput.value = "";
158+
currentSearchTerm = "";
159+
160+
clearBtn.style.display = "none";
161+
162+
searchInput.focus();
140163
applyFiltersAndRender();
141164
});
142165

143166
searchInput.addEventListener("focus", () => {
144-
shortcutBadge.style.opacity = "0";
167+
shortcutBadge.style.display = "none";
145168
});
146169

147170
searchInput.addEventListener("blur", () => {
148171
if (!searchInput.value) {
149-
shortcutBadge.style.opacity = "1";
172+
shortcutBadge.style.display = "inline-flex";
150173
}
151174
});
152175

@@ -159,6 +182,7 @@ <h1 class="page-title">Leaderboard</h1>
159182
if (e.key === "Escape" && document.activeElement === searchInput) {
160183
searchInput.value = "";
161184
currentSearchTerm = "";
185+
clearBtn.style.display = "none";
162186
searchInput.blur();
163187
applyFiltersAndRender();
164188
}
@@ -186,7 +210,7 @@ <h1 class="page-title">Leaderboard</h1>
186210
window.scrollTo({ top: 0, behavior: "smooth" });
187211
}
188212
});
189-
213+
clearBtn.style.display = "none";
190214
fetchLeaderboardData();
191215
});
192216

frontend/styles/main.css

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,32 @@ body::-webkit-scrollbar-thumb {
23352335
transition: opacity 0.15s ease;
23362336
user-select: none;
23372337
}
2338+
.clear-search-btn {
2339+
background: rgba(255, 51, 51, 0.05);
2340+
border: 1px solid rgba(255, 51, 51, 0.4);
2341+
color: var(--red);
2342+
padding: 2px 6px;
2343+
border-radius: 3px;
2344+
cursor: pointer;
2345+
display: none;
2346+
align-items: center;
2347+
justify-content: center;
2348+
font-family: "Fira Code", monospace;
2349+
font-size: 0.65rem;
2350+
font-weight: bold;
2351+
transition: all 0.15s ease;
2352+
}
2353+
2354+
.clear-search-btn:hover {
2355+
background: rgba(255, 51, 51, 0.15);
2356+
border-color: var(--red);
2357+
box-shadow: 0 0 8px rgba(255, 51, 51, 0.2);
2358+
text-shadow: 0 0 5px rgba(255, 51, 51, 0.8);
2359+
}
2360+
2361+
.clear-search-btn:active {
2362+
transform: scale(0.95);
2363+
}
23382364

23392365
.no-results {
23402366
text-align: center;

0 commit comments

Comments
 (0)