Skip to content

Commit 11214ee

Browse files
committedNov 10, 2022
fix(teleport/css-v-bind): fix css v-bind in teleport in child component slot
1 parent 42239cf commit 11214ee

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed
 

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

+7-10
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ export const TeleportImpl = {
223223
}
224224
}
225225

226-
updateCssVars(parentComponent, n2)
226+
updateCssVars(n2)
227227
},
228228

229229
remove(
@@ -385,7 +385,7 @@ function hydrateTeleport(
385385
)
386386
}
387387
}
388-
updateCssVars(parentComponent, vnode)
388+
updateCssVars(vnode)
389389
}
390390
return vnode.anchor && nextSibling(vnode.anchor as Node)
391391
}
@@ -396,19 +396,16 @@ export const Teleport = TeleportImpl as unknown as {
396396
new (): { $props: VNodeProps & TeleportProps }
397397
}
398398

399-
function updateCssVars(
400-
parentComponent: ComponentInternalInstance | null,
401-
vnode: VNode
402-
) {
399+
function updateCssVars(vnode: VNode) {
403400
// presence of .ut method indicates owner component uses css vars.
404401
// code path here can assume browser environment.
405-
if (parentComponent && parentComponent.ut) {
402+
const ctx = vnode.ctx
403+
if (ctx && ctx.ut) {
406404
let node = (vnode.children as VNode[])[0].el!
407405
while (node !== vnode.targetAnchor) {
408-
if (node.nodeType === 1)
409-
node.setAttribute('data-v-owner', parentComponent.uid)
406+
if (node.nodeType === 1) node.setAttribute('data-v-owner', ctx.uid)
410407
node = node.nextSibling
411408
}
412-
parentComponent.ut()
409+
ctx.ut()
413410
}
414411
}

‎packages/runtime-core/src/vnode.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,11 @@ export interface VNode<
205205
// application root node only
206206
appContext: AppContext | null
207207

208+
/**
209+
* @internal lexical scope owner instance
210+
*/
211+
ctx: ComponentInternalInstance | null
212+
208213
/**
209214
* @internal attached by v-memo
210215
*/
@@ -439,7 +444,8 @@ function createBaseVNode(
439444
patchFlag,
440445
dynamicProps,
441446
dynamicChildren: null,
442-
appContext: null
447+
appContext: null,
448+
ctx: currentRenderingInstance
443449
} as VNode
444450

445451
if (needFullChildrenNormalization) {
@@ -661,7 +667,8 @@ export function cloneVNode<T, U>(
661667
ssContent: vnode.ssContent && cloneVNode(vnode.ssContent),
662668
ssFallback: vnode.ssFallback && cloneVNode(vnode.ssFallback),
663669
el: vnode.el,
664-
anchor: vnode.anchor
670+
anchor: vnode.anchor,
671+
ctx: vnode.ctx
665672
}
666673
if (__COMPAT__) {
667674
defineLegacyVNodeProperties(cloned as VNode)

‎packages/runtime-dom/__tests__/helpers/useCssVars.spec.ts

+24
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,30 @@ describe('useCssVars', () => {
218218
}
219219
})
220220

221+
test('with teleport in child slot', async () => {
222+
document.body.innerHTML = ''
223+
const state = reactive({ color: 'red' })
224+
const root = document.createElement('div')
225+
const target = document.body
226+
227+
const Child: FunctionalComponent = (_, { slots }) => {
228+
return h('div', slots.default && slots.default())
229+
}
230+
231+
const App = {
232+
setup() {
233+
useCssVars(() => state)
234+
return () => h(Child, () => [h(Teleport, { to: target }, [h('div')])])
235+
}
236+
}
237+
238+
render(h(App), root)
239+
await nextTick()
240+
for (const c of [].slice.call(target.children as any)) {
241+
expect((c as HTMLElement).style.getPropertyValue(`--color`)).toBe('red')
242+
}
243+
})
244+
221245
test('with teleport(change subTree)', async () => {
222246
document.body.innerHTML = ''
223247
const state = reactive({ color: 'red' })

0 commit comments

Comments
 (0)
Please sign in to comment.