Skip to content

Commit c63bd4a

Browse files
authoredOct 13, 2022
fix(theme-default): improve responsive layout (#1102)
1 parent 31dde34 commit c63bd4a

File tree

5 files changed

+57
-31
lines changed

5 files changed

+57
-31
lines changed
 

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ onMounted(() => {
3232
getCssValue(navbar.value, 'paddingLeft') +
3333
getCssValue(navbar.value, 'paddingRight')
3434
const handleLinksWrapWidth = (): void => {
35-
if (window.innerWidth <= MOBILE_DESKTOP_BREAKPOINT) {
35+
if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) {
3636
linksWrapperMaxWidth.value = 0
3737
} else {
3838
linksWrapperMaxWidth.value =

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

+25-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ 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 } from 'vue'
6+
import { computed, onMounted, ref } from 'vue'
77
import type { ComputedRef } from 'vue'
88
import { useRouter } from 'vue-router'
99
import type {
@@ -143,6 +143,7 @@ const useNavbarConfig = (): ComputedRef<ResolvedNavbarItem[]> => {
143143
return computed(() => (themeLocale.value.navbar || []).map(resolveNavbarItem))
144144
}
145145
146+
const isMobile = ref(false)
146147
const navbarConfig = useNavbarConfig()
147148
const navbarSelectLanguage = useNavbarSelectLanguage()
148149
const navbarRepo = useNavbarRepo()
@@ -151,12 +152,34 @@ const navbarLinks = computed(() => [
151152
...navbarSelectLanguage.value,
152153
...navbarRepo.value,
153154
])
155+
156+
// avoid overlapping of long title and long navbar links
157+
onMounted(() => {
158+
// TODO: migrate to css var
159+
// refer to _variables.scss
160+
const MOBILE_DESKTOP_BREAKPOINT = 719
161+
162+
const handleMobile = (): void => {
163+
if (window.innerWidth < MOBILE_DESKTOP_BREAKPOINT) {
164+
isMobile.value = true
165+
} else {
166+
isMobile.value = false
167+
}
168+
}
169+
handleMobile()
170+
window.addEventListener('resize', handleMobile, false)
171+
window.addEventListener('orientationchange', handleMobile, false)
172+
})
154173
</script>
155174

156175
<template>
157176
<nav v-if="navbarLinks.length" class="navbar-items">
158177
<div v-for="item in navbarLinks" :key="item.text" class="navbar-item">
159-
<NavbarDropdown v-if="item.children" :item="item" />
178+
<NavbarDropdown
179+
v-if="item.children"
180+
:item="item"
181+
:class="isMobile ? 'mobile' : ''"
182+
/>
160183
<AutoLink v-else :item="item" />
161184
</div>
162185
</nav>

‎ecosystem/theme-default/src/client/styles/layout.scss

+8-7
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,17 @@
107107
}
108108
}
109109
}
110+
.theme-container.no-sidebar {
111+
.sidebar {
112+
display: none;
110113

111-
@media (min-width: ($MQMobile + 1px)) {
112-
.theme-container.no-sidebar {
113-
.sidebar {
114-
display: none;
114+
@media (max-width: $MQMobile) {
115+
display: block;
115116
}
117+
}
116118

117-
.page {
118-
padding-left: 0;
119-
}
119+
.page {
120+
padding-left: 0;
120121
}
121122
}
122123

‎ecosystem/theme-default/src/client/styles/navbar-dropdown.scss

+2-6
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,8 @@
108108
}
109109
}
110110
}
111-
}
112111

113-
@media (max-width: $MQMobile) {
114-
.navbar-dropdown-wrapper {
112+
&.mobile {
115113
&.open .navbar-dropdown-title {
116114
margin-bottom: 0.5rem;
117115
}
@@ -148,10 +146,8 @@
148146
}
149147
}
150148
}
151-
}
152149

153-
@media (min-width: ($MQMobile + 1)) {
154-
.navbar-dropdown-wrapper {
150+
&:not(.mobile) {
155151
height: 1.8rem;
156152

157153
&:hover .navbar-dropdown,

‎ecosystem/theme-default/src/client/styles/navbar.scss

+21-15
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
&:hover,
7171
&.router-link-active {
72-
color: var(--c-text-accent);
72+
color: var(--c-text);
7373
}
7474
}
7575

@@ -82,30 +82,36 @@
8282
&:first-child {
8383
margin-left: 0;
8484
}
85+
86+
> a {
87+
&:hover,
88+
&.router-link-active {
89+
margin-bottom: -2px;
90+
border-bottom: 2px solid var(--c-text-accent);
91+
}
92+
}
8593
}
8694
}
8795

8896
@media (max-width: $MQMobile) {
8997
.navbar-items {
9098
.navbar-item {
9199
margin-left: 0;
92-
}
93-
}
94-
}
95100

96-
@media (min-width: $MQMobile) {
97-
.navbar-items a {
98-
&:hover,
99-
&.router-link-active {
100-
color: var(--c-text);
101+
> a {
102+
&:hover,
103+
&.router-link-active {
104+
margin-bottom: 0;
105+
border-bottom: none;
106+
}
107+
}
101108
}
102-
}
103109

104-
.navbar-item > a {
105-
&:hover,
106-
&.router-link-active {
107-
margin-bottom: -2px;
108-
border-bottom: 2px solid var(--c-text-accent);
110+
a {
111+
&:hover,
112+
&.router-link-active {
113+
color: var(--c-text-accent);
114+
}
109115
}
110116
}
111117
}

0 commit comments

Comments
 (0)
Please sign in to comment.