import{r as s,c as t,a as i,h as e}from"./p-Cby2wKoI.js";import{i as h}from"./p-CtINY9El.js";import{d as n,t as o}from"./p-DNHOWgWh.js";import{c as a}from"./p-BfP9ezJQ.js";const r=()=>`:host{display:inline-block}`;const c=class{constructor(i){s(this,i);this.ifxOpen=t(this,"ifxOpen",7);this.ifxClose=t(this,"ifxClose",7);this.ifxDropdown=t(this,"ifxDropdown",7)}placement="bottom-start";defaultOpen=false;internalIsOpen=false;noAppendToBody=false;ifxOpen;ifxClose;ifxDropdown;disabled;noCloseOnOutsideClick=false;noCloseOnMenuClick=false;get el(){return i(this)}trigger;menu;popperInstance;handleOutsideClick(s){const t=s.target;if(!this.noCloseOnOutsideClick&&!this.el.contains(t)&&!this.menu.contains(t)){this.closeDropdown()}}handleFocusOutside(s){const t=s.target;if(this.internalIsOpen&&!this.el.contains(t)&&!this.menu.contains(t)){this.closeDropdown()}}handleTriggerClick=()=>{if(!this.internalIsOpen)this.openDropdown();else this.closeDropdown()};handleTriggerKeyDown=s=>{const t=s.key;if(t==="Tab"&&this.internalIsOpen&&!s.shiftKey){s.preventDefault();this.focusFirstItem()}};handleMenuClick=()=>{if(!this.noCloseOnMenuClick)this.closeDropdown()};getItemFocusables(){if(!this.menu)return[];const s=Array.from(this.menu.querySelectorAll("ifx-dropdown-item"));return s.filter((s=>!(s.getAttribute("hide")==="true"||s.classList.contains("hide")))).map((s=>s.shadowRoot?.querySelector("a"))).filter((s=>!!s))}handleMenuKeyDown=s=>{const t=this.getItemFocusables();if(!t.length)return;const i=t.indexOf(document.activeElement);switch(s.key){case"ArrowDown":s.preventDefault();t[(i+1)%t.length].focus();break;case"ArrowUp":s.preventDefault();t[(i-1+t.length)%t.length].focus();break;case"Home":s.preventDefault();t[0].focus();break;case"End":s.preventDefault();t[t.length-1].focus();break;case"Enter":case" ":case"Spacebar":s.preventDefault();document.activeElement?.click();break;case"Escape":s.preventDefault();this.closeDropdown();this.trigger?.focus();break;case"Tab":if(s.shiftKey&&i===0){s.preventDefault();this.closeDropdown();this.trigger?.focus()}break}};componentWillLoad(){this.updateSlotContent();this.watchHandlerIsOpen(this.defaultOpen,this.internalIsOpen)}async componentDidLoad(){if(!h(this.el)){const s=n();o("ifx-dropdown",await s)}}watchHandlerIsOpen(s,t){if(s!==t&&s!==this.internalIsOpen){if(s){this.openDropdown()}else{this.closeDropdown()}}}watchHandlerDisabled(s){if(this.trigger){this.trigger.disabled=s}}watchHandlerSlot(){this.updateSlotContent()}focusFirstItem(){const[s]=this.getItemFocusables();s?.focus()}updateSlotContent(){const s=this.el.querySelector("ifx-dropdown-trigger-button, ifx-dropdown-trigger");if(s!==this.trigger){if(this.trigger){this.trigger.removeEventListener("click",this.handleTriggerClick);this.trigger.removeEventListener("keydown",this.handleTriggerKeyDown)}this.trigger=s;if(this.trigger){this.trigger.disabled=this.disabled;this.trigger.addEventListener("click",this.handleTriggerClick);this.trigger.addEventListener("keydown",this.handleTriggerKeyDown)}}const t=this.el.querySelector("ifx-dropdown-menu");if(!this.noAppendToBody){if(this.menu&&this.menu!==t){this.menu.removeEventListener("click",this.handleMenuClick);this.menu.removeEventListener("keydown",this.handleMenuKeyDown);this.menu.remove()}this.menu=t;if(this.menu&&!document.body.contains(this.menu)){document.body.append(this.menu)}}else{this.menu=t}if(this.menu){this.menu.removeEventListener("click",this.handleMenuClick);this.menu.removeEventListener("keydown",this.handleMenuKeyDown);this.menu.addEventListener("click",this.handleMenuClick);this.menu.addEventListener("keydown",this.handleMenuKeyDown)}}disconnectedCallback(){if(this.popperInstance){this.popperInstance.destroy();this.popperInstance=null}if(this.menu){this.menu.remove()}}async isOpen(){return this.internalIsOpen}async closeDropdown(){if(this.internalIsOpen){this.internalIsOpen=false;this.trigger.isOpen=false;this.menu.isOpen=false;this.ifxClose.emit()}if(this.popperInstance){this.popperInstance.destroy();this.popperInstance=null}}async openDropdown(){if(!this.internalIsOpen&&!this.disabled){this.internalIsOpen=true;this.trigger.isOpen=true;this.menu.isOpen=true;this.popperInstance=a(this.el,this.menu,{placement:this.placement});this.ifxOpen.emit()}}render(){return e("div",{key:"7ce42124c9ceabab20d3d7bd4b09035c10c143c5","aria-label":"dropdown menu",class:"dropdown"},e("slot",{key:"da24a57538a33fd2164de34e3a71e95db1123875"}))}static get watchers(){return{defaultOpen:[{watchHandlerIsOpen:0}],disabled:[{watchHandlerDisabled:0}]}}};c.style=r();export{c as ifx_dropdown};
//# sourceMappingURL=p-0c1870fe.entry.js.map