Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): handle disabled teleport with updateCssVars #12113

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions packages/runtime-core/src/components/Teleport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export const TeleportImpl = {
}
if (!disabled) {
mount(target, targetAnchor)
updateCssVars(n2)
updateCssVars(n2, false)
}
} else if (__DEV__ && !disabled) {
warn(
Expand All @@ -160,7 +160,7 @@ export const TeleportImpl = {

if (disabled) {
mount(container, mainAnchor)
updateCssVars(n2)
updateCssVars(n2, true)
}

if (isTeleportDeferred(n2.props)) {
Expand Down Expand Up @@ -267,7 +267,7 @@ export const TeleportImpl = {
)
}
}
updateCssVars(n2)
updateCssVars(n2, disabled)
}
},

Expand Down Expand Up @@ -389,12 +389,13 @@ function hydrateTeleport(
querySelector,
))
if (target) {
const disabled = isTeleportDisabled(vnode.props)
// if multiple teleports rendered to the same target element, we need to
// pick up from where the last teleport finished instead of the first node
const targetNode =
(target as TeleportTargetElement)._lpa || target.firstChild
if (vnode.shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
if (isTeleportDisabled(vnode.props)) {
if (disabled) {
vnode.anchor = hydrateChildren(
nextSibling(node),
vnode,
Expand Down Expand Up @@ -446,7 +447,7 @@ function hydrateTeleport(
)
}
}
updateCssVars(vnode)
updateCssVars(vnode, disabled)
}
return vnode.anchor && nextSibling(vnode.anchor as Node)
}
Expand All @@ -462,13 +463,20 @@ export const Teleport = TeleportImpl as unknown as {
}
}

function updateCssVars(vnode: VNode) {
function updateCssVars(vnode: VNode, isDisabled: boolean) {
// presence of .ut method indicates owner component uses css vars.
// code path here can assume browser environment.
const ctx = vnode.ctx
if (ctx && ctx.ut) {
let node = vnode.targetStart
while (node && node !== vnode.targetAnchor) {
let node, anchor
if (isDisabled) {
node = vnode.el
anchor = vnode.anchor
} else {
node = vnode.targetStart
anchor = vnode.targetAnchor
}
while (node && node !== anchor) {
if (node.nodeType === 1) node.setAttribute('data-v-owner', ctx.uid)
node = node.nextSibling
}
Expand Down
1 change: 1 addition & 0 deletions packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ describe('useCssVars', () => {
expect(() => render(h(App), root)).not.toThrow(TypeError)
await nextTick()
expect(target.children.length).toBe(0)
expect(root.children[0].outerHTML.includes('data-v-owner')).toBe(true)
})

test('with string style', async () => {
Expand Down