diff --git a/ecosystem/theme-default/src/client/components/Navbar.vue b/ecosystem/theme-default/src/client/components/Navbar.vue index 8fbfa7f3ab..d08800d7ea 100644 --- a/ecosystem/theme-default/src/client/components/Navbar.vue +++ b/ecosystem/theme-default/src/client/components/Navbar.vue @@ -3,8 +3,12 @@ import NavbarBrand from '@theme/NavbarBrand.vue' import NavbarItems from '@theme/NavbarItems.vue' import ToggleColorModeButton from '@theme/ToggleColorModeButton.vue' import ToggleSidebarButton from '@theme/ToggleSidebarButton.vue' -import { computed, onMounted, ref } from 'vue' -import { useThemeLocaleData } from '../composables/index.js' +import { computed, ref } from 'vue' +import { + DeviceType, + useThemeLocaleData, + useUpdateDeviceStatus, +} from '../composables/index.js' defineEmits(['toggle-sidebar']) @@ -23,16 +27,14 @@ const linksWrapperStyle = computed(() => { } }) -// avoid overlapping of long title and long navbar links -onMounted(() => { - // TODO: migrate to css var - // refer to _variables.scss - const MOBILE_DESKTOP_BREAKPOINT = 719 - const navbarHorizontalPadding = - getCssValue(navbar.value, 'paddingLeft') + - getCssValue(navbar.value, 'paddingRight') - const handleLinksWrapWidth = (): void => { - if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) { +useUpdateDeviceStatus( + DeviceType.MOBILE, + (mobileDesktopBreakpoint: number): void => { + // avoid overlapping of long title and long navbar links + const navbarHorizontalPadding = + getCssValue(navbar.value, 'paddingLeft') + + getCssValue(navbar.value, 'paddingRight') + if (window.innerWidth < mobileDesktopBreakpoint) { linksWrapperMaxWidth.value = 0 } else { linksWrapperMaxWidth.value = @@ -41,10 +43,7 @@ onMounted(() => { (navbarBrand.value?.offsetWidth || 0) } } - handleLinksWrapWidth() - window.addEventListener('resize', handleLinksWrapWidth, false) - window.addEventListener('orientationchange', handleLinksWrapWidth, false) -}) +) function getCssValue(el: HTMLElement | null, property: string): number { // NOTE: Known bug, will return 'auto' if style value is 'auto' diff --git a/ecosystem/theme-default/src/client/components/NavbarItems.vue b/ecosystem/theme-default/src/client/components/NavbarItems.vue index 9d64ad3dc4..4d88c69199 100644 --- a/ecosystem/theme-default/src/client/components/NavbarItems.vue +++ b/ecosystem/theme-default/src/client/components/NavbarItems.vue @@ -3,7 +3,7 @@ import AutoLink from '@theme/AutoLink.vue' import NavbarDropdown from '@theme/NavbarDropdown.vue' import { useRouteLocale, useSiteLocaleData } from '@vuepress/client' import { isLinkHttp, isString } from '@vuepress/shared' -import { computed, onMounted, ref } from 'vue' +import { computed, ref } from 'vue' import type { ComputedRef } from 'vue' import { useRouter } from 'vue-router' import type { @@ -11,7 +11,12 @@ import type { NavbarItem, ResolvedNavbarItem, } from '../../shared/index.js' -import { useNavLink, useThemeLocaleData } from '../composables/index.js' +import { + DeviceType, + useNavLink, + useThemeLocaleData, + useUpdateDeviceStatus, +} from '../composables/index.js' import { resolveRepoType } from '../utils/index.js' /** @@ -152,23 +157,17 @@ const navbarLinks = computed(() => [ ...navbarRepo.value, ]) -// avoid overlapping of long title and long navbar links -onMounted(() => { - // TODO: migrate to css var - // refer to _variables.scss - const MOBILE_DESKTOP_BREAKPOINT = 719 - - const handleMobile = (): void => { - if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) { +useUpdateDeviceStatus( + DeviceType.MOBILE, + (mobileDesktopBreakpoint: number): void => { + // avoid overlapping of long title and long navbar links + if (window.innerWidth < mobileDesktopBreakpoint) { isMobile.value = true } else { isMobile.value = false } } - handleMobile() - window.addEventListener('resize', handleMobile, false) - window.addEventListener('orientationchange', handleMobile, false) -}) +)