-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
VTooltip.ts
233 lines (210 loc) Β· 5.97 KB
/
VTooltip.ts
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import './VTooltip.sass'
// Mixins
import Activatable from '../../mixins/activatable'
import Colorable from '../../mixins/colorable'
import Delayable from '../../mixins/delayable'
import Dependent from '../../mixins/dependent'
import Menuable from '../../mixins/menuable'
import Toggleable from '../../mixins/toggleable'
// Helpers
import { convertToUnit, keyCodes, getSlotType } from '../../util/helpers'
import { consoleError } from '../../util/console'
// Types
import { VNode } from 'vue'
import mixins from '../../util/mixins'
/* @vue/component */
export default mixins(Colorable, Delayable, Dependent, Menuable, Toggleable).extend({
name: 'v-tooltip',
props: {
closeDelay: {
type: [Number, String],
default: 0,
},
disabled: Boolean,
openDelay: {
type: [Number, String],
default: 0,
},
openOnHover: {
type: Boolean,
default: true,
},
openOnFocus: {
type: Boolean,
default: true,
},
tag: {
type: String,
default: 'span',
},
transition: String,
},
data: () => ({
calculatedMinWidth: 0,
closeDependents: false,
}),
computed: {
calculatedLeft (): string {
const { activator, content } = this.dimensions
const unknown = !this.bottom && !this.left && !this.top && !this.right
const activatorLeft = this.attach !== false ? activator.offsetLeft : activator.left
let left = 0
if (this.top || this.bottom || unknown) {
left = (
activatorLeft +
(activator.width / 2) -
(content.width / 2)
)
} else if (this.left || this.right) {
left = (
activatorLeft +
(this.right ? activator.width : -content.width) +
(this.right ? 10 : -10)
)
}
if (this.nudgeLeft) left -= parseInt(this.nudgeLeft)
if (this.nudgeRight) left += parseInt(this.nudgeRight)
return `${this.calcXOverflow(left, this.dimensions.content.width)}px`
},
calculatedTop (): string {
const { activator, content } = this.dimensions
const activatorTop = this.attach !== false ? activator.offsetTop : activator.top
let top = 0
if (this.top || this.bottom) {
top = (
activatorTop +
(this.bottom ? activator.height : -content.height) +
(this.bottom ? 10 : -10)
)
} else if (this.left || this.right) {
top = (
activatorTop +
(activator.height / 2) -
(content.height / 2)
)
}
if (this.nudgeTop) top -= parseInt(this.nudgeTop)
if (this.nudgeBottom) top += parseInt(this.nudgeBottom)
if (this.attach === false) top += this.pageYOffset
return `${this.calcYOverflow(top)}px`
},
classes (): object {
return {
'v-tooltip--top': this.top,
'v-tooltip--right': this.right,
'v-tooltip--bottom': this.bottom,
'v-tooltip--left': this.left,
'v-tooltip--attached':
this.attach === '' ||
this.attach === true ||
this.attach === 'attach',
}
},
computedTransition (): string {
if (this.transition) return this.transition
return this.isActive ? 'scale-transition' : 'fade-transition'
},
offsetY (): boolean {
return this.top || this.bottom
},
offsetX (): boolean {
return this.left || this.right
},
styles (): object {
return {
left: this.calculatedLeft,
maxWidth: convertToUnit(this.maxWidth),
minWidth: convertToUnit(this.minWidth),
top: this.calculatedTop,
zIndex: this.zIndex || this.activeZIndex,
}
},
},
beforeMount () {
this.$nextTick(() => {
this.value && this.callActivate()
})
},
mounted () {
if (getSlotType(this, 'activator', true) === 'v-slot') {
consoleError(`v-tooltip's activator slot must be bound, try '<template #activator="data"><v-btn v-on="data.on>'`, this)
}
},
methods: {
activate () {
// Update coordinates and dimensions of menu
// and its activator
this.updateDimensions()
// Start the transition
requestAnimationFrame(this.startTransition)
},
deactivate () {
this.runDelay('close')
},
genActivatorListeners () {
const listeners = Activatable.options.methods.genActivatorListeners.call(this)
if (this.openOnFocus) {
listeners.focus = (e: Event) => {
this.getActivator(e)
this.runDelay('open')
}
listeners.blur = (e: Event) => {
this.getActivator(e)
this.runDelay('close')
}
}
listeners.keydown = (e: KeyboardEvent) => {
if (e.keyCode === keyCodes.esc) {
this.getActivator(e)
this.runDelay('close')
}
}
return listeners
},
genActivatorAttributes () {
return {
'aria-haspopup': true,
'aria-expanded': String(this.isActive),
}
},
genTransition () {
const content = this.genContent()
if (!this.computedTransition) return content
return this.$createElement('transition', {
props: {
name: this.computedTransition,
},
}, [content])
},
genContent () {
return this.$createElement(
'div',
this.setBackgroundColor(this.color, {
staticClass: 'v-tooltip__content',
class: {
[this.contentClass]: true,
menuable__content__active: this.isActive,
'v-tooltip__content--fixed': this.activatorFixed,
},
style: this.styles,
attrs: this.getScopeIdAttrs(),
directives: [{
name: 'show',
value: this.isContentActive,
}],
ref: 'content',
}),
this.getContentSlot()
)
},
},
render (h): VNode {
return h(this.tag, {
staticClass: 'v-tooltip',
class: this.classes,
}, [
this.showLazyContent(() => [this.genTransition()]),
this.genActivator(),
])
},
})