Skip to content

Fixing merge conflicts on #1015 to add a logo link #2375

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 122 additions & 60 deletions packages/@vuepress/theme-default/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<SidebarButton @toggle-sidebar="$emit('toggle-sidebar')" />

<RouterLink
:to="$localePath"
v-if="!isExternal(logoLink)"
:to="logoLink"
class="home-link"
>
<img
Expand All @@ -19,6 +20,26 @@
:class="{ 'can-hide': $site.themeConfig.logo }"
>{{ $siteTitle }}</span>
</RouterLink>
<a
v-else
:href="logoLink"
class="home-link"
:target="'_blank'"
:rel="'noopener noreferrer'"
>
<img
v-if="$site.themeConfig.logo"
class="logo"
:src="$withBase($site.themeConfig.logo)"
:alt="$siteTitle"
>
<span
v-if="$siteTitle"
ref="siteName"
class="site-name"
:class="{ 'can-hide': $site.themeConfig.logo }"
>{{ $siteTitle }}</span>
</a>

<div
class="links"
Expand All @@ -30,7 +51,9 @@
v-if="isAlgoliaSearch"
:options="algolia"
/>
<SearchBox v-else-if="$site.themeConfig.search !== false && $page.frontmatter.search !== false" />
<SearchBox
v-else-if="$site.themeConfig.search !== false && $page.frontmatter.search !== false"
/>
<NavLinks class="can-hide" />
</div>
</header>
Expand All @@ -41,6 +64,7 @@ import AlgoliaSearchBox from '@AlgoliaSearchBox'
import SearchBox from '@SearchBox'
import SidebarButton from '@theme/components/SidebarButton.vue'
import NavLinks from '@theme/components/NavLinks.vue'
import { isExternal } from '../util'

export default {
name: 'Navbar',
Expand All @@ -60,27 +84,46 @@ export default {

computed: {
algolia () {
return this.$themeLocaleConfig.algolia || this.$site.themeConfig.algolia || {}
return (
this.$themeLocaleConfig.algolia || this.$site.themeConfig.algolia || {}
)
},

isAlgoliaSearch () {
return this.algolia && this.algolia.apiKey && this.algolia.indexName
}
},
},

logoLink () {
return (
this.$themeLocaleConfig.logoLink
|| this.$site.themeConfig.logoLink
|| this.$localePath
)
},

methods: {
isExternal
},

mounted () {
const MOBILE_DESKTOP_BREAKPOINT = 719 // refer to config.styl
const NAVBAR_VERTICAL_PADDING = parseInt(css(this.$el, 'paddingLeft')) + parseInt(css(this.$el, 'paddingRight'))
const handleLinksWrapWidth = () => {
if (document.documentElement.clientWidth < MOBILE_DESKTOP_BREAKPOINT) {
this.linksWrapMaxWidth = null
} else {
this.linksWrapMaxWidth = this.$el.offsetWidth - NAVBAR_VERTICAL_PADDING
- (this.$refs.siteName && this.$refs.siteName.offsetWidth || 0)
mounted () {
const MOBILE_DESKTOP_BREAKPOINT = 719 // refer to config.styl
const NAVBAR_VERTICAL_PADDING
= parseInt(css(this.$el, 'paddingLeft'))
+ parseInt(css(this.$el, 'paddingRight'))
const handleLinksWrapWidth = () => {
if (document.documentElement.clientWidth < MOBILE_DESKTOP_BREAKPOINT) {
this.linksWrapMaxWidth = null
} else {
this.linksWrapMaxWidth
= this.$el.offsetWidth
- NAVBAR_VERTICAL_PADDING
- ((this.$refs.siteName && this.$refs.siteName.offsetWidth) || 0)
}
}
handleLinksWrapWidth()
window.addEventListener('resize', handleLinksWrapWidth, false)
return true;
}
handleLinksWrapWidth()
window.addEventListener('resize', handleLinksWrapWidth, false)
}
}

Expand All @@ -93,48 +136,67 @@ function css (el, property) {
</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
height $navbarHeight - 1.4rem
min-width $navbarHeight - 1.4rem
margin-right 0.8rem
vertical-align top
.site-name
font-size 1.3rem
font-weight 600
color $textColor
position relative
.links
padding-left 1.5rem
box-sizing border-box
background-color white
white-space nowrap
font-size 0.9rem
position absolute
right $navbar-horizontal-padding
top $navbar-vertical-padding
display flex
.search-box
flex: 0 0 auto
vertical-align top

@media (max-width: $MQMobile)
.navbar
padding-left 4rem
.can-hide
display none
.links
padding-left 1.5rem
.site-name
width calc(100vw - 9.4rem)
overflow hidden
white-space nowrap
text-overflow ellipsis
$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 {
height: $navbarHeight - 1.4rem;
min-width: $navbarHeight - 1.4rem;
margin-right: 0.8rem;
vertical-align: top;
}

.site-name {
font-size: 1.3rem;
font-weight: 600;
color: $textColor;
position: relative;
}

.links {
padding-left: 1.5rem;
box-sizing: border-box;
background-color: white;
white-space: nowrap;
font-size: 0.9rem;
position: absolute;
right: $navbar-horizontal-padding;
top: $navbar-vertical-padding;
display: flex;

.search-box {
flex: 0 0 auto;
vertical-align: top;
}
}
}

@media (max-width: $MQMobile) {
.navbar {
padding-left: 4rem;

.can-hide {
display: none;
}

.links {
padding-left: 1.5rem;
}

.site-name {
width: calc(100vw - 9.4rem);
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}
}
}
</style>
34 changes: 34 additions & 0 deletions packages/docs/docs/theme/default-theme-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,40 @@ module.exports = {
}
```

### Logo Link

By default, clicking on the logo redirects you to the [`localePath`](/config/#locales) - the root of the path defined in your current locale. You can change this link on a locale or site-wide level:

```js
// .vuepress/config.js
// Site-wide
module.exports = {
themeConfig: {
logoLink: 'https://yourdomain.com'
}
}
```

```js
// .vuepress/config.js
// Locale-specific
module.exports = {
themeConfig: {
locales: {
'/': {
label: 'English',
logoLink: 'https://yourdomain.com'
},
'/zh': {
label: '简体中文',
logoLink: 'https://yourdomain.cn'
}
}
}
}
```


### Navbar Links

You can add links to the navbar via `themeConfig.nav`:
Expand Down