@@ -8,7 +8,7 @@ import { isSameDomain, matchDomain, getDomainFromUrl, isBlankDomain,
8
8
import { ZalgoPromise } from 'zalgo-promise/src' ;
9
9
import { addEventListener , uniqueID , elementReady , writeElementToWindow , eventEmitter , type EventEmitterType ,
10
10
noop , onResize , extendUrl , appendChild , cleanup , type CleanupType , base64encode ,
11
- once , stringifyError , destroyElement , getElementSafe } from 'belter/src' ;
11
+ once , stringifyError , destroyElement , getElementSafe , showElement , hideElement } from 'belter/src' ;
12
12
13
13
import { ZOID , POST_MESSAGE , CONTEXT , EVENT ,
14
14
INITIAL_PROPS , WINDOW_REFERENCES } from '../constants' ;
@@ -42,7 +42,9 @@ export type ParentExportsType<P> = {|
42
42
close : ( ) => ZalgoPromise < void > ,
43
43
checkClose : CrossDomainFunctionType < [ ] , void> ,
44
44
resize : CrossDomainFunctionType < [ { width ? : ?number , height ? : ?number } ] , void> ,
45
- onError : ( mixed ) => ZalgoPromise < void >
45
+ onError : ( mixed ) => ZalgoPromise < void > ,
46
+ show : ( ) => ZalgoPromise < void > ,
47
+ hide : ( ) => ZalgoPromise < void >
46
48
| } ;
47
49
48
50
export type PropRef =
@@ -74,7 +76,9 @@ export type ParentHelpers<P> = {|
74
76
resize : ( { width : ?number , height : ?number } ) => ZalgoPromise < void > ,
75
77
onError : ( mixed ) => ZalgoPromise < void > ,
76
78
updateProps : PropsInputType < P > => ZalgoPromise < void > ,
77
- event : EventEmitterType
79
+ event : EventEmitterType ,
80
+ show : ( ) => ZalgoPromise < void > ,
81
+ hide : ( ) => ZalgoPromise < void >
78
82
| } ;
79
83
80
84
export class ParentComponent < P > {
@@ -91,8 +95,11 @@ export class ParentComponent<P> {
91
95
state : StateType
92
96
child : ?ChildExportsType < P >
93
97
98
+ proxyContainer : ?ProxyObject < HTMLElement >
94
99
proxyWin : ?ProxyWindow
95
100
101
+ visible : boolean = true
102
+
96
103
constructor ( component : Component < P > , props : PropsInputType < P > ) {
97
104
this. initPromise = new ZalgoPromise ( ) ;
98
105
this . handledErrors = [ ] ;
@@ -163,7 +170,10 @@ export class ParentComponent<P> {
163
170
const openPrerenderFrame = this . openPrerenderFrame ( ) ;
164
171
165
172
const renderContainer = ZalgoPromise . hash ( { proxyContainer : getProxyContainer , proxyFrame : openFrame , proxyPrerenderFrame : openPrerenderFrame } ) . then ( ( { proxyContainer, proxyFrame, proxyPrerenderFrame } ) => {
166
- return this . renderContainer ( proxyContainer , { context, uid, proxyFrame, proxyPrerenderFrame } ) ;
173
+ return this . renderContainer ( proxyContainer , { context, uid, proxyFrame, proxyPrerenderFrame, visible : this . visible } ) ;
174
+ } ) . then ( proxyContainer => {
175
+ this . proxyContainer = proxyContainer ;
176
+ return proxyContainer ;
167
177
} ) ;
168
178
169
179
const open = this . driver . openOnClick
@@ -294,10 +304,32 @@ export class ParentComponent<P> {
294
304
focus : ( ) => this . focus ( ) ,
295
305
resize : ( { width, height } ) => this . resize ( { width, height } ) ,
296
306
onError : ( err ) => this . onError ( err ) ,
297
- updateProps : ( props ) => this . updateProps ( props )
307
+ updateProps : ( props ) => this . updateProps ( props ) ,
308
+ show : ( ) => this . show ( ) ,
309
+ hide : ( ) => this . hide ( )
298
310
} ;
299
311
}
300
312
313
+ show ( ) : ZalgoPromise < void > {
314
+ return ZalgoPromise . try ( ( ) => {
315
+ this . visible = true ;
316
+ if ( this . proxyContainer ) {
317
+ return this . proxyContainer . get ( )
318
+ . then ( showElement ) ;
319
+ }
320
+ } ) ;
321
+ }
322
+
323
+ hide ( ) : ZalgoPromise < void > {
324
+ return ZalgoPromise . try ( ( ) => {
325
+ this . visible = false ;
326
+ if ( this . proxyContainer ) {
327
+ return this . proxyContainer . get ( )
328
+ . then ( hideElement ) ;
329
+ }
330
+ } ) ;
331
+ }
332
+
301
333
setProps ( props : PropsInputType < P > , isUpdate : boolean = false ) {
302
334
if ( this . component . validate ) {
303
335
this . component . validate ( { props } ) ;
@@ -521,10 +553,12 @@ export class ParentComponent<P> {
521
553
const close = ( ) => this . close ( ) ;
522
554
const checkClose = ( ) => this . checkClose ( win ) ;
523
555
const resize = ( { width, height } ) => this . resize ( { width, height } ) ;
556
+ const show = ( ) => this . show ( ) ;
557
+ const hide = ( ) => this . hide ( ) ;
524
558
525
559
init . onError = onError ;
526
560
527
- return { init, close, checkClose, resize, onError } ;
561
+ return { init, close, checkClose, resize, onError, show , hide } ;
528
562
}
529
563
530
564
resize ( { width, height } : { width ? : ?number , height ? : ?number } ) : ZalgoPromise < void > {
@@ -625,8 +659,8 @@ export class ParentComponent<P> {
625
659
} ) ;
626
660
}
627
661
628
- renderContainer ( proxyContainer : ProxyObject < HTMLElement > , { proxyFrame, proxyPrerenderFrame, context, uid } :
629
- { context : $Values < typeof CONTEXT > , uid : string , proxyFrame : ?ProxyObject < HTMLIFrameElement > , proxyPrerenderFrame : ?ProxyObject < HTMLIFrameElement > } ) : ZalgoPromise < ?ProxyObject < HTMLElement >> {
662
+ renderContainer ( proxyContainer : ProxyObject < HTMLElement > , { proxyFrame, proxyPrerenderFrame, context, uid, visible } :
663
+ { context : $Values < typeof CONTEXT > , uid : string , proxyFrame : ?ProxyObject < HTMLIFrameElement > , proxyPrerenderFrame : ?ProxyObject < HTMLIFrameElement > , visible : boolean } ) : ZalgoPromise < ?ProxyObject < HTMLElement >> {
630
664
631
665
632
666
return ZalgoPromise . hash ( {
@@ -638,8 +672,12 @@ export class ParentComponent<P> {
638
672
} ) . then ( ( { container, frame, prerenderFrame } ) => {
639
673
const innerContainer = this . renderTemplate ( this . component . containerTemplate , { context, uid, container, frame, prerenderFrame, doc : document } ) ;
640
674
if ( innerContainer ) {
675
+ if ( ! visible ) {
676
+ hideElement ( innerContainer ) ;
677
+ }
641
678
appendChild ( container , innerContainer ) ;
642
679
this . clean . register ( ( ) => destroyElement ( innerContainer ) ) ;
680
+ this . proxyContainer = getProxyObject ( innerContainer ) ;
643
681
return getProxyObject ( innerContainer ) ;
644
682
}
645
683
} ) ;
0 commit comments