From fdf2960c69f2e494c0abd799daf5be4a18bc5511 Mon Sep 17 00:00:00 2001 From: xidedix Date: Tue, 17 Nov 2026 19:10:35 +0100 Subject: [PATCH] fix(Dropdown): keep the menu shown when visible flips back before the effect runs - the visible effect applies only a changed state: a close and reopen (or an open and close) before it runs keeps the Popper instance, the listeners and the menu, without visibleChange - before: a second Popper instance and listener set, and the menu kept the inline visibility: hidden set for positioning - the menu still follows the current state, since Escape, click outside and Tab close it through the service before the effect runs - activeTrap becomes a read-only getter over the applied state; it was a writable public field, written only by the component and not in the docs --- .../dropdown/dropdown.component.spec.ts | 59 +++++++++++++++++++ .../dropdown/dropdown/dropdown.component.ts | 12 +++- 2 files changed, 69 insertions(+), 2 deletions(-) diff --git a/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.spec.ts b/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.spec.ts index 317cb1fa..25610d05 100644 --- a/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.spec.ts +++ b/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.spec.ts @@ -1277,6 +1277,20 @@ class FocusReturnComponent { readonly visible = signal(false); } +@Component({ + template: ` + + + + + `, + imports: [DropdownToggleDirective, DropdownComponent, DropdownMenuDirective] +}) +class InitialStateComponent { + changes: boolean[] = []; + initial = false; +} + describe('DropdownComponent focus return', () => { let fixture: ComponentFixture; @@ -1374,6 +1388,51 @@ describe('DropdownComponent focus return', () => { expect(document.activeElement).toBe(element('item')); }); + it.each([false, true])('should emit visibleChange once with the initial state %s', async (initial) => { + const initialFixture = TestBed.createComponent(InitialStateComponent); + initialFixture.componentInstance.initial = initial; + await initialFixture.whenStable(); + + expect(initialFixture.componentInstance.changes).toEqual([initial]); + initialFixture.destroy(); + }); + + it('should keep the menu shown with one Popper instance when it closes and opens again in one pass', async () => { + await setVisible(true); + const dropdown: DropdownComponent = fixture.debugElement.query(By.directive(DropdownComponent)).componentInstance; + const menu: HTMLElement = fixture.nativeElement.querySelector('.dropdown-menu'); + const createPopper = vi.spyOn(dropdown, 'createPopperInstance'); + const destroyPopper = vi.spyOn(dropdown, 'destroyPopperInstance'); + const emitted: boolean[] = []; + dropdown.visibleChange.subscribe((visible) => emitted.push(visible)); + + element('item').dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true })); + dropdown.visible.set(true); + await fixture.whenStable(); + + expect(createPopper).not.toHaveBeenCalled(); + expect(destroyPopper).not.toHaveBeenCalled(); + expect(emitted).toEqual([]); + expect(menu.classList).toContain('show'); + expect(menu.style.visibility).toBe(''); + }); + + it('should stay closed without a Popper instance when it opens and closes again in one pass', async () => { + const dropdown: DropdownComponent = fixture.debugElement.query(By.directive(DropdownComponent)).componentInstance; + const menu: HTMLElement = fixture.nativeElement.querySelector('.dropdown-menu'); + const createPopper = vi.spyOn(dropdown, 'createPopperInstance'); + const emitted: boolean[] = []; + dropdown.visibleChange.subscribe((visible) => emitted.push(visible)); + + dropdown.visible.set(true); + dropdown.visible.set(false); + await fixture.whenStable(); + + expect(createPopper).not.toHaveBeenCalled(); + expect(emitted).toEqual([]); + expect(menu.classList).not.toContain('show'); + }); + it('should not return the focus when the window does not have it', async () => { await setVisible(true); element('item').focus(); diff --git a/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.ts b/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.ts index f0ce5034..8779f10e 100644 --- a/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.ts +++ b/projects/coreui-angular/src/lib/dropdown/dropdown/dropdown.component.ts @@ -297,11 +297,17 @@ export class DropdownComponent implements OnDestroy, OnInit { computation: (value) => value }); + #appliedVisible?: boolean; + readonly #visibleEffect = effect(() => { const visible = this.visible(); untracked(() => { + if (visible === this.#appliedVisible) { + this.setVisibleState(visible); + return; + } const closed = this.activeTrap && !visible; - this.activeTrap = visible; + this.#appliedVisible = visible; visible ? this.createPopperInstance() : this.destroyPopperInstance(); this.setVisibleState(visible); this.visibleChange?.emit(visible); @@ -323,7 +329,9 @@ export class DropdownComponent implements OnDestroy, OnInit { readonly _menu = contentChild(DropdownMenuDirective); readonly _menuElementRef = contentChild(DropdownMenuDirective, { read: ElementRef }); - public activeTrap = false; + get activeTrap(): boolean { + return this.#appliedVisible === true; + } private popperInstance!: Instance | undefined; private listeners: (() => void)[] = [];