功能:更新消息中心和 YumiNail 模块
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
|
||||
|
||||
export const ADMIN_MOBILE_BREAKPOINT = 960
|
||||
|
||||
const viewportWidth = ref(
|
||||
typeof window !== 'undefined' ? window.innerWidth : ADMIN_MOBILE_BREAKPOINT + 1
|
||||
)
|
||||
|
||||
let listenerCount = 0
|
||||
|
||||
const updateViewportWidth = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
viewportWidth.value = window.innerWidth
|
||||
}
|
||||
|
||||
const bindViewportListener = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
if (listenerCount === 0) {
|
||||
window.addEventListener('resize', updateViewportWidth, { passive: true })
|
||||
}
|
||||
listenerCount += 1
|
||||
}
|
||||
|
||||
const unbindViewportListener = () => {
|
||||
if (typeof window === 'undefined') {
|
||||
return
|
||||
}
|
||||
listenerCount = Math.max(0, listenerCount - 1)
|
||||
if (listenerCount === 0) {
|
||||
window.removeEventListener('resize', updateViewportWidth)
|
||||
}
|
||||
}
|
||||
|
||||
export const useAdminViewport = () => {
|
||||
onMounted(() => {
|
||||
updateViewportWidth()
|
||||
bindViewportListener()
|
||||
})
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
unbindViewportListener()
|
||||
})
|
||||
|
||||
return {
|
||||
viewportWidth: computed(() => viewportWidth.value),
|
||||
isMobile: computed(() => viewportWidth.value <= ADMIN_MOBILE_BREAKPOINT)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user