overlays.js 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { d as doc } from './index6.js';
  5. import { f as focusVisibleElement, c as componentOnReady, a as addEventListener, b as removeEventListener, g as getElementRoot } from './helpers.js';
  6. import { OVERLAY_BACK_BUTTON_PRIORITY, shouldUseCloseWatcher } from './hardware-back-button.js';
  7. import { c as config, p as printIonError, a as printIonWarning } from './index4.js';
  8. import { b as getIonMode, a as isPlatform } from './ionic-global.js';
  9. import { C as CoreDelegate } from './framework-delegate.js';
  10. import { B as BACKDROP_NO_SCROLL } from './gesture-controller.js';
  11. /**
  12. * This query string selects elements that
  13. * are eligible to receive focus. We select
  14. * interactive elements that meet the following
  15. * criteria:
  16. * 1. Element does not have a negative tabindex
  17. * 2. Element does not have `hidden`
  18. * 3. Element does not have `disabled` for non-Ionic components.
  19. * 4. Element does not have `disabled` or `disabled="true"` for Ionic components.
  20. * Note: We need this distinction because `disabled="false"` is
  21. * valid usage for the disabled property on ion-button.
  22. */
  23. const focusableQueryString = '[tabindex]:not([tabindex^="-"]):not([hidden]):not([disabled]), input:not([type=hidden]):not([tabindex^="-"]):not([hidden]):not([disabled]), textarea:not([tabindex^="-"]):not([hidden]):not([disabled]), button:not([tabindex^="-"]):not([hidden]):not([disabled]), select:not([tabindex^="-"]):not([hidden]):not([disabled]), ion-checkbox:not([tabindex^="-"]):not([hidden]):not([disabled]), ion-radio:not([tabindex^="-"]):not([hidden]):not([disabled]), .ion-focusable:not([tabindex^="-"]):not([hidden]):not([disabled]), .ion-focusable[disabled="false"]:not([tabindex^="-"]):not([hidden])';
  24. /**
  25. * Focuses the first descendant in a context
  26. * that can receive focus. If none exists,
  27. * a fallback element will be focused.
  28. * This fallback is typically an ancestor
  29. * container such as a menu or overlay so focus does not
  30. * leave the container we are trying to trap focus in.
  31. *
  32. * If no fallback is specified then we focus the container itself.
  33. */
  34. const focusFirstDescendant = (ref, fallbackElement) => {
  35. const firstInput = ref.querySelector(focusableQueryString);
  36. focusElementInContext(firstInput, fallbackElement !== null && fallbackElement !== void 0 ? fallbackElement : ref);
  37. };
  38. /**
  39. * Focuses the last descendant in a context
  40. * that can receive focus. If none exists,
  41. * a fallback element will be focused.
  42. * This fallback is typically an ancestor
  43. * container such as a menu or overlay so focus does not
  44. * leave the container we are trying to trap focus in.
  45. *
  46. * If no fallback is specified then we focus the container itself.
  47. */
  48. const focusLastDescendant = (ref, fallbackElement) => {
  49. const inputs = Array.from(ref.querySelectorAll(focusableQueryString));
  50. const lastInput = inputs.length > 0 ? inputs[inputs.length - 1] : null;
  51. focusElementInContext(lastInput, fallbackElement !== null && fallbackElement !== void 0 ? fallbackElement : ref);
  52. };
  53. /**
  54. * Focuses a particular element in a context. If the element
  55. * doesn't have anything focusable associated with it then
  56. * a fallback element will be focused.
  57. *
  58. * This fallback is typically an ancestor
  59. * container such as a menu or overlay so focus does not
  60. * leave the container we are trying to trap focus in.
  61. * This should be used instead of the focus() method
  62. * on most elements because the focusable element
  63. * may not be the host element.
  64. *
  65. * For example, if an ion-button should be focused
  66. * then we should actually focus the native <button>
  67. * element inside of ion-button's shadow root, not
  68. * the host element itself.
  69. */
  70. const focusElementInContext = (hostToFocus, fallbackElement) => {
  71. let elementToFocus = hostToFocus;
  72. const shadowRoot = hostToFocus === null || hostToFocus === void 0 ? void 0 : hostToFocus.shadowRoot;
  73. if (shadowRoot) {
  74. // If there are no inner focusable elements, just focus the host element.
  75. elementToFocus = shadowRoot.querySelector(focusableQueryString) || hostToFocus;
  76. }
  77. if (elementToFocus) {
  78. const radioGroup = elementToFocus.closest('ion-radio-group');
  79. if (radioGroup) {
  80. radioGroup.setFocus();
  81. }
  82. else {
  83. focusVisibleElement(elementToFocus);
  84. }
  85. }
  86. else {
  87. // Focus fallback element instead of letting focus escape
  88. fallbackElement.focus();
  89. }
  90. };
  91. let lastOverlayIndex = 0;
  92. let lastId = 0;
  93. const activeAnimations = new WeakMap();
  94. const createController = (tagName) => {
  95. return {
  96. create(options) {
  97. return createOverlay(tagName, options);
  98. },
  99. dismiss(data, role, id) {
  100. return dismissOverlay(document, data, role, tagName, id);
  101. },
  102. async getTop() {
  103. return getPresentedOverlay(document, tagName);
  104. },
  105. };
  106. };
  107. const alertController = /*@__PURE__*/ createController('ion-alert');
  108. const actionSheetController = /*@__PURE__*/ createController('ion-action-sheet');
  109. const loadingController = /*@__PURE__*/ createController('ion-loading');
  110. const modalController = /*@__PURE__*/ createController('ion-modal');
  111. /**
  112. * @deprecated Use the inline ion-picker component instead.
  113. */
  114. const pickerController = /*@__PURE__*/ createController('ion-picker-legacy');
  115. const popoverController = /*@__PURE__*/ createController('ion-popover');
  116. const toastController = /*@__PURE__*/ createController('ion-toast');
  117. /**
  118. * Prepares the overlay element to be presented.
  119. */
  120. const prepareOverlay = (el) => {
  121. if (typeof document !== 'undefined') {
  122. /**
  123. * Adds a single instance of event listeners for application behaviors:
  124. *
  125. * - Escape Key behavior to dismiss an overlay
  126. * - Trapping focus within an overlay
  127. * - Back button behavior to dismiss an overlay
  128. *
  129. * This only occurs when the first overlay is created.
  130. */
  131. connectListeners(document);
  132. }
  133. const overlayIndex = lastOverlayIndex++;
  134. /**
  135. * overlayIndex is used in the overlay components to set a zIndex.
  136. * This ensures that the most recently presented overlay will be
  137. * on top.
  138. */
  139. el.overlayIndex = overlayIndex;
  140. };
  141. /**
  142. * Assigns an incrementing id to an overlay element, that does not
  143. * already have an id assigned to it.
  144. *
  145. * Used to track unique instances of an overlay element.
  146. */
  147. const setOverlayId = (el) => {
  148. if (!el.hasAttribute('id')) {
  149. el.id = `ion-overlay-${++lastId}`;
  150. }
  151. return el.id;
  152. };
  153. const createOverlay = (tagName, opts) => {
  154. // eslint-disable-next-line @typescript-eslint/prefer-optional-chain
  155. if (typeof window !== 'undefined' && typeof window.customElements !== 'undefined') {
  156. return window.customElements.whenDefined(tagName).then(() => {
  157. const element = document.createElement(tagName);
  158. element.classList.add('overlay-hidden');
  159. /**
  160. * Convert the passed in overlay options into props
  161. * that get passed down into the new overlay.
  162. */
  163. Object.assign(element, Object.assign(Object.assign({}, opts), { hasController: true }));
  164. // append the overlay element to the document body
  165. getAppRoot(document).appendChild(element);
  166. return new Promise((resolve) => componentOnReady(element, resolve));
  167. });
  168. }
  169. return Promise.resolve();
  170. };
  171. const isOverlayHidden = (overlay) => overlay.classList.contains('overlay-hidden');
  172. /**
  173. * Focuses a particular element in an overlay. If the element
  174. * doesn't have anything focusable associated with it then
  175. * the overlay itself will be focused.
  176. * This should be used instead of the focus() method
  177. * on most elements because the focusable element
  178. * may not be the host element.
  179. *
  180. * For example, if an ion-button should be focused
  181. * then we should actually focus the native <button>
  182. * element inside of ion-button's shadow root, not
  183. * the host element itself.
  184. */
  185. const focusElementInOverlay = (hostToFocus, overlay) => {
  186. let elementToFocus = hostToFocus;
  187. const shadowRoot = hostToFocus === null || hostToFocus === void 0 ? void 0 : hostToFocus.shadowRoot;
  188. if (shadowRoot) {
  189. // If there are no inner focusable elements, just focus the host element.
  190. elementToFocus = shadowRoot.querySelector(focusableQueryString) || hostToFocus;
  191. }
  192. if (elementToFocus) {
  193. focusVisibleElement(elementToFocus);
  194. }
  195. else {
  196. // Focus overlay instead of letting focus escape
  197. overlay.focus();
  198. }
  199. };
  200. /**
  201. * Traps keyboard focus inside of overlay components.
  202. * Based on https://w3c.github.io/aria-practices/examples/dialog-modal/alertdialog.html
  203. * This includes the following components: Action Sheet, Alert, Loading, Modal,
  204. * Picker, and Popover.
  205. * Should NOT include: Toast
  206. */
  207. const trapKeyboardFocus = (ev, doc) => {
  208. const lastOverlay = getPresentedOverlay(doc, 'ion-alert,ion-action-sheet,ion-loading,ion-modal,ion-picker-legacy,ion-popover');
  209. const target = ev.target;
  210. /**
  211. * If no active overlay, ignore this event.
  212. *
  213. * If this component uses the shadow dom,
  214. * this global listener is pointless
  215. * since it will not catch the focus
  216. * traps as they are inside the shadow root.
  217. * We need to add a listener to the shadow root
  218. * itself to ensure the focus trap works.
  219. */
  220. if (!lastOverlay || !target) {
  221. return;
  222. }
  223. /**
  224. * If the ion-disable-focus-trap class
  225. * is present on an overlay, then this component
  226. * instance has opted out of focus trapping.
  227. * An example of this is when the sheet modal
  228. * has a backdrop that is disabled. The content
  229. * behind the sheet should be focusable until
  230. * the backdrop is enabled.
  231. */
  232. if (lastOverlay.classList.contains(FOCUS_TRAP_DISABLE_CLASS)) {
  233. return;
  234. }
  235. const trapScopedFocus = () => {
  236. /**
  237. * If we are focusing the overlay, clear
  238. * the last focused element so that hitting
  239. * tab activates the first focusable element
  240. * in the overlay wrapper.
  241. */
  242. if (lastOverlay === target) {
  243. lastOverlay.lastFocus = undefined;
  244. /**
  245. * Toasts can be presented from an overlay.
  246. * However, focus should still be returned to
  247. * the overlay when clicking a toast. Normally,
  248. * focus would be returned to the last focusable
  249. * descendant in the overlay which may not always be
  250. * the button that the toast was presented from. In this case,
  251. * the focus may be returned to an unexpected element.
  252. * To account for this, we make sure to return focus to the
  253. * last focused element in the overlay if focus is
  254. * moved to the toast.
  255. */
  256. }
  257. else if (target.tagName === 'ION-TOAST') {
  258. focusElementInOverlay(lastOverlay.lastFocus, lastOverlay);
  259. /**
  260. * Otherwise, we must be focusing an element
  261. * inside of the overlay. The two possible options
  262. * here are an input/button/etc or the ion-focus-trap
  263. * element. The focus trap element is used to prevent
  264. * the keyboard focus from leaving the overlay when
  265. * using Tab or screen assistants.
  266. */
  267. }
  268. else {
  269. /**
  270. * We do not want to focus the traps, so get the overlay
  271. * wrapper element as the traps live outside of the wrapper.
  272. */
  273. const overlayRoot = getElementRoot(lastOverlay);
  274. if (!overlayRoot.contains(target)) {
  275. return;
  276. }
  277. const overlayWrapper = overlayRoot.querySelector('.ion-overlay-wrapper');
  278. if (!overlayWrapper) {
  279. return;
  280. }
  281. /**
  282. * If the target is inside the wrapper, let the browser
  283. * focus as normal and keep a log of the last focused element.
  284. * Additionally, if the backdrop was tapped we should not
  285. * move focus back inside the wrapper as that could cause
  286. * an interactive elements focus state to activate.
  287. */
  288. if (overlayWrapper.contains(target) || target === overlayRoot.querySelector('ion-backdrop')) {
  289. lastOverlay.lastFocus = target;
  290. }
  291. else {
  292. /**
  293. * Otherwise, we must have focused one of the focus traps.
  294. * We need to wrap the focus to either the first element
  295. * or the last element.
  296. */
  297. /**
  298. * Once we call `focusFirstDescendant` and focus the first
  299. * descendant, another focus event will fire which will
  300. * cause `lastOverlay.lastFocus` to be updated before
  301. * we can run the code after that. We will cache the value
  302. * here to avoid that.
  303. */
  304. const lastFocus = lastOverlay.lastFocus;
  305. // Focus the first element in the overlay wrapper
  306. focusFirstDescendant(overlayWrapper, lastOverlay);
  307. /**
  308. * If the cached last focused element is the
  309. * same as the active element, then we need
  310. * to wrap focus to the last descendant. This happens
  311. * when the first descendant is focused, and the user
  312. * presses Shift + Tab. The previous line will focus
  313. * the same descendant again (the first one), causing
  314. * last focus to equal the active element.
  315. */
  316. if (lastFocus === doc.activeElement) {
  317. focusLastDescendant(overlayWrapper, lastOverlay);
  318. }
  319. lastOverlay.lastFocus = doc.activeElement;
  320. }
  321. }
  322. };
  323. const trapShadowFocus = () => {
  324. /**
  325. * If the target is inside the wrapper, let the browser
  326. * focus as normal and keep a log of the last focused element.
  327. */
  328. if (lastOverlay.contains(target)) {
  329. lastOverlay.lastFocus = target;
  330. /**
  331. * Toasts can be presented from an overlay.
  332. * However, focus should still be returned to
  333. * the overlay when clicking a toast. Normally,
  334. * focus would be returned to the last focusable
  335. * descendant in the overlay which may not always be
  336. * the button that the toast was presented from. In this case,
  337. * the focus may be returned to an unexpected element.
  338. * To account for this, we make sure to return focus to the
  339. * last focused element in the overlay if focus is
  340. * moved to the toast.
  341. */
  342. }
  343. else if (target.tagName === 'ION-TOAST') {
  344. focusElementInOverlay(lastOverlay.lastFocus, lastOverlay);
  345. }
  346. else {
  347. /**
  348. * Otherwise, we are about to have focus
  349. * go out of the overlay. We need to wrap
  350. * the focus to either the first element
  351. * or the last element.
  352. */
  353. /**
  354. * Once we call `focusFirstDescendant` and focus the first
  355. * descendant, another focus event will fire which will
  356. * cause `lastOverlay.lastFocus` to be updated before
  357. * we can run the code after that. We will cache the value
  358. * here to avoid that.
  359. */
  360. const lastFocus = lastOverlay.lastFocus;
  361. // Focus the first element in the overlay wrapper
  362. focusFirstDescendant(lastOverlay);
  363. /**
  364. * If the cached last focused element is the
  365. * same as the active element, then we need
  366. * to wrap focus to the last descendant. This happens
  367. * when the first descendant is focused, and the user
  368. * presses Shift + Tab. The previous line will focus
  369. * the same descendant again (the first one), causing
  370. * last focus to equal the active element.
  371. */
  372. if (lastFocus === doc.activeElement) {
  373. focusLastDescendant(lastOverlay);
  374. }
  375. lastOverlay.lastFocus = doc.activeElement;
  376. }
  377. };
  378. if (lastOverlay.shadowRoot) {
  379. trapShadowFocus();
  380. }
  381. else {
  382. trapScopedFocus();
  383. }
  384. };
  385. const connectListeners = (doc) => {
  386. if (lastOverlayIndex === 0) {
  387. lastOverlayIndex = 1;
  388. doc.addEventListener('focus', (ev) => {
  389. trapKeyboardFocus(ev, doc);
  390. }, true);
  391. // handle back-button click
  392. doc.addEventListener('ionBackButton', (ev) => {
  393. const lastOverlay = getPresentedOverlay(doc);
  394. if (lastOverlay === null || lastOverlay === void 0 ? void 0 : lastOverlay.backdropDismiss) {
  395. ev.detail.register(OVERLAY_BACK_BUTTON_PRIORITY, () => {
  396. /**
  397. * Do not return this promise otherwise
  398. * the hardware back button utility will
  399. * be blocked until the overlay dismisses.
  400. * This is important for a modal with canDismiss.
  401. * If the application presents a confirmation alert
  402. * in the "canDismiss" callback, then it will be impossible
  403. * to use the hardware back button to dismiss the alert
  404. * dialog because the hardware back button utility
  405. * is blocked on waiting for the modal to dismiss.
  406. */
  407. lastOverlay.dismiss(undefined, BACKDROP);
  408. });
  409. }
  410. });
  411. /**
  412. * Handle ESC to close overlay.
  413. * CloseWatcher also handles pressing the Esc
  414. * key, so if a browser supports CloseWatcher then
  415. * this behavior will be handled via the ionBackButton
  416. * event.
  417. */
  418. if (!shouldUseCloseWatcher()) {
  419. doc.addEventListener('keydown', (ev) => {
  420. if (ev.key === 'Escape') {
  421. const lastOverlay = getPresentedOverlay(doc);
  422. if (lastOverlay === null || lastOverlay === void 0 ? void 0 : lastOverlay.backdropDismiss) {
  423. lastOverlay.dismiss(undefined, BACKDROP);
  424. }
  425. }
  426. });
  427. }
  428. }
  429. };
  430. const dismissOverlay = (doc, data, role, overlayTag, id) => {
  431. const overlay = getPresentedOverlay(doc, overlayTag, id);
  432. if (!overlay) {
  433. return Promise.reject('overlay does not exist');
  434. }
  435. return overlay.dismiss(data, role);
  436. };
  437. /**
  438. * Returns a list of all overlays in the DOM even if they are not presented.
  439. */
  440. const getOverlays = (doc, selector) => {
  441. if (selector === undefined) {
  442. selector = 'ion-alert,ion-action-sheet,ion-loading,ion-modal,ion-picker-legacy,ion-popover,ion-toast';
  443. }
  444. return Array.from(doc.querySelectorAll(selector)).filter((c) => c.overlayIndex > 0);
  445. };
  446. /**
  447. * Returns a list of all presented overlays.
  448. * Inline overlays can exist in the DOM but not be presented,
  449. * so there are times when we want to exclude those.
  450. * @param doc The document to find the element within.
  451. * @param overlayTag The selector for the overlay, defaults to Ionic overlay components.
  452. */
  453. const getPresentedOverlays = (doc, overlayTag) => {
  454. return getOverlays(doc, overlayTag).filter((o) => !isOverlayHidden(o));
  455. };
  456. /**
  457. * Returns a presented overlay element.
  458. * @param doc The document to find the element within.
  459. * @param overlayTag The selector for the overlay, defaults to Ionic overlay components.
  460. * @param id The unique identifier for the overlay instance.
  461. * @returns The overlay element or `undefined` if no overlay element is found.
  462. */
  463. const getPresentedOverlay = (doc, overlayTag, id) => {
  464. const overlays = getPresentedOverlays(doc, overlayTag);
  465. return id === undefined ? overlays[overlays.length - 1] : overlays.find((o) => o.id === id);
  466. };
  467. /**
  468. * When an overlay is presented, the main
  469. * focus is the overlay not the page content.
  470. * We need to remove the page content from the
  471. * accessibility tree otherwise when
  472. * users use "read screen from top" gestures with
  473. * TalkBack and VoiceOver, the screen reader will begin
  474. * to read the content underneath the overlay.
  475. *
  476. * We need a container where all page components
  477. * exist that is separate from where the overlays
  478. * are added in the DOM. For most apps, this element
  479. * is the top most ion-router-outlet. In the event
  480. * that devs are not using a router,
  481. * they will need to add the "ion-view-container-root"
  482. * id to the element that contains all of their views.
  483. *
  484. * TODO: If Framework supports having multiple top
  485. * level router outlets we would need to update this.
  486. * Example: One outlet for side menu and one outlet
  487. * for main content.
  488. */
  489. const setRootAriaHidden = (hidden = false) => {
  490. const root = getAppRoot(document);
  491. const viewContainer = root.querySelector('ion-router-outlet, ion-nav, #ion-view-container-root');
  492. if (!viewContainer) {
  493. return;
  494. }
  495. if (hidden) {
  496. viewContainer.setAttribute('aria-hidden', 'true');
  497. }
  498. else {
  499. viewContainer.removeAttribute('aria-hidden');
  500. }
  501. };
  502. const present = async (overlay, name, iosEnterAnimation, mdEnterAnimation, opts) => {
  503. var _a, _b;
  504. if (overlay.presented) {
  505. return;
  506. }
  507. /**
  508. * Due to accessibility guidelines, toasts do not have
  509. * focus traps.
  510. *
  511. * All other overlays should have focus traps to prevent
  512. * the keyboard focus from leaving the overlay.
  513. */
  514. if (overlay.el.tagName !== 'ION-TOAST') {
  515. setRootAriaHidden(true);
  516. document.body.classList.add(BACKDROP_NO_SCROLL);
  517. }
  518. hideUnderlyingOverlaysFromScreenReaders(overlay.el);
  519. hideAnimatingOverlayFromScreenReaders(overlay.el);
  520. overlay.presented = true;
  521. overlay.willPresent.emit();
  522. (_a = overlay.willPresentShorthand) === null || _a === void 0 ? void 0 : _a.emit();
  523. const mode = getIonMode(overlay);
  524. // get the user's animation fn if one was provided
  525. const animationBuilder = overlay.enterAnimation
  526. ? overlay.enterAnimation
  527. : config.get(name, mode === 'ios' ? iosEnterAnimation : mdEnterAnimation);
  528. const completed = await overlayAnimation(overlay, animationBuilder, overlay.el, opts);
  529. if (completed) {
  530. overlay.didPresent.emit();
  531. (_b = overlay.didPresentShorthand) === null || _b === void 0 ? void 0 : _b.emit();
  532. }
  533. /**
  534. * When an overlay that steals focus
  535. * is dismissed, focus should be returned
  536. * to the element that was focused
  537. * prior to the overlay opening. Toast
  538. * does not steal focus and is excluded
  539. * from returning focus as a result.
  540. */
  541. if (overlay.el.tagName !== 'ION-TOAST') {
  542. restoreElementFocus(overlay.el);
  543. }
  544. /**
  545. * If the focused element is already
  546. * inside the overlay component then
  547. * focus should not be moved from that
  548. * to the overlay container.
  549. */
  550. if (overlay.keyboardClose && (document.activeElement === null || !overlay.el.contains(document.activeElement))) {
  551. overlay.el.focus();
  552. }
  553. /**
  554. * If this overlay was previously dismissed without being
  555. * the topmost one (such as by manually calling dismiss()),
  556. * it would still have aria-hidden on being presented again.
  557. * Removing it here ensures the overlay is visible to screen
  558. * readers.
  559. *
  560. * If this overlay was being presented, then it was hidden
  561. * from screen readers during the animation. Now that the
  562. * animation is complete, we can reveal the overlay to
  563. * screen readers.
  564. */
  565. overlay.el.removeAttribute('aria-hidden');
  566. };
  567. /**
  568. * When an overlay component is dismissed,
  569. * focus should be returned to the element
  570. * that presented the overlay. Otherwise
  571. * focus will be set on the body which
  572. * means that people using screen readers
  573. * or tabbing will need to re-navigate
  574. * to where they were before they
  575. * opened the overlay.
  576. */
  577. const restoreElementFocus = async (overlayEl) => {
  578. let previousElement = document.activeElement;
  579. if (!previousElement) {
  580. return;
  581. }
  582. const shadowRoot = previousElement === null || previousElement === void 0 ? void 0 : previousElement.shadowRoot;
  583. if (shadowRoot) {
  584. // If there are no inner focusable elements, just focus the host element.
  585. previousElement = shadowRoot.querySelector(focusableQueryString) || previousElement;
  586. }
  587. await overlayEl.onDidDismiss();
  588. /**
  589. * After onDidDismiss, the overlay loses focus
  590. * because it is removed from the document
  591. *
  592. * > An element will also lose focus [...]
  593. * > if the element is removed from the document)
  594. *
  595. * https://developer.mozilla.org/en-US/docs/Web/API/Element/blur_event
  596. *
  597. * Additionally, `document.activeElement` returns:
  598. *
  599. * > The Element which currently has focus,
  600. * > `<body>` or null if there is
  601. * > no focused element.
  602. *
  603. * https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement#value
  604. *
  605. * However, if the user has already focused
  606. * an element sometime between onWillDismiss
  607. * and onDidDismiss (for example, focusing a
  608. * text box after tapping a button in an
  609. * action sheet) then don't restore focus to
  610. * previous element
  611. */
  612. if (document.activeElement === null || document.activeElement === document.body) {
  613. previousElement.focus();
  614. }
  615. };
  616. const dismiss = async (overlay, data, role, name, iosLeaveAnimation, mdLeaveAnimation, opts) => {
  617. var _a, _b;
  618. if (!overlay.presented) {
  619. return false;
  620. }
  621. const presentedOverlays = doc !== undefined ? getPresentedOverlays(doc) : [];
  622. /**
  623. * For accessibility, toasts lack focus traps and don't receive
  624. * `aria-hidden` on the root element when presented.
  625. *
  626. * All other overlays use focus traps to keep keyboard focus
  627. * within the overlay, setting `aria-hidden` on the root element
  628. * to enhance accessibility.
  629. *
  630. * Therefore, we must remove `aria-hidden` from the root element
  631. * when the last non-toast overlay is dismissed.
  632. */
  633. const overlaysNotToast = presentedOverlays.filter((o) => o.tagName !== 'ION-TOAST');
  634. const lastOverlayNotToast = overlaysNotToast.length === 1 && overlaysNotToast[0].id === overlay.el.id;
  635. /**
  636. * If this is the last visible overlay that is not a toast
  637. * then we want to re-add the root to the accessibility tree.
  638. */
  639. if (lastOverlayNotToast) {
  640. setRootAriaHidden(false);
  641. document.body.classList.remove(BACKDROP_NO_SCROLL);
  642. }
  643. overlay.presented = false;
  644. try {
  645. /**
  646. * There is no need to show the overlay to screen readers during
  647. * the dismiss animation. This is because the overlay will be removed
  648. * from the DOM after the animation is complete.
  649. */
  650. hideAnimatingOverlayFromScreenReaders(overlay.el);
  651. // Overlay contents should not be clickable during dismiss
  652. overlay.el.style.setProperty('pointer-events', 'none');
  653. overlay.willDismiss.emit({ data, role });
  654. (_a = overlay.willDismissShorthand) === null || _a === void 0 ? void 0 : _a.emit({ data, role });
  655. const mode = getIonMode(overlay);
  656. const animationBuilder = overlay.leaveAnimation
  657. ? overlay.leaveAnimation
  658. : config.get(name, mode === 'ios' ? iosLeaveAnimation : mdLeaveAnimation);
  659. // If dismissed via gesture, no need to play leaving animation again
  660. if (role !== GESTURE) {
  661. await overlayAnimation(overlay, animationBuilder, overlay.el, opts);
  662. }
  663. overlay.didDismiss.emit({ data, role });
  664. (_b = overlay.didDismissShorthand) === null || _b === void 0 ? void 0 : _b.emit({ data, role });
  665. // Get a reference to all animations currently assigned to this overlay
  666. // Then tear them down to return the overlay to its initial visual state
  667. const animations = activeAnimations.get(overlay) || [];
  668. animations.forEach((ani) => ani.destroy());
  669. activeAnimations.delete(overlay);
  670. /**
  671. * Make overlay hidden again in case it is being reused.
  672. * We can safely remove pointer-events: none as
  673. * overlay-hidden will set display: none.
  674. */
  675. overlay.el.classList.add('overlay-hidden');
  676. overlay.el.style.removeProperty('pointer-events');
  677. /**
  678. * Clear any focus trapping references
  679. * when the overlay is dismissed.
  680. */
  681. if (overlay.el.lastFocus !== undefined) {
  682. overlay.el.lastFocus = undefined;
  683. }
  684. }
  685. catch (err) {
  686. printIonError(`[${overlay.el.tagName.toLowerCase()}] - `, err);
  687. }
  688. overlay.el.remove();
  689. revealOverlaysToScreenReaders();
  690. return true;
  691. };
  692. const getAppRoot = (doc) => {
  693. return doc.querySelector('ion-app') || doc.body;
  694. };
  695. const overlayAnimation = async (overlay, animationBuilder, baseEl, opts) => {
  696. // Make overlay visible in case it's hidden
  697. baseEl.classList.remove('overlay-hidden');
  698. const aniRoot = overlay.el;
  699. const animation = animationBuilder(aniRoot, opts);
  700. if (!overlay.animated || !config.getBoolean('animated', true)) {
  701. animation.duration(0);
  702. }
  703. if (overlay.keyboardClose) {
  704. animation.beforeAddWrite(() => {
  705. const activeElement = baseEl.ownerDocument.activeElement;
  706. if (activeElement === null || activeElement === void 0 ? void 0 : activeElement.matches('input,ion-input, ion-textarea')) {
  707. activeElement.blur();
  708. }
  709. });
  710. }
  711. const activeAni = activeAnimations.get(overlay) || [];
  712. activeAnimations.set(overlay, [...activeAni, animation]);
  713. await animation.play();
  714. return true;
  715. };
  716. const eventMethod = (element, eventName) => {
  717. let resolve;
  718. const promise = new Promise((r) => (resolve = r));
  719. onceEvent(element, eventName, (event) => {
  720. resolve(event.detail);
  721. });
  722. return promise;
  723. };
  724. const onceEvent = (element, eventName, callback) => {
  725. const handler = (ev) => {
  726. removeEventListener(element, eventName, handler);
  727. callback(ev);
  728. };
  729. addEventListener(element, eventName, handler);
  730. };
  731. const isCancel = (role) => {
  732. return role === 'cancel' || role === BACKDROP;
  733. };
  734. const defaultGate = (h) => h();
  735. /**
  736. * Calls a developer provided method while avoiding
  737. * Angular Zones. Since the handler is provided by
  738. * the developer, we should throw any errors
  739. * received so that developer-provided bug
  740. * tracking software can log it.
  741. */
  742. const safeCall = (handler, arg) => {
  743. if (typeof handler === 'function') {
  744. const jmp = config.get('_zoneGate', defaultGate);
  745. return jmp(() => {
  746. try {
  747. return handler(arg);
  748. }
  749. catch (e) {
  750. throw e;
  751. }
  752. });
  753. }
  754. return undefined;
  755. };
  756. const BACKDROP = 'backdrop';
  757. const GESTURE = 'gesture';
  758. const OVERLAY_GESTURE_PRIORITY = 39;
  759. /**
  760. * Creates a delegate controller.
  761. *
  762. * Requires that the component has the following properties:
  763. * - `el: HTMLElement`
  764. * - `hasController: boolean`
  765. * - `delegate?: FrameworkDelegate`
  766. *
  767. * @param ref The component class instance.
  768. */
  769. const createDelegateController = (ref) => {
  770. let inline = false;
  771. let workingDelegate;
  772. const coreDelegate = CoreDelegate();
  773. /**
  774. * Determines whether or not an overlay is being used
  775. * inline or via a controller/JS and returns the correct delegate.
  776. * By default, subsequent calls to getDelegate will use
  777. * a cached version of the delegate.
  778. * This is useful for calling dismiss after present,
  779. * so that the correct delegate is given.
  780. * @param force `true` to force the non-cached version of the delegate.
  781. * @returns The delegate to use and whether or not the overlay is inline.
  782. */
  783. const getDelegate = (force = false) => {
  784. if (workingDelegate && !force) {
  785. return {
  786. delegate: workingDelegate,
  787. inline,
  788. };
  789. }
  790. const { el, hasController, delegate } = ref;
  791. /**
  792. * If using overlay inline
  793. * we potentially need to use the coreDelegate
  794. * so that this works in vanilla JS apps.
  795. * If a developer has presented this component
  796. * via a controller, then we can assume
  797. * the component is already in the
  798. * correct place.
  799. */
  800. const parentEl = el.parentNode;
  801. inline = parentEl !== null && !hasController;
  802. workingDelegate = inline ? delegate || coreDelegate : delegate;
  803. return { inline, delegate: workingDelegate };
  804. };
  805. /**
  806. * Attaches a component in the DOM. Teleports the component
  807. * to the root of the app.
  808. * @param component The component to optionally construct and append to the element.
  809. */
  810. const attachViewToDom = async (component) => {
  811. const { delegate } = getDelegate(true);
  812. if (delegate) {
  813. return await delegate.attachViewToDom(ref.el, component);
  814. }
  815. const { hasController } = ref;
  816. if (hasController && component !== undefined) {
  817. throw new Error('framework delegate is missing');
  818. }
  819. return null;
  820. };
  821. /**
  822. * Moves a component back to its original location in the DOM.
  823. */
  824. const removeViewFromDom = () => {
  825. const { delegate } = getDelegate();
  826. if (delegate && ref.el !== undefined) {
  827. delegate.removeViewFromDom(ref.el.parentElement, ref.el);
  828. }
  829. };
  830. return {
  831. attachViewToDom,
  832. removeViewFromDom,
  833. };
  834. };
  835. /**
  836. * Constructs a trigger interaction for an overlay.
  837. * Presents an overlay when the trigger is clicked.
  838. *
  839. * Usage:
  840. * ```ts
  841. * triggerController = createTriggerController();
  842. * triggerController.addClickListener(el, trigger);
  843. * ```
  844. */
  845. const createTriggerController = () => {
  846. let destroyTriggerInteraction;
  847. /**
  848. * Removes the click listener from the trigger element.
  849. */
  850. const removeClickListener = () => {
  851. if (destroyTriggerInteraction) {
  852. destroyTriggerInteraction();
  853. destroyTriggerInteraction = undefined;
  854. }
  855. };
  856. /**
  857. * Adds a click listener to the trigger element.
  858. * Presents the overlay when the trigger is clicked.
  859. * @param el The overlay element.
  860. * @param trigger The ID of the element to add a click listener to.
  861. */
  862. const addClickListener = (el, trigger) => {
  863. removeClickListener();
  864. const triggerEl = trigger !== undefined ? document.getElementById(trigger) : null;
  865. if (!triggerEl) {
  866. printIonWarning(`[${el.tagName.toLowerCase()}] - 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 an overlay component.`, el);
  867. return;
  868. }
  869. const configureTriggerInteraction = (targetEl, overlayEl) => {
  870. const openOverlay = () => {
  871. overlayEl.present();
  872. };
  873. targetEl.addEventListener('click', openOverlay);
  874. return () => {
  875. targetEl.removeEventListener('click', openOverlay);
  876. };
  877. };
  878. destroyTriggerInteraction = configureTriggerInteraction(triggerEl, el);
  879. };
  880. return {
  881. addClickListener,
  882. removeClickListener,
  883. };
  884. };
  885. /**
  886. * The overlay that is being animated also needs to hide from screen
  887. * readers during its animation. This ensures that assistive technologies
  888. * like TalkBack do not announce or interact with the content until the
  889. * animation is complete, avoiding confusion for users.
  890. *
  891. * When the overlay is presented on an Android device, TalkBack's focus rings
  892. * may appear in the wrong position due to the transition (specifically
  893. * `transform` styles). This occurs because the focus rings are initially
  894. * displayed at the starting position of the elements before the transition
  895. * begins. This workaround ensures the focus rings do not appear in the
  896. * incorrect location.
  897. *
  898. * If this solution is applied to iOS devices, then it leads to a bug where
  899. * the overlays cannot be accessed by screen readers. This is due to
  900. * VoiceOver not being able to update the accessibility tree when the
  901. * `aria-hidden` is removed.
  902. *
  903. * @param overlay - The overlay that is being animated.
  904. */
  905. const hideAnimatingOverlayFromScreenReaders = (overlay) => {
  906. if (doc === undefined)
  907. return;
  908. if (isPlatform('android')) {
  909. /**
  910. * Once the animation is complete, this attribute will be removed.
  911. * This is done at the end of the `present` method.
  912. */
  913. overlay.setAttribute('aria-hidden', 'true');
  914. }
  915. };
  916. /**
  917. * Ensure that underlying overlays have aria-hidden if necessary so that screen readers
  918. * cannot move focus to these elements. Note that we cannot rely on focus/focusin/focusout
  919. * events here because those events do not fire when the screen readers moves to a non-focusable
  920. * element such as text.
  921. * Without this logic screen readers would be able to move focus outside of the top focus-trapped overlay.
  922. *
  923. * @param newTopMostOverlay - The overlay that is being presented. Since the overlay has not been
  924. * fully presented yet at the time this function is called it will not be included in the getPresentedOverlays result.
  925. */
  926. const hideUnderlyingOverlaysFromScreenReaders = (newTopMostOverlay) => {
  927. var _a;
  928. if (doc === undefined)
  929. return;
  930. const overlays = getPresentedOverlays(doc);
  931. for (let i = overlays.length - 1; i >= 0; i--) {
  932. const presentedOverlay = overlays[i];
  933. const nextPresentedOverlay = (_a = overlays[i + 1]) !== null && _a !== void 0 ? _a : newTopMostOverlay;
  934. /**
  935. * If next overlay has aria-hidden then all remaining overlays will have it too.
  936. * Or, if the next overlay is a Toast that does not have aria-hidden then current overlay
  937. * should not have aria-hidden either so focus can remain in the current overlay.
  938. */
  939. if (nextPresentedOverlay.hasAttribute('aria-hidden') || nextPresentedOverlay.tagName !== 'ION-TOAST') {
  940. presentedOverlay.setAttribute('aria-hidden', 'true');
  941. }
  942. }
  943. };
  944. /**
  945. * When dismissing an overlay we need to reveal the new top-most overlay to screen readers.
  946. * If the top-most overlay is a Toast we potentially need to reveal more overlays since
  947. * focus is never automatically moved to the Toast.
  948. */
  949. const revealOverlaysToScreenReaders = () => {
  950. if (doc === undefined)
  951. return;
  952. const overlays = getPresentedOverlays(doc);
  953. for (let i = overlays.length - 1; i >= 0; i--) {
  954. const currentOverlay = overlays[i];
  955. /**
  956. * If the current we are looking at is a Toast then we can remove aria-hidden.
  957. * However, we potentially need to keep looking at the overlay stack because there
  958. * could be more Toasts underneath. Additionally, we need to unhide the closest non-Toast
  959. * overlay too so focus can move there since focus is never automatically moved to the Toast.
  960. */
  961. currentOverlay.removeAttribute('aria-hidden');
  962. /**
  963. * If we found a non-Toast element then we can just remove aria-hidden and stop searching entirely
  964. * since this overlay should always receive focus. As a result, all underlying overlays should still
  965. * be hidden from screen readers.
  966. */
  967. if (currentOverlay.tagName !== 'ION-TOAST') {
  968. break;
  969. }
  970. }
  971. };
  972. const FOCUS_TRAP_DISABLE_CLASS = 'ion-disable-focus-trap';
  973. export { BACKDROP as B, FOCUS_TRAP_DISABLE_CLASS as F, GESTURE as G, OVERLAY_GESTURE_PRIORITY as O, alertController as a, actionSheetController as b, popoverController as c, createDelegateController as d, createTriggerController as e, present as f, dismiss as g, eventMethod as h, isCancel as i, prepareOverlay as j, setOverlayId as k, loadingController as l, modalController as m, focusFirstDescendant as n, getPresentedOverlay as o, pickerController as p, focusLastDescendant as q, safeCall as s, toastController as t };