Skip to content

Commit

Permalink
Add Vue mastery Promotion Banner (vuejs#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
pieer authored and nick-lai committed Dec 2, 2020
1 parent 0b41973 commit 68018e1
Show file tree
Hide file tree
Showing 8 changed files with 389 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions src/.vuepress/public/images/vuemastery/logo-vuemastery.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions src/.vuepress/theme/components/sponsors/VueMasteryBanner.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<template>
<div id="vm-banner" class="vuemastery-banner hide" role="banner">
<div class="vuemastery-planet"></div>
<a class="vuemastery-banner--link" href="https://www.vuemastery.com/black-friday-2020" target="_blank">
<img class="vuemastery-banner--logo" src="/images/vuemastery/logo-vuemastery.svg" alt="vuemastery" />
<div class="vuemastery-banner--wrapper">
<p><span>Vue Mastery's Black Friday Sale:</span>Save 50% off a year of Vue courses</p>
<button class="vuemastery-button">Get discount</button>
</div>
</a>
<div @click.prevent="$emit('close-banner')" class="vuemastery-banner--close"></div>
</div>
</template>

<script>
export default {
}
</script>
70 changes: 67 additions & 3 deletions src/.vuepress/theme/layouts/Layout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@touchstart="onTouchStart"
@touchend="onTouchEnd"
>
<VueMasteryBanner v-if="isBannerOpen" @close-banner="closeBanner" ref='vueMasteryBanner'/>
<Navbar v-if="shouldShowNavbar" @toggle-sidebar="toggleSidebar" />

<div class="sidebar-mask" @click="toggleSidebar(false)" />
Expand Down Expand Up @@ -36,6 +37,7 @@ import Home from '@theme/components/Home.vue'
import Navbar from '@theme/components/Navbar.vue'
import Page from '@theme/components/Page.vue'
import Sidebar from '@theme/components/Sidebar.vue'
import VueMasteryBanner from '@theme/components/sponsors/VueMasteryBanner.vue'
import { resolveSidebarItems } from '../util'
export default {
Expand All @@ -45,12 +47,17 @@ export default {
Home,
Page,
Sidebar,
Navbar
Navbar,
VueMasteryBanner
},
data () {
return {
isSidebarOpen: false
isSidebarOpen: false,
isBannerOpen: true,
isMenuFixed: false,
nameStorage: 'vuemastery-black-firday-2020-banner',
menuPosition: 0
}
},
Expand Down Expand Up @@ -94,7 +101,9 @@ export default {
{
'no-navbar': !this.shouldShowNavbar,
'sidebar-open': this.isSidebarOpen,
'no-sidebar': !this.shouldShowSidebar
'no-sidebar': !this.shouldShowSidebar,
'vuemastery-menu-fixed': this.isMenuFixed,
'vuemastery-promo': this.isBannerOpen
},
userPageClass
]
Expand All @@ -105,6 +114,13 @@ export default {
this.$router.afterEach(() => {
this.isSidebarOpen = false
})
// Load component according to user preferences
if (!localStorage.getItem(this.nameStorage)) {
this.initBanner()
} else {
this.isBannerOpen = false
}
},
methods: {
Expand All @@ -131,6 +147,54 @@ export default {
this.toggleSidebar(false)
}
}
},
// Vue Mastery Banner
initBanner() {
// Add event listeners
this.toggleBannerEvents(true)
// Add class to the body to push fixed elements
this.isBannerOpen = true
// Get the menu position
this.getMenuPosition()
// Check current page offset position
this.isMenuFixed = this.isUnderBanner()
},
closeBanner (e) {
// Remove events
this.toggleBannerEvents(false)
// Hide the banner
this.isBannerOpen = false
// Save action in the local storage
localStorage.setItem(this.nameStorage, true)
},
getMenuPosition() {
this.menuPosition = this.$refs.vueMasteryBanner.$el.clientHeight
},
isUnderBanner() {
return window.pageYOffset > this.menuPosition
},
fixMenuAfterBanner() {
if (this.isUnderBanner()) {
if (!this.isMenuFixed) {
// The menu will be fixed
this.isMenuFixed = true
}
} else if (this.isMenuFixed) {
// The menu stay under the banner
this.isMenuFixed = false
}
},
toggleBannerEvents(on) {
// Add or remove event listerners attached to the DOM
let method = on ? "addEventListener" : "removeEventListener"
window[method]("resize", this.getMenuPosition)
window[method]("scroll", this.fixMenuAfterBanner)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/.vuepress/theme/styles/index.styl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@require './arrow'
@require './wrapper'
@require './toc'
@require './vuemastery-banner'

html, body
padding 0
Expand Down
Loading

0 comments on commit 68018e1

Please sign in to comment.