ion-popover.entry.js 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { r as registerInstance, c as createEvent, h, e as Host, f as getElement } from './index-527b9e34.js';
  5. import { B as BACKDROP, j as prepareOverlay, k as setOverlayId, f as present, n as focusFirstDescendant, g as dismiss, h as eventMethod, F as FOCUS_TRAP_DISABLE_CLASS } from './overlays-d99dcb0a.js';
  6. import { C as CoreDelegate, a as attachComponent, d as detachComponent } from './framework-delegate-56b467ad.js';
  7. import { r as raf, g as getElementRoot, a as addEventListener, k as hasLazyBuild } from './helpers-d94bc8ad.js';
  8. import { c as createLockController } from './lock-controller-316928be.js';
  9. import { p as printIonWarning } from './index-cfd9c1f2.js';
  10. import { b as getIonMode, a as isPlatform } from './ionic-global-b26f573e.js';
  11. import { g as getClassMap } from './theme-01f3f29c.js';
  12. import { e as deepReady, w as waitForMount } from './index-68c0d151.js';
  13. import { c as createAnimation } from './animation-8b25e105.js';
  14. import './index-a5d50daf.js';
  15. import './hardware-back-button-a7eb8233.js';
  16. import './gesture-controller-314a54f6.js';
  17. /**
  18. * Returns the dimensions of the popover
  19. * arrow on `ios` mode. If arrow is disabled
  20. * returns (0, 0).
  21. */
  22. const getArrowDimensions = (arrowEl) => {
  23. if (!arrowEl) {
  24. return { arrowWidth: 0, arrowHeight: 0 };
  25. }
  26. const { width, height } = arrowEl.getBoundingClientRect();
  27. return { arrowWidth: width, arrowHeight: height };
  28. };
  29. /**
  30. * Returns the recommended dimensions of the popover
  31. * that takes into account whether or not the width
  32. * should match the trigger width.
  33. */
  34. const getPopoverDimensions = (size, contentEl, triggerEl) => {
  35. const contentDimentions = contentEl.getBoundingClientRect();
  36. const contentHeight = contentDimentions.height;
  37. let contentWidth = contentDimentions.width;
  38. if (size === 'cover' && triggerEl) {
  39. const triggerDimensions = triggerEl.getBoundingClientRect();
  40. contentWidth = triggerDimensions.width;
  41. }
  42. return {
  43. contentWidth,
  44. contentHeight,
  45. };
  46. };
  47. const configureDismissInteraction = (triggerEl, triggerAction, popoverEl, parentPopoverEl) => {
  48. let dismissCallbacks = [];
  49. const root = getElementRoot(parentPopoverEl);
  50. const parentContentEl = root.querySelector('.popover-content');
  51. switch (triggerAction) {
  52. case 'hover':
  53. dismissCallbacks = [
  54. {
  55. /**
  56. * Do not use mouseover here
  57. * as this will causes the event to
  58. * be dispatched on each underlying
  59. * element rather than on the popover
  60. * content as a whole.
  61. */
  62. eventName: 'mouseenter',
  63. callback: (ev) => {
  64. /**
  65. * Do not dismiss the popover is we
  66. * are hovering over its trigger.
  67. * This would be easier if we used mouseover
  68. * but this would cause the event to be dispatched
  69. * more often than we would like, potentially
  70. * causing performance issues.
  71. */
  72. const element = document.elementFromPoint(ev.clientX, ev.clientY);
  73. if (element === triggerEl) {
  74. return;
  75. }
  76. popoverEl.dismiss(undefined, undefined, false);
  77. },
  78. },
  79. ];
  80. break;
  81. case 'context-menu':
  82. case 'click':
  83. default:
  84. dismissCallbacks = [
  85. {
  86. eventName: 'click',
  87. callback: (ev) => {
  88. /**
  89. * Do not dismiss the popover is we
  90. * are hovering over its trigger.
  91. */
  92. const target = ev.target;
  93. const closestTrigger = target.closest('[data-ion-popover-trigger]');
  94. if (closestTrigger === triggerEl) {
  95. /**
  96. * stopPropagation here so if the
  97. * popover has dismissOnSelect="true"
  98. * the popover does not dismiss since
  99. * we just clicked a trigger element.
  100. */
  101. ev.stopPropagation();
  102. return;
  103. }
  104. popoverEl.dismiss(undefined, undefined, false);
  105. },
  106. },
  107. ];
  108. break;
  109. }
  110. dismissCallbacks.forEach(({ eventName, callback }) => parentContentEl.addEventListener(eventName, callback));
  111. return () => {
  112. dismissCallbacks.forEach(({ eventName, callback }) => parentContentEl.removeEventListener(eventName, callback));
  113. };
  114. };
  115. /**
  116. * Configures the triggerEl to respond
  117. * to user interaction based upon the triggerAction
  118. * prop that devs have defined.
  119. */
  120. const configureTriggerInteraction = (triggerEl, triggerAction, popoverEl) => {
  121. let triggerCallbacks = [];
  122. /**
  123. * Based upon the kind of trigger interaction
  124. * the user wants, we setup the correct event
  125. * listeners.
  126. */
  127. switch (triggerAction) {
  128. case 'hover':
  129. let hoverTimeout;
  130. triggerCallbacks = [
  131. {
  132. eventName: 'mouseenter',
  133. callback: async (ev) => {
  134. ev.stopPropagation();
  135. if (hoverTimeout) {
  136. clearTimeout(hoverTimeout);
  137. }
  138. /**
  139. * Hovering over a trigger should not
  140. * immediately open the next popover.
  141. */
  142. hoverTimeout = setTimeout(() => {
  143. raf(() => {
  144. popoverEl.presentFromTrigger(ev);
  145. hoverTimeout = undefined;
  146. });
  147. }, 100);
  148. },
  149. },
  150. {
  151. eventName: 'mouseleave',
  152. callback: (ev) => {
  153. if (hoverTimeout) {
  154. clearTimeout(hoverTimeout);
  155. }
  156. /**
  157. * If mouse is over another popover
  158. * that is not this popover then we should
  159. * close this popover.
  160. */
  161. const target = ev.relatedTarget;
  162. if (!target) {
  163. return;
  164. }
  165. if (target.closest('ion-popover') !== popoverEl) {
  166. popoverEl.dismiss(undefined, undefined, false);
  167. }
  168. },
  169. },
  170. {
  171. /**
  172. * stopPropagation here prevents the popover
  173. * from dismissing when dismiss-on-select="true".
  174. */
  175. eventName: 'click',
  176. callback: (ev) => ev.stopPropagation(),
  177. },
  178. {
  179. eventName: 'ionPopoverActivateTrigger',
  180. callback: (ev) => popoverEl.presentFromTrigger(ev, true),
  181. },
  182. ];
  183. break;
  184. case 'context-menu':
  185. triggerCallbacks = [
  186. {
  187. eventName: 'contextmenu',
  188. callback: (ev) => {
  189. /**
  190. * Prevents the platform context
  191. * menu from appearing.
  192. */
  193. ev.preventDefault();
  194. popoverEl.presentFromTrigger(ev);
  195. },
  196. },
  197. {
  198. eventName: 'click',
  199. callback: (ev) => ev.stopPropagation(),
  200. },
  201. {
  202. eventName: 'ionPopoverActivateTrigger',
  203. callback: (ev) => popoverEl.presentFromTrigger(ev, true),
  204. },
  205. ];
  206. break;
  207. case 'click':
  208. default:
  209. triggerCallbacks = [
  210. {
  211. /**
  212. * Do not do a stopPropagation() here
  213. * because if you had two click triggers
  214. * then clicking the first trigger and then
  215. * clicking the second trigger would not cause
  216. * the first popover to dismiss.
  217. */
  218. eventName: 'click',
  219. callback: (ev) => popoverEl.presentFromTrigger(ev),
  220. },
  221. {
  222. eventName: 'ionPopoverActivateTrigger',
  223. callback: (ev) => popoverEl.presentFromTrigger(ev, true),
  224. },
  225. ];
  226. break;
  227. }
  228. triggerCallbacks.forEach(({ eventName, callback }) => triggerEl.addEventListener(eventName, callback));
  229. triggerEl.setAttribute('data-ion-popover-trigger', 'true');
  230. return () => {
  231. triggerCallbacks.forEach(({ eventName, callback }) => triggerEl.removeEventListener(eventName, callback));
  232. triggerEl.removeAttribute('data-ion-popover-trigger');
  233. };
  234. };
  235. /**
  236. * Returns the index of an ion-item in an array of ion-items.
  237. */
  238. const getIndexOfItem = (items, item) => {
  239. if (!item || item.tagName !== 'ION-ITEM') {
  240. return -1;
  241. }
  242. return items.findIndex((el) => el === item);
  243. };
  244. /**
  245. * Given an array of elements and a currently focused ion-item
  246. * returns the next ion-item relative to the focused one or
  247. * undefined.
  248. */
  249. const getNextItem = (items, currentItem) => {
  250. const currentItemIndex = getIndexOfItem(items, currentItem);
  251. return items[currentItemIndex + 1];
  252. };
  253. /**
  254. * Given an array of elements and a currently focused ion-item
  255. * returns the previous ion-item relative to the focused one or
  256. * undefined.
  257. */
  258. const getPrevItem = (items, currentItem) => {
  259. const currentItemIndex = getIndexOfItem(items, currentItem);
  260. return items[currentItemIndex - 1];
  261. };
  262. /** Focus the internal button of the ion-item */
  263. const focusItem = (item) => {
  264. const root = getElementRoot(item);
  265. const button = root.querySelector('button');
  266. if (button) {
  267. raf(() => button.focus());
  268. }
  269. };
  270. /**
  271. * Returns `true` if `el` has been designated
  272. * as a trigger element for an ion-popover.
  273. */
  274. const isTriggerElement = (el) => el.hasAttribute('data-ion-popover-trigger');
  275. const configureKeyboardInteraction = (popoverEl) => {
  276. const callback = async (ev) => {
  277. var _a;
  278. const activeElement = document.activeElement;
  279. let items = [];
  280. const targetTagName = (_a = ev.target) === null || _a === void 0 ? void 0 : _a.tagName;
  281. /**
  282. * Only handle custom keyboard interactions for the host popover element
  283. * and children ion-item elements.
  284. */
  285. if (targetTagName !== 'ION-POPOVER' && targetTagName !== 'ION-ITEM') {
  286. return;
  287. }
  288. /**
  289. * Complex selectors with :not() are :not supported
  290. * in older versions of Chromium so we need to do a
  291. * try/catch here so errors are not thrown.
  292. */
  293. try {
  294. /**
  295. * Select all ion-items that are not children of child popovers.
  296. * i.e. only select ion-item elements that are part of this popover
  297. */
  298. items = Array.from(popoverEl.querySelectorAll('ion-item:not(ion-popover ion-popover *):not([disabled])'));
  299. /* eslint-disable-next-line */
  300. }
  301. catch (_b) { }
  302. switch (ev.key) {
  303. /**
  304. * If we are in a child popover
  305. * then pressing the left arrow key
  306. * should close this popover and move
  307. * focus to the popover that presented
  308. * this one.
  309. */
  310. case 'ArrowLeft':
  311. const parentPopover = await popoverEl.getParentPopover();
  312. if (parentPopover) {
  313. popoverEl.dismiss(undefined, undefined, false);
  314. }
  315. break;
  316. /**
  317. * ArrowDown should move focus to the next focusable ion-item.
  318. */
  319. case 'ArrowDown':
  320. // Disable movement/scroll with keyboard
  321. ev.preventDefault();
  322. const nextItem = getNextItem(items, activeElement);
  323. if (nextItem !== undefined) {
  324. focusItem(nextItem);
  325. }
  326. break;
  327. /**
  328. * ArrowUp should move focus to the previous focusable ion-item.
  329. */
  330. case 'ArrowUp':
  331. // Disable movement/scroll with keyboard
  332. ev.preventDefault();
  333. const prevItem = getPrevItem(items, activeElement);
  334. if (prevItem !== undefined) {
  335. focusItem(prevItem);
  336. }
  337. break;
  338. /**
  339. * Home should move focus to the first focusable ion-item.
  340. */
  341. case 'Home':
  342. ev.preventDefault();
  343. const firstItem = items[0];
  344. if (firstItem !== undefined) {
  345. focusItem(firstItem);
  346. }
  347. break;
  348. /**
  349. * End should move focus to the last focusable ion-item.
  350. */
  351. case 'End':
  352. ev.preventDefault();
  353. const lastItem = items[items.length - 1];
  354. if (lastItem !== undefined) {
  355. focusItem(lastItem);
  356. }
  357. break;
  358. /**
  359. * ArrowRight, Spacebar, or Enter should activate
  360. * the currently focused trigger item to open a
  361. * popover if the element is a trigger item.
  362. */
  363. case 'ArrowRight':
  364. case ' ':
  365. case 'Enter':
  366. if (activeElement && isTriggerElement(activeElement)) {
  367. const rightEvent = new CustomEvent('ionPopoverActivateTrigger');
  368. activeElement.dispatchEvent(rightEvent);
  369. }
  370. break;
  371. }
  372. };
  373. popoverEl.addEventListener('keydown', callback);
  374. return () => popoverEl.removeEventListener('keydown', callback);
  375. };
  376. /**
  377. * Positions a popover by taking into account
  378. * the reference point, preferred side, alignment
  379. * and viewport dimensions.
  380. */
  381. const getPopoverPosition = (isRTL, contentWidth, contentHeight, arrowWidth, arrowHeight, reference, side, align, defaultPosition, triggerEl, event) => {
  382. var _a;
  383. let referenceCoordinates = {
  384. top: 0,
  385. left: 0,
  386. width: 0,
  387. height: 0,
  388. };
  389. /**
  390. * Calculate position relative to the
  391. * x-y coordinates in the event that
  392. * was passed in
  393. */
  394. switch (reference) {
  395. case 'event':
  396. if (!event) {
  397. return defaultPosition;
  398. }
  399. const mouseEv = event;
  400. referenceCoordinates = {
  401. top: mouseEv.clientY,
  402. left: mouseEv.clientX,
  403. width: 1,
  404. height: 1,
  405. };
  406. break;
  407. /**
  408. * Calculate position relative to the bounding
  409. * box on either the trigger element
  410. * specified via the `trigger` prop or
  411. * the target specified on the event
  412. * that was passed in.
  413. */
  414. case 'trigger':
  415. default:
  416. const customEv = event;
  417. /**
  418. * ionShadowTarget is used when we need to align the
  419. * popover with an element inside of the shadow root
  420. * of an Ionic component. Ex: Presenting a popover
  421. * by clicking on the collapsed indicator inside
  422. * of `ion-breadcrumb` and centering it relative
  423. * to the indicator rather than `ion-breadcrumb`
  424. * as a whole.
  425. */
  426. const actualTriggerEl = (triggerEl ||
  427. ((_a = customEv === null || customEv === void 0 ? void 0 : customEv.detail) === null || _a === void 0 ? void 0 : _a.ionShadowTarget) ||
  428. (customEv === null || customEv === void 0 ? void 0 : customEv.target));
  429. if (!actualTriggerEl) {
  430. return defaultPosition;
  431. }
  432. const triggerBoundingBox = actualTriggerEl.getBoundingClientRect();
  433. referenceCoordinates = {
  434. top: triggerBoundingBox.top,
  435. left: triggerBoundingBox.left,
  436. width: triggerBoundingBox.width,
  437. height: triggerBoundingBox.height,
  438. };
  439. break;
  440. }
  441. /**
  442. * Get top/left offset that would allow
  443. * popover to be positioned on the
  444. * preferred side of the reference.
  445. */
  446. const coordinates = calculatePopoverSide(side, referenceCoordinates, contentWidth, contentHeight, arrowWidth, arrowHeight, isRTL);
  447. /**
  448. * Get the top/left adjustments that
  449. * would allow the popover content
  450. * to have the correct alignment.
  451. */
  452. const alignedCoordinates = calculatePopoverAlign(align, side, referenceCoordinates, contentWidth, contentHeight);
  453. const top = coordinates.top + alignedCoordinates.top;
  454. const left = coordinates.left + alignedCoordinates.left;
  455. const { arrowTop, arrowLeft } = calculateArrowPosition(side, arrowWidth, arrowHeight, top, left, contentWidth, contentHeight, isRTL);
  456. const { originX, originY } = calculatePopoverOrigin(side, align, isRTL);
  457. return { top, left, referenceCoordinates, arrowTop, arrowLeft, originX, originY };
  458. };
  459. /**
  460. * Determines the transform-origin
  461. * of the popover animation so that it
  462. * is in line with what the side and alignment
  463. * prop values are. Currently only used
  464. * with the MD animation.
  465. */
  466. const calculatePopoverOrigin = (side, align, isRTL) => {
  467. switch (side) {
  468. case 'top':
  469. return { originX: getOriginXAlignment(align), originY: 'bottom' };
  470. case 'bottom':
  471. return { originX: getOriginXAlignment(align), originY: 'top' };
  472. case 'left':
  473. return { originX: 'right', originY: getOriginYAlignment(align) };
  474. case 'right':
  475. return { originX: 'left', originY: getOriginYAlignment(align) };
  476. case 'start':
  477. return { originX: isRTL ? 'left' : 'right', originY: getOriginYAlignment(align) };
  478. case 'end':
  479. return { originX: isRTL ? 'right' : 'left', originY: getOriginYAlignment(align) };
  480. }
  481. };
  482. const getOriginXAlignment = (align) => {
  483. switch (align) {
  484. case 'start':
  485. return 'left';
  486. case 'center':
  487. return 'center';
  488. case 'end':
  489. return 'right';
  490. }
  491. };
  492. const getOriginYAlignment = (align) => {
  493. switch (align) {
  494. case 'start':
  495. return 'top';
  496. case 'center':
  497. return 'center';
  498. case 'end':
  499. return 'bottom';
  500. }
  501. };
  502. /**
  503. * Calculates where the arrow positioning
  504. * should be relative to the popover content.
  505. */
  506. const calculateArrowPosition = (side, arrowWidth, arrowHeight, top, left, contentWidth, contentHeight, isRTL) => {
  507. /**
  508. * Note: When side is left, right, start, or end, the arrow is
  509. * been rotated using a `transform`, so to move the arrow up or down
  510. * by its dimension, you need to use `arrowWidth`.
  511. */
  512. const leftPosition = {
  513. arrowTop: top + contentHeight / 2 - arrowWidth / 2,
  514. arrowLeft: left + contentWidth - arrowWidth / 2,
  515. };
  516. /**
  517. * Move the arrow to the left by arrowWidth and then
  518. * again by half of its width because we have rotated
  519. * the arrow using a transform.
  520. */
  521. const rightPosition = { arrowTop: top + contentHeight / 2 - arrowWidth / 2, arrowLeft: left - arrowWidth * 1.5 };
  522. switch (side) {
  523. case 'top':
  524. return { arrowTop: top + contentHeight, arrowLeft: left + contentWidth / 2 - arrowWidth / 2 };
  525. case 'bottom':
  526. return { arrowTop: top - arrowHeight, arrowLeft: left + contentWidth / 2 - arrowWidth / 2 };
  527. case 'left':
  528. return leftPosition;
  529. case 'right':
  530. return rightPosition;
  531. case 'start':
  532. return isRTL ? rightPosition : leftPosition;
  533. case 'end':
  534. return isRTL ? leftPosition : rightPosition;
  535. default:
  536. return { arrowTop: 0, arrowLeft: 0 };
  537. }
  538. };
  539. /**
  540. * Calculates the required top/left
  541. * values needed to position the popover
  542. * content on the side specified in the
  543. * `side` prop.
  544. */
  545. const calculatePopoverSide = (side, triggerBoundingBox, contentWidth, contentHeight, arrowWidth, arrowHeight, isRTL) => {
  546. const sideLeft = {
  547. top: triggerBoundingBox.top,
  548. left: triggerBoundingBox.left - contentWidth - arrowWidth,
  549. };
  550. const sideRight = {
  551. top: triggerBoundingBox.top,
  552. left: triggerBoundingBox.left + triggerBoundingBox.width + arrowWidth,
  553. };
  554. switch (side) {
  555. case 'top':
  556. return {
  557. top: triggerBoundingBox.top - contentHeight - arrowHeight,
  558. left: triggerBoundingBox.left,
  559. };
  560. case 'right':
  561. return sideRight;
  562. case 'bottom':
  563. return {
  564. top: triggerBoundingBox.top + triggerBoundingBox.height + arrowHeight,
  565. left: triggerBoundingBox.left,
  566. };
  567. case 'left':
  568. return sideLeft;
  569. case 'start':
  570. return isRTL ? sideRight : sideLeft;
  571. case 'end':
  572. return isRTL ? sideLeft : sideRight;
  573. }
  574. };
  575. /**
  576. * Calculates the required top/left
  577. * offset values needed to provide the
  578. * correct alignment regardless while taking
  579. * into account the side the popover is on.
  580. */
  581. const calculatePopoverAlign = (align, side, triggerBoundingBox, contentWidth, contentHeight) => {
  582. switch (align) {
  583. case 'center':
  584. return calculatePopoverCenterAlign(side, triggerBoundingBox, contentWidth, contentHeight);
  585. case 'end':
  586. return calculatePopoverEndAlign(side, triggerBoundingBox, contentWidth, contentHeight);
  587. case 'start':
  588. default:
  589. return { top: 0, left: 0 };
  590. }
  591. };
  592. /**
  593. * Calculate the end alignment for
  594. * the popover. If side is on the x-axis
  595. * then the align values refer to the top
  596. * and bottom margins of the content.
  597. * If side is on the y-axis then the
  598. * align values refer to the left and right
  599. * margins of the content.
  600. */
  601. const calculatePopoverEndAlign = (side, triggerBoundingBox, contentWidth, contentHeight) => {
  602. switch (side) {
  603. case 'start':
  604. case 'end':
  605. case 'left':
  606. case 'right':
  607. return {
  608. top: -(contentHeight - triggerBoundingBox.height),
  609. left: 0,
  610. };
  611. case 'top':
  612. case 'bottom':
  613. default:
  614. return {
  615. top: 0,
  616. left: -(contentWidth - triggerBoundingBox.width),
  617. };
  618. }
  619. };
  620. /**
  621. * Calculate the center alignment for
  622. * the popover. If side is on the x-axis
  623. * then the align values refer to the top
  624. * and bottom margins of the content.
  625. * If side is on the y-axis then the
  626. * align values refer to the left and right
  627. * margins of the content.
  628. */
  629. const calculatePopoverCenterAlign = (side, triggerBoundingBox, contentWidth, contentHeight) => {
  630. switch (side) {
  631. case 'start':
  632. case 'end':
  633. case 'left':
  634. case 'right':
  635. return {
  636. top: -(contentHeight / 2 - triggerBoundingBox.height / 2),
  637. left: 0,
  638. };
  639. case 'top':
  640. case 'bottom':
  641. default:
  642. return {
  643. top: 0,
  644. left: -(contentWidth / 2 - triggerBoundingBox.width / 2),
  645. };
  646. }
  647. };
  648. /**
  649. * Adjusts popover positioning coordinates
  650. * such that popover does not appear offscreen
  651. * or overlapping safe area bounds.
  652. */
  653. const calculateWindowAdjustment = (side, coordTop, coordLeft, bodyPadding, bodyWidth, bodyHeight, contentWidth, contentHeight, safeAreaMargin, contentOriginX, contentOriginY, triggerCoordinates, coordArrowTop = 0, coordArrowLeft = 0, arrowHeight = 0) => {
  654. let arrowTop = coordArrowTop;
  655. const arrowLeft = coordArrowLeft;
  656. let left = coordLeft;
  657. let top = coordTop;
  658. let bottom;
  659. let originX = contentOriginX;
  660. let originY = contentOriginY;
  661. let checkSafeAreaLeft = false;
  662. let checkSafeAreaRight = false;
  663. const triggerTop = triggerCoordinates
  664. ? triggerCoordinates.top + triggerCoordinates.height
  665. : bodyHeight / 2 - contentHeight / 2;
  666. const triggerHeight = triggerCoordinates ? triggerCoordinates.height : 0;
  667. let addPopoverBottomClass = false;
  668. /**
  669. * Adjust popover so it does not
  670. * go off the left of the screen.
  671. */
  672. if (left < bodyPadding + safeAreaMargin) {
  673. left = bodyPadding;
  674. checkSafeAreaLeft = true;
  675. originX = 'left';
  676. /**
  677. * Adjust popover so it does not
  678. * go off the right of the screen.
  679. */
  680. }
  681. else if (contentWidth + bodyPadding + left + safeAreaMargin > bodyWidth) {
  682. checkSafeAreaRight = true;
  683. left = bodyWidth - contentWidth - bodyPadding;
  684. originX = 'right';
  685. }
  686. /**
  687. * Adjust popover so it does not
  688. * go off the top of the screen.
  689. * If popover is on the left or the right of
  690. * the trigger, then we should not adjust top
  691. * margins.
  692. */
  693. if (triggerTop + triggerHeight + contentHeight > bodyHeight && (side === 'top' || side === 'bottom')) {
  694. if (triggerTop - contentHeight > 0) {
  695. /**
  696. * While we strive to align the popover with the trigger
  697. * on smaller screens this is not always possible. As a result,
  698. * we adjust the popover up so that it does not hang
  699. * off the bottom of the screen. However, we do not want to move
  700. * the popover up so much that it goes off the top of the screen.
  701. *
  702. * We chose 12 here so that the popover position looks a bit nicer as
  703. * it is not right up against the edge of the screen.
  704. */
  705. top = Math.max(12, triggerTop - contentHeight - triggerHeight - (arrowHeight - 1));
  706. arrowTop = top + contentHeight;
  707. originY = 'bottom';
  708. addPopoverBottomClass = true;
  709. /**
  710. * If not enough room for popover to appear
  711. * above trigger, then cut it off.
  712. */
  713. }
  714. else {
  715. bottom = bodyPadding;
  716. }
  717. }
  718. return {
  719. top,
  720. left,
  721. bottom,
  722. originX,
  723. originY,
  724. checkSafeAreaLeft,
  725. checkSafeAreaRight,
  726. arrowTop,
  727. arrowLeft,
  728. addPopoverBottomClass,
  729. };
  730. };
  731. const shouldShowArrow = (side, didAdjustBounds = false, ev, trigger) => {
  732. /**
  733. * If no event provided and
  734. * we do not have a trigger,
  735. * then this popover was likely
  736. * presented via the popoverController
  737. * or users called `present` manually.
  738. * In this case, the arrow should not be
  739. * shown as we do not have a reference.
  740. */
  741. if (!ev && !trigger) {
  742. return false;
  743. }
  744. /**
  745. * If popover is on the left or the right
  746. * of a trigger, but we needed to adjust the
  747. * popover due to screen bounds, then we should
  748. * hide the arrow as it will never be pointing
  749. * at the trigger.
  750. */
  751. if (side !== 'top' && side !== 'bottom' && didAdjustBounds) {
  752. return false;
  753. }
  754. return true;
  755. };
  756. const POPOVER_IOS_BODY_PADDING = 5;
  757. /**
  758. * iOS Popover Enter Animation
  759. */
  760. // TODO(FW-2832): types
  761. const iosEnterAnimation = (baseEl, opts) => {
  762. var _a;
  763. const { event: ev, size, trigger, reference, side, align } = opts;
  764. const doc = baseEl.ownerDocument;
  765. const isRTL = doc.dir === 'rtl';
  766. const bodyWidth = doc.defaultView.innerWidth;
  767. const bodyHeight = doc.defaultView.innerHeight;
  768. const root = getElementRoot(baseEl);
  769. const contentEl = root.querySelector('.popover-content');
  770. const arrowEl = root.querySelector('.popover-arrow');
  771. const referenceSizeEl = trigger || ((_a = ev === null || ev === void 0 ? void 0 : ev.detail) === null || _a === void 0 ? void 0 : _a.ionShadowTarget) || (ev === null || ev === void 0 ? void 0 : ev.target);
  772. const { contentWidth, contentHeight } = getPopoverDimensions(size, contentEl, referenceSizeEl);
  773. const { arrowWidth, arrowHeight } = getArrowDimensions(arrowEl);
  774. const defaultPosition = {
  775. top: bodyHeight / 2 - contentHeight / 2,
  776. left: bodyWidth / 2 - contentWidth / 2,
  777. originX: isRTL ? 'right' : 'left',
  778. originY: 'top',
  779. };
  780. const results = getPopoverPosition(isRTL, contentWidth, contentHeight, arrowWidth, arrowHeight, reference, side, align, defaultPosition, trigger, ev);
  781. const padding = size === 'cover' ? 0 : POPOVER_IOS_BODY_PADDING;
  782. const margin = size === 'cover' ? 0 : 25;
  783. const { originX, originY, top, left, bottom, checkSafeAreaLeft, checkSafeAreaRight, arrowTop, arrowLeft, addPopoverBottomClass, } = calculateWindowAdjustment(side, results.top, results.left, padding, bodyWidth, bodyHeight, contentWidth, contentHeight, margin, results.originX, results.originY, results.referenceCoordinates, results.arrowTop, results.arrowLeft, arrowHeight);
  784. const baseAnimation = createAnimation();
  785. const backdropAnimation = createAnimation();
  786. const contentAnimation = createAnimation();
  787. backdropAnimation
  788. .addElement(root.querySelector('ion-backdrop'))
  789. .fromTo('opacity', 0.01, 'var(--backdrop-opacity)')
  790. .beforeStyles({
  791. 'pointer-events': 'none',
  792. })
  793. .afterClearStyles(['pointer-events']);
  794. // In Chromium, if the wrapper animates, the backdrop filter doesn't work.
  795. // The Chromium team stated that this behavior is expected and not a bug. The element animating opacity creates a backdrop root for the backdrop-filter.
  796. // To get around this, instead of animating the wrapper, animate both the arrow and content.
  797. // https://bugs.chromium.org/p/chromium/issues/detail?id=1148826
  798. contentAnimation
  799. .addElement(root.querySelector('.popover-arrow'))
  800. .addElement(root.querySelector('.popover-content'))
  801. .fromTo('opacity', 0.01, 1);
  802. // TODO(FW-4376) Ensure that arrow also blurs when translucent
  803. return baseAnimation
  804. .easing('ease')
  805. .duration(100)
  806. .beforeAddWrite(() => {
  807. if (size === 'cover') {
  808. baseEl.style.setProperty('--width', `${contentWidth}px`);
  809. }
  810. if (addPopoverBottomClass) {
  811. baseEl.classList.add('popover-bottom');
  812. }
  813. if (bottom !== undefined) {
  814. contentEl.style.setProperty('bottom', `${bottom}px`);
  815. }
  816. const safeAreaLeft = ' + var(--ion-safe-area-left, 0)';
  817. const safeAreaRight = ' - var(--ion-safe-area-right, 0)';
  818. let leftValue = `${left}px`;
  819. if (checkSafeAreaLeft) {
  820. leftValue = `${left}px${safeAreaLeft}`;
  821. }
  822. if (checkSafeAreaRight) {
  823. leftValue = `${left}px${safeAreaRight}`;
  824. }
  825. contentEl.style.setProperty('top', `calc(${top}px + var(--offset-y, 0))`);
  826. contentEl.style.setProperty('left', `calc(${leftValue} + var(--offset-x, 0))`);
  827. contentEl.style.setProperty('transform-origin', `${originY} ${originX}`);
  828. if (arrowEl !== null) {
  829. const didAdjustBounds = results.top !== top || results.left !== left;
  830. const showArrow = shouldShowArrow(side, didAdjustBounds, ev, trigger);
  831. if (showArrow) {
  832. arrowEl.style.setProperty('top', `calc(${arrowTop}px + var(--offset-y, 0))`);
  833. arrowEl.style.setProperty('left', `calc(${arrowLeft}px + var(--offset-x, 0))`);
  834. }
  835. else {
  836. arrowEl.style.setProperty('display', 'none');
  837. }
  838. }
  839. })
  840. .addAnimation([backdropAnimation, contentAnimation]);
  841. };
  842. /**
  843. * iOS Popover Leave Animation
  844. */
  845. const iosLeaveAnimation = (baseEl) => {
  846. const root = getElementRoot(baseEl);
  847. const contentEl = root.querySelector('.popover-content');
  848. const arrowEl = root.querySelector('.popover-arrow');
  849. const baseAnimation = createAnimation();
  850. const backdropAnimation = createAnimation();
  851. const contentAnimation = createAnimation();
  852. backdropAnimation.addElement(root.querySelector('ion-backdrop')).fromTo('opacity', 'var(--backdrop-opacity)', 0);
  853. contentAnimation
  854. .addElement(root.querySelector('.popover-arrow'))
  855. .addElement(root.querySelector('.popover-content'))
  856. .fromTo('opacity', 0.99, 0);
  857. return baseAnimation
  858. .easing('ease')
  859. .afterAddWrite(() => {
  860. baseEl.style.removeProperty('--width');
  861. baseEl.classList.remove('popover-bottom');
  862. contentEl.style.removeProperty('top');
  863. contentEl.style.removeProperty('left');
  864. contentEl.style.removeProperty('bottom');
  865. contentEl.style.removeProperty('transform-origin');
  866. if (arrowEl) {
  867. arrowEl.style.removeProperty('top');
  868. arrowEl.style.removeProperty('left');
  869. arrowEl.style.removeProperty('display');
  870. }
  871. })
  872. .duration(300)
  873. .addAnimation([backdropAnimation, contentAnimation]);
  874. };
  875. const POPOVER_MD_BODY_PADDING = 12;
  876. /**
  877. * Md Popover Enter Animation
  878. */
  879. // TODO(FW-2832): types
  880. const mdEnterAnimation = (baseEl, opts) => {
  881. var _a;
  882. const { event: ev, size, trigger, reference, side, align } = opts;
  883. const doc = baseEl.ownerDocument;
  884. const isRTL = doc.dir === 'rtl';
  885. const bodyWidth = doc.defaultView.innerWidth;
  886. const bodyHeight = doc.defaultView.innerHeight;
  887. const root = getElementRoot(baseEl);
  888. const contentEl = root.querySelector('.popover-content');
  889. const referenceSizeEl = trigger || ((_a = ev === null || ev === void 0 ? void 0 : ev.detail) === null || _a === void 0 ? void 0 : _a.ionShadowTarget) || (ev === null || ev === void 0 ? void 0 : ev.target);
  890. const { contentWidth, contentHeight } = getPopoverDimensions(size, contentEl, referenceSizeEl);
  891. const defaultPosition = {
  892. top: bodyHeight / 2 - contentHeight / 2,
  893. left: bodyWidth / 2 - contentWidth / 2,
  894. originX: isRTL ? 'right' : 'left',
  895. originY: 'top',
  896. };
  897. const results = getPopoverPosition(isRTL, contentWidth, contentHeight, 0, 0, reference, side, align, defaultPosition, trigger, ev);
  898. const padding = size === 'cover' ? 0 : POPOVER_MD_BODY_PADDING;
  899. const { originX, originY, top, left, bottom } = calculateWindowAdjustment(side, results.top, results.left, padding, bodyWidth, bodyHeight, contentWidth, contentHeight, 0, results.originX, results.originY, results.referenceCoordinates);
  900. const baseAnimation = createAnimation();
  901. const backdropAnimation = createAnimation();
  902. const wrapperAnimation = createAnimation();
  903. const contentAnimation = createAnimation();
  904. const viewportAnimation = createAnimation();
  905. backdropAnimation
  906. .addElement(root.querySelector('ion-backdrop'))
  907. .fromTo('opacity', 0.01, 'var(--backdrop-opacity)')
  908. .beforeStyles({
  909. 'pointer-events': 'none',
  910. })
  911. .afterClearStyles(['pointer-events']);
  912. wrapperAnimation.addElement(root.querySelector('.popover-wrapper')).duration(150).fromTo('opacity', 0.01, 1);
  913. contentAnimation
  914. .addElement(contentEl)
  915. .beforeStyles({
  916. top: `calc(${top}px + var(--offset-y, 0px))`,
  917. left: `calc(${left}px + var(--offset-x, 0px))`,
  918. 'transform-origin': `${originY} ${originX}`,
  919. })
  920. .beforeAddWrite(() => {
  921. if (bottom !== undefined) {
  922. contentEl.style.setProperty('bottom', `${bottom}px`);
  923. }
  924. })
  925. .fromTo('transform', 'scale(0.8)', 'scale(1)');
  926. viewportAnimation.addElement(root.querySelector('.popover-viewport')).fromTo('opacity', 0.01, 1);
  927. return baseAnimation
  928. .easing('cubic-bezier(0.36,0.66,0.04,1)')
  929. .duration(300)
  930. .beforeAddWrite(() => {
  931. if (size === 'cover') {
  932. baseEl.style.setProperty('--width', `${contentWidth}px`);
  933. }
  934. if (originY === 'bottom') {
  935. baseEl.classList.add('popover-bottom');
  936. }
  937. })
  938. .addAnimation([backdropAnimation, wrapperAnimation, contentAnimation, viewportAnimation]);
  939. };
  940. /**
  941. * Md Popover Leave Animation
  942. */
  943. const mdLeaveAnimation = (baseEl) => {
  944. const root = getElementRoot(baseEl);
  945. const contentEl = root.querySelector('.popover-content');
  946. const baseAnimation = createAnimation();
  947. const backdropAnimation = createAnimation();
  948. const wrapperAnimation = createAnimation();
  949. backdropAnimation.addElement(root.querySelector('ion-backdrop')).fromTo('opacity', 'var(--backdrop-opacity)', 0);
  950. wrapperAnimation.addElement(root.querySelector('.popover-wrapper')).fromTo('opacity', 0.99, 0);
  951. return baseAnimation
  952. .easing('ease')
  953. .afterAddWrite(() => {
  954. baseEl.style.removeProperty('--width');
  955. baseEl.classList.remove('popover-bottom');
  956. contentEl.style.removeProperty('top');
  957. contentEl.style.removeProperty('left');
  958. contentEl.style.removeProperty('bottom');
  959. contentEl.style.removeProperty('transform-origin');
  960. })
  961. .duration(150)
  962. .addAnimation([backdropAnimation, wrapperAnimation]);
  963. };
  964. const popoverIosCss = ":host{--background:var(--ion-background-color, #fff);--min-width:0;--min-height:0;--max-width:auto;--height:auto;--offset-x:0px;--offset-y:0px;left:0;right:0;top:0;bottom:0;display:-ms-flexbox;display:flex;position:fixed;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;outline:none;color:var(--ion-text-color, #000);z-index:1001}:host(.popover-nested){pointer-events:none}:host(.popover-nested) .popover-wrapper{pointer-events:auto}:host(.overlay-hidden){display:none}.popover-wrapper{z-index:10}.popover-content{display:-ms-flexbox;display:flex;position:absolute;-ms-flex-direction:column;flex-direction:column;width:var(--width);min-width:var(--min-width);max-width:var(--max-width);height:var(--height);min-height:var(--min-height);max-height:var(--max-height);background:var(--background);-webkit-box-shadow:var(--box-shadow);box-shadow:var(--box-shadow);overflow:auto;z-index:10}::slotted(.popover-viewport){--ion-safe-area-top:0px;--ion-safe-area-right:0px;--ion-safe-area-bottom:0px;--ion-safe-area-left:0px;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column}:host(.popover-nested.popover-side-left){--offset-x:5px}:host(.popover-nested.popover-side-right){--offset-x:-5px}:host(.popover-nested.popover-side-start){--offset-x:5px}:host-context([dir=rtl]):host(.popover-nested.popover-side-start),:host-context([dir=rtl]).popover-nested.popover-side-start{--offset-x:-5px}@supports selector(:dir(rtl)){:host(.popover-nested.popover-side-start:dir(rtl)){--offset-x:-5px}}:host(.popover-nested.popover-side-end){--offset-x:-5px}:host-context([dir=rtl]):host(.popover-nested.popover-side-end),:host-context([dir=rtl]).popover-nested.popover-side-end{--offset-x:5px}@supports selector(:dir(rtl)){:host(.popover-nested.popover-side-end:dir(rtl)){--offset-x:5px}}:host{--width:200px;--max-height:90%;--box-shadow:none;--backdrop-opacity:var(--ion-backdrop-opacity, 0.08)}:host(.popover-desktop){--box-shadow:0px 4px 16px 0px rgba(0, 0, 0, 0.12)}.popover-content{border-radius:10px}:host(.popover-desktop) .popover-content{border:0.5px solid var(--ion-color-step-100, var(--ion-background-color-step-100, #e6e6e6))}.popover-arrow{display:block;position:absolute;width:20px;height:10px;overflow:hidden;z-index:11}.popover-arrow::after{top:3px;border-radius:3px;position:absolute;width:14px;height:14px;-webkit-transform:rotate(45deg);transform:rotate(45deg);background:var(--background);content:\"\";z-index:10}.popover-arrow::after{inset-inline-start:3px}:host(.popover-bottom) .popover-arrow{top:auto;bottom:-10px}:host(.popover-bottom) .popover-arrow::after{top:-6px}:host(.popover-side-left) .popover-arrow{-webkit-transform:rotate(90deg);transform:rotate(90deg)}:host(.popover-side-right) .popover-arrow{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}:host(.popover-side-top) .popover-arrow{-webkit-transform:rotate(180deg);transform:rotate(180deg)}:host(.popover-side-start) .popover-arrow{-webkit-transform:rotate(90deg);transform:rotate(90deg)}:host-context([dir=rtl]):host(.popover-side-start) .popover-arrow,:host-context([dir=rtl]).popover-side-start .popover-arrow{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}@supports selector(:dir(rtl)){:host(.popover-side-start:dir(rtl)) .popover-arrow{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}}:host(.popover-side-end) .popover-arrow{-webkit-transform:rotate(-90deg);transform:rotate(-90deg)}:host-context([dir=rtl]):host(.popover-side-end) .popover-arrow,:host-context([dir=rtl]).popover-side-end .popover-arrow{-webkit-transform:rotate(90deg);transform:rotate(90deg)}@supports selector(:dir(rtl)){:host(.popover-side-end:dir(rtl)) .popover-arrow{-webkit-transform:rotate(90deg);transform:rotate(90deg)}}.popover-arrow,.popover-content{opacity:0}@supports ((-webkit-backdrop-filter: blur(0)) or (backdrop-filter: blur(0))){:host(.popover-translucent) .popover-content,:host(.popover-translucent) .popover-arrow::after{background:rgba(var(--ion-background-color-rgb, 255, 255, 255), 0.8);-webkit-backdrop-filter:saturate(180%) blur(20px);backdrop-filter:saturate(180%) blur(20px)}}";
  965. const IonPopoverIosStyle0 = popoverIosCss;
  966. const popoverMdCss = ":host{--background:var(--ion-background-color, #fff);--min-width:0;--min-height:0;--max-width:auto;--height:auto;--offset-x:0px;--offset-y:0px;left:0;right:0;top:0;bottom:0;display:-ms-flexbox;display:flex;position:fixed;-ms-flex-align:center;align-items:center;-ms-flex-pack:center;justify-content:center;outline:none;color:var(--ion-text-color, #000);z-index:1001}:host(.popover-nested){pointer-events:none}:host(.popover-nested) .popover-wrapper{pointer-events:auto}:host(.overlay-hidden){display:none}.popover-wrapper{z-index:10}.popover-content{display:-ms-flexbox;display:flex;position:absolute;-ms-flex-direction:column;flex-direction:column;width:var(--width);min-width:var(--min-width);max-width:var(--max-width);height:var(--height);min-height:var(--min-height);max-height:var(--max-height);background:var(--background);-webkit-box-shadow:var(--box-shadow);box-shadow:var(--box-shadow);overflow:auto;z-index:10}::slotted(.popover-viewport){--ion-safe-area-top:0px;--ion-safe-area-right:0px;--ion-safe-area-bottom:0px;--ion-safe-area-left:0px;display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column}:host(.popover-nested.popover-side-left){--offset-x:5px}:host(.popover-nested.popover-side-right){--offset-x:-5px}:host(.popover-nested.popover-side-start){--offset-x:5px}:host-context([dir=rtl]):host(.popover-nested.popover-side-start),:host-context([dir=rtl]).popover-nested.popover-side-start{--offset-x:-5px}@supports selector(:dir(rtl)){:host(.popover-nested.popover-side-start:dir(rtl)){--offset-x:-5px}}:host(.popover-nested.popover-side-end){--offset-x:-5px}:host-context([dir=rtl]):host(.popover-nested.popover-side-end),:host-context([dir=rtl]).popover-nested.popover-side-end{--offset-x:5px}@supports selector(:dir(rtl)){:host(.popover-nested.popover-side-end:dir(rtl)){--offset-x:5px}}:host{--width:250px;--max-height:90%;--box-shadow:0 5px 5px -3px rgba(0, 0, 0, 0.2), 0 8px 10px 1px rgba(0, 0, 0, 0.14), 0 3px 14px 2px rgba(0, 0, 0, 0.12);--backdrop-opacity:var(--ion-backdrop-opacity, 0.32)}.popover-content{border-radius:4px;-webkit-transform-origin:left top;transform-origin:left top}:host-context([dir=rtl]) .popover-content{-webkit-transform-origin:right top;transform-origin:right top}[dir=rtl] .popover-content{-webkit-transform-origin:right top;transform-origin:right top}@supports selector(:dir(rtl)){.popover-content:dir(rtl){-webkit-transform-origin:right top;transform-origin:right top}}.popover-viewport{-webkit-transition-delay:100ms;transition-delay:100ms}.popover-wrapper{opacity:0}";
  967. const IonPopoverMdStyle0 = popoverMdCss;
  968. const Popover = class {
  969. constructor(hostRef) {
  970. registerInstance(this, hostRef);
  971. this.didPresent = createEvent(this, "ionPopoverDidPresent", 7);
  972. this.willPresent = createEvent(this, "ionPopoverWillPresent", 7);
  973. this.willDismiss = createEvent(this, "ionPopoverWillDismiss", 7);
  974. this.didDismiss = createEvent(this, "ionPopoverDidDismiss", 7);
  975. this.didPresentShorthand = createEvent(this, "didPresent", 7);
  976. this.willPresentShorthand = createEvent(this, "willPresent", 7);
  977. this.willDismissShorthand = createEvent(this, "willDismiss", 7);
  978. this.didDismissShorthand = createEvent(this, "didDismiss", 7);
  979. this.ionMount = createEvent(this, "ionMount", 7);
  980. this.parentPopover = null;
  981. this.coreDelegate = CoreDelegate();
  982. this.lockController = createLockController();
  983. this.inline = false;
  984. this.focusDescendantOnPresent = false;
  985. this.onBackdropTap = () => {
  986. this.dismiss(undefined, BACKDROP);
  987. };
  988. this.onLifecycle = (modalEvent) => {
  989. const el = this.usersElement;
  990. const name = LIFECYCLE_MAP[modalEvent.type];
  991. if (el && name) {
  992. const event = new CustomEvent(name, {
  993. bubbles: false,
  994. cancelable: false,
  995. detail: modalEvent.detail,
  996. });
  997. el.dispatchEvent(event);
  998. }
  999. };
  1000. this.configureTriggerInteraction = () => {
  1001. const { trigger, triggerAction, el, destroyTriggerInteraction } = this;
  1002. if (destroyTriggerInteraction) {
  1003. destroyTriggerInteraction();
  1004. }
  1005. if (trigger === undefined) {
  1006. return;
  1007. }
  1008. const triggerEl = (this.triggerEl = trigger !== undefined ? document.getElementById(trigger) : null);
  1009. if (!triggerEl) {
  1010. printIonWarning(`[ion-popover] - A trigger element with the ID "${trigger}" was not found in the DOM. The trigger element must be in the DOM when the "trigger" property is set on ion-popover.`, this.el);
  1011. return;
  1012. }
  1013. this.destroyTriggerInteraction = configureTriggerInteraction(triggerEl, triggerAction, el);
  1014. };
  1015. this.configureKeyboardInteraction = () => {
  1016. const { destroyKeyboardInteraction, el } = this;
  1017. if (destroyKeyboardInteraction) {
  1018. destroyKeyboardInteraction();
  1019. }
  1020. this.destroyKeyboardInteraction = configureKeyboardInteraction(el);
  1021. };
  1022. this.configureDismissInteraction = () => {
  1023. const { destroyDismissInteraction, parentPopover, triggerAction, triggerEl, el } = this;
  1024. if (!parentPopover || !triggerEl) {
  1025. return;
  1026. }
  1027. if (destroyDismissInteraction) {
  1028. destroyDismissInteraction();
  1029. }
  1030. this.destroyDismissInteraction = configureDismissInteraction(triggerEl, triggerAction, el, parentPopover);
  1031. };
  1032. this.presented = false;
  1033. this.hasController = false;
  1034. this.delegate = undefined;
  1035. this.overlayIndex = undefined;
  1036. this.enterAnimation = undefined;
  1037. this.leaveAnimation = undefined;
  1038. this.component = undefined;
  1039. this.componentProps = undefined;
  1040. this.keyboardClose = true;
  1041. this.cssClass = undefined;
  1042. this.backdropDismiss = true;
  1043. this.event = undefined;
  1044. this.showBackdrop = true;
  1045. this.translucent = false;
  1046. this.animated = true;
  1047. this.htmlAttributes = undefined;
  1048. this.triggerAction = 'click';
  1049. this.trigger = undefined;
  1050. this.size = 'auto';
  1051. this.dismissOnSelect = false;
  1052. this.reference = 'trigger';
  1053. this.side = 'bottom';
  1054. this.alignment = undefined;
  1055. this.arrow = true;
  1056. this.isOpen = false;
  1057. this.keyboardEvents = false;
  1058. this.focusTrap = true;
  1059. this.keepContentsMounted = false;
  1060. }
  1061. onTriggerChange() {
  1062. this.configureTriggerInteraction();
  1063. }
  1064. onIsOpenChange(newValue, oldValue) {
  1065. if (newValue === true && oldValue === false) {
  1066. this.present();
  1067. }
  1068. else if (newValue === false && oldValue === true) {
  1069. this.dismiss();
  1070. }
  1071. }
  1072. connectedCallback() {
  1073. const { configureTriggerInteraction, el } = this;
  1074. prepareOverlay(el);
  1075. configureTriggerInteraction();
  1076. }
  1077. disconnectedCallback() {
  1078. const { destroyTriggerInteraction } = this;
  1079. if (destroyTriggerInteraction) {
  1080. destroyTriggerInteraction();
  1081. }
  1082. }
  1083. componentWillLoad() {
  1084. var _a, _b;
  1085. const { el } = this;
  1086. const popoverId = (_b = (_a = this.htmlAttributes) === null || _a === void 0 ? void 0 : _a.id) !== null && _b !== void 0 ? _b : setOverlayId(el);
  1087. this.parentPopover = el.closest(`ion-popover:not(#${popoverId})`);
  1088. if (this.alignment === undefined) {
  1089. this.alignment = getIonMode(this) === 'ios' ? 'center' : 'start';
  1090. }
  1091. }
  1092. componentDidLoad() {
  1093. const { parentPopover, isOpen } = this;
  1094. /**
  1095. * If popover was rendered with isOpen="true"
  1096. * then we should open popover immediately.
  1097. */
  1098. if (isOpen === true) {
  1099. raf(() => this.present());
  1100. }
  1101. if (parentPopover) {
  1102. addEventListener(parentPopover, 'ionPopoverWillDismiss', () => {
  1103. this.dismiss(undefined, undefined, false);
  1104. });
  1105. }
  1106. /**
  1107. * When binding values in frameworks such as Angular
  1108. * it is possible for the value to be set after the Web Component
  1109. * initializes but before the value watcher is set up in Stencil.
  1110. * As a result, the watcher callback may not be fired.
  1111. * We work around this by manually calling the watcher
  1112. * callback when the component has loaded and the watcher
  1113. * is configured.
  1114. */
  1115. this.configureTriggerInteraction();
  1116. }
  1117. /**
  1118. * When opening a popover from a trigger, we should not be
  1119. * modifying the `event` prop from inside the component.
  1120. * Additionally, when pressing the "Right" arrow key, we need
  1121. * to shift focus to the first descendant in the newly presented
  1122. * popover.
  1123. *
  1124. * @internal
  1125. */
  1126. async presentFromTrigger(event, focusDescendant = false) {
  1127. this.focusDescendantOnPresent = focusDescendant;
  1128. await this.present(event);
  1129. this.focusDescendantOnPresent = false;
  1130. }
  1131. /**
  1132. * Determines whether or not an overlay
  1133. * is being used inline or via a controller/JS
  1134. * and returns the correct delegate.
  1135. * By default, subsequent calls to getDelegate
  1136. * will use a cached version of the delegate.
  1137. * This is useful for calling dismiss after
  1138. * present so that the correct delegate is given.
  1139. */
  1140. getDelegate(force = false) {
  1141. if (this.workingDelegate && !force) {
  1142. return {
  1143. delegate: this.workingDelegate,
  1144. inline: this.inline,
  1145. };
  1146. }
  1147. /**
  1148. * If using overlay inline
  1149. * we potentially need to use the coreDelegate
  1150. * so that this works in vanilla JS apps.
  1151. * If a developer has presented this component
  1152. * via a controller, then we can assume
  1153. * the component is already in the
  1154. * correct place.
  1155. */
  1156. const parentEl = this.el.parentNode;
  1157. const inline = (this.inline = parentEl !== null && !this.hasController);
  1158. const delegate = (this.workingDelegate = inline ? this.delegate || this.coreDelegate : this.delegate);
  1159. return { inline, delegate };
  1160. }
  1161. /**
  1162. * Present the popover overlay after it has been created.
  1163. * Developers can pass a mouse, touch, or pointer event
  1164. * to position the popover relative to where that event
  1165. * was dispatched.
  1166. */
  1167. async present(event) {
  1168. const unlock = await this.lockController.lock();
  1169. if (this.presented) {
  1170. unlock();
  1171. return;
  1172. }
  1173. const { el } = this;
  1174. const { inline, delegate } = this.getDelegate(true);
  1175. /**
  1176. * Emit ionMount so JS Frameworks have an opportunity
  1177. * to add the child component to the DOM. The child
  1178. * component will be assigned to this.usersElement below.
  1179. */
  1180. this.ionMount.emit();
  1181. this.usersElement = await attachComponent(delegate, el, this.component, ['popover-viewport'], this.componentProps, inline);
  1182. if (!this.keyboardEvents) {
  1183. this.configureKeyboardInteraction();
  1184. }
  1185. this.configureDismissInteraction();
  1186. /**
  1187. * When using the lazy loaded build of Stencil, we need to wait
  1188. * for every Stencil component instance to be ready before presenting
  1189. * otherwise there can be a flash of unstyled content. With the
  1190. * custom elements bundle we need to wait for the JS framework
  1191. * mount the inner contents of the overlay otherwise WebKit may
  1192. * get the transition incorrect.
  1193. */
  1194. if (hasLazyBuild(el)) {
  1195. await deepReady(this.usersElement);
  1196. /**
  1197. * If keepContentsMounted="true" then the
  1198. * JS Framework has already mounted the inner
  1199. * contents so there is no need to wait.
  1200. * Otherwise, we need to wait for the JS
  1201. * Framework to mount the inner contents
  1202. * of this component.
  1203. */
  1204. }
  1205. else if (!this.keepContentsMounted) {
  1206. await waitForMount();
  1207. }
  1208. await present(this, 'popoverEnter', iosEnterAnimation, mdEnterAnimation, {
  1209. event: event || this.event,
  1210. size: this.size,
  1211. trigger: this.triggerEl,
  1212. reference: this.reference,
  1213. side: this.side,
  1214. align: this.alignment,
  1215. });
  1216. /**
  1217. * If popover is nested and was
  1218. * presented using the "Right" arrow key,
  1219. * we need to move focus to the first
  1220. * descendant inside of the popover.
  1221. */
  1222. if (this.focusDescendantOnPresent) {
  1223. focusFirstDescendant(el);
  1224. }
  1225. unlock();
  1226. }
  1227. /**
  1228. * Dismiss the popover overlay after it has been presented.
  1229. *
  1230. * @param data Any data to emit in the dismiss events.
  1231. * @param role The role of the element that is dismissing the popover. For example, 'cancel' or 'backdrop'.
  1232. * @param dismissParentPopover If `true`, dismissing this popover will also dismiss
  1233. * a parent popover if this popover is nested. Defaults to `true`.
  1234. *
  1235. * This is a no-op if the overlay has not been presented yet. If you want
  1236. * to remove an overlay from the DOM that was never presented, use the
  1237. * [remove](https://developer.mozilla.org/en-US/docs/Web/API/Element/remove) method.
  1238. */
  1239. async dismiss(data, role, dismissParentPopover = true) {
  1240. const unlock = await this.lockController.lock();
  1241. const { destroyKeyboardInteraction, destroyDismissInteraction } = this;
  1242. if (dismissParentPopover && this.parentPopover) {
  1243. this.parentPopover.dismiss(data, role, dismissParentPopover);
  1244. }
  1245. const shouldDismiss = await dismiss(this, data, role, 'popoverLeave', iosLeaveAnimation, mdLeaveAnimation, this.event);
  1246. if (shouldDismiss) {
  1247. if (destroyKeyboardInteraction) {
  1248. destroyKeyboardInteraction();
  1249. this.destroyKeyboardInteraction = undefined;
  1250. }
  1251. if (destroyDismissInteraction) {
  1252. destroyDismissInteraction();
  1253. this.destroyDismissInteraction = undefined;
  1254. }
  1255. /**
  1256. * If using popover inline
  1257. * we potentially need to use the coreDelegate
  1258. * so that this works in vanilla JS apps
  1259. */
  1260. const { delegate } = this.getDelegate();
  1261. await detachComponent(delegate, this.usersElement);
  1262. }
  1263. unlock();
  1264. return shouldDismiss;
  1265. }
  1266. /**
  1267. * @internal
  1268. */
  1269. async getParentPopover() {
  1270. return this.parentPopover;
  1271. }
  1272. /**
  1273. * Returns a promise that resolves when the popover did dismiss.
  1274. */
  1275. onDidDismiss() {
  1276. return eventMethod(this.el, 'ionPopoverDidDismiss');
  1277. }
  1278. /**
  1279. * Returns a promise that resolves when the popover will dismiss.
  1280. */
  1281. onWillDismiss() {
  1282. return eventMethod(this.el, 'ionPopoverWillDismiss');
  1283. }
  1284. render() {
  1285. const mode = getIonMode(this);
  1286. const { onLifecycle, parentPopover, dismissOnSelect, side, arrow, htmlAttributes, focusTrap } = this;
  1287. const desktop = isPlatform('desktop');
  1288. const enableArrow = arrow && !parentPopover;
  1289. return (h(Host, Object.assign({ key: 'ff24e8d9677711248a36994cce568e74ba151499', "aria-modal": "true", "no-router": true, tabindex: "-1" }, htmlAttributes, { style: {
  1290. zIndex: `${20000 + this.overlayIndex}`,
  1291. }, class: Object.assign(Object.assign({}, getClassMap(this.cssClass)), { [mode]: true, 'popover-translucent': this.translucent, 'overlay-hidden': true, 'popover-desktop': desktop, [`popover-side-${side}`]: true, [FOCUS_TRAP_DISABLE_CLASS]: focusTrap === false, 'popover-nested': !!parentPopover }), onIonPopoverDidPresent: onLifecycle, onIonPopoverWillPresent: onLifecycle, onIonPopoverWillDismiss: onLifecycle, onIonPopoverDidDismiss: onLifecycle, onIonBackdropTap: this.onBackdropTap }), !parentPopover && h("ion-backdrop", { key: 'aca68b4002a08b0e563a976a867141162c20f8b4', tappable: this.backdropDismiss, visible: this.showBackdrop, part: "backdrop" }), h("div", { key: '62d21d1eab5c6d675d49932559ffb161747e5fec', class: "popover-wrapper ion-overlay-wrapper", onClick: dismissOnSelect ? () => this.dismiss() : undefined }, enableArrow && h("div", { key: '1b46cc77d5302637fc979353483bb5fd780fd1d3', class: "popover-arrow", part: "arrow" }), h("div", { key: 'a5657bff26e46d1959b71eb0992e7dc8fcae86f1', class: "popover-content", part: "content" }, h("slot", { key: 'e1a98007226a46b51109e7004c4d338ca1bc0f9e' })))));
  1292. }
  1293. get el() { return getElement(this); }
  1294. static get watchers() { return {
  1295. "trigger": ["onTriggerChange"],
  1296. "triggerAction": ["onTriggerChange"],
  1297. "isOpen": ["onIsOpenChange"]
  1298. }; }
  1299. };
  1300. const LIFECYCLE_MAP = {
  1301. ionPopoverDidPresent: 'ionViewDidEnter',
  1302. ionPopoverWillPresent: 'ionViewWillEnter',
  1303. ionPopoverWillDismiss: 'ionViewWillLeave',
  1304. ionPopoverDidDismiss: 'ionViewDidLeave',
  1305. };
  1306. Popover.style = {
  1307. ios: IonPopoverIosStyle0,
  1308. md: IonPopoverMdStyle0
  1309. };
  1310. export { Popover as ion_popover };