Skip to content

Commit eb0d0ea

Browse files
authoredMay 10, 2023
feat(theme-default): improve css variable acquisition (#1322)
1 parent 23bdec6 commit eb0d0ea

File tree

6 files changed

+69
-30
lines changed

6 files changed

+69
-30
lines changed
 

‎ecosystem/theme-default/src/client/components/Navbar.vue

+15-16
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ import NavbarBrand from '@theme/NavbarBrand.vue'
33
import NavbarItems from '@theme/NavbarItems.vue'
44
import ToggleColorModeButton from '@theme/ToggleColorModeButton.vue'
55
import ToggleSidebarButton from '@theme/ToggleSidebarButton.vue'
6-
import { computed, onMounted, ref } from 'vue'
7-
import { useThemeLocaleData } from '../composables/index.js'
6+
import { computed, ref } from 'vue'
7+
import {
8+
DeviceType,
9+
useThemeLocaleData,
10+
useUpdateDeviceStatus,
11+
} from '../composables/index.js'
812
913
defineEmits(['toggle-sidebar'])
1014
@@ -23,16 +27,14 @@ const linksWrapperStyle = computed(() => {
2327
}
2428
})
2529
26-
// avoid overlapping of long title and long navbar links
27-
onMounted(() => {
28-
// TODO: migrate to css var
29-
// refer to _variables.scss
30-
const MOBILE_DESKTOP_BREAKPOINT = 719
31-
const navbarHorizontalPadding =
32-
getCssValue(navbar.value, 'paddingLeft') +
33-
getCssValue(navbar.value, 'paddingRight')
34-
const handleLinksWrapWidth = (): void => {
35-
if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) {
30+
useUpdateDeviceStatus(
31+
DeviceType.MOBILE,
32+
(mobileDesktopBreakpoint: number): void => {
33+
// avoid overlapping of long title and long navbar links
34+
const navbarHorizontalPadding =
35+
getCssValue(navbar.value, 'paddingLeft') +
36+
getCssValue(navbar.value, 'paddingRight')
37+
if (window.innerWidth < mobileDesktopBreakpoint) {
3638
linksWrapperMaxWidth.value = 0
3739
} else {
3840
linksWrapperMaxWidth.value =
@@ -41,10 +43,7 @@ onMounted(() => {
4143
(navbarBrand.value?.offsetWidth || 0)
4244
}
4345
}
44-
handleLinksWrapWidth()
45-
window.addEventListener('resize', handleLinksWrapWidth, false)
46-
window.addEventListener('orientationchange', handleLinksWrapWidth, false)
47-
})
46+
)
4847
4948
function getCssValue(el: HTMLElement | null, property: string): number {
5049
// NOTE: Known bug, will return 'auto' if style value is 'auto'

‎ecosystem/theme-default/src/client/components/NavbarItems.vue

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import AutoLink from '@theme/AutoLink.vue'
33
import NavbarDropdown from '@theme/NavbarDropdown.vue'
44
import { useRouteLocale, useSiteLocaleData } from '@vuepress/client'
55
import { isLinkHttp, isString } from '@vuepress/shared'
6-
import { computed, onMounted, ref } from 'vue'
6+
import { computed, ref } from 'vue'
77
import type { ComputedRef } from 'vue'
88
import { useRouter } from 'vue-router'
99
import type {
1010
NavbarGroup,
1111
NavbarItem,
1212
ResolvedNavbarItem,
1313
} from '../../shared/index.js'
14-
import { useNavLink, useThemeLocaleData } from '../composables/index.js'
14+
import {
15+
DeviceType,
16+
useNavLink,
17+
useThemeLocaleData,
18+
useUpdateDeviceStatus,
19+
} from '../composables/index.js'
1520
import { resolveRepoType } from '../utils/index.js'
1621
1722
/**
@@ -152,23 +157,17 @@ const navbarLinks = computed(() => [
152157
...navbarRepo.value,
153158
])
154159
155-
// avoid overlapping of long title and long navbar links
156-
onMounted(() => {
157-
// TODO: migrate to css var
158-
// refer to _variables.scss
159-
const MOBILE_DESKTOP_BREAKPOINT = 719
160-
161-
const handleMobile = (): void => {
162-
if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) {
160+
useUpdateDeviceStatus(
161+
DeviceType.MOBILE,
162+
(mobileDesktopBreakpoint: number): void => {
163+
// avoid overlapping of long title and long navbar links
164+
if (window.innerWidth < mobileDesktopBreakpoint) {
163165
isMobile.value = true
164166
} else {
165167
isMobile.value = false
166168
}
167169
}
168-
handleMobile()
169-
window.addEventListener('resize', handleMobile, false)
170-
window.addEventListener('orientationchange', handleMobile, false)
171-
})
170+
)
172171
</script>
173172

174173
<template>

‎ecosystem/theme-default/src/client/composables/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ export * from './useResolveRouteWithRedirect.js'
44
export * from './useScrollPromise.js'
55
export * from './useSidebarItems.js'
66
export * from './useThemeData.js'
7+
export * from './useUpdateDeviceStatus.js'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { onMounted } from 'vue'
2+
import cssVars from '../styles/_variables.module.scss?module'
3+
4+
export enum DeviceType {
5+
MOBILE = 'mobile',
6+
}
7+
8+
const DeviceTypeMap = {
9+
[DeviceType.MOBILE]: Number.parseInt(cssVars.mobile?.replace('px', ''), 10),
10+
}
11+
12+
/**
13+
* add listener to detect screen though device type
14+
*/
15+
export const useUpdateDeviceStatus = (
16+
deviceType: DeviceType,
17+
callback: (width: number) => void
18+
): void => {
19+
const width = DeviceTypeMap[deviceType]
20+
if (!Number.isInteger(width)) {
21+
if (__VUEPRESS_DEV__) throw new Error('device width must be a integer')
22+
return
23+
}
24+
25+
onMounted(() => {
26+
callback(width)
27+
window.addEventListener('resize', () => callback(width), false)
28+
window.addEventListener('orientationchange', () => callback(width), false)
29+
})
30+
}

‎ecosystem/theme-default/src/client/shim.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,8 @@ declare module '*.vue' {
33
const comp: ComponentOptions
44
export default comp
55
}
6+
7+
declare module '*.module.scss?module' {
8+
const cssVars: Record<string, string>
9+
export default cssVars
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
@import '_variables';
2+
3+
:export {
4+
mobile: $MQMobile;
5+
}

0 commit comments

Comments
 (0)
Please sign in to comment.