feat: Initialize SvelteKit project, add tsconfig.json, and introduce a new Calculator.svelte component.
This commit is contained in:
197
hdyc-svelte/src/lib/components/Sidebar.svelte
Normal file
197
hdyc-svelte/src/lib/components/Sidebar.svelte
Normal file
@@ -0,0 +1,197 @@
|
||||
<script lang="ts">
|
||||
import { page } from '$app/stores';
|
||||
import { categories, getCalculatorsByCategory } from '$lib/data/calculators';
|
||||
|
||||
let expandedCategory = '';
|
||||
|
||||
$: currentPath = $page.url.pathname;
|
||||
|
||||
function toggle(cat: string) {
|
||||
expandedCategory = expandedCategory === cat ? '' : cat;
|
||||
}
|
||||
|
||||
export let open = false;
|
||||
</script>
|
||||
|
||||
<aside class="sidebar" class:open>
|
||||
<div class="sidebar-header">
|
||||
<h3>All Converters</h3>
|
||||
<button class="close-btn" on:click={() => (open = false)} aria-label="Close sidebar">✕</button>
|
||||
</div>
|
||||
<nav>
|
||||
{#each Object.entries(categories) as [key, meta]}
|
||||
{@const calcs = getCalculatorsByCategory(key)}
|
||||
<div class="cat-section">
|
||||
<button
|
||||
class="cat-toggle"
|
||||
class:active={expandedCategory === key || currentPath.includes(`/category/${key}`)}
|
||||
on:click={() => toggle(key)}
|
||||
>
|
||||
<span class="cat-icon">{meta.icon}</span>
|
||||
<span class="cat-label">{meta.label}</span>
|
||||
<span class="chevron" class:expanded={expandedCategory === key}>›</span>
|
||||
</button>
|
||||
{#if expandedCategory === key}
|
||||
<ul class="cat-list" >
|
||||
{#each calcs as calc}
|
||||
<li>
|
||||
<a
|
||||
href="/{calc.slug}"
|
||||
class:current={currentPath === `/${calc.slug}`}
|
||||
>
|
||||
{calc.name}
|
||||
</a>
|
||||
</li>
|
||||
{/each}
|
||||
<li>
|
||||
<a href="/category/{key}" class="view-all">View all {meta.label} →</a>
|
||||
</li>
|
||||
</ul>
|
||||
{/if}
|
||||
</div>
|
||||
{/each}
|
||||
</nav>
|
||||
</aside>
|
||||
|
||||
{#if open}
|
||||
<!-- svelte-ignore a11y_click_events_have_key_events -->
|
||||
<!-- svelte-ignore a11y_no_static_element_interactions -->
|
||||
<div class="overlay" on:click={() => (open = false)}></div>
|
||||
{/if}
|
||||
|
||||
<style>
|
||||
.sidebar {
|
||||
width: 280px;
|
||||
height: 100%;
|
||||
overflow-y: auto;
|
||||
background: var(--sidebar-bg);
|
||||
border-right: 1px solid var(--border);
|
||||
padding: 0;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
.sidebar-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 1.25rem 1rem;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
.sidebar-header h3 {
|
||||
margin: 0;
|
||||
font-size: 0.9rem;
|
||||
font-weight: 700;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
color: var(--accent);
|
||||
}
|
||||
.close-btn {
|
||||
display: none;
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--text-muted);
|
||||
font-size: 1.2rem;
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
}
|
||||
|
||||
nav {
|
||||
padding: 0.5rem 0;
|
||||
}
|
||||
|
||||
.cat-toggle {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
padding: 0.6rem 1rem;
|
||||
border: none;
|
||||
background: none;
|
||||
cursor: pointer;
|
||||
font-size: 0.88rem;
|
||||
color: var(--text);
|
||||
gap: 0.5rem;
|
||||
transition: background 0.15s;
|
||||
text-align: left;
|
||||
}
|
||||
.cat-toggle:hover {
|
||||
background: var(--hover-bg);
|
||||
}
|
||||
.cat-toggle.active {
|
||||
color: var(--accent);
|
||||
font-weight: 600;
|
||||
}
|
||||
.cat-icon {
|
||||
font-size: 1rem;
|
||||
flex-shrink: 0;
|
||||
width: 1.4rem;
|
||||
text-align: center;
|
||||
}
|
||||
.cat-label {
|
||||
flex: 1;
|
||||
}
|
||||
.chevron {
|
||||
font-size: 1.1rem;
|
||||
font-weight: 700;
|
||||
color: var(--text-muted);
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
.chevron.expanded {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
|
||||
.cat-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0 0 0.5rem;
|
||||
}
|
||||
.cat-list li a {
|
||||
display: block;
|
||||
padding: 0.35rem 1rem 0.35rem 2.8rem;
|
||||
font-size: 0.82rem;
|
||||
color: var(--text-muted);
|
||||
text-decoration: none;
|
||||
transition: color 0.15s, background 0.15s;
|
||||
border-radius: 0;
|
||||
}
|
||||
.cat-list li a:hover {
|
||||
color: var(--accent);
|
||||
background: var(--hover-bg);
|
||||
}
|
||||
.cat-list li a.current {
|
||||
color: var(--accent);
|
||||
font-weight: 600;
|
||||
background: var(--accent-glow);
|
||||
}
|
||||
.view-all {
|
||||
font-weight: 600 !important;
|
||||
color: var(--accent) !important;
|
||||
}
|
||||
|
||||
.overlay {
|
||||
display: none;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.sidebar {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: -300px;
|
||||
z-index: 100;
|
||||
height: 100vh;
|
||||
transition: left 0.3s ease;
|
||||
box-shadow: 4px 0 24px rgba(0, 0, 0, 0.2);
|
||||
}
|
||||
.sidebar.open {
|
||||
left: 0;
|
||||
}
|
||||
.close-btn {
|
||||
display: block;
|
||||
}
|
||||
.overlay {
|
||||
display: block;
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
z-index: 99;
|
||||
background: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user