Line data Source code
1 : import type { Directive } from 'vue'
2 :
3 : import { registerCoachmark, unregisterCoachmark } from './coachmarkRegistry'
4 : import type { CoachId } from './onboardingTours'
5 :
6 : // The element's `data-coach-id` is the record of what it is registered as.
7 11701 : function sync(el: HTMLElement, id: CoachId | undefined | null) {
8 11701 : const prev = el.dataset.coachId as CoachId | undefined
9 11701 : if (prev === id) return
10 11701 : if (prev) {
11 276 : unregisterCoachmark(prev, el)
12 276 : delete el.dataset.coachId
13 : }
14 5646 : if (id) {
15 4340 : el.dataset.coachId = id
16 4340 : registerCoachmark(id, el)
17 : }
18 : }
19 :
20 : /**
21 : * Marks an element as a coach-mark target: registers it in the reactive
22 : * registry and mirrors the id to `data-coach-id` for e2e locators. A falsy
23 : * value is a no-op, so it can be bound to a conditional id.
24 : */
25 2355 : export const vCoachmark: Directive<HTMLElement, CoachId | undefined | null> = {
26 : mounted: (el, { value }) => sync(el, value),
27 : updated: (el, { value }) => sync(el, value),
28 : unmounted: (el) => sync(el, null)
29 : }
|