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)[] = [];