Skip to content

Commit 3e1da57

Browse files
authored
fix: navbar's messy layout at narrow screen (#714)
1 parent bb48a77 commit 3e1da57

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

lib/default-theme/NavLinks.vue

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ export default {
130130
display inline-block
131131
margin-left 1.5rem
132132
line-height 2rem
133+
&:first-child
134+
margin-left 0
133135
.repo-link
134136
margin-left 1.5rem
135137

lib/default-theme/Navbar.vue

+52-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,19 @@
1313
:alt="$siteTitle"
1414
>
1515
<span
16+
ref="siteName"
1617
class="site-name"
1718
v-if="$siteTitle"
1819
:class="{ 'can-hide': $site.themeConfig.logo }"
1920
>{{ $siteTitle }}</span>
2021
</router-link>
2122

22-
<div class="links">
23+
<div
24+
class="links"
25+
:style="{
26+
'max-width': linksWrapMaxWidth + 'px'
27+
}"
28+
>
2329
<AlgoliaSearchBox
2430
v-if="isAlgoliaSearch"
2531
:options="algolia"
@@ -39,6 +45,26 @@ import NavLinks from './NavLinks.vue'
3945
export default {
4046
components: { SidebarButton, NavLinks, SearchBox, AlgoliaSearchBox },
4147
48+
data () {
49+
return {
50+
linksWrapMaxWidth: null
51+
}
52+
},
53+
54+
mounted () {
55+
const MOBILE_DESKTOP_BREAKPOINT = 719 // refer to config.styl
56+
const NAVBAR_VERTICAL_PADDING = parseInt(css(this.$el, 'paddingLeft')) + parseInt(css(this.$el, 'paddingRight'))
57+
const handleLinksWrapWidth = () => {
58+
if (document.documentElement.clientWidth < MOBILE_DESKTOP_BREAKPOINT) {
59+
this.linksWrapMaxWidth = null
60+
} else {
61+
this.linksWrapMaxWidth = this.$el.offsetWidth - NAVBAR_VERTICAL_PADDING - this.$refs.siteName.offsetWidth
62+
}
63+
}
64+
handleLinksWrapWidth()
65+
window.addEventListener('resize', handleLinksWrapWidth, false)
66+
},
67+
4268
computed: {
4369
algolia () {
4470
return this.$themeLocaleConfig.algolia || this.$site.themeConfig.algolia || {}
@@ -49,13 +75,23 @@ export default {
4975
}
5076
}
5177
}
78+
79+
function css (el, property) {
80+
// NOTE: Known bug, will return 'auto' if style value is 'auto'
81+
const win = el.ownerDocument.defaultView
82+
// null means not to return pseudo styles
83+
return win.getComputedStyle(el, null)[property]
84+
}
5285
</script>
5386

5487
<style lang="stylus">
5588
@import './styles/config.styl'
5689
90+
$navbar-vertical-padding = 0.7rem
91+
$navbar-horizontal-padding = 1.5rem
92+
5793
.navbar
58-
padding 0.7rem 1.5rem
94+
padding $navbar-vertical-padding $navbar-horizontal-padding
5995
line-height $navbarHeight - 1.4rem
6096
position relative
6197
a, span, img
@@ -71,14 +107,26 @@ export default {
71107
color $textColor
72108
position relative
73109
.links
110+
padding-left 1.5rem
111+
box-sizing border-box
112+
background-color white
113+
white-space nowrap
74114
font-size 0.9rem
75115
position absolute
76-
right 1.5rem
77-
top 0.7rem
116+
right $navbar-horizontal-padding
117+
top $navbar-vertical-padding
118+
display flex
119+
.search-box
120+
flex: 0 0 auto
121+
vertical-align top
122+
.nav-links
123+
flex 1
78124
79125
@media (max-width: $MQMobile)
80126
.navbar
81127
padding-left 4rem
82128
.can-hide
83129
display none
130+
.links
131+
padding-left 1.5rem
84132
</style>

lib/default-theme/SearchBox.vue

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
@input="query = $event.target.value"
55
aria-label="Search"
66
:value="query"
7+
:class="{ 'focused': focused }"
78
autocomplete="off"
89
spellcheck="false"
910
@focus="focused = true"
@@ -156,7 +157,7 @@ export default {
156157
.search-box
157158
display inline-block
158159
position relative
159-
margin-right 0.5rem
160+
margin-right 1rem
160161
input
161162
cursor text
162163
width 10rem
@@ -209,7 +210,6 @@ export default {
209210
width 0
210211
border-color transparent
211212
position relative
212-
left 1rem
213213
&:focus
214214
cursor text
215215
left 0
@@ -223,6 +223,8 @@ export default {
223223
@media (max-width: $MQMobile)
224224
.search-box
225225
margin-right 0
226+
input
227+
left 1rem
226228
.suggestions
227229
right 0
228230

0 commit comments

Comments
 (0)