/
VOnboardingStep.vue
147 lines (144 loc) · 5.16 KB
/
VOnboardingStep.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
<template>
<div v-show="show">
<svg style="width: 100%; height: 100%; position: fixed; top: 0; left: 0; opacity: 0.5; z-index: var(--v-onboarding-overlay-z, 10); pointer-events: none;">
<path :d="path" />
</svg>
<div ref="stepElement" style="position: relative; z-index: var(--v-onboarding-step-z, 20);">
<slot v-if="step">
<div class="v-onboarding-item">
<div class="v-onboarding-item__header">
<span
v-if="step.content.title"
class="v-onboarding-item__header-title"
>{{ step.content.title }}</span>
<button @click="exit" class="v-onboarding-item__header-close">
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
<p
v-if="step.content.description"
class="v-onboarding-item__description"
>{{ step.content.description }}</p>
<div class="v-onboarding-item__actions">
<button
v-if="!isFirst"
type="button"
@click="onPrevious"
class="v-onboarding-btn-secondary"
>{{ buttonLabels.previous }}</button>
<button
@click="onNext"
type="button"
class="v-onboarding-btn-primary"
>{{ isLast ? buttonLabels.finish : buttonLabels.next }}</button>
</div>
</div>
</slot>
<div data-popper-arrow />
</div>
</div>
</template>
<script lang="ts">
import useGetElement from '../composables/useGetElement';
import useSvgOverlay from '../composables/useSvgOverlay';
import { StepEntity } from '../types/StepEntity';
import { VOnboardingWrapperOptions } from '../types/VOnboardingWrapper';
import { createPopper } from '@popperjs/core';
import merge from 'lodash.merge';
import { computed, ComputedRef, defineComponent, inject, onMounted, ref, watch } from 'vue';
export default defineComponent({
name: "VOnboardingStep",
setup(_, { slots }) {
const show = ref(false)
const nextStep = inject('next-step', () => { });
const previousStep = inject('previous-step', () => { });
const exit = inject('exit', () => { });
const options = inject<ComputedRef<VOnboardingWrapperOptions>>('options')
const mergedOptions = computed(() => merge({}, options?.value, step.value.options))
const isFirst = inject<ComputedRef<boolean>>('is-first-step')
const isLast = inject<ComputedRef<boolean>>('is-last-step')
const step = inject<ComputedRef<StepEntity>>('step', {} as ComputedRef<StepEntity>);
const buttonLabels = computed(() => {
return {
previous: mergedOptions.value?.labels?.previousButton,
next: mergedOptions.value?.labels?.nextButton,
finish: mergedOptions.value?.labels?.finishButton,
}
})
const onNext = () => {
beforeStepEnd();
nextStep()
}
const onPrevious = () => {
beforeStepEnd();
previousStep()
}
watch(() => step.value, (_, oldStep) => {
if (!slots.default?.()) return
beforeStepEnd(oldStep)
})
const stepElement = ref<HTMLElement | null>(null);
const { updatePath, path } = useSvgOverlay();
const attachElement = () => {
const element = useGetElement(step?.value?.attachTo?.element);
if (element && stepElement.value) {
show.value = true
if (mergedOptions.value?.scrollToStep?.enabled) {
element.scrollIntoView(mergedOptions.value?.scrollToStep?.options)
}
createPopper(element, stepElement.value, mergedOptions.value.popper);
if (mergedOptions.value?.overlay?.enabled) {
updatePath(element, {
padding: mergedOptions.value?.overlay?.padding,
borderRadius: mergedOptions.value?.overlay?.borderRadius,
});
}
setTargetElementClassName(element);
}
};
const beforeStepStart = async () => {
await step?.value?.on?.beforeStep?.();
attachElement();
}
const beforeStepEnd = (stepObj = step.value) => {
stepObj?.on?.afterStep?.();
unsetTargetElementClassName(useGetElement(stepObj?.attachTo?.element), stepObj.attachTo?.classList);
}
const setTargetElementClassName = (element = useGetElement(step.value.attachTo.element)) => {
const classList = step.value.attachTo.classList;
if (!classList || !element) return;
element.classList.add(...classList)
}
const unsetTargetElementClassName = (element = useGetElement(step.value.attachTo.element), classList?: string[]) => {
if (!classList || !element) return;
element.classList.remove(...classList)
}
onMounted(beforeStepStart)
return {
stepElement,
onNext,
onPrevious,
path,
show,
step,
isFirst,
isLast,
exit,
buttonLabels
};
},
})
</script>