D.A BLOG PROJECTS NOW DATA NOTES

Implementation of a Filter System

How I set up a filtering system for my blog

Date: 5 Sept 2023 · 3 min read

The idea

At the moment, there are very few posts, but in future (perhaps) the number will grow and it will be difficult to find a specific post. So I’ve decided to implement a filtering system. The idea is quite simple, as I’m doing it in JavaScript and NOT using an API. Anyway, let’s get started.

The result

The end result I wanted to achieve was a simple dialogue box where you could select a tag from the posts and then display the posts with that tag. So, I started thinking about how to do this without changing the NieR Automata UI too much.

Sistema di Filtri

Let’s move on to the code.

The code

CSS

<style>
	.filter-title {
		display: flex;
		justify-content: space-between;
		align-items: center;
		gap: 0.5em;
		padding: 0.5em;
		cursor: pointer;
	}
	p {
		display: flex;
		align-items: center;
		gap: 0.2em;
		padding: 0.19em;
		cursor: pointer;
	}

	.filters {
		display: flex;
		flex-direction: column;
		gap: 1rem;
		background-color: var(--automataBg);
		padding: 1em;
		border-top: 2px solid var(--automataBlackOpacity);
		border-bottom: 2px solid var(--automataBlackOpacity);
		max-height: 300px;
		height: 0;
		min-width: 300px;
		overflow: scroll;
		opacity: 0;
		transition: all 0.3s ease-in-out;
	}

	.filter-item {
		color: var(--automataColor);
		text-transform: uppercase;
		position: relative;
		z-index: 2;
	}

	.filter-item::before {
		content: '';
		background-color: var(--automataColor);
		width: 100%;
		position: absolute;
		left: 0;
		height: 100%;
		transform: scaleX(0);
		transform-origin: left;
		transition: transform 0.3s ease-in-out;
		z-index: -1;
		cursor: pointer;
	}

	.filter-item:hover::before {
		transform: scaleX(1);
	}

	.filter-item:hover {
		color: var(--automataBg) !important;
		transition: 0.2s ease-in-out;
	}

	.backdrop {
		position: absolute;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		z-index: -1;
		background-color: var(--automataColor);
		opacity: 0;
	}

	::-webkit-scrollbar-corner {
		background-color: var(--automataBg);
	}

	::-webkit-scrollbar {
		width: 0.5em;
	}

	.clear {
		background-color: var(--automataRedOpacity);
		font-size: 1.2em;
		padding: 0.5em;
		color: var(--automataColor);
		text-align: center;
		cursor: pointer;
		justify-content: center;
	}

	.clear:hover {
		background-color: var(--automataRed);
		transition: 0.2s ease-in-out;
	}
</style>

Svelte

<section>
	<div class="filter-title" on:click={() => handleClick()} id="1">
		<h3>FILTRI</h3>
		{#if isShowing}
			<ChevronsDownUp cursor="pointer" />
		{:else}
			<ChevronsUpDown cursor="pointer" />
		{/if}
	</div>
	<div bind:this={filters} class="filters">
		{#each categories as category, i}
			<div on:click={() => setActive(i)}>
				<p class="filter-item" id="-{i}">
					<span class="backdrop" /><Cpu />{category}
				</p>
			</div>
		{/each}
	</div>
	{#if activeFilters.length > 0}
		<p on:click={() => clearFilters()} transition:fly class="clear">CLEAR</p>
	{/if}
</section>

The main functions are handled within the `filter-title` div, the script for which is shown below.

Then, for each category in the list, the string is added to an array called `activeFilters`. This array is used to filter the posts by passing it via an `eventDispatcher` to the component that manages the blog content.

TypeScript


I could have handled the CSS by referencing the `HTMLElement`, but I was feeling a bit lazy and didn’t want to do that. So, I used the `style` property to set the CSS selectors.

There isn’t much else to say; the code pretty much speaks for itself. You can copy and use this code in your own projects, but I’d appreciate it if you could give me some credit 🙏.

Conclusion

I’m quite happy with the result – it’s simple and it works. I’m not sure if I’ll update it in the future, but I probably will. I hope you enjoyed this post, and see you in the next one!

Author

Daniele lovaion Avolio

Developer, video game lover 🎮 and fan of Japanese pop culture 🗾