@@ -54,7 +54,6 @@ import { ModalDialogComponent } from '../modal-dialog/modal-dialog.component';
54
54
imports : [ ModalDialogComponent , ModalContentComponent , A11yModule ]
55
55
} )
56
56
export class ModalComponent implements OnInit , OnDestroy , AfterViewInit {
57
-
58
57
#destroyRef = inject ( DestroyRef ) ;
59
58
#focusMonitor = inject ( FocusMonitor ) ;
60
59
@@ -64,7 +63,7 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
64
63
private hostElement : ElementRef ,
65
64
private modalService : ModalService ,
66
65
private backdropService : BackdropService
67
- ) { }
66
+ ) { }
68
67
69
68
/**
70
69
* Align the modal in the center or top of the screen.
@@ -90,34 +89,40 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
90
89
* @default true
91
90
*/
92
91
@Input ( { transform : booleanAttribute } ) keyboard : boolean = true ;
92
+
93
93
@Input ( ) id ?: string ;
94
+
94
95
/**
95
96
* Size the component small, large, or extra large.
96
97
*/
97
98
@Input ( ) size ?: 'sm' | 'lg' | 'xl' ;
99
+
98
100
/**
99
101
* Remove animation to create modal that simply appear rather than fade in to view.
100
102
*/
101
103
@Input ( { transform : booleanAttribute } ) transition = true ;
104
+
102
105
/**
103
106
* Default role for modal. [docs]
104
107
* @type string
105
108
* @default 'dialog'
106
109
*/
107
110
@Input ( ) @HostBinding ( 'attr.role' ) role : string = 'dialog' ;
111
+
108
112
/**
109
113
* Set aria-modal html attr for modal. [docs]
110
114
* @type boolean
111
115
* @default null
112
116
*/
113
- @Input ( ) @HostBinding ( 'attr.aria-modal' )
117
+ @Input ( )
118
+ @HostBinding ( 'attr.aria-modal' )
114
119
set ariaModal ( value : boolean | null ) {
115
120
this . #ariaModal = value ;
116
121
}
117
122
118
123
get ariaModal ( ) : boolean | null {
119
124
return this . visible || this . #ariaModal ? true : null ;
120
- } ;
125
+ }
121
126
122
127
#ariaModal: boolean | null = null ;
123
128
@@ -154,8 +159,12 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
154
159
this . #activeElement = this . document . activeElement as HTMLElement ;
155
160
// this.#activeElement?.blur();
156
161
setTimeout ( ( ) => {
157
- const focusable = this . modalContentRef . nativeElement . querySelectorAll ( '[tabindex]:not([tabindex="-1"]), button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled])' ) ;
158
- this . #focusMonitor. focusVia ( focusable [ 0 ] , 'keyboard' ) ;
162
+ const focusable = this . modalContentRef . nativeElement . querySelectorAll (
163
+ '[tabindex]:not([tabindex="-1"]), button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled])'
164
+ ) ;
165
+ if ( focusable . length ) {
166
+ this . #focusMonitor. focusVia ( focusable [ 0 ] , 'keyboard' ) ;
167
+ }
159
168
} ) ;
160
169
} else {
161
170
if ( this . document . contains ( this . #activeElement) ) {
@@ -191,7 +200,7 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
191
200
@HostBinding ( 'attr.aria-hidden' )
192
201
get ariaHidden ( ) : boolean | null {
193
202
return this . visible ? null : true ;
194
- } ;
203
+ }
195
204
196
205
@HostBinding ( 'attr.tabindex' )
197
206
get tabIndex ( ) : string | null {
@@ -255,15 +264,13 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
255
264
256
265
@HostListener ( 'click' , [ '$event' ] )
257
266
public onClickHandler ( $event : MouseEvent ) : void {
258
-
259
267
if ( this . mouseDownTarget !== $event . target ) {
260
268
this . mouseDownTarget = null ;
261
269
return ;
262
270
}
263
271
264
272
const targetElement = $event . target ;
265
273
if ( targetElement === this . hostElement . nativeElement ) {
266
-
267
274
if ( this . backdrop === 'static' ) {
268
275
this . setStaticBackdrop ( ) ;
269
276
return ;
@@ -289,27 +296,23 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
289
296
}
290
297
291
298
private stateToggleSubscribe ( ) : void {
292
- this . modalService . modalState$
293
- . pipe (
294
- takeUntilDestroyed ( this . #destroyRef)
295
- )
296
- . subscribe (
297
- ( action ) => {
298
- if ( this === action . modal || this . id === action . id ) {
299
- if ( 'show' in action ) {
300
- this . visible = action ?. show === 'toggle' ? ! this . visible : action . show ;
301
- }
302
- } else {
303
- if ( this . visible ) {
304
- this . visible = false ;
305
- }
306
- }
299
+ this . modalService . modalState$ . pipe ( takeUntilDestroyed ( this . #destroyRef) ) . subscribe ( ( action ) => {
300
+ if ( this === action . modal || this . id === action . id ) {
301
+ if ( 'show' in action ) {
302
+ this . visible = action ?. show === 'toggle' ? ! this . visible : action . show ;
307
303
}
308
- ) ;
304
+ } else {
305
+ if ( this . visible ) {
306
+ this . visible = false ;
307
+ }
308
+ }
309
+ } ) ;
309
310
}
310
311
311
312
private setBackdrop ( setBackdrop : boolean ) : void {
312
- this . #activeBackdrop = setBackdrop ? this . backdropService . setBackdrop ( 'modal' ) : this . backdropService . clearBackdrop ( this . #activeBackdrop) ;
313
+ this . #activeBackdrop = setBackdrop
314
+ ? this . backdropService . setBackdrop ( 'modal' )
315
+ : this . backdropService . clearBackdrop ( this . #activeBackdrop) ;
313
316
}
314
317
315
318
private setBodyStyles ( open : boolean ) : void {
0 commit comments