-
Notifications
You must be signed in to change notification settings - Fork 1
/
Navbar.vue
165 lines (146 loc) · 3.89 KB
/
Navbar.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
<header class="navbar">
<SidebarButton @toggle-sidebar="$emit('toggle-sidebar')" />
<router-link :to="$localePath" class="home-link">
<img class="logo" v-if="$site.themeConfig.logo" :src="$withBase($site.themeConfig.logo)" :alt="$siteTitle" />
<span
ref="siteName"
class="site-name"
v-if="$page.title || $siteTitle"
:class="{ 'can-hide': $site.themeConfig.logo }"
>{{ $page.title || $siteTitle }}</span
>
</router-link>
<!-- begin added -->
<img
v-if="$frontmatter.sidebarImage"
class="img-profile img-profile-navbar"
:class="imgProfileClasses"
:src="$withBase($frontmatter.sidebarImage)"
:alt="$frontmatter.sidebarImageAlt"
/>
<!-- end added -->
<div
class="links"
:style="
linksWrapMaxWidth
? {
'max-width': linksWrapMaxWidth + 'px',
}
: {}
"
>
<AlgoliaSearchBox v-if="isAlgoliaSearch" :options="algolia" />
<SearchBox v-else-if="$site.themeConfig.search !== false && $page.frontmatter.search !== false" />
<NavLinks class="can-hide" />
</div>
</header>
</template>
<script>
import ParentNavbar from "@parent-theme/components/Navbar.vue"
import throttle from "lodash/throttle"
import extend from "lodash/extend"
const Navbar = extend({}, ParentNavbar, {
data() {
return extend(ParentNavbar.data(), {
hasScrolledDown: false,
})
},
computed: extend({}, ParentNavbar.computed, {
imgProfileClasses() {
// if it has been scrolled down, add an additional class
return this.hasScrolledDown ? "img-profile-navbar-small" : null
},
}),
mounted() {
if (ParentNavbar.mounted) {
ParentNavbar.mounted.call(this)
}
// console.log("register handleScroll")
this.handleThrottledScroll = throttle(this.handleScroll, 250)
window.addEventListener("scroll", this.handleThrottledScroll)
},
beforeDestroy() {
if (ParentNavbar.beforeDestroy) {
ParentNavbar.beforeDestroy.call(this)
}
// console.log("remove handleScroll")
window.removeEventListener("scroll", this.handleThrottledScroll)
},
methods: extend({}, ParentNavbar.methods, {
handleScroll(event) {
// Any code to be executed when the window is scrolled
// console.log("calling handleScroll", window.scrollY)
this.hasScrolledDown = window.scrollY > 20
},
}),
})
export default Navbar
</script>
<style lang="stylus">
$navbar-vertical-padding = 0.7rem;
$navbar-horizontal-padding = 1.5rem;
.navbar {
padding: $navbar-vertical-padding $navbar-horizontal-padding;
line-height: $navbarHeight - 1.4rem;
a, span, img {
display: inline-block;
}
.logo {
margin-right: 0.8rem;
min-width: $navbarHeight - 1.4rem;
height: $navbarHeight - 1.4rem;
vertical-align: top;
}
.site-name {
position: relative;
color: $textColor;
font-weight: 600;
font-size: 1.3rem;
}
.links {
position: absolute;
top: $navbar-vertical-padding;
right: $navbar-horizontal-padding;
display: flex;
box-sizing: border-box;
padding-left: 1.5rem;
background-color: white;
white-space: nowrap;
font-size: 0.9rem;
.search-box {
flex: 0 0 auto;
vertical-align: top;
}
}
.img-profile {
position: fixed;
top: 0.7rem;
right: 0.7rem;
max-width: 20vw;
max-height: 20vh;
border-radius: $borderRadius;
transition: max-height $transitionSpeedTransform;
&.img-profile-navbar-small:not(:hover):not(:focus) {
max-height: 3.6rem - 0.7rem - 0.7rem;
}
}
}
@media (max-width: $MQMobile) {
.navbar {
padding-left: 4rem;
.can-hide {
display: none;
}
.links {
padding-left: 1.5rem;
}
.site-name {
overflow: hidden;
width: calc(100vw - 9.4rem);
text-overflow: ellipsis;
white-space: nowrap;
}
}
}
</style>