@@ -37,7 +37,7 @@ import { ModalContentComponent } from '../modal-content/modal-content.component'
37
37
display : 'none'
38
38
} )
39
39
) ,
40
- transition ( 'visible <=> *' , [ animate ( '300ms' ) ] , )
40
+ transition ( 'visible <=> *' , [ animate ( '300ms' ) ] )
41
41
] )
42
42
] ,
43
43
templateUrl : './modal.component.html' ,
@@ -50,7 +50,7 @@ export class ModalComponent implements OnInit, OnDestroy {
50
50
static ngAcceptInputType_visible : BooleanInput ;
51
51
52
52
constructor (
53
- @Inject ( DOCUMENT ) private document : any ,
53
+ @Inject ( DOCUMENT ) private document : Document ,
54
54
private renderer : Renderer2 ,
55
55
private hostElement : ElementRef ,
56
56
private modalService : ModalService ,
@@ -111,9 +111,11 @@ export class ModalComponent implements OnInit, OnDestroy {
111
111
set scrollable ( value : boolean ) {
112
112
this . _scrollable = coerceBooleanProperty ( value ) ;
113
113
}
114
+
114
115
get scrollable ( ) : boolean {
115
116
return this . _scrollable ;
116
117
}
118
+
117
119
private _scrollable = false ;
118
120
119
121
/**
@@ -123,35 +125,36 @@ export class ModalComponent implements OnInit, OnDestroy {
123
125
@Input ( )
124
126
set visible ( value : boolean ) {
125
127
const newValue = coerceBooleanProperty ( value ) ;
126
- if ( this . _visible !== newValue ) {
128
+ if ( this . _visible !== newValue ) {
127
129
this . _visible = newValue ;
128
130
this . setBackdrop ( this . backdrop !== false && newValue ) ;
129
131
this . setBodyStyles ( newValue ) ;
130
132
this . visibleChange . emit ( newValue ) ;
131
133
}
132
134
}
135
+
133
136
get visible ( ) : boolean {
134
137
return this . _visible ;
135
138
}
139
+
136
140
private _visible ! : boolean ;
137
141
138
142
/**
139
143
* Event triggered on modal dismiss.
140
144
*/
141
145
@Output ( ) visibleChange = new EventEmitter < boolean > ( ) ;
142
146
143
- @ViewChild ( ModalContentComponent , { read : ElementRef } ) modalContent ! : ElementRef ;
147
+ @ViewChild ( ModalContentComponent , { read : ElementRef } ) modalContent ! : ElementRef ;
144
148
private activeBackdrop ! : any ;
145
149
private stateToggleSubscription ! : Subscription ;
146
150
private inBoundingClientRect ! : boolean ;
147
151
148
-
149
152
@HostBinding ( 'class' )
150
153
get hostClasses ( ) : any {
151
154
return {
152
155
modal : true ,
153
156
fade : this . transition ,
154
- show : this . show ,
157
+ show : this . show
155
158
} ;
156
159
}
157
160
@@ -170,14 +173,16 @@ export class ModalComponent implements OnInit, OnDestroy {
170
173
return this . visible ? 'visible' : 'hidden' ;
171
174
}
172
175
173
- private _show = true ;
174
176
get show ( ) : boolean {
175
177
return this . visible && this . _show ;
176
178
}
179
+
177
180
set show ( value : boolean ) {
178
181
this . _show = value ;
179
182
}
180
183
184
+ private _show = true ;
185
+
181
186
private get scrollbarWidth ( ) {
182
187
// https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
183
188
const documentWidth = this . document . documentElement . clientWidth ;
@@ -187,28 +192,21 @@ export class ModalComponent implements OnInit, OnDestroy {
187
192
188
193
@HostListener ( '@showHide.start' , [ '$event' ] )
189
194
animateStart ( event : AnimationEvent ) {
190
- this . renderer . setStyle ( this . hostElement . nativeElement , 'overflow-y' , 'hidden' ) ;
191
- this . show = this . visible ;
195
+ const scrollbarWidth = this . scrollbarWidth ;
196
+ if ( event . toState === 'visible' ) {
197
+ this . renderer . setStyle ( this . document . body , 'overflow' , 'hidden' ) ;
198
+ this . renderer . setStyle ( this . document . body , 'padding-right' , scrollbarWidth ) ;
199
+ }
192
200
}
193
201
194
202
@HostListener ( '@showHide.done' , [ '$event' ] )
195
203
animateDone ( event : AnimationEvent ) {
196
- const scrollbarWidth = this . scrollbarWidth ;
197
204
setTimeout ( ( ) => {
198
- if ( event . toState === 'visible' ) {
199
- this . inBoundingClientRect = this . modalContent ?. nativeElement ?. getBoundingClientRect ( ) . height <= this . document ?. documentElement . clientHeight ;
200
- if ( ! this . inBoundingClientRect ) {
201
- this . renderer . setStyle ( this . document . body , 'overflow-y' , 'hidden' ) ;
202
- this . renderer . setStyle ( this . document . body , 'padding-right' , scrollbarWidth ) ;
203
- } else {
204
- this . renderer . removeStyle ( this . document . body , 'overflow-y' ) ;
205
- this . renderer . removeStyle ( this . document . body , 'padding-right' ) ;
206
- }
207
- if ( this . fullscreen === true ) {
208
- this . renderer . setStyle ( this . document . body , 'overflow-y' , 'hidden' ) ;
209
- }
210
- this . renderer . removeStyle ( this . hostElement . nativeElement , 'overflow-y' ) ;
205
+ if ( event . toState === 'hidden' ) {
206
+ this . renderer . removeStyle ( this . document . body , 'overflow' ) ;
207
+ this . renderer . removeStyle ( this . document . body , 'padding-right' ) ;
211
208
}
209
+ this . show = this . visible ;
212
210
} ) ;
213
211
}
214
212
@@ -218,7 +216,7 @@ export class ModalComponent implements OnInit, OnDestroy {
218
216
if ( this . backdrop === 'static' ) {
219
217
this . setStaticBackdrop ( ) ;
220
218
} else {
221
- this . modalService . toggle ( { show : false , modal : this } ) ;
219
+ this . modalService . toggle ( { show : false , modal : this } ) ;
222
220
}
223
221
}
224
222
}
@@ -241,15 +239,15 @@ export class ModalComponent implements OnInit, OnDestroy {
241
239
this . setStaticBackdrop ( ) ;
242
240
return ;
243
241
}
244
- this . modalService . toggle ( { show : false , modal : this } ) ;
242
+ this . modalService . toggle ( { show : false , modal : this } ) ;
245
243
}
246
244
247
245
ngOnInit ( ) : void {
248
246
this . stateToggleSubscribe ( ) ;
249
247
}
250
248
251
249
ngOnDestroy ( ) : void {
252
- this . modalService . toggle ( { show : false , modal : this } ) ;
250
+ this . modalService . toggle ( { show : false , modal : this } ) ;
253
251
this . stateToggleSubscribe ( false ) ;
254
252
}
255
253
@@ -283,16 +281,11 @@ export class ModalComponent implements OnInit, OnDestroy {
283
281
284
282
private setBodyStyles ( open : boolean ) : void {
285
283
if ( open ) {
286
- if ( this . fullscreen === true ) {
287
- this . renderer . setStyle ( this . document . body , 'overflow-y' , 'hidden' ) ;
288
- }
289
284
if ( this . backdrop === true ) {
290
285
this . renderer . addClass ( this . document . body , 'modal-open' ) ;
291
286
}
292
287
} else {
293
288
this . renderer . removeClass ( this . document . body , 'modal-open' ) ;
294
- this . renderer . removeStyle ( this . document . body , 'overflow-y' ) ;
295
- this . renderer . removeStyle ( this . document . body , 'padding-right' ) ;
296
289
}
297
290
}
298
291
@@ -303,7 +296,7 @@ export class ModalComponent implements OnInit, OnDestroy {
303
296
setTimeout ( ( ) => {
304
297
this . renderer . removeClass ( this . hostElement . nativeElement , 'modal-static' ) ;
305
298
this . renderer . removeStyle ( this . hostElement . nativeElement , 'overflow-y' ) ;
306
- } , 400 ) ;
299
+ } , 300 ) ;
307
300
}
308
301
}
309
302
}
0 commit comments