mirror of
https://github.com/danbulant/design
synced 2026-05-27 05:41:50 +00:00
add tag filters
This commit is contained in:
parent
b8651f4e55
commit
55fd9c183c
1 changed files with 59 additions and 6 deletions
|
|
@ -1,5 +1,5 @@
|
||||||
<script context="module">
|
<script context="module">
|
||||||
/** @type {import('./[slug]').Load} */
|
/** @type {import('./index').Load} */
|
||||||
export async function load({ params, fetch, session, stuff }) {
|
export async function load({ params, fetch, session, stuff }) {
|
||||||
const response = await fetch("/api/posts.json");
|
const response = await fetch("/api/posts.json");
|
||||||
|
|
||||||
|
|
@ -15,9 +15,26 @@
|
||||||
import HeroPost from "$lib/components/heroPost.svelte";
|
import HeroPost from "$lib/components/heroPost.svelte";
|
||||||
import Post from "$lib/components/post.svelte";
|
import Post from "$lib/components/post.svelte";
|
||||||
import darkmode from "$lib/stores/darkmode";
|
import darkmode from "$lib/stores/darkmode";
|
||||||
|
import { flip } from 'svelte/animate';
|
||||||
|
|
||||||
var currentHover = null;
|
var currentHover = null;
|
||||||
export var posts;
|
export var posts;
|
||||||
|
|
||||||
|
var tags = posts && posts.map(t => t.categories).flat().filter((t, i, a) => a.indexOf(t) == i).sort();
|
||||||
|
|
||||||
|
var selectedTags = [];
|
||||||
|
|
||||||
|
function toggle(tag) {
|
||||||
|
if(selectedTags.includes(tag)) {
|
||||||
|
selectedTags = selectedTags.filter(t => t != tag);
|
||||||
|
} else {
|
||||||
|
selectedTags.push(tag);
|
||||||
|
selectedTags = selectedTags;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var filteredPosts = selectedTags ? posts.slice(1).filter(post => !selectedTags.length || selectedTags.find(tag => post.categories.includes(tag))) : posts.slice(1);
|
||||||
|
$: filteredPosts = selectedTags ? posts.slice(1).filter(post => !selectedTags.length || selectedTags.find(tag => post.categories.includes(tag))) : posts.slice(1);
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
|
|
@ -36,14 +53,29 @@
|
||||||
<div class="posts" class:dark={$darkmode}>
|
<div class="posts" class:dark={$darkmode}>
|
||||||
<h1>Posts</h1>
|
<h1>Posts</h1>
|
||||||
|
|
||||||
<hr>
|
{#if tags}
|
||||||
|
<div class="tags">
|
||||||
<HeroPost {...posts[0]} />
|
{#each tags as tag}
|
||||||
|
<div class="tag" class:selected={selectedTags.includes(tag)} on:click={() => toggle(tag)}>
|
||||||
|
{tag}
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
|
|
||||||
{#each posts.slice(1) as post (post.title)}
|
{#if !selectedTags.length || selectedTags.find(tag => posts[0].categories.includes(tag))}
|
||||||
<Post {...post} bind:currentHover />
|
<HeroPost {...posts[0]} />
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
{/if}
|
||||||
|
|
||||||
|
|
||||||
|
{#each filteredPosts as post (post.title)}
|
||||||
|
<div animate:flip={{ duration: 250 }}>
|
||||||
|
<Post {...post} bind:currentHover />
|
||||||
|
</div>
|
||||||
{/each}
|
{/each}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
@ -53,6 +85,27 @@
|
||||||
max-width: 700px;
|
max-width: 700px;
|
||||||
padding: 0 20px;
|
padding: 0 20px;
|
||||||
}
|
}
|
||||||
|
.tags {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.5rem;
|
||||||
|
}
|
||||||
|
.dark .tag {
|
||||||
|
background: rgb(77, 77, 77);
|
||||||
|
}
|
||||||
|
.tag {
|
||||||
|
background:rgb(173, 173, 173);
|
||||||
|
padding: 5px 10px;
|
||||||
|
border-radius: 99px;
|
||||||
|
cursor: pointer;
|
||||||
|
user-select: none;
|
||||||
|
}
|
||||||
|
.dark .tag.selected {
|
||||||
|
background: rgb(0, 108, 170);
|
||||||
|
}
|
||||||
|
.tag.selected {
|
||||||
|
background: rgb(0, 162, 255);
|
||||||
|
}
|
||||||
h1 {
|
h1 {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
padding: 0;
|
padding: 0;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue