209 lines
5.0 KiB
Vue
209 lines
5.0 KiB
Vue
<template>
|
|
<div
|
|
ref="headerRef"
|
|
class="app-header px-4 pb-2 top-0 wrap-fix background-color-transition"
|
|
:class="{ 'app-header--scrolled': isHeaderScrolled }"
|
|
>
|
|
<div class="flex justify-between my-2 items-center">
|
|
<div class="flex items-center gap-2" @click.stop="router.push({ name: 'user' })">
|
|
<div class="z-100000 rounded-full border-1 text-primary border-primary">
|
|
<Icon icon="vuesax-linear:user" size="20" />
|
|
</div>
|
|
<div class="text-primary color-transition font-bold">
|
|
{{ userStore.userInfo?.name }}
|
|
</div>
|
|
</div>
|
|
|
|
<button
|
|
type="button"
|
|
class="mhome-header-btn mhome-header-btn--logout"
|
|
@click="$emit('logout')"
|
|
>
|
|
<Icon icon="vuesax-linear:logout" size="18" />
|
|
<span>خروج</span>
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import { nextTick, onActivated, onMounted, onUnmounted, ref, watch } from 'vue'
|
|
import { useRoute } from 'vue-router'
|
|
import { useUserStore } from '@/store/userStore'
|
|
import router from '@/router'
|
|
import Icon from '@/components/BasicPacks/BasicIcon.vue'
|
|
|
|
const headerRef = ref<HTMLElement | null>(null)
|
|
const isHeaderScrolled = ref(false)
|
|
const route = useRoute()
|
|
|
|
const userStore = useUserStore()
|
|
|
|
let intersectionObserver: IntersectionObserver | null = null
|
|
let scrollTarget: HTMLElement | Window | null = null
|
|
|
|
function isScrollableOverflow(value: string) {
|
|
return value === 'auto' || value === 'scroll' || value === 'overlay'
|
|
}
|
|
|
|
function findScrollParent(el: HTMLElement | null): HTMLElement | null {
|
|
let node = el?.parentElement ?? null
|
|
|
|
while (node) {
|
|
const style = getComputedStyle(node)
|
|
const overflowY = style.overflowY
|
|
const overflow = style.overflow
|
|
|
|
if (isScrollableOverflow(overflowY) || isScrollableOverflow(overflow)) {
|
|
return node
|
|
}
|
|
|
|
node = node.parentElement
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
function resolveScrollRoot(): HTMLElement | null {
|
|
const header = headerRef.value
|
|
if (!header) return null
|
|
|
|
const chromeInner = header.closest('.main-with-bottom-chrome-inner')
|
|
if (chromeInner instanceof HTMLElement) {
|
|
return chromeInner
|
|
}
|
|
|
|
const fromParent = findScrollParent(header)
|
|
if (fromParent) {
|
|
return fromParent
|
|
}
|
|
|
|
const global = document.querySelector('.main-with-bottom-chrome-inner')
|
|
return global instanceof HTMLElement ? global : null
|
|
}
|
|
|
|
function resolveScrollSentinel(scrollRoot: HTMLElement | null): HTMLElement | null {
|
|
if (!scrollRoot) return null
|
|
|
|
const local = scrollRoot.querySelector('.app-header-scroll-sentinel')
|
|
if (local instanceof HTMLElement) {
|
|
return local
|
|
}
|
|
|
|
return null
|
|
}
|
|
|
|
function updateScrollStateFromPosition() {
|
|
if (!scrollTarget) return
|
|
|
|
const next =
|
|
scrollTarget instanceof Window
|
|
? scrollTarget.scrollY > 50
|
|
: scrollTarget.scrollTop > 50
|
|
|
|
isHeaderScrolled.value = next
|
|
}
|
|
|
|
function teardownScrollTracking() {
|
|
intersectionObserver?.disconnect()
|
|
intersectionObserver = null
|
|
|
|
scrollTarget?.removeEventListener('scroll', updateScrollStateFromPosition)
|
|
scrollTarget = null
|
|
}
|
|
|
|
function setupScrollTracking() {
|
|
teardownScrollTracking()
|
|
|
|
const scrollRoot = resolveScrollRoot()
|
|
const sentinel = resolveScrollSentinel(scrollRoot)
|
|
|
|
if (scrollRoot && sentinel && typeof IntersectionObserver !== 'undefined') {
|
|
intersectionObserver = new IntersectionObserver(
|
|
([entry]) => {
|
|
isHeaderScrolled.value = !entry?.isIntersecting
|
|
},
|
|
{
|
|
root: scrollRoot,
|
|
threshold: 0,
|
|
rootMargin: '-1px 0px 0px 0px',
|
|
},
|
|
)
|
|
intersectionObserver.observe(sentinel)
|
|
return
|
|
}
|
|
|
|
scrollTarget = scrollRoot ?? window
|
|
scrollTarget.addEventListener('scroll', updateScrollStateFromPosition, { passive: true })
|
|
updateScrollStateFromPosition()
|
|
}
|
|
|
|
onMounted(async () => {
|
|
setupScrollTracking()
|
|
await nextTick()
|
|
setupScrollTracking()
|
|
requestAnimationFrame(() => setupScrollTracking())
|
|
})
|
|
|
|
onActivated(() => {
|
|
isHeaderScrolled.value = false
|
|
setupScrollTracking()
|
|
})
|
|
|
|
watch(
|
|
() => route.fullPath,
|
|
() => {
|
|
if (route.name === 'home') {
|
|
isHeaderScrolled.value = false
|
|
nextTick(() => setupScrollTracking())
|
|
}
|
|
},
|
|
)
|
|
|
|
onUnmounted(() => {
|
|
teardownScrollTracking()
|
|
})
|
|
|
|
defineEmits<{
|
|
logout: []
|
|
}>()
|
|
</script>
|
|
|
|
<style scoped>
|
|
.app-header {
|
|
z-index: 1999;
|
|
box-sizing: border-box;
|
|
padding-top: calc(0.5rem + var(--app-safe-top, env(safe-area-inset-top, 0px)));
|
|
background-color: transparent;
|
|
}
|
|
|
|
.app-header--scrolled {
|
|
background-color: rgba(255, 255, 255, 0.92);
|
|
box-shadow:
|
|
0 1px 3px 0 rgb(0 0 0 / 0.1),
|
|
0 1px 2px -1px rgb(0 0 0 / 0.1);
|
|
}
|
|
|
|
.mhome-header-btn {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
gap: 4px;
|
|
padding: 6px 10px;
|
|
border: 1px solid #e5e7eb;
|
|
border-radius: 8px;
|
|
background: #fff;
|
|
color: #0a1b37;
|
|
font-size: 12px;
|
|
font-weight: 600;
|
|
font-family: inherit;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.mhome-header-btn--logout {
|
|
color: #dc2626;
|
|
border-color: #fecaca;
|
|
background: #fef2f2;
|
|
}
|
|
</style>
|