Files
Modrinth-plus/src/components/NavRow.svelte

138 lines
2.8 KiB
Svelte
Raw Normal View History

<script lang="ts">
2022-06-21 19:08:22 -07:00
import { prerendering } from '$app/env'
2022-06-04 00:46:23 -07:00
import { page } from '$app/stores'
2022-06-21 17:25:07 -07:00
import { onMount } from 'svelte'
2022-06-04 00:46:23 -07:00
interface Link {
href: string
label: string
}
2022-06-04 00:46:23 -07:00
export let links: Link[]
2022-06-07 14:00:53 -07:00
/** Query param name to use (to not use queries, leave this prop blank) */
2022-06-04 00:46:23 -07:00
export let query = ''
2022-06-04 00:46:23 -07:00
export let resetScroll = false
2022-06-04 00:46:23 -07:00
/** Path level in URL, zero-indexed */
export let level = 0
2022-06-04 00:46:23 -07:00
let path: string[]
$: path = [
...$page.url.pathname
.substring(1)
.split('/')
.map((route) => '/' + route),
'/',
]
2022-06-04 00:46:23 -07:00
$: basePath = path.slice(0, level).join('')
2022-06-21 17:25:07 -07:00
$: activeIndex = query
2022-06-21 19:08:22 -07:00
? prerendering
? -1
: links.findIndex((link) => ($page.url.searchParams.get(query) || '') === link.href)
2022-06-21 17:25:07 -07:00
: links.findIndex((link) => path[level] === link.href || path[level] === link.href.slice(0, -1))
const linkElements: HTMLAnchorElement[] = []
let indicatorReady = false
onMount(() => {
setTimeout(() => {
indicatorReady = true
}, 300)
})
</script>
2022-06-21 17:25:07 -07:00
<nav class="navigation" class:static-indicator={!indicatorReady}>
{#each links as link, index}
2022-06-04 00:46:23 -07:00
<a
href={query
? link.href
? `?${query}=${link.href}`
: '?'
: level === 0
? link.href
: basePath + link.href}
on:click={() => {
if (resetScroll) document.body.scrollTo(0, 0)
}}
class="navigation__link"
2022-06-21 19:08:22 -07:00
class:is-active={index === activeIndex}
2022-06-21 17:25:07 -07:00
sveltekit:noscroll={!resetScroll || null}
bind:this={linkElements[index]}>
{link.label}
</a>
2022-06-04 00:46:23 -07:00
{/each}
2022-06-21 17:25:07 -07:00
<div
class="navigation__indicator"
style:left={linkElements
.slice(0, activeIndex)
.map((element, index) => element.offsetWidth + 16)
.reduce((acc, a) => acc + a, 0) + 'px'}
style:width={linkElements[activeIndex]?.offsetWidth + 'px'} />
</nav>
<style lang="postcss">
2022-06-04 00:46:23 -07:00
.navigation {
display: flex;
flex-direction: row;
align-items: center;
2022-06-07 14:00:53 -07:00
gap: 1rem;
2022-06-04 00:46:23 -07:00
flex-wrap: wrap;
2022-06-21 17:25:07 -07:00
position: relative;
2022-06-04 00:46:23 -07:00
&__link {
font-weight: var(--font-weight-bold);
color: var(--color-text-light);
2022-06-21 17:25:07 -07:00
transition: color 0.3s ease-in-out;
position: relative;
2022-06-04 00:46:23 -07:00
&.is-active {
color: var(--color-text);
2022-06-21 17:25:07 -07:00
}
&::after {
content: '';
display: block;
position: absolute;
bottom: -2px;
width: 100%;
border-radius: var(--rounded-max);
height: 0.25rem;
transition: background-color 0.2s ease-in-out;
}
&:hover::after {
background-color: var(--color-brand-light);
}
}
&.static-indicator {
.navigation__link {
&.is-active::after {
2022-06-04 00:46:23 -07:00
background-color: var(--color-brand);
}
}
}
2022-06-21 17:25:07 -07:00
&__indicator {
position: absolute;
bottom: -2px;
width: 1rem;
height: 0.25rem;
border-radius: var(--rounded-max);
background-color: var(--color-brand);
transition: width 0.3s ease-out, left 0.3s ease-out;
visibility: hidden;
}
&:not(.static-indicator) {
.navigation__indicator {
visibility: visible;
}
}
2022-06-04 00:46:23 -07:00
}
</style>