-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirective.ts
More file actions
90 lines (81 loc) · 2.39 KB
/
Copy pathdirective.ts
File metadata and controls
90 lines (81 loc) · 2.39 KB
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
import { Placement } from '@floating-ui/vue'
import { DirectiveBinding } from 'vue'
import { onMouseout, onMouseover } from './listeners'
import { getOption } from './options'
import {
deleteContent,
generateKey,
setContent,
} from './state'
import type {
Binding,
Content,
Modifier,
TooltipDirective,
} from './types.ts'
import { ensureKey } from './utils'
const toContent = (value: Binding): Content =>
typeof value === 'string'
? { text: value }
: typeof value.content === 'string'
? { text: value.content }
: value.content
const extractPlacement = (
binding: DirectiveBinding<Binding, Modifier, Placement>,
) => {
const { value, arg } = binding
if (typeof value !== 'string' && 'placement' in value) {
return value.placement
}
if (!arg) return getOption('defaultPlacement')
return arg
}
const truncationDirection = (
modifiers: Partial<Record<Modifier, boolean | undefined>>,
) => {
if (modifiers.none) return 'none'
if (modifiers.both) return 'both'
if (modifiers.x && modifiers.y) return 'both'
if (modifiers.x) return 'x'
if (modifiers.y) return 'y'
return getOption('defaultTruncateDetection')
}
export const vueltipDirective = {
updated: (el, binding) => {
ensureKey(el, (key) => {
el.setAttribute(
getOption('placementAttribute'),
extractPlacement(binding),
)
el.setAttribute(
getOption('truncateAttribute'),
truncationDirection(binding.modifiers ?? {}),
)
setContent(key, toContent(binding.value))
})
},
created: (el, binding) => {
const key = generateKey()
setContent(key, toContent(binding.value))
el.setAttribute(getOption('keyAttribute'), key)
el.setAttribute(
getOption('placementAttribute'),
extractPlacement(binding),
)
el.setAttribute(
getOption('truncateAttribute'),
truncationDirection(binding.modifiers ?? {}),
)
el.addEventListener('mouseenter', onMouseover)
el.addEventListener('focus', onMouseover)
el.addEventListener('mouseleave', onMouseout)
el.addEventListener('blur', onMouseout)
},
beforeUnmount: (el) => {
ensureKey(el, (key) => deleteContent(key))
el.removeEventListener('mouseenter', onMouseover)
el.removeEventListener('focus', onMouseover)
el.removeEventListener('mouseleave', onMouseout)
el.removeEventListener('blur', onMouseout)
},
} satisfies TooltipDirective