{"version":3,"names":["internalReadTask","readTask","isOpen","component","opened","open","onToggleOpenCloseComponent","transitionEl","whenTransitionDone","openTransitionProp","onBeforeOpen","onBeforeClose","onOpen","onClose"],"sources":["src/utils/openCloseComponent.ts"],"sourcesContent":["import { readTask } from \"@stencil/core\";\nimport { whenTransitionDone } from \"./dom\";\n\n/**\n * Exported for testing purposes only\n */\nexport const internalReadTask = readTask;\n\n/**\n * Defines interface for components with open/close public emitter.\n * All implementations of this interface must handle the following events: `beforeOpen`, `open`, `beforeClose`, `close`.\n */\nexport interface OpenCloseComponent {\n /**\n * The host element.\n */\n readonly el: HTMLElement;\n\n /**\n * When true, the component opens.\n */\n open?: boolean;\n\n /**\n * When true, the component is open.\n */\n opened?: boolean;\n\n /**\n * Specifies the name of transitionProp.\n */\n transitionProp?: string;\n\n /**\n * Specifies property on which active transition is watched for.\n */\n openTransitionProp: string;\n\n /**\n * Specifies element that the transition is allowed to emit on.\n */\n transitionEl: HTMLElement;\n\n /**\n * Defines method for `beforeOpen` event handler.\n */\n onBeforeOpen: () => void;\n\n /**\n * Defines method for `open` event handler:\n */\n onOpen: () => void;\n\n /**\n * Defines method for `beforeClose` event handler:\n */\n onBeforeClose: () => void;\n\n /**\n * Defines method for `close` event handler:\n */\n onClose: () => void;\n}\n\nfunction isOpen(component: OpenCloseComponent): boolean {\n return \"opened\" in component ? component.opened : component.open;\n}\n\n/**\n * Helper to determine globally set transition duration on the given openTransitionProp, which is imported and set in the @Watch(\"open\").\n * Used to emit (before)open/close events both for when the opacity transition is present and when there is none (transition-duration is set to 0).\n *\n * @example\n * import { onToggleOpenCloseComponent, OpenCloseComponent } from \"../../utils/openCloseComponent\";\n *\n * async componentWillLoad() {\n * // When component initially renders, if `open` was set we need to trigger on load as watcher doesn't fire.\n * if (this.open) {\n * onToggleOpenCloseComponent(this);\n * }\n * @Watch(\"open\")\n * async toggleModal(value: boolean): Promise {\n * onToggleOpenCloseComponent(this);\n * }\n *\n * @param component - OpenCloseComponent uses `open` prop to emit (before)open/close.\n */\nexport function onToggleOpenCloseComponent(component: OpenCloseComponent): void {\n internalReadTask((): void => {\n if (!component.transitionEl) {\n return;\n }\n\n whenTransitionDone(\n component.transitionEl,\n component.openTransitionProp,\n () => {\n if (isOpen(component)) {\n component.onBeforeOpen();\n } else {\n component.onBeforeClose();\n }\n },\n () => {\n if (isOpen(component)) {\n component.onOpen();\n } else {\n component.onClose();\n }\n },\n );\n });\n}\n"],"mappings":";;;;;mEAMO,MAAMA,EAAmBC,EA0DhC,SAASC,EAAOC,GACd,MAAO,WAAYA,EAAYA,EAAUC,OAASD,EAAUE,IAC9D,C,SAqBgBC,EAA2BH,GACzCH,GAAiB,KACf,IAAKG,EAAUI,aAAc,CAC3B,M,CAGFC,EACEL,EAAUI,aACVJ,EAAUM,oBACV,KACE,GAAIP,EAAOC,GAAY,CACrBA,EAAUO,c,KACL,CACLP,EAAUQ,e,KAGd,KACE,GAAIT,EAAOC,GAAY,CACrBA,EAAUS,Q,KACL,CACLT,EAAUU,S,IAGf,GAEL,Q","ignoreList":[]}