Files
JayuCloud/JC-Salon-Frontend/src/composables/useAdminViewport.ts
T

81 lines
3.0 KiB
TypeScript

import { computed, onBeforeUnmount, onMounted, ref } from 'vue'
/**
* Purpose: ADMIN_MOBILE_BREAKPOINT supports useAdminViewport.ts by holding or executing the admin mobile breakpoint page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const ADMIN_MOBILE_BREAKPOINT = 960
/**
* Purpose: viewportWidth supports useAdminViewport.ts by holding or executing the viewport width page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const viewportWidth = ref(
typeof window !== 'undefined' ? window.innerWidth : ADMIN_MOBILE_BREAKPOINT + 1
)
/**
* Purpose: listenerCount supports useAdminViewport.ts by holding or executing the listener count page or API role.
* Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow.
*/
let listenerCount = 0
/**
* Purpose: updateViewportWidth supports useAdminViewport.ts by holding or executing the update viewport width page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const updateViewportWidth = () => {
if (typeof window === 'undefined') {
return
}
viewportWidth.value = window.innerWidth
}
/**
* Purpose: bindViewportListener supports useAdminViewport.ts by holding or executing the bind viewport listener page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const bindViewportListener = () => {
if (typeof window === 'undefined') {
return
}
if (listenerCount === 0) {
window.addEventListener('resize', updateViewportWidth, { passive: true })
}
listenerCount += 1
}
/**
* Purpose: unbindViewportListener supports useAdminViewport.ts by holding or executing the unbind viewport listener page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
const unbindViewportListener = () => {
if (typeof window === 'undefined') {
return
}
listenerCount = Math.max(0, listenerCount - 1)
if (listenerCount === 0) {
window.removeEventListener('resize', updateViewportWidth)
}
}
/**
* Purpose: useAdminViewport supports useAdminViewport.ts by holding or executing the use admin viewport page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const useAdminViewport = () => {
onMounted(() => {
updateViewportWidth()
bindViewportListener()
})
onBeforeUnmount(() => {
unbindViewportListener()
})
return {
viewportWidth: computed(() => viewportWidth.value),
isMobile: computed(() => viewportWidth.value <= ADMIN_MOBILE_BREAKPOINT)
}
}