AppHeader.vue 5.25 KB
<template>
  <header class="home-header" :class="{ 'rtl': currentLang === 'ar' }">
    <img
      src="@/assets/common/logo.png"
      class="logo"
      :alt="$t('nav.home')"
      @click="goTab(0, tabs[0])"
      :class="{ 'logo-rtl': currentLang === 'ar' }"
    />
    <nav class="nav">
      <a
        v-for="(tab, idx) in tabs"
        :key="tab.path"
        class="nav-link"
        :class="{ active: store.index === idx }"
        @click="goTab(idx, tab)"
        >{{ $t(tab.name) }}</a
      >
    </nav>
    <div class="header-actions">
      <!-- <button class="topup-btn">Top-Up</button> -->
      <div class="lang-select">
        <div class="lang-btn" @click="toggleLangDropdown">
          <span class="lang-btn-label">{{
            langList.find((l) => l.value === currentLang)?.label
          }}</span>
        </div>
        <transition name="fade">
          <div v-if="showLang" class="lang-dropdown">
            <div
              v-for="lang in langList"
              :key="lang.value"
              class="lang-item"
              :class="{ active: currentLang === lang.value }"
              @click="selectLang(lang.value)"
            >
              {{ lang.label }}
            </div>
          </div>
        </transition>
      </div>
    </div>
  </header>
</template>

<script setup>
import { useCommonStore } from "@/store/commonStore";
import { useRoute, useRouter } from "vue-router";
import { ref, watch, onMounted } from "vue";
import { useI18n } from 'vue-i18n';

const { t, locale } = useI18n();
const store = useCommonStore();
const route = useRoute();
const router = useRouter();

const tabs = [
  { name: "nav.home", path: "/" },
  { name: "nav.about", path: "/about" },
  { name: "nav.leadership", path: "/leadership" },
  { name: "nav.contact", path: "/contact" },
];

function goTab(idx, tab) {
  store.setIndex(idx);
  router.push(tab.path);
}

function updateIndexByRoute() {
  const idx = tabs.findIndex((tab) => tab.path === route.path);
  store.setIndex(idx === -1 ? 0 : idx);
}

watch(() => route.path, updateIndexByRoute, { immediate: true });
onMounted(updateIndexByRoute);

const langList = [
  { label: t('nav.english'), value: "en" },
  { label: t('nav.arabic'), value: "ar" },
];
const showLang = ref(false);
const currentLang = ref(locale.value);

function toggleLangDropdown() {
  showLang.value = !showLang.value;
}
function selectLang(val) {
  locale.value = val;
  currentLang.value = val;
  showLang.value = false;
  document.dir = val === 'ar' ? 'rtl' : 'ltr';
  localStorage.setItem('language', val);
}

watch(locale, (val) => {
  currentLang.value = val;
});
</script>

<style scoped>
.home-header {
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  /* width: 100vw; */
  height: 98px;
  flex-shrink: 0;
  padding-left: 500px;
  padding-right: 98px;
  background: #000;
  z-index: 10;
}
.logo {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 78px;
  cursor: pointer;
  transition: left 0.3s, right 0.3s;
}
/* RTL 适配 */
.home-header.rtl {
  padding-left: 98px;
  padding-right: 500px;
}
.logo-rtl {
  left: auto !important;
  right: 0;
}
.nav {
  display: flex;
  gap: 120px;
}
.nav-link {
  color: #fff;
  font-size: 20px;
  font-weight: 500;
  text-decoration: none;
  padding-bottom: 4px;
  border-bottom: 2px solid transparent;
  transition: border-color 0.2s;
  cursor: pointer;
}
.nav-link.active {
  color: #fff;
  border-bottom: 4px solid #a259ff;
}
.header-actions {
  display: flex;
  align-items: center;
  gap: 16px;
  transition: margin 0.3s;
}
.rtl .header-actions {
  margin-right: auto;
  margin-left: 0;
}
.topup-btn {
  background: #a259ff;
  color: #fff;
  border: none;
  border-radius: 20px;
  padding: 8px 24px;
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  margin-right: 8px;
}
.lang-select {
  position: relative;
}
.lang-btn {
  width: 142px;
  height: 58px;
  /* padding: 16px 0 15px 17px; */
  padding-left: 17px;
  padding-top: 13px;
  background-image: url("@/assets/common/language.png");
  background-repeat: no-repeat;
  background-size: cover;
  cursor: pointer;
  text-align: left;
}
.rtl .lang-btn {
  padding-left: 17px;
  padding-right: 17px;
}
.lang-btn-label {
  z-index: 1;
  font-family: Microsoft YaHei, Microsoft YaHei;
  font-weight: 600;
  font-size: 20px;
  color: #ffffff;
  text-align: center;
  font-style: normal;
  text-transform: none;
  background: linear-gradient(90deg, #c22fff 0%, #8b17ff 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-fill-color: transparent;
}
.lang-dropdown {
  position: absolute;
  top: 56px;
  right: 0;
  min-width: 160px;
  background: #e9eef7;
  border-radius: 16px;
  box-shadow: 0 8px 32px 0 rgba(24, 31, 43, 0.18);
  padding: 18px 0 10px 0;
  display: flex;
  flex-direction: column;
  z-index: 100;
  border: 1px solid #e0e0e0;
}
.rtl .lang-dropdown {
  right: auto;
  left: 0;
}
.lang-item {
  font-size: 22px;
  color: #181f2b;
  font-weight: 500;
  padding: 12px 32px;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.2s;
  text-align: left;
}
.lang-item.active,
.lang-item:hover {
  background: #f3eaff;
  color: #a259ff;
}
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.2s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>