@@ -30,22 +30,22 @@ export const VaporTeleportImpl = {
3030 ? new TeleportFragment ( 'teleport' )
3131 : new TeleportFragment ( )
3232
33- const resolvedProps = new Proxy (
34- props ,
35- rawPropsProxyHandlers ,
36- ) as any as TeleportProps
37-
3833 let children : Block
39-
4034 renderEffect ( ( ) => {
4135 frag . updateChildren (
4236 ( children = slots . default && ( slots . default as BlockFn ) ( ) ) ,
4337 )
4438 } )
4539
4640 renderEffect ( ( ) => {
47- // access the props to trigger tracking
48- frag . update ( extend ( { } , resolvedProps ) , children ! )
41+ frag . update (
42+ // access the props to trigger tracking
43+ extend (
44+ { } ,
45+ new Proxy ( props , rawPropsProxyHandlers ) as any as TeleportProps ,
46+ ) ,
47+ children ! ,
48+ )
4949 } )
5050
5151 return frag
@@ -54,89 +54,98 @@ export const VaporTeleportImpl = {
5454
5555export class TeleportFragment extends VaporFragment {
5656 anchor : Node
57- targetStart ?: Node | null
58- mainAnchor ?: Node
59- placeholder ?: Node
60- container ?: ParentNode | null
61- currentAnchor ?: Node | null
57+
58+ private targetStart ?: Node
59+ private mainAnchor ?: Node
60+ private placeholder ?: Node
61+ private mountContainer ?: ParentNode | null
62+ private mountAnchor ?: Node | null
6263
6364 constructor ( anchorLabel ?: string ) {
6465 super ( [ ] )
6566 this . anchor =
6667 __DEV__ && anchorLabel ? createComment ( anchorLabel ) : createTextNode ( )
6768 }
6869
69- updateChildren ( children : Block ) : void {
70- const parent = this . anchor . parentNode
71- if ( ! parent ) return
70+ get currentParent ( ) : ParentNode {
71+ return ( this . mountContainer || this . parent ) !
72+ }
7273
73- const container = this . container || parent
74+ get currentAnchor ( ) : Node | null {
75+ return this . mountAnchor || this . anchor
76+ }
7477
75- // teardown previous
76- remove ( this . nodes , container )
78+ get parent ( ) : ParentNode | null {
79+ return this . anchor . parentNode
80+ }
81+
82+ updateChildren ( children : Block ) : void {
83+ // not mounted yet, early return
84+ if ( ! this . parent ) return
7785
78- insert (
79- ( this . nodes = children ) ,
80- container ,
81- this . currentAnchor || this . anchor ,
82- )
86+ // teardown previous children
87+ remove ( this . nodes , this . currentParent )
88+
89+ // mount new
90+ insert ( ( this . nodes = children ) , this . currentParent , this . currentAnchor )
8391 }
8492
8593 update ( props : TeleportProps , children : Block ) : void {
86- const parent = this . anchor . parentNode
8794 this . nodes = children
88- const disabled = isTeleportDisabled ( props )
8995
9096 const mount = ( parent : ParentNode , anchor : Node | null ) => {
9197 insert (
9298 this . nodes ,
93- ( this . container = parent ) ,
94- ( this . currentAnchor = anchor ) ,
99+ ( this . mountContainer = parent ) ,
100+ ( this . mountAnchor = anchor ) ,
95101 )
96102 }
97103
98104 const mountToTarget = ( ) => {
99105 const target = ( this . target = resolveTarget ( props , querySelector ) )
100106 if ( target ) {
101107 if (
102- // initial mount
103- ! this . targetStart ||
108+ // initial mount into target
109+ ! this . targetAnchor ||
104110 // target changed
105- this . targetStart . parentNode !== target
111+ this . targetAnchor . parentNode !== target
106112 ) {
107113 ; [ this . targetAnchor , this . targetStart ] = prepareAnchor ( target )
108114 }
109115
110116 mount ( target , this . targetAnchor ! )
111117 } else if ( __DEV__ ) {
112118 warn (
113- `Invalid Teleport target on ${ this . targetStart ? 'update' : 'mount' } :` ,
119+ `Invalid Teleport target on ${ this . targetAnchor ? 'update' : 'mount' } :` ,
114120 target ,
115121 `(${ typeof target } )` ,
116122 )
117123 }
118124 }
119125
120- if ( parent && disabled ) {
121- if ( ! this . mainAnchor ) {
122- this . mainAnchor = __DEV__
123- ? createComment ( 'teleport end' )
124- : createTextNode ( )
125- }
126- if ( ! this . placeholder ) {
127- this . placeholder = __DEV__
128- ? createComment ( 'teleport start' )
129- : createTextNode ( )
130- }
131- if ( ! this . mainAnchor . isConnected ) {
132- insert ( this . placeholder , parent , this . anchor )
133- insert ( this . mainAnchor , parent , this . anchor )
134- }
126+ // mount into main container
127+ if ( isTeleportDisabled ( props ) ) {
128+ if ( this . parent ) {
129+ if ( ! this . mainAnchor ) {
130+ this . mainAnchor = __DEV__
131+ ? createComment ( 'teleport end' )
132+ : createTextNode ( )
133+ }
134+ if ( ! this . placeholder ) {
135+ this . placeholder = __DEV__
136+ ? createComment ( 'teleport start' )
137+ : createTextNode ( )
138+ }
139+ if ( ! this . mainAnchor . isConnected ) {
140+ insert ( this . placeholder , this . parent , this . anchor )
141+ insert ( this . mainAnchor , this . parent , this . anchor )
142+ }
135143
136- mount ( parent , this . mainAnchor )
144+ mount ( this . parent , this . mainAnchor )
145+ }
137146 }
138-
139- if ( ! disabled ) {
147+ // mount into target container
148+ else {
140149 if ( isTeleportDeferred ( props ) ) {
141150 queuePostFlushCb ( mountToTarget )
142151 } else {
@@ -147,13 +156,12 @@ export class TeleportFragment extends VaporFragment {
147156
148157 remove = ( parent : ParentNode | undefined ) : void => {
149158 // remove nodes
150- remove ( this . nodes , this . container || parent )
159+ remove ( this . nodes , this . currentParent )
151160
152161 // remove anchors
153162 if ( this . targetStart ) {
154- let parentNode = this . targetStart . parentNode !
155- remove ( this . targetStart ! , parentNode )
156- remove ( this . targetAnchor ! , parentNode )
163+ remove ( this . targetStart ! , this . target ! )
164+ remove ( this . targetAnchor ! , this . target ! )
157165 }
158166 if ( this . placeholder ) {
159167 remove ( this . placeholder ! , parent )
@@ -167,12 +175,11 @@ export class TeleportFragment extends VaporFragment {
167175}
168176
169177function prepareAnchor ( target : ParentNode | null ) {
170- const targetStart = createTextNode ( '' )
178+ const targetStart = createTextNode ( '' ) as Text & { [ TeleportEndKey ] : Node }
171179 const targetAnchor = createTextNode ( '' )
172180
173181 // attach a special property, so we can skip teleported content in
174182 // renderer's nextSibling search
175- // @ts -expect-error
176183 targetStart [ TeleportEndKey ] = targetAnchor
177184
178185 if ( target ) {
0 commit comments