Skip to content

Commit 75c8928

Browse files
committed
fix(runtime-vapor): expect css vars on teleport roots when hydrating
1 parent c5c1855 commit 75c8928

2 files changed

Lines changed: 74 additions & 7 deletions

File tree

‎packages/runtime-vapor/__tests__/hydration/teleport.spec.ts‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,5 +1032,54 @@ describe('Vapor Mode hydration', () => {
10321032
await nextTick()
10331033
expect(span.style.getPropertyValue('--color')).toBe('green')
10341034
})
1035+
1036+
test.each([
1037+
{ disabled: false, clientOpacity: 0.5, mismatches: 0 },
1038+
{ disabled: true, clientOpacity: 0.5, mismatches: 0 },
1039+
{ disabled: false, clientOpacity: 0.75, mismatches: 1 },
1040+
{ disabled: true, clientOpacity: 0.75, mismatches: 1 },
1041+
])(
1042+
'hydrates css vars on teleport roots (disabled: $disabled, mismatches: $mismatches)',
1043+
async ({ disabled, clientOpacity, mismatches }) => {
1044+
const data = ref({ disabled, opacity: 0.5, color: 'red' })
1045+
const code = `<script vapor>const data = _data</script>
1046+
<template>
1047+
<Teleport to="#teleport-css-vars-root" :disabled="data.disabled">
1048+
<div :style="{ opacity: data.opacity }">
1049+
<i :style="{ opacity: 0.5 }" />
1050+
</div>
1051+
</Teleport>
1052+
</template>
1053+
<style>div { color: v-bind('data.color') }</style>`
1054+
const ctx = {} as any
1055+
const mainHtml = await VueServerRenderer.renderToString(
1056+
runtimeDom.createSSRApp(
1057+
compileVaporComponent(code, data, undefined, true),
1058+
),
1059+
ctx,
1060+
)
1061+
const target = document.createElement('div')
1062+
target.id = 'teleport-css-vars-root'
1063+
target.innerHTML = ctx.teleports!['#teleport-css-vars-root']
1064+
document.body.appendChild(target)
1065+
1066+
data.value.opacity = clientOpacity
1067+
const { container } = await mountWithHydration(mainHtml, code, data)
1068+
await nextTick()
1069+
expect(`Hydration style mismatch`).toHaveBeenWarnedTimes(mismatches)
1070+
const root = (disabled ? container : target).querySelector('div')!
1071+
const child = root.firstElementChild as HTMLElement
1072+
const cssVar = Array.from(root.style).find(name =>
1073+
name.startsWith('--'),
1074+
)!
1075+
expect(root.style.getPropertyValue(cssVar)).toBe('red')
1076+
expect(child.style.getPropertyValue(cssVar)).toBe('')
1077+
1078+
data.value.color = 'blue'
1079+
await nextTick()
1080+
expect(root.style.getPropertyValue(cssVar)).toBe('blue')
1081+
expect(child.style.getPropertyValue(cssVar)).toBe('')
1082+
},
1083+
)
10351084
})
10361085
})

‎packages/runtime-vapor/src/dom/prop.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ import { type Block, normalizeBlock } from '../block'
7373
import type { VaporElement } from '../apiDefineCustomElement'
7474
import type { RootMeta } from './template'
7575
import { isTransitionEnabled } from '../transition'
76+
import { isTeleportEnabled, isTeleportFragment } from '../teleport'
77+
import { isFragment } from '../fragment'
7678
import { isInteropEnabled } from '../vdomInteropState'
7779
import { currentRenderContext } from '../renderContext'
7880
import { inOnce } from '../once'
@@ -982,8 +984,8 @@ function styleHasMismatch(
982984
expectedStyleMap.set('display', 'none')
983985
}
984986

985-
// handle css vars
986-
if (isVaporComponent(instance)) {
987+
// handle css vars, which only dev builds expose
988+
if (__DEV__ && isVaporComponent(instance)) {
987989
resolveCssVars(instance, el, expectedStyleMap)
988990
}
989991

@@ -1019,9 +1021,11 @@ function resolveCssVars(
10191021
): void {
10201022
if (!instance.isMounted) return
10211023
const rootBlocks = normalizeBlock(instance)
1024+
const nodes = normalizeBlock(block)
1025+
const isRoot = nodes.every(b => rootBlocks.includes(b))
10221026
if (
10231027
(instance as GenericComponentInstance).getCssVars &&
1024-
normalizeBlock(block).every(b => rootBlocks.includes(b))
1028+
(isRoot || isTeleportRoot(instance.block, nodes))
10251029
) {
10261030
const cssVars = (instance as GenericComponentInstance).getCssVars!()
10271031
for (const key in cssVars) {
@@ -1030,10 +1034,7 @@ function resolveCssVars(
10301034
}
10311035
}
10321036

1033-
if (
1034-
normalizeBlock(block).every(b => rootBlocks.includes(b)) &&
1035-
instance.parent
1036-
) {
1037+
if (isRoot && instance.parent) {
10371038
if (isVaporComponent(instance.parent)) {
10381039
resolveCssVars(instance.parent, instance.block, expectedMap)
10391040
} else if (isInteropEnabled && instance.interopVNode) {
@@ -1047,6 +1048,23 @@ function resolveCssVars(
10471048
}
10481049
}
10491050

1051+
/**
1052+
* dev only. The roots of a teleport on the root chain carry the owner's css
1053+
* vars too.
1054+
*/
1055+
function isTeleportRoot(block: Block, nodes: Node[]): boolean {
1056+
if (isArray(block)) {
1057+
return block.some(b => isTeleportRoot(b, nodes))
1058+
}
1059+
if (isTeleportEnabled && isTeleportFragment(block)) {
1060+
const roots = normalizeBlock(block.nodes)
1061+
return (
1062+
nodes.every(n => roots.includes(n)) || isTeleportRoot(block.nodes, nodes)
1063+
)
1064+
}
1065+
return isFragment(block) && isTeleportRoot(block.nodes, nodes)
1066+
}
1067+
10501068
function attributeHasMismatch(el: any, key: string, value: any): boolean {
10511069
if (isValidHtmlOrSvgAttribute(el, key)) {
10521070
const { actual, expected } = getAttributeMismatch(el, key, value)

0 commit comments

Comments
 (0)