Skip to content

Commit e65f802

Browse files
committed
feature(vue3): replace transporter implementation with teleport
* Use built-in teleport in Vue3
1 parent 358ee9b commit e65f802

File tree

4 files changed

+40
-6
lines changed

4 files changed

+40
-6
lines changed

src/components/modal/modal.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createWrapper, mount } from '@vue/test-utils'
2+
import { isVue3 } from '../../vue'
23
import { waitNT, waitRAF } from '../../../tests/utils'
34
import { getInstanceFromVNode } from '../../utils/get-instance-from-vnode'
45
import { BModal } from './modal'
@@ -177,7 +178,9 @@ describe('modal', () => {
177178
expect(outer).not.toBe(null)
178179

179180
expect(getInstanceFromVNode(outer)).toBeDefined() // Target
180-
expect(getInstanceFromVNode(outer).$options.name).toBe('BVTransporterTarget')
181+
if (!isVue3) {
182+
expect(getInstanceFromVNode(outer).$options.name).toBe('BVTransporterTarget')
183+
}
181184
expect(outer.parentElement).toBeDefined()
182185
expect(outer.parentElement).toBe(document.body)
183186

src/components/toast/toaster.spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PortalTarget } from 'portal-vue'
22
import { mount } from '@vue/test-utils'
3+
import { isVue3 } from '../../vue'
34
import { waitNT, waitRAF } from '../../../tests/utils'
45
import { BToaster } from './toaster'
56

@@ -27,7 +28,9 @@ describe('b-toaster', () => {
2728

2829
expect(wrapper.find('.b-toaster-slot').exists()).toBe(true)
2930
const $slot = wrapper.find('.b-toaster-slot')
30-
expect($slot.findComponent(PortalTarget).exists()).toBe(true)
31+
if (!isVue3) {
32+
expect($slot.findComponent(PortalTarget).exists()).toBe(true)
33+
}
3134
expect($slot.element.tagName).toBe('DIV')
3235
expect($slot.classes()).toContain('b-toaster-slot')
3336
expect($slot.classes()).toContain('vue-portal-target')
@@ -60,7 +63,9 @@ describe('b-toaster', () => {
6063

6164
expect(wrapper.find('.b-toaster-slot').exists()).toBe(true)
6265
const $slot = wrapper.find('.b-toaster-slot')
63-
expect($slot.findComponent(PortalTarget).exists()).toBe(true)
66+
if (!isVue3) {
67+
expect($slot.findComponent(PortalTarget).exists()).toBe(true)
68+
}
6469
expect($slot.element.tagName).toBe('DIV')
6570
expect($slot.classes()).toContain('b-toaster-slot')
6671
expect($slot.classes()).toContain('vue-portal-target')

src/components/transporter/transporter.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Vue } from '../../vue'
1+
import { Vue, isVue3 } from '../../vue'
22
import { NAME_TRANSPORTER, NAME_TRANSPORTER_TARGET } from '../../constants/components'
33
import { IS_BROWSER } from '../../constants/env'
44
import {
@@ -79,7 +79,7 @@ export const props = {
7979
// --- Main component ---
8080

8181
// @vue/component
82-
export const BVTransporter = /*#__PURE__*/ Vue.extend({
82+
const BVTransporterVue2 = /*#__PURE__*/ Vue.extend({
8383
name: NAME_TRANSPORTER,
8484
mixins: [normalizeSlotMixin],
8585
props,
@@ -177,3 +177,26 @@ export const BVTransporter = /*#__PURE__*/ Vue.extend({
177177
return h()
178178
}
179179
})
180+
181+
const BVTransporterVue3 = /*#__PURE__*/ Vue.extend({
182+
name: NAME_TRANSPORTER,
183+
mixins: [normalizeSlotMixin],
184+
props,
185+
render(h) {
186+
if (this.disabled) {
187+
const $nodes = concat(this.normalizeSlot()).filter(identity)
188+
if ($nodes.length > 0) {
189+
return $nodes[0]
190+
}
191+
}
192+
return h(
193+
Vue.Teleport,
194+
{
195+
to: this.container
196+
},
197+
this.normalizeSlot()
198+
)
199+
}
200+
})
201+
202+
export const BVTransporter = isVue3 ? BVTransporterVue3 : BVTransporterVue2

src/components/transporter/transporter.spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { isVue3 } from '../../vue'
12
import { mount } from '@vue/test-utils'
23
import { waitNT } from '../../../tests/utils'
34
import { getInstanceFromVNode } from '../../utils/get-instance-from-vnode'
@@ -45,7 +46,9 @@ describe('utils/transporter component', () => {
4546
expect(target).toBeDefined()
4647
expect(target).not.toBe(null)
4748
expect(getInstanceFromVNode(target)).toBeDefined() // Target
48-
expect(getInstanceFromVNode(target).$options.name).toBe('BVTransporterTarget')
49+
if (!isVue3) {
50+
expect(getInstanceFromVNode(target).$options.name).toBe('BVTransporterTarget')
51+
}
4952
expect(target.tagName).toEqual('DIV')
5053
expect(target.parentElement).toBeDefined()
5154
expect(target.parentElement).toBe(document.body)

0 commit comments

Comments
 (0)