{
const el = e.target as HTMLDivElement;
if (e.animationName === "dropdownShow") {
el.classList.remove("animateShow");
}
if (e.animationName === "dropdownHide") {
el.classList.add("hidden");
el.classList.remove("animateHide");
this.isActive = false;
}
}}
>
`;
}
private async onFocusout(e: FocusEvent) {
const relatedTarget = e.relatedTarget as HTMLElement;
if (!this.open) {
return;
}
if (
!relatedTarget ||
(!this.anchor?.some((item) => item === relatedTarget) &&
!this.menuItems?.some((item) => item === relatedTarget))
) {
await this.updateComplete;
this.dispatchEvent(new CustomEvent("request-close"));
}
}
private onKeydown(e: KeyboardEvent) {
if (this.open && e.key === "ArrowDown") {
if (this.menu && this.menuItems?.length && !this.menu.getCurrentItem()) {
// Focus on first menu item
e.preventDefault();
const menuItem = this.menuItems[0];
this.menu!.setCurrentItem(menuItem);
menuItem.focus();
}
}
}
private async onKeyup(e: KeyboardEvent) {
if (this.open && e.key === "Escape") {
await this.updateComplete;
this.dispatchEvent(new CustomEvent("request-close"));
}
}
private async openDropdown() {
this.isActive = true;
await this.combobox?.updateComplete;
// Manually sync dropdown width instead of using `sync="width"`
// to get around ResizeObserver loop error
if (this.anchor?.length && this.dropdown) {
const anchorWidth = this.anchor[0].clientWidth;
if (anchorWidth) {
this.dropdown.style.width = `${anchorWidth}px`;
}
}
this.dropdown?.classList.add("animateShow");
this.dropdown?.classList.remove("hidden");
}
private closeDropdown() {
this.dropdown?.classList.add("animateHide");
}
}