Skip to content

Commit 5114dc0

Browse files
committed
fix(dropdown): dropdown not closed on outclick target modal
1 parent 38cfb1d commit 5114dc0

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
} from '@angular/core';
2222
import { DOCUMENT } from '@angular/common';
2323
import { BooleanInput, coerceBooleanProperty } from '@angular/cdk/coercion';
24+
import { FocusMonitor } from '@angular/cdk/a11y';
2425
import { Subscription } from 'rxjs';
2526

2627
import { createPopper, Instance, Options, Placement } from '@popperjs/core';
@@ -33,7 +34,7 @@ export abstract class DropdownToken {}
3334

3435
@Directive({
3536
selector: '[cDropdownToggle]',
36-
providers: [{provide: DropdownToken, useExisting: forwardRef(() => DropdownComponent)}],
37+
providers: [{ provide: DropdownToken, useExisting: forwardRef(() => DropdownComponent) }],
3738
exportAs: 'cDropdownToggle'
3839
})
3940
export class DropdownToggleDirective implements AfterViewInit {
@@ -75,9 +76,11 @@ export class DropdownToggleDirective implements AfterViewInit {
7576
set split(value: boolean) {
7677
this._split = coerceBooleanProperty(value);
7778
}
79+
7880
get split(): boolean {
7981
return this._split;
8082
}
83+
8184
private _split = false;
8285

8386
@HostBinding('class')
@@ -132,9 +135,11 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
132135
set dark(value: boolean) {
133136
this._dark = coerceBooleanProperty(value);
134137
};
138+
135139
get dark() {
136140
return this._dark;
137141
}
142+
138143
private _dark = false;
139144

140145
/**
@@ -158,9 +163,11 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
158163
set popper(value: boolean) {
159164
this._popper = coerceBooleanProperty(value);
160165
}
166+
161167
get popper(): boolean {
162168
return this._popper;
163169
}
170+
164171
private _popper = true;
165172

166173
/**
@@ -171,6 +178,7 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
171178
set popperOptions(value: Partial<Options>) {
172179
this._popperOptions = { ...this._popperOptions, ...value };
173180
};
181+
174182
get popperOptions(): Partial<Options> {
175183
let placement = this.placement;
176184
switch (this.direction) {
@@ -201,14 +209,15 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
201209
this._popperOptions = { ...this._popperOptions, placement: placement };
202210
return this._popperOptions;
203211
}
212+
204213
private _popperOptions: Partial<Options> = {
205214
placement: this.placement,
206215
modifiers: [],
207216
strategy: 'absolute'
208217
};
209218

210219
/**
211-
* Set the dropdown variant to an btn-group, dropdown, input-group, and nav-item.
220+
* Set the dropdown variant to a btn-group, dropdown, input-group, and nav-item.
212221
*/
213222
@Input() variant?: 'btn-group' | 'dropdown' | 'input-group' | 'nav-item' = 'dropdown';
214223

@@ -225,9 +234,11 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
225234
_value ? this.createPopperInstance() : this.destroyPopperInstance();
226235
this.visibleChange.emit(_value);
227236
}
237+
228238
get visible(): boolean {
229239
return this._visible;
230240
}
241+
231242
private _visible = false;
232243

233244
@Output() visibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
@@ -243,11 +254,12 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
243254
private listeners: (() => void)[] = [];
244255

245256
constructor(
246-
@Inject(DOCUMENT) private document: any,
257+
@Inject(DOCUMENT) private document: Document,
247258
private elementRef: ElementRef,
248259
private renderer: Renderer2,
249260
private ngZone: NgZone,
250261
private changeDetectorRef: ChangeDetectorRef,
262+
private focusMonitor: FocusMonitor,
251263
public dropdownService: DropdownService
252264
) {}
253265

@@ -259,7 +271,7 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
259271
!this.direction,
260272
[`${this.direction}`]: !!this.direction,
261273
[`${this.variant}`]: !!this.variant,
262-
'dropup' : this.direction === 'dropup' || this.direction === 'dropup-center',
274+
'dropup': this.direction === 'dropup' || this.direction === 'dropup-center',
263275
show: this.visible
264276
};
265277
}
@@ -308,6 +320,14 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
308320
if (this.variant === 'nav-item') {
309321
this.renderer.addClass(this._toggler.elementRef.nativeElement, 'nav-link');
310322
}
323+
this.focusMonitor.monitor(this.elementRef, true).subscribe(origin => {
324+
this.ngZone.run(() => {
325+
if ((!origin) && (this.autoClose === true || this.autoClose === 'outside')) {
326+
this.setVisibleState(false);
327+
this.changeDetectorRef.markForCheck();
328+
}
329+
});
330+
});
311331
}
312332

313333
ngOnInit(): void {
@@ -316,6 +336,7 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
316336
}
317337

318338
ngOnDestroy(): void {
339+
this.focusMonitor?.stopMonitoring(this.elementRef);
319340
this.clearListeners();
320341
this.dropdownStateSubscribe(false);
321342
this.destroyPopperInstance();
@@ -341,6 +362,7 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
341362
}
342363
this.ngZone.run(() => {
343364
this.setListeners();
365+
this.changeDetectorRef.markForCheck();
344366
this.changeDetectorRef.detectChanges();
345367
});
346368
});

0 commit comments

Comments
 (0)