ion-toast.entry.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 { E as ENABLE_HTML_CONTENT_DEFAULT, a as sanitizeDOMString } from './config-9898ed97.js';
  6. import { g as getElementRoot, r as raf } from './helpers-d94bc8ad.js';
  7. import { c as createLockController } from './lock-controller-316928be.js';
  8. import { p as printIonWarning, c as config, d as printIonError } from './index-cfd9c1f2.js';
  9. import { O as OVERLAY_GESTURE_PRIORITY, d as createDelegateController, e as createTriggerController, i as isCancel, j as prepareOverlay, k as setOverlayId, f as present, g as dismiss, h as eventMethod, s as safeCall, G as GESTURE } from './overlays-d99dcb0a.js';
  10. import { c as createColorClasses, g as getClassMap } from './theme-01f3f29c.js';
  11. import { b as getIonMode } from './ionic-global-b26f573e.js';
  12. import { c as createAnimation } from './animation-8b25e105.js';
  13. import { w as win } from './index-a5d50daf.js';
  14. import { createGesture } from './index-39782642.js';
  15. import './hardware-back-button-a7eb8233.js';
  16. import './framework-delegate-56b467ad.js';
  17. import './gesture-controller-314a54f6.js';
  18. /**
  19. * Calculate the CSS top and bottom position of the toast, to be used
  20. * as starting points for the animation keyframes.
  21. *
  22. * The default animations for both MD and iOS
  23. * use translateY, which calculates from the
  24. * top edge of the screen. This behavior impacts
  25. * how we compute the offset when a toast has
  26. * position='bottom' since we need to calculate from
  27. * the bottom edge of the screen instead.
  28. *
  29. * @param position The value of the toast's position prop.
  30. * @param positionAnchor The element the toast should be anchored to,
  31. * if applicable.
  32. * @param mode The toast component's mode (md, ios, etc).
  33. * @param toast A reference to the toast element itself.
  34. */
  35. function getAnimationPosition(position, positionAnchor, mode, toast) {
  36. /**
  37. * Start with a predefined offset from the edge the toast will be
  38. * positioned relative to, whether on the screen or anchor element.
  39. */
  40. let offset;
  41. if (mode === 'md') {
  42. offset = position === 'top' ? 8 : -8;
  43. }
  44. else {
  45. offset = position === 'top' ? 10 : -10;
  46. }
  47. /**
  48. * If positionAnchor is defined, add in the distance from the target
  49. * screen edge to the target anchor edge. For position="top", the
  50. * bottom anchor edge is targeted. For position="bottom", the top
  51. * anchor edge is targeted.
  52. */
  53. if (positionAnchor && win) {
  54. warnIfAnchorIsHidden(positionAnchor, toast);
  55. const box = positionAnchor.getBoundingClientRect();
  56. if (position === 'top') {
  57. offset += box.bottom;
  58. }
  59. else if (position === 'bottom') {
  60. /**
  61. * Just box.top is the distance from the top edge of the screen
  62. * to the top edge of the anchor. We want to calculate from the
  63. * bottom edge of the screen instead.
  64. */
  65. offset -= win.innerHeight - box.top;
  66. }
  67. /**
  68. * We don't include safe area here because that should already be
  69. * accounted for when checking the position of the anchor.
  70. */
  71. return {
  72. top: `${offset}px`,
  73. bottom: `${offset}px`,
  74. };
  75. }
  76. else {
  77. return {
  78. top: `calc(${offset}px + var(--ion-safe-area-top, 0px))`,
  79. bottom: `calc(${offset}px - var(--ion-safe-area-bottom, 0px))`,
  80. };
  81. }
  82. }
  83. /**
  84. * If the anchor element is hidden, getBoundingClientRect()
  85. * will return all 0s for it, which can cause unexpected
  86. * results in the position calculation when animating.
  87. */
  88. function warnIfAnchorIsHidden(positionAnchor, toast) {
  89. if (positionAnchor.offsetParent === null) {
  90. printIonWarning('[ion-toast] - The positionAnchor element for ion-toast was found in the DOM, but appears to be hidden. This may lead to unexpected positioning of the toast.', toast);
  91. }
  92. }
  93. /**
  94. * Returns the top offset required to place
  95. * the toast in the middle of the screen.
  96. * Only needed when position="toast".
  97. * @param toastHeight - The height of the ion-toast element
  98. * @param wrapperHeight - The height of the .toast-wrapper element
  99. * inside the toast's shadow root.
  100. */
  101. const getOffsetForMiddlePosition = (toastHeight, wrapperHeight) => {
  102. return Math.floor(toastHeight / 2 - wrapperHeight / 2);
  103. };
  104. /**
  105. * iOS Toast Enter Animation
  106. */
  107. const iosEnterAnimation = (baseEl, opts) => {
  108. const baseAnimation = createAnimation();
  109. const wrapperAnimation = createAnimation();
  110. const { position, top, bottom } = opts;
  111. const root = getElementRoot(baseEl);
  112. const wrapperEl = root.querySelector('.toast-wrapper');
  113. wrapperAnimation.addElement(wrapperEl);
  114. switch (position) {
  115. case 'top':
  116. wrapperAnimation.fromTo('transform', 'translateY(-100%)', `translateY(${top})`);
  117. break;
  118. case 'middle':
  119. const topPosition = getOffsetForMiddlePosition(baseEl.clientHeight, wrapperEl.clientHeight);
  120. wrapperEl.style.top = `${topPosition}px`;
  121. wrapperAnimation.fromTo('opacity', 0.01, 1);
  122. break;
  123. default:
  124. wrapperAnimation.fromTo('transform', 'translateY(100%)', `translateY(${bottom})`);
  125. break;
  126. }
  127. return baseAnimation.easing('cubic-bezier(.155,1.105,.295,1.12)').duration(400).addAnimation(wrapperAnimation);
  128. };
  129. /**
  130. * iOS Toast Leave Animation
  131. */
  132. const iosLeaveAnimation = (baseEl, opts) => {
  133. const baseAnimation = createAnimation();
  134. const wrapperAnimation = createAnimation();
  135. const { position, top, bottom } = opts;
  136. const root = getElementRoot(baseEl);
  137. const wrapperEl = root.querySelector('.toast-wrapper');
  138. wrapperAnimation.addElement(wrapperEl);
  139. switch (position) {
  140. case 'top':
  141. wrapperAnimation.fromTo('transform', `translateY(${top})`, 'translateY(-100%)');
  142. break;
  143. case 'middle':
  144. wrapperAnimation.fromTo('opacity', 0.99, 0);
  145. break;
  146. default:
  147. wrapperAnimation.fromTo('transform', `translateY(${bottom})`, 'translateY(100%)');
  148. break;
  149. }
  150. return baseAnimation.easing('cubic-bezier(.36,.66,.04,1)').duration(300).addAnimation(wrapperAnimation);
  151. };
  152. /**
  153. * MD Toast Enter Animation
  154. */
  155. const mdEnterAnimation = (baseEl, opts) => {
  156. const baseAnimation = createAnimation();
  157. const wrapperAnimation = createAnimation();
  158. const { position, top, bottom } = opts;
  159. const root = getElementRoot(baseEl);
  160. const wrapperEl = root.querySelector('.toast-wrapper');
  161. wrapperAnimation.addElement(wrapperEl);
  162. switch (position) {
  163. case 'top':
  164. wrapperEl.style.setProperty('transform', `translateY(${top})`);
  165. wrapperAnimation.fromTo('opacity', 0.01, 1);
  166. break;
  167. case 'middle':
  168. const topPosition = getOffsetForMiddlePosition(baseEl.clientHeight, wrapperEl.clientHeight);
  169. wrapperEl.style.top = `${topPosition}px`;
  170. wrapperAnimation.fromTo('opacity', 0.01, 1);
  171. break;
  172. default:
  173. wrapperEl.style.setProperty('transform', `translateY(${bottom})`);
  174. wrapperAnimation.fromTo('opacity', 0.01, 1);
  175. break;
  176. }
  177. return baseAnimation.easing('cubic-bezier(.36,.66,.04,1)').duration(400).addAnimation(wrapperAnimation);
  178. };
  179. /**
  180. * md Toast Leave Animation
  181. */
  182. const mdLeaveAnimation = (baseEl) => {
  183. const baseAnimation = createAnimation();
  184. const wrapperAnimation = createAnimation();
  185. const root = getElementRoot(baseEl);
  186. const wrapperEl = root.querySelector('.toast-wrapper');
  187. wrapperAnimation.addElement(wrapperEl).fromTo('opacity', 0.99, 0);
  188. return baseAnimation.easing('cubic-bezier(.36,.66,.04,1)').duration(300).addAnimation(wrapperAnimation);
  189. };
  190. /**
  191. * Create a gesture that allows the Toast
  192. * to be swiped to dismiss.
  193. * @param el - The Toast element
  194. * @param toastPosition - The last computed position of the Toast. This is computed in the "present" method.
  195. * @param onDismiss - A callback to fire when the Toast was swiped to dismiss.
  196. */
  197. const createSwipeToDismissGesture = (el, toastPosition, onDismiss) => {
  198. /**
  199. * Users should swipe on the visible toast wrapper
  200. * rather than on ion-toast which covers the entire screen.
  201. * When testing the class instance the inner wrapper will not
  202. * be defined. As a result, we use a placeholder element in those environments.
  203. */
  204. const wrapperEl = getElementRoot(el).querySelector('.toast-wrapper');
  205. const hostElHeight = el.clientHeight;
  206. const wrapperElBox = wrapperEl.getBoundingClientRect();
  207. /**
  208. * The maximum amount that
  209. * the toast can be swiped. This should
  210. * account for the wrapper element's height
  211. * too so the toast can be swiped offscreen
  212. * completely.
  213. */
  214. let MAX_SWIPE_DISTANCE = 0;
  215. /**
  216. * The step value at which a toast
  217. * is eligible for dismissing via gesture.
  218. */
  219. const DISMISS_THRESHOLD = 0.5;
  220. /**
  221. * The middle position Toast starts 50% of the way
  222. * through the animation, so we need to use this
  223. * as the starting point for our step values.
  224. */
  225. const STEP_OFFSET = el.position === 'middle' ? 0.5 : 0;
  226. /**
  227. * When the Toast is at the top users will be
  228. * swiping up. As a result, the delta values will be
  229. * negative numbers which will result in negative steps
  230. * and thresholds. As a result, we need to make those numbers
  231. * positive.
  232. */
  233. const INVERSION_FACTOR = el.position === 'top' ? -1 : 1;
  234. /**
  235. * The top offset that places the
  236. * toast in the middle of the screen.
  237. * Only needed when position="middle".
  238. */
  239. const topPosition = getOffsetForMiddlePosition(hostElHeight, wrapperElBox.height);
  240. const SWIPE_UP_DOWN_KEYFRAMES = [
  241. { offset: 0, transform: `translateY(-${topPosition + wrapperElBox.height}px)` },
  242. { offset: 0.5, transform: `translateY(0px)` },
  243. { offset: 1, transform: `translateY(${topPosition + wrapperElBox.height}px)` },
  244. ];
  245. const swipeAnimation = createAnimation('toast-swipe-to-dismiss-animation')
  246. .addElement(wrapperEl)
  247. /**
  248. * The specific value here does not actually
  249. * matter. We just need this to be a positive
  250. * value so the animation does not jump
  251. * to the end when the user beings to drag.
  252. */
  253. .duration(100);
  254. switch (el.position) {
  255. case 'middle':
  256. MAX_SWIPE_DISTANCE = hostElHeight + wrapperElBox.height;
  257. swipeAnimation.keyframes(SWIPE_UP_DOWN_KEYFRAMES);
  258. /**
  259. * Toast can be swiped up or down but
  260. * should start in the middle of the screen.
  261. */
  262. swipeAnimation.progressStart(true, 0.5);
  263. break;
  264. case 'top':
  265. /**
  266. * The bottom edge of the wrapper
  267. * includes the distance between the top
  268. * of the screen and the top of the wrapper
  269. * as well as the wrapper height so the wrapper
  270. * can be dragged fully offscreen.
  271. */
  272. MAX_SWIPE_DISTANCE = wrapperElBox.bottom;
  273. swipeAnimation.keyframes([
  274. { offset: 0, transform: `translateY(${toastPosition.top})` },
  275. { offset: 1, transform: 'translateY(-100%)' },
  276. ]);
  277. swipeAnimation.progressStart(true, 0);
  278. break;
  279. case 'bottom':
  280. default:
  281. /**
  282. * This computes the distance between the
  283. * top of the wrapper and the bottom of the
  284. * screen including the height of the wrapper
  285. * element so it can be dragged fully offscreen.
  286. */
  287. MAX_SWIPE_DISTANCE = hostElHeight - wrapperElBox.top;
  288. swipeAnimation.keyframes([
  289. { offset: 0, transform: `translateY(${toastPosition.bottom})` },
  290. { offset: 1, transform: 'translateY(100%)' },
  291. ]);
  292. swipeAnimation.progressStart(true, 0);
  293. break;
  294. }
  295. const computeStep = (delta) => {
  296. return (delta * INVERSION_FACTOR) / MAX_SWIPE_DISTANCE;
  297. };
  298. const onMove = (detail) => {
  299. const step = STEP_OFFSET + computeStep(detail.deltaY);
  300. swipeAnimation.progressStep(step);
  301. };
  302. const onEnd = (detail) => {
  303. const velocity = detail.velocityY;
  304. const threshold = ((detail.deltaY + velocity * 1000) / MAX_SWIPE_DISTANCE) * INVERSION_FACTOR;
  305. /**
  306. * Disable the gesture for the remainder of the animation.
  307. * It will be re-enabled if the toast animates back to
  308. * its initial presented position.
  309. */
  310. gesture.enable(false);
  311. let shouldDismiss = true;
  312. let playTo = 1;
  313. let step = 0;
  314. let remainingDistance = 0;
  315. if (el.position === 'middle') {
  316. /**
  317. * A middle positioned Toast appears
  318. * in the middle of the screen (at animation offset 0.5).
  319. * As a result, the threshold will be calculated relative
  320. * to this starting position. In other words at animation offset 0.5
  321. * the threshold will be 0. We want the middle Toast to be eligible
  322. * for dismiss when the user has swiped either half way up or down the
  323. * screen. As a result, we divide DISMISS_THRESHOLD in half. We also
  324. * consider when the threshold is a negative in the event the
  325. * user drags up (since the deltaY will also be negative).
  326. */
  327. shouldDismiss = threshold >= DISMISS_THRESHOLD / 2 || threshold <= -DISMISS_THRESHOLD / 2;
  328. /**
  329. * Since we are replacing the keyframes
  330. * below the animation always starts from
  331. * the beginning of the new keyframes.
  332. * Similarly, we are always playing to
  333. * the end of the new keyframes.
  334. */
  335. playTo = 1;
  336. step = 0;
  337. /**
  338. * The Toast should animate from wherever its
  339. * current position is to the desired end state.
  340. *
  341. * To begin, we get the current position of the
  342. * Toast for its starting state.
  343. */
  344. const wrapperElBox = wrapperEl.getBoundingClientRect();
  345. const startOffset = wrapperElBox.top - topPosition;
  346. const startPosition = `${startOffset}px`;
  347. /**
  348. * If the deltaY is negative then the user is swiping
  349. * up, so the Toast should animate to the top of the screen.
  350. * If the deltaY is positive then the user is swiping
  351. * down, so the Toast should animate to the bottom of the screen.
  352. * We also account for when the deltaY is 0, but realistically
  353. * that should never happen because it means the user did not drag
  354. * the toast.
  355. */
  356. const offsetFactor = detail.deltaY <= 0 ? -1 : 1;
  357. const endOffset = (topPosition + wrapperElBox.height) * offsetFactor;
  358. /**
  359. * If the Toast should dismiss
  360. * then we need to figure out which edge of
  361. * the screen it should animate towards.
  362. * By default, the Toast will come
  363. * back to its default state in the
  364. * middle of the screen.
  365. */
  366. const endPosition = shouldDismiss ? `${endOffset}px` : '0px';
  367. const KEYFRAMES = [
  368. { offset: 0, transform: `translateY(${startPosition})` },
  369. { offset: 1, transform: `translateY(${endPosition})` },
  370. ];
  371. swipeAnimation.keyframes(KEYFRAMES);
  372. /**
  373. * Compute the remaining amount of pixels the
  374. * toast needs to move to be fully dismissed.
  375. */
  376. remainingDistance = endOffset - startOffset;
  377. }
  378. else {
  379. shouldDismiss = threshold >= DISMISS_THRESHOLD;
  380. playTo = shouldDismiss ? 1 : 0;
  381. step = computeStep(detail.deltaY);
  382. /**
  383. * Compute the remaining amount of pixels the
  384. * toast needs to move to be fully dismissed.
  385. */
  386. const remainingStepAmount = shouldDismiss ? 1 - step : step;
  387. remainingDistance = remainingStepAmount * MAX_SWIPE_DISTANCE;
  388. }
  389. /**
  390. * The animation speed should depend on how quickly
  391. * the user flicks the toast across the screen. However,
  392. * it should be no slower than 200ms.
  393. * We use Math.abs on the remainingDistance because that value
  394. * can be negative when swiping up on a middle position toast.
  395. */
  396. const duration = Math.min(Math.abs(remainingDistance) / Math.abs(velocity), 200);
  397. swipeAnimation
  398. .onFinish(() => {
  399. if (shouldDismiss) {
  400. onDismiss();
  401. swipeAnimation.destroy();
  402. }
  403. else {
  404. if (el.position === 'middle') {
  405. /**
  406. * If the toast snapped back to
  407. * the middle of the screen we need
  408. * to reset the keyframes
  409. * so the toast can be swiped
  410. * up or down again.
  411. */
  412. swipeAnimation.keyframes(SWIPE_UP_DOWN_KEYFRAMES).progressStart(true, 0.5);
  413. }
  414. else {
  415. swipeAnimation.progressStart(true, 0);
  416. }
  417. /**
  418. * If the toast did not dismiss then
  419. * the user should be able to swipe again.
  420. */
  421. gesture.enable(true);
  422. }
  423. /**
  424. * This must be a one time callback
  425. * otherwise a new callback will
  426. * be added every time onEnd runs.
  427. */
  428. }, { oneTimeCallback: true })
  429. .progressEnd(playTo, step, duration);
  430. };
  431. const gesture = createGesture({
  432. el: wrapperEl,
  433. gestureName: 'toast-swipe-to-dismiss',
  434. gesturePriority: OVERLAY_GESTURE_PRIORITY,
  435. /**
  436. * Toast only supports vertical swipes.
  437. * This needs to be updated if we later
  438. * support horizontal swipes.
  439. */
  440. direction: 'y',
  441. onMove,
  442. onEnd,
  443. });
  444. return gesture;
  445. };
  446. const toastIosCss = ":host{--border-width:0;--border-style:none;--border-color:initial;--box-shadow:none;--min-width:auto;--width:auto;--min-height:auto;--height:auto;--max-height:auto;--white-space:normal;top:0;display:block;position:absolute;width:100%;height:100%;outline:none;color:var(--color);font-family:var(--ion-font-family, inherit);contain:strict;z-index:1001;pointer-events:none}:host{inset-inline-start:0}:host(.overlay-hidden){display:none}:host(.ion-color){--button-color:inherit;color:var(--ion-color-contrast)}:host(.ion-color) .toast-button-cancel{color:inherit}:host(.ion-color) .toast-wrapper{background:var(--ion-color-base)}.toast-wrapper{border-radius:var(--border-radius);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);border-width:var(--border-width);border-style:var(--border-style);border-color:var(--border-color);background:var(--background);-webkit-box-shadow:var(--box-shadow);box-shadow:var(--box-shadow);pointer-events:auto}.toast-wrapper{inset-inline-start:var(--start);inset-inline-end:var(--end)}.toast-wrapper.toast-top{-webkit-transform:translate3d(0, -100%, 0);transform:translate3d(0, -100%, 0);top:0}.toast-wrapper.toast-bottom{-webkit-transform:translate3d(0, 100%, 0);transform:translate3d(0, 100%, 0);bottom:0}.toast-container{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;height:inherit;min-height:inherit;max-height:inherit;contain:content}.toast-layout-stacked .toast-container{-ms-flex-wrap:wrap;flex-wrap:wrap}.toast-layout-baseline .toast-content{display:-ms-flexbox;display:flex;-ms-flex:1;flex:1;-ms-flex-direction:column;flex-direction:column;-ms-flex-pack:center;justify-content:center}.toast-icon{-webkit-margin-start:16px;margin-inline-start:16px}.toast-content{min-width:0}.toast-message{-ms-flex:1;flex:1;white-space:var(--white-space)}.toast-button-group{display:-ms-flexbox;display:flex}.toast-layout-stacked .toast-button-group{-ms-flex-pack:end;justify-content:end;width:100%}.toast-button{border:0;outline:none;color:var(--button-color);z-index:0}.toast-icon,.toast-button-icon{font-size:1.4em}.toast-button-inner{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}@media (any-hover: hover){.toast-button:hover{cursor:pointer}}:host{--background:var(--ion-color-step-50, var(--ion-background-color-step-50, #f2f2f2));--border-radius:14px;--button-color:var(--ion-color-primary, #0054e9);--color:var(--ion-color-step-850, var(--ion-text-color-step-150, #262626));--max-width:700px;--max-height:478px;--start:10px;--end:10px;font-size:clamp(14px, 0.875rem, 43.4px)}.toast-wrapper{-webkit-margin-start:auto;margin-inline-start:auto;-webkit-margin-end:auto;margin-inline-end:auto;margin-top:auto;margin-bottom:auto;display:block;position:absolute;z-index:10}@supports ((-webkit-backdrop-filter: blur(0)) or (backdrop-filter: blur(0))){:host(.toast-translucent) .toast-wrapper{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)}:host(.ion-color.toast-translucent) .toast-wrapper{background:rgba(var(--ion-color-base-rgb), 0.8)}}.toast-wrapper.toast-middle{opacity:0.01}.toast-content{-webkit-padding-start:15px;padding-inline-start:15px;-webkit-padding-end:15px;padding-inline-end:15px;padding-top:15px;padding-bottom:15px}.toast-header{margin-bottom:2px;font-weight:500}.toast-button{-webkit-padding-start:15px;padding-inline-start:15px;-webkit-padding-end:15px;padding-inline-end:15px;padding-top:10px;padding-bottom:10px;min-height:44px;-webkit-transition:background-color, opacity 100ms linear;transition:background-color, opacity 100ms linear;border:0;background-color:transparent;font-family:var(--ion-font-family);font-size:clamp(17px, 1.0625rem, 21.998px);font-weight:500;overflow:hidden}.toast-button.ion-activated{opacity:0.4}@media (any-hover: hover){.toast-button:hover{opacity:0.6}}";
  447. const IonToastIosStyle0 = toastIosCss;
  448. const toastMdCss = ":host{--border-width:0;--border-style:none;--border-color:initial;--box-shadow:none;--min-width:auto;--width:auto;--min-height:auto;--height:auto;--max-height:auto;--white-space:normal;top:0;display:block;position:absolute;width:100%;height:100%;outline:none;color:var(--color);font-family:var(--ion-font-family, inherit);contain:strict;z-index:1001;pointer-events:none}:host{inset-inline-start:0}:host(.overlay-hidden){display:none}:host(.ion-color){--button-color:inherit;color:var(--ion-color-contrast)}:host(.ion-color) .toast-button-cancel{color:inherit}:host(.ion-color) .toast-wrapper{background:var(--ion-color-base)}.toast-wrapper{border-radius:var(--border-radius);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);border-width:var(--border-width);border-style:var(--border-style);border-color:var(--border-color);background:var(--background);-webkit-box-shadow:var(--box-shadow);box-shadow:var(--box-shadow);pointer-events:auto}.toast-wrapper{inset-inline-start:var(--start);inset-inline-end:var(--end)}.toast-wrapper.toast-top{-webkit-transform:translate3d(0, -100%, 0);transform:translate3d(0, -100%, 0);top:0}.toast-wrapper.toast-bottom{-webkit-transform:translate3d(0, 100%, 0);transform:translate3d(0, 100%, 0);bottom:0}.toast-container{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center;height:inherit;min-height:inherit;max-height:inherit;contain:content}.toast-layout-stacked .toast-container{-ms-flex-wrap:wrap;flex-wrap:wrap}.toast-layout-baseline .toast-content{display:-ms-flexbox;display:flex;-ms-flex:1;flex:1;-ms-flex-direction:column;flex-direction:column;-ms-flex-pack:center;justify-content:center}.toast-icon{-webkit-margin-start:16px;margin-inline-start:16px}.toast-content{min-width:0}.toast-message{-ms-flex:1;flex:1;white-space:var(--white-space)}.toast-button-group{display:-ms-flexbox;display:flex}.toast-layout-stacked .toast-button-group{-ms-flex-pack:end;justify-content:end;width:100%}.toast-button{border:0;outline:none;color:var(--button-color);z-index:0}.toast-icon,.toast-button-icon{font-size:1.4em}.toast-button-inner{display:-ms-flexbox;display:flex;-ms-flex-align:center;align-items:center}@media (any-hover: hover){.toast-button:hover{cursor:pointer}}:host{--background:var(--ion-color-step-800, var(--ion-background-color-step-800, #333333));--border-radius:4px;--box-shadow:0 3px 5px -1px rgba(0, 0, 0, 0.2), 0 6px 10px 0 rgba(0, 0, 0, 0.14), 0 1px 18px 0 rgba(0, 0, 0, 0.12);--button-color:var(--ion-color-primary, #0054e9);--color:var(--ion-color-step-50, var(--ion-text-color-step-950, #f2f2f2));--max-width:700px;--start:8px;--end:8px;font-size:0.875rem}.toast-wrapper{-webkit-margin-start:auto;margin-inline-start:auto;-webkit-margin-end:auto;margin-inline-end:auto;margin-top:auto;margin-bottom:auto;display:block;position:absolute;opacity:0.01;z-index:10}.toast-content{-webkit-padding-start:16px;padding-inline-start:16px;-webkit-padding-end:16px;padding-inline-end:16px;padding-top:14px;padding-bottom:14px}.toast-header{margin-bottom:2px;font-weight:500;line-height:1.25rem}.toast-message{line-height:1.25rem}.toast-layout-baseline .toast-button-group-start{-webkit-margin-start:8px;margin-inline-start:8px}.toast-layout-stacked .toast-button-group-start{-webkit-margin-end:8px;margin-inline-end:8px;margin-top:8px}.toast-layout-baseline .toast-button-group-end{-webkit-margin-end:8px;margin-inline-end:8px}.toast-layout-stacked .toast-button-group-end{-webkit-margin-end:8px;margin-inline-end:8px;margin-bottom:8px}.toast-button{-webkit-padding-start:15px;padding-inline-start:15px;-webkit-padding-end:15px;padding-inline-end:15px;padding-top:10px;padding-bottom:10px;position:relative;background-color:transparent;font-family:var(--ion-font-family);font-size:0.875rem;font-weight:500;letter-spacing:0.84px;text-transform:uppercase;overflow:hidden}.toast-button-cancel{color:var(--ion-color-step-100, var(--ion-text-color-step-900, #e6e6e6))}.toast-button-icon-only{border-radius:50%;-webkit-padding-start:9px;padding-inline-start:9px;-webkit-padding-end:9px;padding-inline-end:9px;padding-top:9px;padding-bottom:9px;width:36px;height:36px}@media (any-hover: hover){.toast-button:hover{background-color:rgba(var(--ion-color-primary-rgb, 0, 84, 233), 0.08)}.toast-button-cancel:hover{background-color:rgba(var(--ion-background-color-rgb, 255, 255, 255), 0.08)}}";
  449. const IonToastMdStyle0 = toastMdCss;
  450. const Toast = class {
  451. constructor(hostRef) {
  452. registerInstance(this, hostRef);
  453. this.didPresent = createEvent(this, "ionToastDidPresent", 7);
  454. this.willPresent = createEvent(this, "ionToastWillPresent", 7);
  455. this.willDismiss = createEvent(this, "ionToastWillDismiss", 7);
  456. this.didDismiss = createEvent(this, "ionToastDidDismiss", 7);
  457. this.didPresentShorthand = createEvent(this, "didPresent", 7);
  458. this.willPresentShorthand = createEvent(this, "willPresent", 7);
  459. this.willDismissShorthand = createEvent(this, "willDismiss", 7);
  460. this.didDismissShorthand = createEvent(this, "didDismiss", 7);
  461. this.delegateController = createDelegateController(this);
  462. this.lockController = createLockController();
  463. this.triggerController = createTriggerController();
  464. this.customHTMLEnabled = config.get('innerHTMLTemplatesEnabled', ENABLE_HTML_CONTENT_DEFAULT);
  465. this.presented = false;
  466. this.dispatchCancelHandler = (ev) => {
  467. const role = ev.detail.role;
  468. if (isCancel(role)) {
  469. const cancelButton = this.getButtons().find((b) => b.role === 'cancel');
  470. this.callButtonHandler(cancelButton);
  471. }
  472. };
  473. /**
  474. * Create a new swipe gesture so Toast
  475. * can be swiped to dismiss.
  476. */
  477. this.createSwipeGesture = (toastPosition) => {
  478. const gesture = (this.gesture = createSwipeToDismissGesture(this.el, toastPosition, () => {
  479. /**
  480. * If the gesture completed then
  481. * we should dismiss the toast.
  482. */
  483. this.dismiss(undefined, GESTURE);
  484. }));
  485. gesture.enable(true);
  486. };
  487. /**
  488. * Destroy an existing swipe gesture
  489. * so Toast can no longer be swiped to dismiss.
  490. */
  491. this.destroySwipeGesture = () => {
  492. const { gesture } = this;
  493. if (gesture === undefined) {
  494. return;
  495. }
  496. gesture.destroy();
  497. this.gesture = undefined;
  498. };
  499. /**
  500. * Returns `true` if swipeGesture
  501. * is configured to a value that enables the swipe behavior.
  502. * Returns `false` otherwise.
  503. */
  504. this.prefersSwipeGesture = () => {
  505. const { swipeGesture } = this;
  506. return swipeGesture === 'vertical';
  507. };
  508. this.revealContentToScreenReader = false;
  509. this.overlayIndex = undefined;
  510. this.delegate = undefined;
  511. this.hasController = false;
  512. this.color = undefined;
  513. this.enterAnimation = undefined;
  514. this.leaveAnimation = undefined;
  515. this.cssClass = undefined;
  516. this.duration = config.getNumber('toastDuration', 0);
  517. this.header = undefined;
  518. this.layout = 'baseline';
  519. this.message = undefined;
  520. this.keyboardClose = false;
  521. this.position = 'bottom';
  522. this.positionAnchor = undefined;
  523. this.buttons = undefined;
  524. this.translucent = false;
  525. this.animated = true;
  526. this.icon = undefined;
  527. this.htmlAttributes = undefined;
  528. this.swipeGesture = undefined;
  529. this.isOpen = false;
  530. this.trigger = undefined;
  531. }
  532. swipeGestureChanged() {
  533. /**
  534. * If the Toast is presented, then we need to destroy
  535. * any actives gestures before a new gesture is potentially
  536. * created below.
  537. *
  538. * If the Toast is dismissed, then no gesture should be available
  539. * since the Toast is not visible. This case should never
  540. * happen since the "dismiss" method handles destroying
  541. * any active swipe gestures, but we keep this code
  542. * around to handle the first case.
  543. */
  544. this.destroySwipeGesture();
  545. /**
  546. * A new swipe gesture should only be created
  547. * if the Toast is presented. If the Toast is not
  548. * yet presented then the "present" method will
  549. * handle calling the swipe gesture setup function.
  550. */
  551. if (this.presented && this.prefersSwipeGesture()) {
  552. /**
  553. * If the Toast is presented then
  554. * lastPresentedPosition is defined.
  555. */
  556. this.createSwipeGesture(this.lastPresentedPosition);
  557. }
  558. }
  559. onIsOpenChange(newValue, oldValue) {
  560. if (newValue === true && oldValue === false) {
  561. this.present();
  562. }
  563. else if (newValue === false && oldValue === true) {
  564. this.dismiss();
  565. }
  566. }
  567. triggerChanged() {
  568. const { trigger, el, triggerController } = this;
  569. if (trigger) {
  570. triggerController.addClickListener(el, trigger);
  571. }
  572. }
  573. connectedCallback() {
  574. prepareOverlay(this.el);
  575. this.triggerChanged();
  576. }
  577. disconnectedCallback() {
  578. this.triggerController.removeClickListener();
  579. }
  580. componentWillLoad() {
  581. var _a;
  582. if (!((_a = this.htmlAttributes) === null || _a === void 0 ? void 0 : _a.id)) {
  583. setOverlayId(this.el);
  584. }
  585. }
  586. componentDidLoad() {
  587. /**
  588. * If toast was rendered with isOpen="true"
  589. * then we should open toast immediately.
  590. */
  591. if (this.isOpen === true) {
  592. raf(() => this.present());
  593. }
  594. /**
  595. * When binding values in frameworks such as Angular
  596. * it is possible for the value to be set after the Web Component
  597. * initializes but before the value watcher is set up in Stencil.
  598. * As a result, the watcher callback may not be fired.
  599. * We work around this by manually calling the watcher
  600. * callback when the component has loaded and the watcher
  601. * is configured.
  602. */
  603. this.triggerChanged();
  604. }
  605. /**
  606. * Present the toast overlay after it has been created.
  607. */
  608. async present() {
  609. const unlock = await this.lockController.lock();
  610. await this.delegateController.attachViewToDom();
  611. const { el, position } = this;
  612. const anchor = this.getAnchorElement();
  613. const animationPosition = getAnimationPosition(position, anchor, getIonMode(this), el);
  614. /**
  615. * Cache the calculated position of the toast, so we can re-use it
  616. * in the dismiss animation.
  617. */
  618. this.lastPresentedPosition = animationPosition;
  619. await present(this, 'toastEnter', iosEnterAnimation, mdEnterAnimation, {
  620. position,
  621. top: animationPosition.top,
  622. bottom: animationPosition.bottom,
  623. });
  624. /**
  625. * Content is revealed to screen readers after
  626. * the transition to avoid jank since this
  627. * state updates will cause a re-render.
  628. */
  629. this.revealContentToScreenReader = true;
  630. if (this.duration > 0) {
  631. this.durationTimeout = setTimeout(() => this.dismiss(undefined, 'timeout'), this.duration);
  632. }
  633. /**
  634. * If the Toast has a swipe gesture then we can
  635. * create the gesture so users can swipe the
  636. * presented Toast.
  637. */
  638. if (this.prefersSwipeGesture()) {
  639. this.createSwipeGesture(animationPosition);
  640. }
  641. unlock();
  642. }
  643. /**
  644. * Dismiss the toast overlay after it has been presented.
  645. *
  646. * @param data Any data to emit in the dismiss events.
  647. * @param role The role of the element that is dismissing the toast.
  648. * This can be useful in a button handler for determining which button was
  649. * clicked to dismiss the toast.
  650. * Some examples include: ``"cancel"`, `"destructive"`, "selected"`, and `"backdrop"`.
  651. *
  652. * This is a no-op if the overlay has not been presented yet. If you want
  653. * to remove an overlay from the DOM that was never presented, use the
  654. * [remove](https://developer.mozilla.org/en-US/docs/Web/API/Element/remove) method.
  655. */
  656. async dismiss(data, role) {
  657. var _a, _b;
  658. const unlock = await this.lockController.lock();
  659. const { durationTimeout, position, lastPresentedPosition } = this;
  660. if (durationTimeout) {
  661. clearTimeout(durationTimeout);
  662. }
  663. const dismissed = await dismiss(this, data, role, 'toastLeave', iosLeaveAnimation, mdLeaveAnimation,
  664. /**
  665. * Fetch the cached position that was calculated back in the present
  666. * animation. We always want to animate the dismiss from the same
  667. * position the present stopped at, so the animation looks continuous.
  668. */
  669. {
  670. position,
  671. top: (_a = lastPresentedPosition === null || lastPresentedPosition === void 0 ? void 0 : lastPresentedPosition.top) !== null && _a !== void 0 ? _a : '',
  672. bottom: (_b = lastPresentedPosition === null || lastPresentedPosition === void 0 ? void 0 : lastPresentedPosition.bottom) !== null && _b !== void 0 ? _b : '',
  673. });
  674. if (dismissed) {
  675. this.delegateController.removeViewFromDom();
  676. this.revealContentToScreenReader = false;
  677. }
  678. this.lastPresentedPosition = undefined;
  679. /**
  680. * If the Toast has a swipe gesture then we can
  681. * safely destroy it now that it is dismissed.
  682. */
  683. this.destroySwipeGesture();
  684. unlock();
  685. return dismissed;
  686. }
  687. /**
  688. * Returns a promise that resolves when the toast did dismiss.
  689. */
  690. onDidDismiss() {
  691. return eventMethod(this.el, 'ionToastDidDismiss');
  692. }
  693. /**
  694. * Returns a promise that resolves when the toast will dismiss.
  695. */
  696. onWillDismiss() {
  697. return eventMethod(this.el, 'ionToastWillDismiss');
  698. }
  699. getButtons() {
  700. const buttons = this.buttons
  701. ? this.buttons.map((b) => {
  702. return typeof b === 'string' ? { text: b } : b;
  703. })
  704. : [];
  705. return buttons;
  706. }
  707. /**
  708. * Returns the element specified by the positionAnchor prop,
  709. * or undefined if prop's value is an ID string and the element
  710. * is not found in the DOM.
  711. */
  712. getAnchorElement() {
  713. const { position, positionAnchor, el } = this;
  714. /**
  715. * If positionAnchor is undefined then
  716. * no anchor should be used when presenting the toast.
  717. */
  718. if (positionAnchor === undefined) {
  719. return;
  720. }
  721. if (position === 'middle' && positionAnchor !== undefined) {
  722. printIonWarning('[ion-toast] - The positionAnchor property is ignored when using position="middle".', this.el);
  723. return undefined;
  724. }
  725. if (typeof positionAnchor === 'string') {
  726. /**
  727. * If the anchor is defined as an ID, find the element.
  728. * We do this on every present so the toast doesn't need
  729. * to account for the surrounding DOM changing since the
  730. * last time it was presented.
  731. */
  732. const foundEl = document.getElementById(positionAnchor);
  733. if (foundEl === null) {
  734. printIonWarning(`[ion-toast] - An anchor element with an ID of "${positionAnchor}" was not found in the DOM.`, el);
  735. return undefined;
  736. }
  737. return foundEl;
  738. }
  739. if (positionAnchor instanceof HTMLElement) {
  740. return positionAnchor;
  741. }
  742. printIonWarning('[ion-toast] - Invalid positionAnchor value:', positionAnchor, el);
  743. return undefined;
  744. }
  745. async buttonClick(button) {
  746. const role = button.role;
  747. if (isCancel(role)) {
  748. return this.dismiss(undefined, role);
  749. }
  750. const shouldDismiss = await this.callButtonHandler(button);
  751. if (shouldDismiss) {
  752. return this.dismiss(undefined, role);
  753. }
  754. return Promise.resolve();
  755. }
  756. async callButtonHandler(button) {
  757. if (button === null || button === void 0 ? void 0 : button.handler) {
  758. // a handler has been provided, execute it
  759. // pass the handler the values from the inputs
  760. try {
  761. const rtn = await safeCall(button.handler);
  762. if (rtn === false) {
  763. // if the return value of the handler is false then do not dismiss
  764. return false;
  765. }
  766. }
  767. catch (e) {
  768. printIonError('[ion-toast] - Exception in callButtonHandler:', e);
  769. }
  770. }
  771. return true;
  772. }
  773. renderButtons(buttons, side) {
  774. if (buttons.length === 0) {
  775. return;
  776. }
  777. const mode = getIonMode(this);
  778. const buttonGroupsClasses = {
  779. 'toast-button-group': true,
  780. [`toast-button-group-${side}`]: true,
  781. };
  782. return (h("div", { class: buttonGroupsClasses }, buttons.map((b) => (h("button", Object.assign({}, b.htmlAttributes, { type: "button", class: buttonClass(b), tabIndex: 0, onClick: () => this.buttonClick(b), part: buttonPart(b) }), h("div", { class: "toast-button-inner" }, b.icon && (h("ion-icon", { "aria-hidden": "true", icon: b.icon, slot: b.text === undefined ? 'icon-only' : undefined, class: "toast-button-icon" })), b.text), mode === 'md' && (h("ion-ripple-effect", { type: b.icon !== undefined && b.text === undefined ? 'unbounded' : 'bounded' })))))));
  783. }
  784. /**
  785. * Render the `message` property.
  786. * @param key - A key to give the element a stable identity. This is used to improve compatibility with screen readers.
  787. * @param ariaHidden - If "true" then content will be hidden from screen readers.
  788. */
  789. renderToastMessage(key, ariaHidden = null) {
  790. const { customHTMLEnabled, message } = this;
  791. if (customHTMLEnabled) {
  792. return (h("div", { key: key, "aria-hidden": ariaHidden, class: "toast-message", part: "message", innerHTML: sanitizeDOMString(message) }));
  793. }
  794. return (h("div", { key: key, "aria-hidden": ariaHidden, class: "toast-message", part: "message" }, message));
  795. }
  796. /**
  797. * Render the `header` property.
  798. * @param key - A key to give the element a stable identity. This is used to improve compatibility with screen readers.
  799. * @param ariaHidden - If "true" then content will be hidden from screen readers.
  800. */
  801. renderHeader(key, ariaHidden = null) {
  802. return (h("div", { key: key, class: "toast-header", "aria-hidden": ariaHidden, part: "header" }, this.header));
  803. }
  804. render() {
  805. const { layout, el, revealContentToScreenReader, header, message } = this;
  806. const allButtons = this.getButtons();
  807. const startButtons = allButtons.filter((b) => b.side === 'start');
  808. const endButtons = allButtons.filter((b) => b.side !== 'start');
  809. const mode = getIonMode(this);
  810. const wrapperClass = {
  811. 'toast-wrapper': true,
  812. [`toast-${this.position}`]: true,
  813. [`toast-layout-${layout}`]: true,
  814. };
  815. /**
  816. * Stacked buttons are only meant to be
  817. * used with one type of button.
  818. */
  819. if (layout === 'stacked' && startButtons.length > 0 && endButtons.length > 0) {
  820. printIonWarning('[ion-toast] - This toast is using start and end buttons with the stacked toast layout. We recommend following the best practice of using either start or end buttons with the stacked toast layout.', el);
  821. }
  822. return (h(Host, Object.assign({ key: 'a2216d860255c99337464370dcb12f6125871a50', tabindex: "-1" }, this.htmlAttributes, { style: {
  823. zIndex: `${60000 + this.overlayIndex}`,
  824. }, class: createColorClasses(this.color, Object.assign(Object.assign({ [mode]: true }, getClassMap(this.cssClass)), { 'overlay-hidden': true, 'toast-translucent': this.translucent })), onIonToastWillDismiss: this.dispatchCancelHandler }), h("div", { key: 'd5adf8bc4c6c52431600033a76c4795689f9b412', class: wrapperClass }, h("div", { key: 'ab694497ae37ceba123217eb48800129b9bebb84', class: "toast-container", part: "container" }, this.renderButtons(startButtons, 'start'), this.icon !== undefined && (h("ion-icon", { key: '224854fa3989ce0eb69416cb5b0cc55fc9f131ea', class: "toast-icon", part: "icon", icon: this.icon, lazy: false, "aria-hidden": "true" })), h("div", { key: 'c8e11fb5bdac202987f5c8613a0ebbd42bda946e', class: "toast-content", role: "status", "aria-atomic": "true", "aria-live": "polite" }, !revealContentToScreenReader && header !== undefined && this.renderHeader('oldHeader', 'true'), !revealContentToScreenReader && message !== undefined && this.renderToastMessage('oldMessage', 'true'), revealContentToScreenReader && header !== undefined && this.renderHeader('header'), revealContentToScreenReader && message !== undefined && this.renderToastMessage('header')), this.renderButtons(endButtons, 'end')))));
  825. }
  826. get el() { return getElement(this); }
  827. static get watchers() { return {
  828. "swipeGesture": ["swipeGestureChanged"],
  829. "isOpen": ["onIsOpenChange"],
  830. "trigger": ["triggerChanged"]
  831. }; }
  832. };
  833. const buttonClass = (button) => {
  834. return {
  835. 'toast-button': true,
  836. 'toast-button-icon-only': button.icon !== undefined && button.text === undefined,
  837. [`toast-button-${button.role}`]: button.role !== undefined,
  838. 'ion-focusable': true,
  839. 'ion-activatable': true,
  840. };
  841. };
  842. const buttonPart = (button) => {
  843. return isCancel(button.role) ? 'button cancel' : 'button';
  844. };
  845. Toast.style = {
  846. ios: IonToastIosStyle0,
  847. md: IonToastMdStyle0
  848. };
  849. export { Toast as ion_toast };