Skip to content

Commit 8433a2b

Browse files
committed
wip: work with teleport
1 parent 03a9bb9 commit 8433a2b

File tree

2 files changed

+52
-3
lines changed

2 files changed

+52
-3
lines changed

packages/runtime-vapor/__tests__/helpers/useCssVars.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
VaporTeleport,
23
createComponent,
34
createIf,
45
createPlainElement,
@@ -160,13 +161,39 @@ describe('useVaporCssVars', () => {
160161
}
161162
})
162163

163-
test.todo('with teleport', async () => {})
164+
test('with teleport', async () => {
165+
const state = reactive({ color: 'red' })
166+
const target = document.createElement('div')
167+
document.body.appendChild(target)
168+
169+
define({
170+
setup() {
171+
useVaporCssVars(() => state)
172+
return createComponent(
173+
VaporTeleport,
174+
{
175+
to: () => target,
176+
},
177+
{
178+
default: () => template('<div></div>', true)(),
179+
},
180+
)
181+
},
182+
}).render()
183+
184+
await nextTick()
185+
expect(target.innerHTML).toBe('<div style="--color: red;"></div>')
186+
187+
state.color = 'green'
188+
await nextTick()
189+
expect(target.innerHTML).toBe('<div style="--color: green;"></div>')
190+
})
164191

165192
test.todo('with teleport in child slot', async () => {})
166193

167-
test('with teleport(change subTree)', async () => {})
194+
test.todo('with teleport(change subTree)', async () => {})
168195

169-
test('with teleport(disabled)', async () => {})
196+
test.todo('with teleport(disabled)', async () => {})
170197

171198
test('with string style', async () => {
172199
const state = reactive({ color: 'red' })

packages/runtime-vapor/src/components/Teleport.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ export class TeleportFragment extends VaporFragment {
162162
}
163163

164164
mount(target, this.targetAnchor!)
165+
updateCssVars(this, false)
165166
} else if (__DEV__) {
166167
warn(
167168
`Invalid Teleport target on ${this.targetAnchor ? 'update' : 'mount'}:`,
@@ -174,6 +175,7 @@ export class TeleportFragment extends VaporFragment {
174175
// mount into main container
175176
if (isTeleportDisabled(this.resolvedProps!)) {
176177
mount(this.parent, this.anchor!)
178+
updateCssVars(this, true)
177179
}
178180
// mount into target container
179181
else {
@@ -330,3 +332,23 @@ function locateTeleportEndAnchor(
330332
}
331333
return null
332334
}
335+
336+
function updateCssVars(frag: TeleportFragment, isDisabled: boolean) {
337+
const ctx = currentInstance as GenericComponentInstance
338+
if (ctx && ctx.ut) {
339+
let node, anchor
340+
if (isDisabled) {
341+
node = frag.placeholder
342+
anchor = frag.anchor
343+
} else {
344+
node = frag.targetStart
345+
anchor = frag.targetAnchor
346+
}
347+
while (node && node !== anchor) {
348+
if (node.nodeType === 1)
349+
(node as Element).setAttribute('data-v-owner', String(ctx.uid))
350+
node = node.nextSibling
351+
}
352+
ctx.ut()
353+
}
354+
}

0 commit comments

Comments
 (0)