-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: add tag filtering for projects
- Loading branch information
Showing
17 changed files
with
267 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
document.addEventListener('DOMContentLoaded', () => { | ||
const cards = document.querySelectorAll('.card'); | ||
const filterLinks = document.querySelectorAll('.filter-controls a'); | ||
const allProjectsFilter = document.querySelector('#all-projects-filter'); | ||
if (!cards.length || !filterLinks.length) return; | ||
allProjectsFilter.style.display = 'block'; | ||
|
||
// Create a Map for O(1) lookups of links by filter value. | ||
const linkMap = new Map( | ||
Array.from(filterLinks).map(link => [link.dataset.filter, link]) | ||
); | ||
|
||
// Pre-process cards data for faster filtering. | ||
const cardData = Array.from(cards).map(card => ({ | ||
element: card, | ||
tags: card.dataset.tags?.toLowerCase().split(',').filter(Boolean) ?? [] | ||
})); | ||
|
||
function getTagSlugFromUrl(url) { | ||
return url.split('/').filter(Boolean).pop(); | ||
} | ||
|
||
function getFilterFromHash() { | ||
if (!window.location.hash) return 'all'; | ||
const hash = decodeURIComponent(window.location.hash.slice(1)); | ||
const matchingLink = Array.from(filterLinks).find(link => | ||
getTagSlugFromUrl(link.getAttribute('href')) === hash | ||
); | ||
return matchingLink?.dataset.filter ?? 'all'; | ||
} | ||
|
||
function setActiveFilter(filterValue, updateHash = true) { | ||
if (updateHash) { | ||
if (filterValue === 'all') { | ||
history.pushState(null, '', window.location.pathname); | ||
} else { | ||
const activeLink = linkMap.get(filterValue); | ||
if (activeLink) { | ||
const tagSlug = getTagSlugFromUrl(activeLink.getAttribute('href')); | ||
history.pushState(null, '', `#${tagSlug}`); | ||
} | ||
} | ||
} | ||
const isAll = filterValue === 'all'; | ||
const display = isAll ? '' : 'none'; | ||
const ariaHidden = isAll ? 'false' : 'true'; | ||
requestAnimationFrame(() => { | ||
filterLinks.forEach(link => { | ||
const isActive = link.dataset.filter === filterValue; | ||
link.classList.toggle('active', isActive); | ||
link.setAttribute('aria-pressed', isActive); | ||
}); | ||
if (isAll) { | ||
cardData.forEach(({ element }) => { | ||
element.style.display = display; | ||
element.setAttribute('aria-hidden', ariaHidden); | ||
}); | ||
} else { | ||
cardData.forEach(({ element, tags }) => { | ||
const shouldShow = tags.includes(filterValue); | ||
element.style.display = shouldShow ? '' : 'none'; | ||
element.setAttribute('aria-hidden', !shouldShow); | ||
}); | ||
} | ||
}); | ||
} | ||
|
||
const filterContainer = filterLinks[0].parentElement.parentElement; | ||
filterContainer.addEventListener('click', e => { | ||
const link = e.target.closest('a'); | ||
if (!link) return; | ||
e.preventDefault(); | ||
const filterValue = link.dataset.filter; | ||
if (filterValue) setActiveFilter(filterValue); | ||
}); | ||
|
||
filterContainer.addEventListener('keydown', e => { | ||
const link = e.target.closest('a'); | ||
if (!link) return; | ||
if (e.key === ' ' || e.key === 'Enter') { | ||
e.preventDefault(); | ||
link.click(); | ||
} | ||
}); | ||
|
||
filterLinks.forEach(link => { | ||
link.setAttribute('role', 'button'); | ||
link.setAttribute('aria-pressed', link.classList.contains('active')); | ||
}); | ||
|
||
window.addEventListener('popstate', () => { | ||
setActiveFilter(getFilterFromHash(), false); | ||
}); | ||
|
||
const initialFilter = getFilterFromHash(); | ||
if (initialFilter !== 'all') { | ||
setActiveFilter(initialFilter, false); | ||
} | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{#- Collect all terms. -#} | ||
{#- We don't use `get_taxonomy` so users aren't forced to use 'tags' -#} | ||
{% set all_terms = [] %} | ||
{% for page in show_pages %} | ||
{% if page.taxonomies %} | ||
{% for tax_name, terms in page.taxonomies %} | ||
{% for term in terms %} | ||
{% set_global all_terms = all_terms | concat(with=term) %} | ||
{% endfor %} | ||
{% endfor %} | ||
{% endif %} | ||
{% endfor %} | ||
|
||
{#- Display unique terms -#} | ||
{%- if all_terms -%} | ||
<ul class="filter-controls" role="group" aria-label="{{ macros_translate::translate(key='project_filters', default='Project filters', language_strings=language_strings) }}"> | ||
<li class="taxonomy-item no-hover-padding"> | ||
<a id="all-projects-filter" class="no-hover-padding active" | ||
href="{{ get_url(path="projects", lang=lang) }}" | ||
data-filter="all"> | ||
{{- macros_translate::translate(key="all_projects", default="All projects", language_strings=language_strings) -}} | ||
</a> | ||
</li> | ||
{% for term in all_terms | unique | sort %} | ||
<li class="taxonomy-item no-hover-padding"> | ||
<a class="no-hover-padding" | ||
href="{{ get_taxonomy_url(kind="tags", name=term) }}" | ||
data-filter="{{ term | lower }}">{{ term }}</a> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
{% endif %} | ||
|
||
{#- Load the script -#} | ||
<script src="{{ get_url(path='js/filterCards.min.js', trailing_slash=false) | safe }}" async></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters