Change Switcher labels and close it when clicking outside

This commit is contained in:
2021-11-16 23:47:24 +01:00
parent a9d42dcc0d
commit 8a53079fa1
2 changed files with 27 additions and 12 deletions

View File

@@ -1,7 +1,13 @@
<script lang="ts">
import { createEventDispatcher } from 'svelte'
// Components
import Icon from '$components/atoms/Icon.svelte'
let isOpen: boolean = false
let switcherEl: HTMLElement
let isOpen = false
const dispatch = createEventDispatcher()
// Links
const links = [
@@ -9,19 +15,19 @@
icon: "globe",
iconLabel: "Globe icon",
url: "/locations",
text: "Discover locations"
text: "Explore Locations"
},
{
icon: "photos",
iconLabel: "Photos icon",
url: "/photos",
text: "Browse all photos"
text: "Photo Grid"
},
{
icon: "bag",
iconLabel: "Bag icon",
url: "/shop",
text: "Shop the prints"
text: "Shop Posters"
}
]
@@ -32,9 +38,22 @@
const toggleSwitcher = () => {
isOpen = !isOpen
}
/**
* Detect outside click
*/
const windowClick = ({ target }) => {
if (!switcherEl.contains(target)) {
// Close switcher
isOpen = false
}
}
</script>
<aside class="switcher" class:is-open={isOpen}>
<svelte:window on:click={windowClick} />
<aside class="switcher" class:is-open={isOpen} bind:this={switcherEl}>
<nav class="switcher__links">
<ul>
{#each links as { icon, iconLabel, url, text }}
@@ -52,9 +71,9 @@
on:click={toggleSwitcher}
>
<span>
{#each Array(3) as _}
<i />
<i />
<i />
{/each}
</span>
</button>
</aside>

View File

@@ -74,7 +74,6 @@
span {
display: inline-flex;
flex-flow: column;
transition: transform 0.8s var(--ease-quart);
}
// Dot
i {
@@ -113,9 +112,6 @@
}
&__button {
span {
transform: rotate3d(70deg, 120deg, 180deg) translateZ(0);
}
i {
&:nth-child(1) { transform: translate3d(-7px, 8px, 0); }
&:nth-child(2) { transform: translate3d(7px, -2px, 0); }