Skip to content

Commit

Permalink
Feat: add sticky steps to allow targetless steps
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan committed Oct 26, 2018
1 parent c704b5e commit ee50e46
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
49 changes: 40 additions & 9 deletions src/components/VStep.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<template>
<div class="v-step" :id="'v-step-' + hash" :ref="'v-step-' + hash">
<div v-bind:class="{ 'v-step-sticky': isSticky }" class="v-step" :id="'v-step-' + hash" :ref="'v-step-' + hash">
<slot name="header">
<div v-if="step.header" class="v-step__header">
<div v-if="step.header.title" v-html="step.header.title"></div>
Expand Down Expand Up @@ -30,7 +30,7 @@
import Popper from 'popper.js'
import jump from 'jump.js'
import sum from 'hash-sum'
import { DEFAULT_STEP_OPTIONS } from '../shared/constants'
import { DEFAULT_STEP_OPTIONS, STICKY } from '../shared/constants'
export default {
name: 'v-step',
Expand Down Expand Up @@ -59,7 +59,8 @@ export default {
},
data () {
return {
hash: sum(this.step.target)
hash: sum(this.step.target),
targetElement: document.querySelector(this.step.target)
}
},
computed: {
Expand All @@ -68,16 +69,32 @@ export default {
...DEFAULT_STEP_OPTIONS,
...this.step.params
}
},
isSticky () {
return (this.params.type === 'sticky')
}
},
methods: {
createStep () {
let targetElement = document.querySelector(this.step.target)
// TODO: debug mode
// console.log('[Vue Tour] The target element ' + this.step.target + ' of .v-step[id="' + this.hash + '"] is:', targetElement)
if (targetElement) {
if (this.isSticky) {
this.targetElement = document.getElementById(STICKY.ID)
if (!this.targetElement) {
const newDiv = document.createElement('div')
newDiv.id = STICKY.ID
this.targetElement = document.body.appendChild(newDiv)
} else {
this.targetElement.style.display = 'block'
}
} else {
document.getElementById(STICKY.ID).style.display = 'none'
}
if (this.targetElement) {
if (this.params.enableScrolling) {
if (this.step.duration || this.step.offset) {
let jumpOptions = {
Expand All @@ -87,16 +104,16 @@ export default {
a11y: false
}
jump(targetElement, jumpOptions)
jump(this.targetElement, jumpOptions)
} else {
// Use the native scroll by default if no scroll options has been defined
targetElement.scrollIntoView({ behavior: 'smooth' })
this.targetElement.scrollIntoView({ behavior: 'smooth' })
}
}
/* eslint-disable no-new */
new Popper(
targetElement,
this.targetElement,
this.$refs['v-step-' + this.hash],
this.params
)
Expand All @@ -121,6 +138,20 @@ export default {
filter: drop-shadow(0 0 2px rgba(0, 0, 0, 0.5));
padding: 1rem;
text-align: center;
&.v-step-sticky {
position: absolute;
left: 0;
right: 0;
width: 50vw;
max-width: 90vw;
z-index: 99999;
& .v-step__arrow {
opacity: 0;
display: none;
}
}
}
.v-step .v-step__arrow {
Expand Down
24 changes: 23 additions & 1 deletion src/components/VTour.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
</template>

<script>
import { DEFAULT_CALLBACKS, DEFAULT_OPTIONS, KEYS } from '../shared/constants'
import { DEFAULT_CALLBACKS, DEFAULT_OPTIONS, KEYS, STICKY } from '../shared/constants'
export default {
name: 'v-tour',
Expand Down Expand Up @@ -122,6 +122,7 @@ export default {
},
stop () {
this.customCallbacks.onStop()
document.getElementById(STICKY.ID).remove()
this.currentStep = -1
},
Expand All @@ -143,3 +144,24 @@ export default {
}
}
</script>

<style lang="scss">
#v-tour-sticky {
position: absolute;
width: 0;
height: 0;
left: 50%;
top: 5vh;
z-index: 9999;
&:after {
content: "";
position: fixed;
width: 100vw;
height: 100vh;
left: 0;
top: 0;
background: rgba(0, 0, 0, 0.5);
}
}
</style>
4 changes: 4 additions & 0 deletions src/shared/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const DEFAULT_CALLBACKS = {
onStop: () => {}
}

export const STICKY = {
ID: 'v-tour-sticky'
}

export const DEFAULT_OPTIONS = {
useKeyboardNavigation: true,
startTimeout: 0,
Expand Down

0 comments on commit ee50e46

Please sign in to comment.