Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,20 @@ class FocusReturnComponent {
readonly visible = signal(false);
}

@Component({
template: `
<c-dropdown (visibleChange)="changes.push($event)" [visible]="initial">
<button cDropdownToggle>Toggle</button>
<ul cDropdownMenu></ul>
</c-dropdown>
`,
imports: [DropdownToggleDirective, DropdownComponent, DropdownMenuDirective]
})
class InitialStateComponent {
changes: boolean[] = [];
initial = false;
}

describe('DropdownComponent focus return', () => {
let fixture: ComponentFixture<FocusReturnComponent>;

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)[] = [];
Expand Down
Loading