content.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*!
  2. * (C) Ionic http://ionicframework.com - MIT License
  3. */
  4. import { proxyCustomElement, HTMLElement, createEvent, Build, readTask, forceUpdate, h, Host } from '@stencil/core/internal/client';
  5. import { i as inheritAriaAttributes, j as hasLazyBuild, c as componentOnReady } from './helpers.js';
  6. import { b as getIonMode, a as isPlatform } from './ionic-global.js';
  7. import { i as isRTL } from './dir.js';
  8. import { c as createColorClasses, h as hostContext } from './theme.js';
  9. const contentCss = ":host{--background:var(--ion-background-color, #fff);--color:var(--ion-text-color, #000);--padding-top:0px;--padding-bottom:0px;--padding-start:0px;--padding-end:0px;--keyboard-offset:0px;--offset-top:0px;--offset-bottom:0px;--overflow:auto;display:block;position:relative;-ms-flex:1;flex:1;width:100%;height:100%;margin:0 !important;padding:0 !important;font-family:var(--ion-font-family, inherit);contain:size style}:host(.ion-color) .inner-scroll{background:var(--ion-color-base);color:var(--ion-color-contrast)}#background-content{left:0px;right:0px;top:calc(var(--offset-top) * -1);bottom:calc(var(--offset-bottom) * -1);position:absolute;background:var(--background)}.inner-scroll{left:0px;right:0px;top:calc(var(--offset-top) * -1);bottom:calc(var(--offset-bottom) * -1);-webkit-padding-start:var(--padding-start);padding-inline-start:var(--padding-start);-webkit-padding-end:var(--padding-end);padding-inline-end:var(--padding-end);padding-top:calc(var(--padding-top) + var(--offset-top));padding-bottom:calc(var(--padding-bottom) + var(--keyboard-offset) + var(--offset-bottom));position:absolute;color:var(--color);-webkit-box-sizing:border-box;box-sizing:border-box;overflow:hidden;-ms-touch-action:pan-x pan-y pinch-zoom;touch-action:pan-x pan-y pinch-zoom}.scroll-y,.scroll-x{-webkit-overflow-scrolling:touch;z-index:0;will-change:scroll-position}.scroll-y{overflow-y:var(--overflow);overscroll-behavior-y:contain}.scroll-x{overflow-x:var(--overflow);overscroll-behavior-x:contain}.overscroll::before,.overscroll::after{position:absolute;width:1px;height:1px;content:\"\"}.overscroll::before{bottom:-1px}.overscroll::after{top:-1px}:host(.content-sizing){display:-ms-flexbox;display:flex;-ms-flex-direction:column;flex-direction:column;min-height:0;contain:none}:host(.content-sizing) .inner-scroll{position:relative;top:0;bottom:0;margin-top:calc(var(--offset-top) * -1);margin-bottom:calc(var(--offset-bottom) * -1)}.transition-effect{display:none;position:absolute;width:100%;height:100vh;opacity:0;pointer-events:none}:host(.content-ltr) .transition-effect{left:-100%;}:host(.content-rtl) .transition-effect{right:-100%;}.transition-cover{position:absolute;right:0;width:100%;height:100%;background:black;opacity:0.1}.transition-shadow{display:block;position:absolute;width:100%;height:100%;-webkit-box-shadow:inset -9px 0 9px 0 rgba(0, 0, 100, 0.03);box-shadow:inset -9px 0 9px 0 rgba(0, 0, 100, 0.03)}:host(.content-ltr) .transition-shadow{right:0;}:host(.content-rtl) .transition-shadow{left:0;-webkit-transform:scaleX(-1);transform:scaleX(-1)}::slotted([slot=fixed]){position:absolute;-webkit-transform:translateZ(0);transform:translateZ(0)}";
  10. const IonContentStyle0 = contentCss;
  11. const Content = /*@__PURE__*/ proxyCustomElement(class Content extends HTMLElement {
  12. constructor() {
  13. super();
  14. this.__registerHost();
  15. this.__attachShadow();
  16. this.ionScrollStart = createEvent(this, "ionScrollStart", 7);
  17. this.ionScroll = createEvent(this, "ionScroll", 7);
  18. this.ionScrollEnd = createEvent(this, "ionScrollEnd", 7);
  19. this.watchDog = null;
  20. this.isScrolling = false;
  21. this.lastScroll = 0;
  22. this.queued = false;
  23. this.cTop = -1;
  24. this.cBottom = -1;
  25. this.isMainContent = true;
  26. this.resizeTimeout = null;
  27. this.inheritedAttributes = {};
  28. this.tabsElement = null;
  29. // Detail is used in a hot loop in the scroll event, by allocating it here
  30. // V8 will be able to inline any read/write to it since it's a monomorphic class.
  31. // https://mrale.ph/blog/2015/01/11/whats-up-with-monomorphism.html
  32. this.detail = {
  33. scrollTop: 0,
  34. scrollLeft: 0,
  35. type: 'scroll',
  36. event: undefined,
  37. startX: 0,
  38. startY: 0,
  39. startTime: 0,
  40. currentX: 0,
  41. currentY: 0,
  42. velocityX: 0,
  43. velocityY: 0,
  44. deltaX: 0,
  45. deltaY: 0,
  46. currentTime: 0,
  47. data: undefined,
  48. isScrolling: true,
  49. };
  50. this.color = undefined;
  51. this.fullscreen = false;
  52. this.fixedSlotPlacement = 'after';
  53. this.forceOverscroll = undefined;
  54. this.scrollX = false;
  55. this.scrollY = true;
  56. this.scrollEvents = false;
  57. }
  58. componentWillLoad() {
  59. this.inheritedAttributes = inheritAriaAttributes(this.el);
  60. }
  61. connectedCallback() {
  62. this.isMainContent = this.el.closest('ion-menu, ion-popover, ion-modal') === null;
  63. /**
  64. * The fullscreen content offsets need to be
  65. * computed after the tab bar has loaded. Since
  66. * lazy evaluation means components are not hydrated
  67. * at the same time, we need to wait for the ionTabBarLoaded
  68. * event to fire. This does not impact dist-custom-elements
  69. * because there is no hydration there.
  70. */
  71. if (hasLazyBuild(this.el)) {
  72. /**
  73. * We need to cache the reference to the tabs.
  74. * If just the content is unmounted then we won't
  75. * be able to query for the closest tabs on disconnectedCallback
  76. * since the content has been removed from the DOM tree.
  77. */
  78. const closestTabs = (this.tabsElement = this.el.closest('ion-tabs'));
  79. if (closestTabs !== null) {
  80. /**
  81. * When adding and removing the event listener
  82. * we need to make sure we pass the same function reference
  83. * otherwise the event listener will not be removed properly.
  84. * We can't only pass `this.resize` because "this" in the function
  85. * context becomes a reference to IonTabs instead of IonContent.
  86. *
  87. * Additionally, we listen for ionTabBarLoaded on the IonTabs
  88. * instance rather than the IonTabBar instance. It's possible for
  89. * a tab bar to be conditionally rendered/mounted. Since ionTabBarLoaded
  90. * bubbles, we can catch any instances of child tab bars loading by listening
  91. * on IonTabs.
  92. */
  93. this.tabsLoadCallback = () => this.resize();
  94. closestTabs.addEventListener('ionTabBarLoaded', this.tabsLoadCallback);
  95. }
  96. }
  97. }
  98. disconnectedCallback() {
  99. this.onScrollEnd();
  100. if (hasLazyBuild(this.el)) {
  101. /**
  102. * The event listener and tabs caches need to
  103. * be cleared otherwise this will create a memory
  104. * leak where the IonTabs instance can never be
  105. * garbage collected.
  106. */
  107. const { tabsElement, tabsLoadCallback } = this;
  108. if (tabsElement !== null && tabsLoadCallback !== undefined) {
  109. tabsElement.removeEventListener('ionTabBarLoaded', tabsLoadCallback);
  110. }
  111. this.tabsElement = null;
  112. this.tabsLoadCallback = undefined;
  113. }
  114. }
  115. /**
  116. * Rotating certain devices can update
  117. * the safe area insets. As a result,
  118. * the fullscreen feature on ion-content
  119. * needs to be recalculated.
  120. *
  121. * We listen for "resize" because we
  122. * do not care what the orientation of
  123. * the device is. Other APIs
  124. * such as ScreenOrientation or
  125. * the deviceorientation event must have
  126. * permission from the user first whereas
  127. * the "resize" event does not.
  128. *
  129. * We also throttle the callback to minimize
  130. * thrashing when quickly resizing a window.
  131. */
  132. onResize() {
  133. if (this.resizeTimeout) {
  134. clearTimeout(this.resizeTimeout);
  135. this.resizeTimeout = null;
  136. }
  137. this.resizeTimeout = setTimeout(() => {
  138. /**
  139. * Resize should only happen
  140. * if the content is visible.
  141. * When the content is hidden
  142. * then offsetParent will be null.
  143. */
  144. if (this.el.offsetParent === null) {
  145. return;
  146. }
  147. this.resize();
  148. }, 100);
  149. }
  150. shouldForceOverscroll() {
  151. const { forceOverscroll } = this;
  152. const mode = getIonMode(this);
  153. return forceOverscroll === undefined ? mode === 'ios' && isPlatform('ios') : forceOverscroll;
  154. }
  155. resize() {
  156. /**
  157. * Only force update if the component is rendered in a browser context.
  158. * Using `forceUpdate` in a server context with pre-rendering can lead to an infinite loop.
  159. * The `hydrateDocument` function in `@stencil/core` will render the `ion-content`, but
  160. * `forceUpdate` will trigger another render, locking up the server.
  161. *
  162. * TODO: Remove if STENCIL-834 determines Stencil will account for this.
  163. */
  164. if (Build.isBrowser) {
  165. if (this.fullscreen) {
  166. readTask(() => this.readDimensions());
  167. }
  168. else if (this.cTop !== 0 || this.cBottom !== 0) {
  169. this.cTop = this.cBottom = 0;
  170. forceUpdate(this);
  171. }
  172. }
  173. }
  174. readDimensions() {
  175. const page = getPageElement(this.el);
  176. const top = Math.max(this.el.offsetTop, 0);
  177. const bottom = Math.max(page.offsetHeight - top - this.el.offsetHeight, 0);
  178. const dirty = top !== this.cTop || bottom !== this.cBottom;
  179. if (dirty) {
  180. this.cTop = top;
  181. this.cBottom = bottom;
  182. forceUpdate(this);
  183. }
  184. }
  185. onScroll(ev) {
  186. const timeStamp = Date.now();
  187. const shouldStart = !this.isScrolling;
  188. this.lastScroll = timeStamp;
  189. if (shouldStart) {
  190. this.onScrollStart();
  191. }
  192. if (!this.queued && this.scrollEvents) {
  193. this.queued = true;
  194. readTask((ts) => {
  195. this.queued = false;
  196. this.detail.event = ev;
  197. updateScrollDetail(this.detail, this.scrollEl, ts, shouldStart);
  198. this.ionScroll.emit(this.detail);
  199. });
  200. }
  201. }
  202. /**
  203. * Get the element where the actual scrolling takes place.
  204. * This element can be used to subscribe to `scroll` events or manually modify
  205. * `scrollTop`. However, it's recommended to use the API provided by `ion-content`:
  206. *
  207. * i.e. Using `ionScroll`, `ionScrollStart`, `ionScrollEnd` for scrolling events
  208. * and `scrollToPoint()` to scroll the content into a certain point.
  209. */
  210. async getScrollElement() {
  211. /**
  212. * If this gets called in certain early lifecycle hooks (ex: Vue onMounted),
  213. * scrollEl won't be defined yet with the custom elements build, so wait for it to load in.
  214. */
  215. if (!this.scrollEl) {
  216. await new Promise((resolve) => componentOnReady(this.el, resolve));
  217. }
  218. return Promise.resolve(this.scrollEl);
  219. }
  220. /**
  221. * Returns the background content element.
  222. * @internal
  223. */
  224. async getBackgroundElement() {
  225. if (!this.backgroundContentEl) {
  226. await new Promise((resolve) => componentOnReady(this.el, resolve));
  227. }
  228. return Promise.resolve(this.backgroundContentEl);
  229. }
  230. /**
  231. * Scroll to the top of the component.
  232. *
  233. * @param duration The amount of time to take scrolling to the top. Defaults to `0`.
  234. */
  235. scrollToTop(duration = 0) {
  236. return this.scrollToPoint(undefined, 0, duration);
  237. }
  238. /**
  239. * Scroll to the bottom of the component.
  240. *
  241. * @param duration The amount of time to take scrolling to the bottom. Defaults to `0`.
  242. */
  243. async scrollToBottom(duration = 0) {
  244. const scrollEl = await this.getScrollElement();
  245. const y = scrollEl.scrollHeight - scrollEl.clientHeight;
  246. return this.scrollToPoint(undefined, y, duration);
  247. }
  248. /**
  249. * Scroll by a specified X/Y distance in the component.
  250. *
  251. * @param x The amount to scroll by on the horizontal axis.
  252. * @param y The amount to scroll by on the vertical axis.
  253. * @param duration The amount of time to take scrolling by that amount.
  254. */
  255. async scrollByPoint(x, y, duration) {
  256. const scrollEl = await this.getScrollElement();
  257. return this.scrollToPoint(x + scrollEl.scrollLeft, y + scrollEl.scrollTop, duration);
  258. }
  259. /**
  260. * Scroll to a specified X/Y location in the component.
  261. *
  262. * @param x The point to scroll to on the horizontal axis.
  263. * @param y The point to scroll to on the vertical axis.
  264. * @param duration The amount of time to take scrolling to that point. Defaults to `0`.
  265. */
  266. async scrollToPoint(x, y, duration = 0) {
  267. const el = await this.getScrollElement();
  268. if (duration < 32) {
  269. if (y != null) {
  270. el.scrollTop = y;
  271. }
  272. if (x != null) {
  273. el.scrollLeft = x;
  274. }
  275. return;
  276. }
  277. let resolve;
  278. let startTime = 0;
  279. const promise = new Promise((r) => (resolve = r));
  280. const fromY = el.scrollTop;
  281. const fromX = el.scrollLeft;
  282. const deltaY = y != null ? y - fromY : 0;
  283. const deltaX = x != null ? x - fromX : 0;
  284. // scroll loop
  285. const step = (timeStamp) => {
  286. const linearTime = Math.min(1, (timeStamp - startTime) / duration) - 1;
  287. const easedT = Math.pow(linearTime, 3) + 1;
  288. if (deltaY !== 0) {
  289. el.scrollTop = Math.floor(easedT * deltaY + fromY);
  290. }
  291. if (deltaX !== 0) {
  292. el.scrollLeft = Math.floor(easedT * deltaX + fromX);
  293. }
  294. if (easedT < 1) {
  295. // do not use DomController here
  296. // must use nativeRaf in order to fire in the next frame
  297. requestAnimationFrame(step);
  298. }
  299. else {
  300. resolve();
  301. }
  302. };
  303. // chill out for a frame first
  304. requestAnimationFrame((ts) => {
  305. startTime = ts;
  306. step(ts);
  307. });
  308. return promise;
  309. }
  310. onScrollStart() {
  311. this.isScrolling = true;
  312. this.ionScrollStart.emit({
  313. isScrolling: true,
  314. });
  315. if (this.watchDog) {
  316. clearInterval(this.watchDog);
  317. }
  318. // watchdog
  319. this.watchDog = setInterval(() => {
  320. if (this.lastScroll < Date.now() - 120) {
  321. this.onScrollEnd();
  322. }
  323. }, 100);
  324. }
  325. onScrollEnd() {
  326. if (this.watchDog)
  327. clearInterval(this.watchDog);
  328. this.watchDog = null;
  329. if (this.isScrolling) {
  330. this.isScrolling = false;
  331. this.ionScrollEnd.emit({
  332. isScrolling: false,
  333. });
  334. }
  335. }
  336. render() {
  337. const { fixedSlotPlacement, inheritedAttributes, isMainContent, scrollX, scrollY, el } = this;
  338. const rtl = isRTL(el) ? 'rtl' : 'ltr';
  339. const mode = getIonMode(this);
  340. const forceOverscroll = this.shouldForceOverscroll();
  341. const transitionShadow = mode === 'ios';
  342. this.resize();
  343. return (h(Host, Object.assign({ key: 'f2a24aa66dbf5c76f9d4b06f708eb73cadc239df', role: isMainContent ? 'main' : undefined, class: createColorClasses(this.color, {
  344. [mode]: true,
  345. 'content-sizing': hostContext('ion-popover', this.el),
  346. overscroll: forceOverscroll,
  347. [`content-${rtl}`]: true,
  348. }), style: {
  349. '--offset-top': `${this.cTop}px`,
  350. '--offset-bottom': `${this.cBottom}px`,
  351. } }, inheritedAttributes), h("div", { key: '6480ca7648b278abb36477b3838bccbcd4995e2a', ref: (el) => (this.backgroundContentEl = el), id: "background-content", part: "background" }), fixedSlotPlacement === 'before' ? h("slot", { name: "fixed" }) : null, h("div", { key: '29a23b663f5f0215bb000820c01e1814c0d55985', class: {
  352. 'inner-scroll': true,
  353. 'scroll-x': scrollX,
  354. 'scroll-y': scrollY,
  355. overscroll: (scrollX || scrollY) && forceOverscroll,
  356. }, ref: (scrollEl) => (this.scrollEl = scrollEl), onScroll: this.scrollEvents ? (ev) => this.onScroll(ev) : undefined, part: "scroll" }, h("slot", { key: '0fe1bd05609a4b88ae2ce9addf5d5dc5dc1806f0' })), transitionShadow ? (h("div", { class: "transition-effect" }, h("div", { class: "transition-cover" }), h("div", { class: "transition-shadow" }))) : null, fixedSlotPlacement === 'after' ? h("slot", { name: "fixed" }) : null));
  357. }
  358. get el() { return this; }
  359. static get style() { return IonContentStyle0; }
  360. }, [1, "ion-content", {
  361. "color": [513],
  362. "fullscreen": [4],
  363. "fixedSlotPlacement": [1, "fixed-slot-placement"],
  364. "forceOverscroll": [1028, "force-overscroll"],
  365. "scrollX": [4, "scroll-x"],
  366. "scrollY": [4, "scroll-y"],
  367. "scrollEvents": [4, "scroll-events"],
  368. "getScrollElement": [64],
  369. "getBackgroundElement": [64],
  370. "scrollToTop": [64],
  371. "scrollToBottom": [64],
  372. "scrollByPoint": [64],
  373. "scrollToPoint": [64]
  374. }, [[9, "resize", "onResize"]]]);
  375. const getParentElement = (el) => {
  376. var _a;
  377. if (el.parentElement) {
  378. // normal element with a parent element
  379. return el.parentElement;
  380. }
  381. if ((_a = el.parentNode) === null || _a === void 0 ? void 0 : _a.host) {
  382. // shadow dom's document fragment
  383. return el.parentNode.host;
  384. }
  385. return null;
  386. };
  387. const getPageElement = (el) => {
  388. const tabs = el.closest('ion-tabs');
  389. if (tabs) {
  390. return tabs;
  391. }
  392. /**
  393. * If we're in a popover, we need to use its wrapper so we can account for space
  394. * between the popover and the edges of the screen. But if the popover contains
  395. * its own page element, we should use that instead.
  396. */
  397. const page = el.closest('ion-app, ion-page, .ion-page, page-inner, .popover-content');
  398. if (page) {
  399. return page;
  400. }
  401. return getParentElement(el);
  402. };
  403. // ******** DOM READ ****************
  404. const updateScrollDetail = (detail, el, timestamp, shouldStart) => {
  405. const prevX = detail.currentX;
  406. const prevY = detail.currentY;
  407. const prevT = detail.currentTime;
  408. const currentX = el.scrollLeft;
  409. const currentY = el.scrollTop;
  410. const timeDelta = timestamp - prevT;
  411. if (shouldStart) {
  412. // remember the start positions
  413. detail.startTime = timestamp;
  414. detail.startX = currentX;
  415. detail.startY = currentY;
  416. detail.velocityX = detail.velocityY = 0;
  417. }
  418. detail.currentTime = timestamp;
  419. detail.currentX = detail.scrollLeft = currentX;
  420. detail.currentY = detail.scrollTop = currentY;
  421. detail.deltaX = currentX - detail.startX;
  422. detail.deltaY = currentY - detail.startY;
  423. if (timeDelta > 0 && timeDelta < 100) {
  424. const velocityX = (currentX - prevX) / timeDelta;
  425. const velocityY = (currentY - prevY) / timeDelta;
  426. detail.velocityX = velocityX * 0.7 + detail.velocityX * 0.3;
  427. detail.velocityY = velocityY * 0.7 + detail.velocityY * 0.3;
  428. }
  429. };
  430. function defineCustomElement() {
  431. if (typeof customElements === "undefined") {
  432. return;
  433. }
  434. const components = ["ion-content"];
  435. components.forEach(tagName => { switch (tagName) {
  436. case "ion-content":
  437. if (!customElements.get(tagName)) {
  438. customElements.define(tagName, Content);
  439. }
  440. break;
  441. } });
  442. }
  443. export { Content as C, defineCustomElement as d };