Skip to content

Commit eee5095

Browse files
committed
refactor: rename <portal> to <teleport>
BREAKING CHANGE: `<portal>` has been renamed to `<teleport>`. `target` prop is also renmaed to `to`, so the new usage will be: ```html <Teleport to="#modal-layer" :disabled="isMobile"> <div class="modal"> hello </div> </Teleport> ``` The primary reason for the renaming is to avoid potential naming conflict with [native portals](https://wicg.github.io/portals/).
1 parent 8080c38 commit eee5095

File tree

26 files changed

+291
-284
lines changed

26 files changed

+291
-284
lines changed

packages/compiler-core/__tests__/transforms/transformElement.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
RESOLVE_DIRECTIVE,
1212
TO_HANDLERS,
1313
helperNameMap,
14-
PORTAL,
14+
TELEPORT,
1515
RESOLVE_DYNAMIC_COMPONENT,
1616
SUSPENSE,
1717
KEEP_ALIVE,
@@ -272,16 +272,16 @@ describe('compiler: element transform', () => {
272272
})
273273
})
274274

275-
test('should handle <Portal> with normal children', () => {
275+
test('should handle <Teleport> with normal children', () => {
276276
function assert(tag: string) {
277277
const { root, node } = parseWithElementTransform(
278278
`<${tag} target="#foo"><span /></${tag}>`
279279
)
280280
expect(root.components.length).toBe(0)
281-
expect(root.helpers).toContain(PORTAL)
281+
expect(root.helpers).toContain(TELEPORT)
282282

283283
expect(node).toMatchObject({
284-
tag: PORTAL,
284+
tag: TELEPORT,
285285
props: createObjectMatcher({
286286
target: '#foo'
287287
}),
@@ -298,8 +298,8 @@ describe('compiler: element transform', () => {
298298
})
299299
}
300300

301-
assert(`portal`)
302-
assert(`Portal`)
301+
assert(`teleport`)
302+
assert(`Teleport`)
303303
})
304304

305305
test('should handle <Suspense>', () => {

packages/compiler-core/src/runtimeHelpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const FRAGMENT = Symbol(__DEV__ ? `Fragment` : ``)
2-
export const PORTAL = Symbol(__DEV__ ? `Portal` : ``)
2+
export const TELEPORT = Symbol(__DEV__ ? `Teleport` : ``)
33
export const SUSPENSE = Symbol(__DEV__ ? `Suspense` : ``)
44
export const KEEP_ALIVE = Symbol(__DEV__ ? `KeepAlive` : ``)
55
export const BASE_TRANSITION = Symbol(__DEV__ ? `BaseTransition` : ``)
@@ -33,7 +33,7 @@ export const WITH_CTX = Symbol(__DEV__ ? `withCtx` : ``)
3333
// Using `any` here because TS doesn't allow symbols as index type.
3434
export const helperNameMap: any = {
3535
[FRAGMENT]: `Fragment`,
36-
[PORTAL]: `Portal`,
36+
[TELEPORT]: `Teleport`,
3737
[SUSPENSE]: `Suspense`,
3838
[KEEP_ALIVE]: `KeepAlive`,
3939
[BASE_TRANSITION]: `BaseTransition`,

packages/compiler-core/src/transforms/transformElement.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
RESOLVE_DYNAMIC_COMPONENT,
2929
MERGE_PROPS,
3030
TO_HANDLERS,
31-
PORTAL,
31+
TELEPORT,
3232
KEEP_ALIVE
3333
} from '../runtimeHelpers'
3434
import {
@@ -124,8 +124,8 @@ export const transformElement: NodeTransform = (node, context) => {
124124

125125
const shouldBuildAsSlots =
126126
isComponent &&
127-
// Portal is not a real component and has dedicated runtime handling
128-
vnodeTag !== PORTAL &&
127+
// Teleport is not a real component and has dedicated runtime handling
128+
vnodeTag !== TELEPORT &&
129129
// explained above.
130130
vnodeTag !== KEEP_ALIVE
131131

@@ -135,7 +135,7 @@ export const transformElement: NodeTransform = (node, context) => {
135135
if (hasDynamicSlots) {
136136
patchFlag |= PatchFlags.DYNAMIC_SLOTS
137137
}
138-
} else if (node.children.length === 1 && vnodeTag !== PORTAL) {
138+
} else if (node.children.length === 1 && vnodeTag !== TELEPORT) {
139139
const child = node.children[0]
140140
const type = child.type
141141
// check for dynamic text children
@@ -217,7 +217,7 @@ export function resolveComponentType(
217217
}
218218
}
219219

220-
// 2. built-in components (Portal, Transition, KeepAlive, Suspense...)
220+
// 2. built-in components (Teleport, Transition, KeepAlive, Suspense...)
221221
const builtIn = isCoreComponent(tag) || context.isBuiltInComponent(tag)
222222
if (builtIn) {
223223
// built-ins are simply fallthroughs / have special handling during ssr

packages/compiler-core/src/transforms/vIf.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
FRAGMENT,
2828
CREATE_COMMENT,
2929
OPEN_BLOCK,
30-
PORTAL
30+
TELEPORT
3131
} from '../runtimeHelpers'
3232
import { injectProp } from '../utils'
3333
import { PatchFlags, PatchFlagNames } from '@vue/shared'
@@ -218,8 +218,8 @@ function createChildrenCodegenNode(
218218
// component vnodes are always tracked and its children are
219219
// compiled into slots so no need to make it a block
220220
((firstChild as ElementNode).tagType !== ElementTypes.COMPONENT ||
221-
// portal has component type but isn't always tracked
222-
vnodeCall.tag === PORTAL)
221+
// teleport has component type but isn't always tracked
222+
vnodeCall.tag === TELEPORT)
223223
) {
224224
vnodeCall.isBlock = true
225225
helper(OPEN_BLOCK)

packages/compiler-core/src/utils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
import { TransformContext } from './transform'
2626
import {
2727
MERGE_PROPS,
28-
PORTAL,
28+
TELEPORT,
2929
SUSPENSE,
3030
KEEP_ALIVE,
3131
BASE_TRANSITION
@@ -38,8 +38,8 @@ export const isBuiltInType = (tag: string, expected: string): boolean =>
3838
tag === expected || tag === hyphenate(expected)
3939

4040
export function isCoreComponent(tag: string): symbol | void {
41-
if (isBuiltInType(tag, 'Portal')) {
42-
return PORTAL
41+
if (isBuiltInType(tag, 'Teleport')) {
42+
return TELEPORT
4343
} else if (isBuiltInType(tag, 'Suspense')) {
4444
return SUSPENSE
4545
} else if (isBuiltInType(tag, 'KeepAlive')) {

packages/compiler-ssr/__tests__/ssrPortal.spec.ts

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,40 @@
11
import { compile } from '../src'
22

3-
describe('ssr compile: portal', () => {
3+
describe('ssr compile: teleport', () => {
44
test('should work', () => {
5-
expect(compile(`<portal :target="target"><div/></portal>`).code)
5+
expect(compile(`<teleport :target="target"><div/></teleport>`).code)
66
.toMatchInlineSnapshot(`
7-
"const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\")
7+
"const { ssrRenderTeleport: _ssrRenderTeleport } = require(\\"@vue/server-renderer\\")
88
99
return function ssrRender(_ctx, _push, _parent) {
10-
_ssrRenderPortal(_push, (_push) => {
10+
_ssrRenderTeleport(_push, (_push) => {
1111
_push(\`<div></div>\`)
1212
}, _ctx.target, false, _parent)
1313
}"
1414
`)
1515
})
1616

1717
test('disabled prop handling', () => {
18-
expect(compile(`<portal :target="target" disabled><div/></portal>`).code)
19-
.toMatchInlineSnapshot(`
20-
"const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\")
18+
expect(
19+
compile(`<teleport :target="target" disabled><div/></teleport>`).code
20+
).toMatchInlineSnapshot(`
21+
"const { ssrRenderTeleport: _ssrRenderTeleport } = require(\\"@vue/server-renderer\\")
2122
2223
return function ssrRender(_ctx, _push, _parent) {
23-
_ssrRenderPortal(_push, (_push) => {
24+
_ssrRenderTeleport(_push, (_push) => {
2425
_push(\`<div></div>\`)
2526
}, _ctx.target, true, _parent)
2627
}"
2728
`)
2829

2930
expect(
30-
compile(`<portal :target="target" :disabled="foo"><div/></portal>`).code
31+
compile(`<teleport :target="target" :disabled="foo"><div/></teleport>`)
32+
.code
3133
).toMatchInlineSnapshot(`
32-
"const { ssrRenderPortal: _ssrRenderPortal } = require(\\"@vue/server-renderer\\")
34+
"const { ssrRenderTeleport: _ssrRenderTeleport } = require(\\"@vue/server-renderer\\")
3335
3436
return function ssrRender(_ctx, _push, _parent) {
35-
_ssrRenderPortal(_push, (_push) => {
37+
_ssrRenderTeleport(_push, (_push) => {
3638
_push(\`<div></div>\`)
3739
}, _ctx.target, _ctx.foo, _parent)
3840
}"

packages/compiler-ssr/src/errors.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ export function createSSRCompilerError(
1919
export const enum SSRErrorCodes {
2020
X_SSR_CUSTOM_DIRECTIVE_NO_TRANSFORM = DOMErrorCodes.__EXTEND_POINT__,
2121
X_SSR_UNSAFE_ATTR_NAME,
22-
X_SSR_NO_PORTAL_TARGET
22+
X_SSR_NO_TELEPORT_TARGET
2323
}
2424

2525
export const SSRErrorMessages: { [code: number]: string } = {
2626
[SSRErrorCodes.X_SSR_CUSTOM_DIRECTIVE_NO_TRANSFORM]: `Custom directive is missing corresponding SSR transform and will be ignored.`,
2727
[SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME]: `Unsafe attribute name for SSR.`,
28-
[SSRErrorCodes.X_SSR_NO_PORTAL_TARGET]: `No target prop on portal element.`
28+
[SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET]: `No target prop on teleport element.`
2929
}

packages/compiler-ssr/src/runtimeHelpers.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const SSR_LOOSE_EQUAL = Symbol(`ssrLooseEqual`)
1313
export const SSR_LOOSE_CONTAIN = Symbol(`ssrLooseContain`)
1414
export const SSR_RENDER_DYNAMIC_MODEL = Symbol(`ssrRenderDynamicModel`)
1515
export const SSR_GET_DYNAMIC_MODEL_PROPS = Symbol(`ssrGetDynamicModelProps`)
16-
export const SSR_RENDER_PORTAL = Symbol(`ssrRenderPortal`)
16+
export const SSR_RENDER_TELEPORT = Symbol(`ssrRenderTeleport`)
1717
export const SSR_RENDER_SUSPENSE = Symbol(`ssrRenderSuspense`)
1818

1919
export const ssrHelpers = {
@@ -30,7 +30,7 @@ export const ssrHelpers = {
3030
[SSR_LOOSE_CONTAIN]: `ssrLooseContain`,
3131
[SSR_RENDER_DYNAMIC_MODEL]: `ssrRenderDynamicModel`,
3232
[SSR_GET_DYNAMIC_MODEL_PROPS]: `ssrGetDynamicModelProps`,
33-
[SSR_RENDER_PORTAL]: `ssrRenderPortal`,
33+
[SSR_RENDER_TELEPORT]: `ssrRenderTeleport`,
3434
[SSR_RENDER_SUSPENSE]: `ssrRenderSuspense`
3535
}
3636

packages/compiler-ssr/src/transforms/ssrTransformComponent.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
buildSlots,
1212
FunctionExpression,
1313
TemplateChildNode,
14-
PORTAL,
14+
TELEPORT,
1515
createIfStatement,
1616
createSimpleExpression,
1717
getBaseTransformPreset,
@@ -39,7 +39,7 @@ import {
3939
processChildren,
4040
processChildrenAsStatement
4141
} from '../ssrCodegenTransform'
42-
import { ssrProcessPortal } from './ssrTransformPortal'
42+
import { ssrProcessTeleport } from './ssrTransformTeleport'
4343
import {
4444
ssrProcessSuspense,
4545
ssrTransformSuspense
@@ -146,8 +146,8 @@ export function ssrProcessComponent(
146146
if (!node.ssrCodegenNode) {
147147
// this is a built-in component that fell-through.
148148
const component = componentTypeMap.get(node)!
149-
if (component === PORTAL) {
150-
return ssrProcessPortal(node, context)
149+
if (component === TELEPORT) {
150+
return ssrProcessTeleport(node, context)
151151
} else if (component === SUSPENSE) {
152152
return ssrProcessSuspense(node, context)
153153
} else {

packages/compiler-ssr/src/transforms/ssrTransformPortal.ts renamed to packages/compiler-ssr/src/transforms/ssrTransformTeleport.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ import {
1212
processChildrenAsStatement
1313
} from '../ssrCodegenTransform'
1414
import { createSSRCompilerError, SSRErrorCodes } from '../errors'
15-
import { SSR_RENDER_PORTAL } from '../runtimeHelpers'
15+
import { SSR_RENDER_TELEPORT } from '../runtimeHelpers'
1616

1717
// Note: this is a 2nd-pass codegen transform.
18-
export function ssrProcessPortal(
18+
export function ssrProcessTeleport(
1919
node: ComponentNode,
2020
context: SSRTransformContext
2121
) {
2222
const targetProp = findProp(node, 'target')
2323
if (!targetProp) {
2424
context.onError(
25-
createSSRCompilerError(SSRErrorCodes.X_SSR_NO_PORTAL_TARGET, node.loc)
25+
createSSRCompilerError(SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET, node.loc)
2626
)
2727
return
2828
}
@@ -37,7 +37,7 @@ export function ssrProcessPortal(
3737
if (!target) {
3838
context.onError(
3939
createSSRCompilerError(
40-
SSRErrorCodes.X_SSR_NO_PORTAL_TARGET,
40+
SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET,
4141
targetProp.loc
4242
)
4343
)
@@ -60,7 +60,7 @@ export function ssrProcessPortal(
6060
)
6161
contentRenderFn.body = processChildrenAsStatement(node.children, context)
6262
context.pushStatement(
63-
createCallExpression(context.helper(SSR_RENDER_PORTAL), [
63+
createCallExpression(context.helper(SSR_RENDER_TELEPORT), [
6464
`_push`,
6565
contentRenderFn,
6666
target,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -731,5 +731,5 @@ describe('Suspense', () => {
731731
expect(serializeInner(root)).toBe(`<div>Child A</div><div>Child B</div>`)
732732
})
733733

734-
test.todo('portal inside suspense')
734+
test.todo('teleport inside suspense')
735735
})

0 commit comments

Comments
 (0)