CREATE_BLOCK,
FRAGMENT,
CREATE_COMMENT,
- OPEN_BLOCK
+ OPEN_BLOCK,
+ PORTAL
} from '../runtimeHelpers'
import { injectProp } from '../utils'
import { PatchFlags, PatchFlagNames } from '@vue/shared'
vnodeCall.type === NodeTypes.VNODE_CALL &&
// component vnodes are always tracked and its children are
// compiled into slots so no need to make it a block
- (firstChild as ElementNode).tagType !== ElementTypes.COMPONENT
+ ((firstChild as ElementNode).tagType !== ElementTypes.COMPONENT ||
+ // portal has component type but isn't always tracked
+ vnodeCall.tag === PORTAL)
) {
vnodeCall.isBlock = true
helper(OPEN_BLOCK)
expect(serializeInner(target)).toMatchSnapshot()
})
+
+ test('should remove children when unmounted', () => {
+ const target = nodeOps.createElement('div')
+ const root = nodeOps.createElement('div')
+
+ const Comp = defineComponent(() => () => [
+ h(Portal, { target }, h('div', 'teleported')),
+ h('div', 'root')
+ ])
+ render(h(Comp), root)
+ expect(serializeInner(target)).toMatchInlineSnapshot(
+ `"<div>teleported</div>"`
+ )
+
+ render(null, root)
+ expect(serializeInner(target)).toBe('')
+ })
})
// Jest Snapshot v1, https://goo.gl/fbAQLP
+exports[`renderer: portal should remove children when unmounted 1`] = `"<div>teleported</div>"`;
+
exports[`renderer: portal should update children 1`] = `"<div>teleported</div>"`;
exports[`renderer: portal should update children 2`] = `""`;
}
}
}
+ },
+
+ remove(
+ vnode: VNode,
+ { r: remove, o: { setElementText } }: RendererInternals
+ ) {
+ const { target, shapeFlag, children } = vnode
+ if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
+ setElementText(target!, '')
+ } else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
+ for (let i = 0; i < (children as VNode[]).length; i++) {
+ remove((children as VNode[])[i])
+ }
+ }
}
}
> {
p: PatchFn
um: UnmountFn
+ r: RemoveFn
m: MoveFn
mt: MountComponentFn
mc: MountChildrenFn
doRemove?: boolean
) => void
+type RemoveFn = (vnode: VNode) => void
+
type UnmountChildrenFn = (
children: VNode[],
parentComponent: ComponentInternalInstance | null,
unmountChildren(children as VNode[], parentComponent, parentSuspense)
}
+ // an unmounted portal should always remove its children
+ if (shapeFlag & ShapeFlags.PORTAL) {
+ ;(vnode.type as typeof PortalImpl).remove(vnode, internals)
+ }
+
if (doRemove) {
remove(vnode)
}
}
}
- const remove = (vnode: VNode) => {
+ const remove: RemoveFn = vnode => {
const { type, el, anchor, transition } = vnode
if (type === Fragment) {
removeFragment(el!, anchor!)
p: patch,
um: unmount,
m: move,
+ r: remove,
mt: mountComponent,
mc: mountChildren,
pc: patchChildren,