<template>
  <header class="header">
    <div class="logo" />
    <a :href="titleTo" class="appname" v-if="titleTo">{{ title }}</a>
    <div class="appname" v-else>{{ title }}</div>
    <nav class="tabs floating show-lg">
      <router-link
        class="tab"
        active-class="selected"
        v-for="item in tabs"
        :key="item.to"
        :to="item.to"
      >
        {{ item.name }}
      </router-link>
    </nav>
    <div class="floating show-sm burger" @click="showMenu = true" v-if="tabs.length > 0">
      <i class="material-icons">menu</i>
    </div>
  </header>
  <transition name="fade">
    <div class="modal" v-show="showMenu" @click="closeModal">
      <div class="sidenav" ref="sidenav" :class="{ in: showMenu }">
        <nav>
          <router-link
            class="sidenav-elt"
            active-class="selected"
            v-for="item in tabs"
            :key="item.to"
            :to="item.to"
            @click="showMenu = false"
          >
            {{ item.name }}
          </router-link>
        </nav>
      </div>
    </div>
  </transition>
</template>

<script lang="ts">
import { defineComponent, PropType } from "vue";
import "@/assets/css/main.css";
import "@/assets/css/tabs.css";

export type HeaderLink = {
  to: string;
  name: string;
};
export default defineComponent({
  props: {
    tabs: { type: Array as PropType<Array<HeaderLink>>, default: () => [] },
    title: String,
    titleTo: String,
  },
  data() {
    return { showMenu: false };
  },
  methods: {
    closeModal(e: MouseEvent) {
      if (!(this.$refs.sidenav as HTMLElement).contains(e.target as HTMLElement)) {
        this.showMenu = false;
      }
    },
  },
});
</script>

<style>
.header {
  --bg-color: var(--color-neutral-100);
  --color: var(--color-accent-600);
  top: 0;
  left: auto;
  right: 0;
  width: 100%;
  display: -webkit-box;
  display: flex;
  padding: 0;
  padding-right: 0px;
  z-index: 1200;
  position: -webkit-sticky;
  position: sticky;
  background-color: var(--bg-color);
  color: var(--color);
  box-shadow: 0 1px 4px 0 rgba(0, 0, 0, 0.2);
  box-sizing: border-box;
  min-height: 3.5rem;
  overflow-x: hidden;
  -webkit-box-align: center;
  align-items: center;
  flex-shrink: 0;
  font-family: Roboto, Helvetica Neue, Helvetica, Arial, sans-serif;
  padding-right: 1rem;
}
.header > :not(:first-child) {
  margin-right: 0.5rem;
}
.appname {
  -webkit-box-flex: 1;
  flex: 1;
  color: inherit;
  font-size: 1rem;
  text-align: left;
  border-left: 1px solid var(--color);
  font-weight: 400;
  line-height: 1.5rem;
  white-space: nowrap;
  padding-left: 1rem;
  padding-right: 1rem;
  text-decoration: none;
}
@media (max-width: 600px) {
  .show-lg {
    display: none !important;
  }
}
@media (min-width: 600px) {
  .show-sm {
    display: none !important;
  }
}
.fade-leave-active,
.fade-enter-active {
  transition: opacity 0.1s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
.burger {
  cursor: pointer;
}
.sidenav {
  position: absolute;
  top: 3.5rem;
  left: 0;
  bottom: 0;
  background: #ffffff;
  display: block;
  width: 80%;
  overflow-y: auto;
}
.fade-enter-active .sidenav,
.fade-leave-active .sidenav {
  transition: left 0.2s;
  left: 0;
}
.fade-enter-active .sidenav {
  transition-delay: 0.1s;
}
.fade-enter-from .sidenav,
.fade-leave-to .sidenav {
  left: -80%;
}
.sidenav-elt {
  display: block;
  padding: 16px;
  color: var(--color-accent-400);
  text-decoration: none;
  position: relative;
}
.sidenav-elt.selected {
  background: var(--color-accent-800);
  color: var(--color-accent-200);
}
.sidenav-elt.selected:before {
  content: "";
  position: absolute;
  left: 0px;
  top: 2px;
  bottom: 2px;
  width: 4px;
  background-color: var(--color-accent-400);
  border-radius: 0 4px 4px 0;
}
</style>
