Skip to content

Commit a9491b4

Browse files
committed
Fix: Allow to change the target for the current popover.
This help to point the current BPopover.vue configuration to new instance, and also destroy a previous instance of a popover.
1 parent 9d079da commit a9491b4

File tree

1 file changed

+43
-16
lines changed

1 file changed

+43
-16
lines changed

src/components/BPopover.vue

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,16 @@
2424
// import type {BPopoverEmits, BPopoverProps} from '@/types/components'
2525
import type {BPopoverDelayObject} from '@/types/components'
2626
import Popover from 'bootstrap/js/dist/popover'
27-
import {ComponentPublicInstance, computed, nextTick, onMounted, ref, useSlots, watch} from 'vue'
27+
import {
28+
ComponentPublicInstance,
29+
computed,
30+
nextTick,
31+
onBeforeUnmount,
32+
onMounted,
33+
ref,
34+
useSlots,
35+
watch,
36+
} from 'vue'
2837
import useEventListener from '@/composables/useEventListener'
2938
import type {ColorVariant} from '@/types'
3039
@@ -100,23 +109,29 @@ const getElement = (element: HTMLElement | string | undefined): HTMLElement | un
100109
return element
101110
}
102111
112+
const generatePopoverInstance = (
113+
targetValue: string | ComponentPublicInstance<HTMLElement> | HTMLElement | undefined
114+
) => {
115+
target.value = getElement(cleanElementProp(targetValue))
116+
117+
if (!target.value) return
118+
119+
instance.value = new Popover(target.value, {
120+
container: cleanElementProp(props.container),
121+
trigger: props.triggers,
122+
placement: props.placement,
123+
title: props.title || slots.title ? titleRef.value : '',
124+
content: contentRef.value,
125+
html: props.html,
126+
delay: props.delay,
127+
sanitize: props.sanitize,
128+
offset: props.offset,
129+
})
130+
}
131+
103132
onMounted(() => {
104133
nextTick(() => {
105-
target.value = getElement(cleanElementProp(props.target))
106-
107-
if (target.value)
108-
instance.value = new Popover(target.value, {
109-
container: cleanElementProp(props.container),
110-
trigger: props.triggers,
111-
placement: props.placement,
112-
title: props.title || slots.title ? titleRef.value : '',
113-
content: contentRef.value,
114-
html: props.html,
115-
delay: props.delay,
116-
sanitize: props.sanitize,
117-
offset: props.offset,
118-
})
119-
else console.warn('[B-Popover] Target is a mandatory props.')
134+
generatePopoverInstance(props.target)
120135
})
121136
122137
element.value?.parentNode?.removeChild(element.value)
@@ -126,6 +141,18 @@ onMounted(() => {
126141
}
127142
})
128143
144+
onBeforeUnmount(() => {
145+
instance.value?.dispose()
146+
})
147+
148+
watch(
149+
() => props.target,
150+
(newValue) => {
151+
instance.value?.dispose()
152+
generatePopoverInstance(newValue)
153+
}
154+
)
155+
129156
watch(
130157
() => props.show,
131158
(show, oldVal) => {

0 commit comments

Comments
 (0)