Skip to content

Commit 93b8ff9

Browse files
authored
polish(teleport): do not warn missing target when teleport is disabled (#2021)
1 parent 0d0970f commit 93b8ff9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

packages/runtime-core/__tests__/components/Teleport.spec.ts

+30
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,36 @@ describe('renderer: teleport', () => {
179179
)
180180
})
181181

182+
test('should work when using template ref as target', async () => {
183+
const root = nodeOps.createElement('div')
184+
const target = ref(null)
185+
const disabled = ref(true)
186+
187+
const App = {
188+
setup() {
189+
return () =>
190+
h(Fragment, [
191+
h('div', { ref: target }),
192+
h(
193+
Teleport,
194+
{ to: target.value, disabled: disabled.value },
195+
h('div', 'teleported')
196+
)
197+
])
198+
}
199+
}
200+
render(h(App), root)
201+
expect(serializeInner(root)).toMatchInlineSnapshot(
202+
`"<div></div><!--teleport start--><div>teleported</div><!--teleport end-->"`
203+
)
204+
205+
disabled.value = false
206+
await nextTick()
207+
expect(serializeInner(root)).toMatchInlineSnapshot(
208+
`"<div><div>teleported</div></div><!--teleport start--><!--teleport end-->"`
209+
)
210+
})
211+
182212
test('disabled', () => {
183213
const target = nodeOps.createElement('div')
184214
const root = nodeOps.createElement('div')

packages/runtime-core/src/components/Teleport.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { warn } from '../warning'
1414
export type TeleportVNode = VNode<RendererNode, RendererElement, TeleportProps>
1515

1616
export interface TeleportProps {
17-
to: string | RendererElement
17+
to: string | RendererElement | null | undefined
1818
disabled?: boolean
1919
}
2020

@@ -50,7 +50,7 @@ const resolveTarget = <T = RendererElement>(
5050
return target as any
5151
}
5252
} else {
53-
if (__DEV__ && !targetSelector) {
53+
if (__DEV__ && !targetSelector && !isTeleportDisabled(props)) {
5454
warn(`Invalid Teleport target: ${targetSelector}`)
5555
}
5656
return targetSelector as any
@@ -94,7 +94,7 @@ export const TeleportImpl = {
9494
const targetAnchor = (n2.targetAnchor = createText(''))
9595
if (target) {
9696
insert(targetAnchor, target)
97-
} else if (__DEV__) {
97+
} else if (__DEV__ && !disabled) {
9898
warn('Invalid Teleport target on mount:', target, `(${typeof target})`)
9999
}
100100

0 commit comments

Comments
 (0)