Skip to content

Commit fa68c23

Browse files
committed
refactor(modal): minor syntax cleanup
1 parent 45c2f74 commit fa68c23

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

projects/coreui-angular/src/lib/modal/modal/modal.component.ts

+29-26
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import { ModalDialogComponent } from '../modal-dialog/modal-dialog.component';
5454
imports: [ModalDialogComponent, ModalContentComponent, A11yModule]
5555
})
5656
export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
57-
5857
#destroyRef = inject(DestroyRef);
5958
#focusMonitor = inject(FocusMonitor);
6059

@@ -64,7 +63,7 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
6463
private hostElement: ElementRef,
6564
private modalService: ModalService,
6665
private backdropService: BackdropService
67-
) { }
66+
) {}
6867

6968
/**
7069
* Align the modal in the center or top of the screen.
@@ -90,34 +89,40 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
9089
* @default true
9190
*/
9291
@Input({ transform: booleanAttribute }) keyboard: boolean = true;
92+
9393
@Input() id?: string;
94+
9495
/**
9596
* Size the component small, large, or extra large.
9697
*/
9798
@Input() size?: 'sm' | 'lg' | 'xl';
99+
98100
/**
99101
* Remove animation to create modal that simply appear rather than fade in to view.
100102
*/
101103
@Input({ transform: booleanAttribute }) transition = true;
104+
102105
/**
103106
* Default role for modal. [docs]
104107
* @type string
105108
* @default 'dialog'
106109
*/
107110
@Input() @HostBinding('attr.role') role: string = 'dialog';
111+
108112
/**
109113
* Set aria-modal html attr for modal. [docs]
110114
* @type boolean
111115
* @default null
112116
*/
113-
@Input() @HostBinding('attr.aria-modal')
117+
@Input()
118+
@HostBinding('attr.aria-modal')
114119
set ariaModal(value: boolean | null) {
115120
this.#ariaModal = value;
116121
}
117122

118123
get ariaModal(): boolean | null {
119124
return this.visible || this.#ariaModal ? true : null;
120-
};
125+
}
121126

122127
#ariaModal: boolean | null = null;
123128

@@ -154,8 +159,12 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
154159
this.#activeElement = this.document.activeElement as HTMLElement;
155160
// this.#activeElement?.blur();
156161
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+
}
159168
});
160169
} else {
161170
if (this.document.contains(this.#activeElement)) {
@@ -191,7 +200,7 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
191200
@HostBinding('attr.aria-hidden')
192201
get ariaHidden(): boolean | null {
193202
return this.visible ? null : true;
194-
};
203+
}
195204

196205
@HostBinding('attr.tabindex')
197206
get tabIndex(): string | null {
@@ -255,15 +264,13 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
255264

256265
@HostListener('click', ['$event'])
257266
public onClickHandler($event: MouseEvent): void {
258-
259267
if (this.mouseDownTarget !== $event.target) {
260268
this.mouseDownTarget = null;
261269
return;
262270
}
263271

264272
const targetElement = $event.target;
265273
if (targetElement === this.hostElement.nativeElement) {
266-
267274
if (this.backdrop === 'static') {
268275
this.setStaticBackdrop();
269276
return;
@@ -289,27 +296,23 @@ export class ModalComponent implements OnInit, OnDestroy, AfterViewInit {
289296
}
290297

291298
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;
307303
}
308-
);
304+
} else {
305+
if (this.visible) {
306+
this.visible = false;
307+
}
308+
}
309+
});
309310
}
310311

311312
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);
313316
}
314317

315318
private setBodyStyles(open: boolean): void {

0 commit comments

Comments
 (0)