Skip to content

Commit 649c38d

Browse files
committed
feat: single app for v-tooltip directives
1 parent 0225182 commit 649c38d

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

packages/demo-vue3/src/views/directive/VTooltipDemo1.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
Et consectetur voluptates magnam quia dolor fugiat quod aliquid. Nihil sunt quasi provident qui ea et beatae est. Dolorem dicta optio deserunt.
1212

13-
Saepe neque quaerat sint architecto labore quos consequatur. Sequi perspiciatis dolores sunt occaecati quas ratione aut aliquam. Repellat iusto et et cum fuga.
13+
Saepe neque quaerat sint architecto labore quos consequatur. Sequi perspiciatis <b v-tooltip="'Hello!'">dolores</b> sunt occaecati quas ratione aut aliquam. Repellat iusto et et cum fuga.
1414
</div>
1515
</template>

packages/floating-vue/src/directives/v-tooltip.ts

+45-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createApp, h, ref } from 'vue'
1+
import { App, createApp, h, Ref, ref } from 'vue'
22
import TooltipDirective from '../components/TooltipDirective.vue'
33
import { getDefaultConfig } from '../config'
44
import { placements } from '../util/popper'
@@ -39,47 +39,68 @@ export function getOptions (el, value, modifiers) {
3939
return options
4040
}
4141

42-
export function createTooltip (el, value, modifiers) {
43-
const options = ref(getOptions(el, value, modifiers))
44-
const component = ref()
42+
interface Directive {
43+
options: Ref<any>
44+
shown: Ref<boolean>
45+
}
46+
47+
let directiveApp: App
48+
let directives: Ref<Directive[]>
4549

46-
const tooltipApp = createApp({
47-
name: 'VTooltipDirective',
50+
function ensureDirectiveApp () {
51+
if (directiveApp) return
52+
53+
directives = ref([])
54+
55+
directiveApp = createApp({
56+
name: 'VTooltipDirectiveApp',
4857
setup () {
4958
return {
50-
options,
51-
tooltip: component,
59+
directives,
5260
}
5361
},
5462
render () {
55-
return h(TooltipDirective, {
56-
...this.options,
57-
ref: 'tooltip',
63+
return this.directives.map((directive) => {
64+
return h(TooltipDirective, {
65+
...directive.options,
66+
shown: directive.shown.value || directive.options.shown,
67+
})
5868
})
5969
},
6070
devtools: {
6171
hide: true,
6272
},
6373
})
74+
6475
const mountTarget = document.createElement('div')
6576
document.body.appendChild(mountTarget)
66-
tooltipApp.mount(mountTarget)
67-
el.$_popperMountTarget = mountTarget
77+
directiveApp.mount(mountTarget)
78+
}
79+
80+
export function createTooltip (el, value, modifiers) {
81+
ensureDirectiveApp()
82+
const options = ref(getOptions(el, value, modifiers))
83+
const shown = ref(false)
84+
85+
const item = {
86+
options,
87+
shown,
88+
}
89+
directives.value.push(item)
6890

6991
// Class on target
7092
if (el.classList) {
7193
el.classList.add(TARGET_CLASS)
7294
}
7395

7496
const result = el.$_popper = {
75-
app: tooltipApp,
7697
options,
77-
component,
98+
item,
7899
show () {
79-
component.value.show()
100+
shown.value = true
80101
},
81102
hide () {
82-
component.value.hide()
103+
shown.value = false
83104
},
84105
}
85106

@@ -88,10 +109,8 @@ export function createTooltip (el, value, modifiers) {
88109

89110
export function destroyTooltip (el) {
90111
if (el.$_popper) {
91-
el.$_popper.app.unmount()
92-
if (el.$_popperMountTarget.parentElement) {
93-
el.$_popperMountTarget.parentElement.removeChild(el.$_popperMountTarget)
94-
}
112+
const index = directives.value.indexOf(el.$_popper.item)
113+
if (index !== -1) directives.value.splice(index, 1)
95114

96115
delete el.$_popper
97116
delete el.$_popperOldShown
@@ -108,18 +127,18 @@ export function bind (el, { value, oldValue, modifiers }) {
108127
if (!options.content || getDefaultConfig(options.theme || 'tooltip', 'disabled')) {
109128
destroyTooltip(el)
110129
} else {
111-
let tooltipApp
130+
let directive
112131
if (el.$_popper) {
113-
tooltipApp = el.$_popper
114-
tooltipApp.options.value = options
132+
directive = el.$_popper
133+
directive.options.value = options
115134
} else {
116-
tooltipApp = createTooltip(el, value, modifiers)
135+
directive = createTooltip(el, value, modifiers)
117136
}
118137

119138
// Manual show
120139
if (typeof value.shown !== 'undefined' && value.shown !== el.$_popperOldShown) {
121140
el.$_popperOldShown = value.shown
122-
value.shown ? tooltipApp.show() : tooltipApp.hide()
141+
value.shown ? directive.show() : directive.hide()
123142
}
124143
}
125144
}

0 commit comments

Comments
 (0)