Skip to content

Commit 79d0eb7

Browse files
committed
fix(dropdown): focus lost on click, add close on tabOut
1 parent 8eec97e commit 79d0eb7

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
// todo: find better soulution
1+
// todo: find better solution
22

33
:host-context(.dropdown, .dropup):not(.btn-group) {
44
display: block;
5+
min-width: fit-content;
56
}
67

78
:host-context(.dropstart, .dropend):not(.btn-group) {

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

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ 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';
2524
import { Subscription } from 'rxjs';
2625

2726
import { createPopper, Instance, Options, Placement } from '@popperjs/core';
@@ -118,6 +117,15 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
118117
static ngAcceptInputType_dark: BooleanInput;
119118
static ngAcceptInputType_visible: BooleanInput;
120119

120+
constructor(
121+
@Inject(DOCUMENT) private document: Document,
122+
private elementRef: ElementRef,
123+
private renderer: Renderer2,
124+
private ngZone: NgZone,
125+
private changeDetectorRef: ChangeDetectorRef,
126+
public dropdownService: DropdownService
127+
) {}
128+
121129
/**
122130
* Set alignment of dropdown menu.
123131
* @type {'start' | 'end' | { xs: 'start' | 'end' } | { sm: 'start' | 'end' } | { md: 'start' | 'end' } | { lg: 'start' | 'end' } | { xl: 'start' | 'end'} | { xxl: 'start' | 'end'}}
@@ -246,23 +254,14 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
246254
dropdownContext = { $implicit: this.visible };
247255
@ContentChild(DropdownToggleDirective) _toggler!: DropdownToggleDirective;
248256
@ContentChild(DropdownMenuDirective) _menu!: DropdownMenuDirective;
257+
@ContentChild(DropdownMenuDirective, { read: ElementRef }) _menuElementRef!: ElementRef;
249258

250259
public activeTrap = false;
251260

252261
private dropdownStateSubscription!: Subscription;
253262
private popperInstance!: Instance | undefined;
254263
private listeners: (() => void)[] = [];
255264

256-
constructor(
257-
@Inject(DOCUMENT) private document: Document,
258-
private elementRef: ElementRef,
259-
private renderer: Renderer2,
260-
private ngZone: NgZone,
261-
private changeDetectorRef: ChangeDetectorRef,
262-
private focusMonitor: FocusMonitor,
263-
public dropdownService: DropdownService
264-
) {}
265-
266265
@HostBinding('class')
267266
get hostClasses(): any {
268267
return {
@@ -320,14 +319,6 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
320319
if (this.variant === 'nav-item') {
321320
this.renderer.addClass(this._toggler.elementRef.nativeElement, 'nav-link');
322321
}
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-
});
331322
}
332323

333324
ngOnInit(): void {
@@ -336,7 +327,6 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
336327
}
337328

338329
ngOnDestroy(): void {
339-
this.focusMonitor?.stopMonitoring(this.elementRef);
340330
this.clearListeners();
341331
this.dropdownStateSubscribe(false);
342332
this.destroyPopperInstance();
@@ -380,6 +370,9 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
380370
this.listeners.push(
381371
this.renderer.listen(this.document, 'click', (event) => {
382372
const target = event.target as HTMLElement;
373+
if (this._menuElementRef?.nativeElement.contains(event.target)) {
374+
this.clickedTarget = target;
375+
}
383376
if (this._toggler?.elementRef.nativeElement.contains(event.target)) {
384377
return;
385378
}
@@ -406,6 +399,14 @@ export class DropdownComponent implements AfterContentInit, OnDestroy, OnInit {
406399
}
407400
})
408401
);
402+
this.listeners.push(
403+
this.renderer.listen(this.document, 'keyup', (event) => {
404+
if (event.key === 'Tab' && this.autoClose !== false && !this.elementRef.nativeElement.contains(event.target)) {
405+
this.setVisibleState(false);
406+
return;
407+
}
408+
})
409+
);
409410
}
410411

411412
private clearListeners(): void {

0 commit comments

Comments
 (0)