Add NavRow animation

This commit is contained in:
venashial
2022-06-22 23:17:00 -07:00
parent 1109fbfcc3
commit 8494ed8ac3
3 changed files with 58 additions and 12 deletions

View File

@@ -57,7 +57,7 @@
position: absolute; position: absolute;
top: 0; top: 0;
right: 0; right: 0;
padding: 0.5rem; padding: 1rem;
display: flex; display: flex;
justify-content: flex-end; justify-content: flex-end;

View File

@@ -24,6 +24,18 @@
href: 'modpacks', href: 'modpacks',
label: 'Modpacks', label: 'Modpacks',
}, },
{
href: 'textures',
label: 'Textures',
},
{
href: 'shaders',
label: 'Shaders',
},
{
href: 'maps',
label: 'Maps',
},
]} /> ]} />
</div> </div>
``` ```

View File

@@ -1,5 +1,5 @@
<script lang="ts"> <script lang="ts">
import { prerendering } from '$app/env' import { browser, prerendering } from '$app/env'
import { page } from '$app/stores' import { page } from '$app/stores'
import { onMount } from 'svelte' import { onMount } from 'svelte'
@@ -28,16 +28,42 @@
$: basePath = path.slice(0, level).join('') $: basePath = path.slice(0, level).join('')
/* Animation */
const FAST_TIMING = 'cubic-bezier(1,0,.3,1) -140ms'
const SLOW_TIMING = 'cubic-bezier(.75,-0.01,.24,.99) -40ms'
$: activeIndex = query $: activeIndex = query
? prerendering ? prerendering
? -1 ? -1
: links.findIndex((link) => ($page.url.searchParams.get(query) || '') === link.href) : links.findIndex((link) => ($page.url.searchParams.get(query) || '') === link.href)
: links.findIndex((link) => path[level] === link.href || path[level] === link.href.slice(0, -1)) : links.findIndex((link) => path[level] === link.href || path[level] === link.href.slice(0, -1))
let oldIndex = -1
const linkElements: HTMLAnchorElement[] = [] const linkElements: HTMLAnchorElement[] = []
let indicatorReady = false let indicatorReady = false
const indicator = {
left: 0,
right: 0,
direction: 'right',
}
$: if (activeIndex > -1 && browser && linkElements.length > 0) startAnimation()
function startAnimation() {
indicator.direction = activeIndex < oldIndex ? 'left' : 'right'
indicator.left = linkElements[activeIndex].offsetLeft
indicator.right =
linkElements[activeIndex].parentElement.offsetWidth -
linkElements[activeIndex].offsetLeft -
linkElements[activeIndex].offsetWidth
oldIndex = activeIndex
}
onMount(() => { onMount(() => {
setTimeout(() => { setTimeout(() => {
indicatorReady = true indicatorReady = true
@@ -68,11 +94,11 @@
<div <div
class="navigation__indicator" class="navigation__indicator"
style:visibility={indicatorReady && activeIndex !== -1 ? 'visible' : 'hidden'} style:visibility={indicatorReady && activeIndex !== -1 ? 'visible' : 'hidden'}
style:left={linkElements style:left={indicator.left + 'px'}
.slice(0, activeIndex) style:right={indicator.right + 'px'}
.map((element, index) => element.offsetWidth + 16) style:transition={`left 350ms ${
.reduce((acc, a) => acc + a, 0) + 'px'} indicator.direction === 'left' ? FAST_TIMING : SLOW_TIMING
style:width={linkElements[activeIndex]?.offsetWidth + 'px'} /> },right 350ms ${indicator.direction === 'right' ? FAST_TIMING : SLOW_TIMING}`} />
</nav> </nav>
<style lang="postcss"> <style lang="postcss">
@@ -101,20 +127,28 @@
width: 100%; width: 100%;
border-radius: var(--rounded-max); border-radius: var(--rounded-max);
height: 0.25rem; height: 0.25rem;
transition: opacity 0.2s ease-in-out; transition: opacity 0.1s ease-in-out;
background-color: var(--color-brand); background-color: var(--color-brand);
opacity: 0; opacity: 0;
} }
&:hover {
color: var(--color-text);
}
&:hover::after { &:hover::after {
opacity: 0.5; opacity: 0.4;
}
&:active::after {
opacity: 0.1;
} }
} }
&.static-indicator { &.static-indicator {
.navigation__link { .navigation__link {
&.is-active::after { &.is-active::after {
background-color: var(--color-brand); opacity: 1;
} }
} }
} }
@@ -122,11 +156,11 @@
&__indicator { &__indicator {
position: absolute; position: absolute;
bottom: -2px; bottom: -2px;
width: 1rem;
height: 0.25rem; height: 0.25rem;
border-radius: var(--rounded-max); border-radius: var(--rounded-max);
background-color: var(--color-brand); background-color: var(--color-brand);
transition: width 0.3s ease-out, left 0.3s ease-out; transition-property: left, right;
transition-duration: 350ms;
visibility: hidden; visibility: hidden;
} }
} }