Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(theme-default): optimizing css variable acquisition #1322

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 12 additions & 18 deletions ecosystem/theme-default/src/client/components/Navbar.vue
Expand Up @@ -3,8 +3,9 @@ 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 { computed, ref } from 'vue'
import { useThemeLocaleData } from '../composables/index.js'
import { DeviceType, updateDeviceStatus } from '../utils/index.js'

defineEmits(['toggle-sidebar'])

Expand All @@ -24,27 +25,20 @@ 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 handleLinksWrapWidth = (mobileDesktopBreakpoint: number): void => {
const navbarHorizontalPadding =
getCssValue(navbar.value, 'paddingLeft') +
getCssValue(navbar.value, 'paddingRight')
const handleLinksWrapWidth = (): void => {
if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) {
linksWrapperMaxWidth.value = 0
} else {
linksWrapperMaxWidth.value =
navbar.value!.offsetWidth -
navbarHorizontalPadding -
(navbarBrand.value?.offsetWidth || 0)
}
if (window.innerWidth < mobileDesktopBreakpoint) {
linksWrapperMaxWidth.value = 0
} else {
linksWrapperMaxWidth.value =
navbar.value!.offsetWidth -
navbarHorizontalPadding -
(navbarBrand.value?.offsetWidth || 0)
}
handleLinksWrapWidth()
window.addEventListener('resize', handleLinksWrapWidth, false)
window.addEventListener('orientationchange', handleLinksWrapWidth, false)
})
}
updateDeviceStatus(DeviceType.MOBILE, handleLinksWrapWidth)
zhaopan-pan marked this conversation as resolved.
Show resolved Hide resolved

function getCssValue(el: HTMLElement | null, property: string): number {
// NOTE: Known bug, will return 'auto' if style value is 'auto'
Expand Down
30 changes: 13 additions & 17 deletions ecosystem/theme-default/src/client/components/NavbarItems.vue
Expand Up @@ -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 {
Expand All @@ -12,7 +12,11 @@ import type {
ResolvedNavbarItem,
} from '../../shared/index.js'
import { useNavLink, useThemeLocaleData } from '../composables/index.js'
import { resolveRepoType } from '../utils/index.js'
import {
DeviceType,
resolveRepoType,
updateDeviceStatus,
} from '../utils/index.js'

/**
* Get navbar config of select language dropdown
Expand Down Expand Up @@ -153,22 +157,14 @@ const navbarLinks = 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 handleMobile = (): void => {
if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) {
isMobile.value = true
} else {
isMobile.value = false
}
const handleMobile = (mobileDesktopBreakpoint: number): void => {
if (window.innerWidth < mobileDesktopBreakpoint) {
isMobile.value = true
} else {
isMobile.value = false
}
handleMobile()
window.addEventListener('resize', handleMobile, false)
window.addEventListener('orientationchange', handleMobile, false)
})
}
updateDeviceStatus(DeviceType.MOBILE, handleMobile)
</script>

<template>
Expand Down
8 changes: 8 additions & 0 deletions ecosystem/theme-default/src/client/shim.d.ts
Expand Up @@ -3,3 +3,11 @@ declare module '*.vue' {
const comp: ComponentOptions
export default comp
}

declare module '*.module.scss?module' {
type Variables = {
[className: string]: string
}
const cssVar: Variables
export default cssVar
zhaopan-pan marked this conversation as resolved.
Show resolved Hide resolved
}
@@ -0,0 +1,5 @@
@import '_variables';

:export {
mobile: $MQMobile;
}
1 change: 1 addition & 0 deletions ecosystem/theme-default/src/client/utils/index.ts
@@ -1,3 +1,4 @@
export * from './isActiveSidebarItem.js'
export * from './resolveEditLink.js'
export * from './resolveRepoType.js'
export * from './updateDeviceStatus.js'
27 changes: 27 additions & 0 deletions ecosystem/theme-default/src/client/utils/updateDeviceStatus.ts
@@ -0,0 +1,27 @@
import { onMounted } from 'vue'
import cssVars from '../styles/_variables.module.scss?module'

export enum DeviceType {
MOBILE = 'mobile',
}

const DeviceTypeMap = {
[DeviceType.MOBILE]: Number(cssVars.mobile?.replace('px', '')),
zhaopan-pan marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* add listener to detect screen though device type
*/
export const updateDeviceStatus = (
zhaopan-pan marked this conversation as resolved.
Show resolved Hide resolved
deviceType: DeviceType,
callback: (width: number) => void
): void => {
const width = DeviceTypeMap[deviceType]
if (!width) return
zhaopan-pan marked this conversation as resolved.
Show resolved Hide resolved

onMounted(() => {
callback(width)
window.addEventListener('resize', () => callback(width), false)
window.addEventListener('orientationchange', () => callback(width), false)
})
}