clean up js (#801)

* clean up javascript
* set element id instead of class on the `clear-search` button
pull/800/head^2
augusto 2 years ago committed by GitHub
parent 70a706492b
commit e552f2ee71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -7,7 +7,7 @@ description: 'The fast-loading recipe site with cooking only and no ads.'
<div class="search js-only"> <div class="search js-only">
<input type="text" id="search" placeholder="Search..."> <input type="text" id="search" placeholder="Search...">
<button class="clear-search"> <button id="clear-search">
<svg xmlns="http://www.w3.org/2000/svg" class="ionicon" viewBox="0 0 512 512"><title>Backspace</title><path d="M135.19 390.14a28.79 28.79 0 0021.68 9.86h246.26A29 29 0 00432 371.13V140.87A29 29 0 00403.13 112H156.87a28.84 28.84 0 00-21.67 9.84v0L46.33 256l88.86 134.11z" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="32"></path><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32" d="M336.67 192.33L206.66 322.34M336.67 322.34L206.66 192.33M336.67 192.33L206.66 322.34M336.67 322.34L206.66 192.33"></path></svg> <svg xmlns="http://www.w3.org/2000/svg" class="ionicon" viewBox="0 0 512 512"><title>Backspace</title><path d="M135.19 390.14a28.79 28.79 0 0021.68 9.86h246.26A29 29 0 00432 371.13V140.87A29 29 0 00403.13 112H156.87a28.84 28.84 0 00-21.67 9.84v0L46.33 256l88.86 134.11z" fill="none" stroke="currentColor" stroke-linejoin="round" stroke-width="32"></path><path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="32" d="M336.67 192.33L206.66 322.34M336.67 322.34L206.66 192.33M336.67 192.33L206.66 322.34M336.67 322.34L206.66 192.33"></path></svg>
</button> </button>
</div> </div>
@ -15,45 +15,41 @@ description: 'The fast-loading recipe site with cooking only and no ads.'
<script> <script>
// @license magnet:?xt=urn:btih:5ac446d35272cc2e4e85e4325b146d0b7ca8f50c&dn=unlicense.txt Unlicense // @license magnet:?xt=urn:btih:5ac446d35272cc2e4e85e4325b146d0b7ca8f50c&dn=unlicense.txt Unlicense
document.addEventListener('DOMContentLoaded', () => { document.addEventListener("DOMContentLoaded", () => {
for (e of document.getElementsByClassName("js-only")) { for (e of document.getElementsByClassName("js-only")) {
e.classList.remove("js-only") e.classList.remove("js-only");
} }
const rec = document.querySelectorAll('#artlist li')
const search = document.querySelector('#search')
const clearSearch = document.querySelector('.clear-search')
const artlist = document.getElementById('artlist')
search.addEventListener('input', e => { const recipes = document.querySelectorAll("#artlist li");
// grab search input value const search = document.getElementById("search");
const searchText = e.target.value.toLowerCase() const clearSearch = document.getElementById("clear-search");
const artlist = document.getElementById("artlist");
search.addEventListener("input", () => {
// grab search input value
const searchText = search.value.toLowerCase();
const hasFilter = searchText.length > 0; const hasFilter = searchText.length > 0;
artlist.classList.toggle("list-searched", hasFilter);
// for each recipe hide all but matched // for each recipe hide all but matched
let matchCount = 0; recipes.forEach(recipe => {
rec.forEach(el => { const recipeName = recipe.textContent.toLowerCase();
const recipeName = el.textContent.toLowerCase() const isMatch = recipeName.includes(searchText);
const isMatch = recipeName.includes(searchText)
el.hidden = !isMatch
el.classList.toggle('matched-recipe', isMatch && searchText.length !== 0);
if (hasFilter && isMatch) {
matchCount++;
}
})
artlist.classList.toggle('list-searched', matchCount > 0); recipe.hidden = !isMatch;
recipe.classList.toggle("matched-recipe", hasFilter && isMatch);
})
}) })
clearSearch.addEventListener('click', e => { clearSearch.addEventListener("click", () => {
search.value = '' search.value = "";
rec.forEach(el => { recipes.forEach(recipe => {
el.hidden = false recipe.hidden = false;
el.classList.remove('matched-recipe'); recipe.classList.remove("matched-recipe");
}) })
artlist.classList.remove('list-searched') ; artlist.classList.remove("list-searched");
}) })
}) })
// @license-end // @license-end

@ -137,7 +137,7 @@ input#search {
display: flex; display: flex;
} }
button.clear-search { button#clear-search {
all: unset; all: unset;
position: absolute; position: absolute;
right: 4px; right: 4px;
@ -148,7 +148,7 @@ button.clear-search {
cursor: pointer; cursor: pointer;
transition: color 180ms ease-in-out; transition: color 180ms ease-in-out;
} }
button.clear-search:hover { button#clear-search:hover {
color: #eee; color: #eee;
} }

Loading…
Cancel
Save