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