|
1 | 1 | import type { ComponentInterface, EventEmitter } from '@stencil/core';
|
2 | 2 | import { Build, Component, Element, Event, Host, Listen, Method, Prop, forceUpdate, h, readTask } from '@stencil/core';
|
3 |
| -import { componentOnReady } from '@utils/helpers'; |
| 3 | +import { componentOnReady, hasLazyBuild } from '@utils/helpers'; |
4 | 4 | import { isPlatform } from '@utils/platform';
|
5 | 5 | import { isRTL } from '@utils/rtl';
|
6 | 6 | import { createColorClasses, hostContext } from '@utils/theme';
|
@@ -34,6 +34,9 @@ export class Content implements ComponentInterface {
|
34 | 34 | private isMainContent = true;
|
35 | 35 | private resizeTimeout: ReturnType<typeof setTimeout> | null = null;
|
36 | 36 |
|
| 37 | + private tabsElement: HTMLElement | null = null; |
| 38 | + private tabsLoadCallback?: () => void; |
| 39 | + |
37 | 40 | // Detail is used in a hot loop in the scroll event, by allocating it here
|
38 | 41 | // V8 will be able to inline any read/write to it since it's a monomorphic class.
|
39 | 42 | // https://mrale.ph/blog/2015/01/11/whats-up-with-monomorphism.html
|
@@ -115,15 +118,61 @@ export class Content implements ComponentInterface {
|
115 | 118 |
|
116 | 119 | connectedCallback() {
|
117 | 120 | this.isMainContent = this.el.closest('ion-menu, ion-popover, ion-modal') === null;
|
| 121 | + |
| 122 | + /** |
| 123 | + * The fullscreen content offsets need to be |
| 124 | + * computed after the tab bar has loaded. Since |
| 125 | + * lazy evaluation means components are not hydrated |
| 126 | + * at the same time, we need to wait for the ionTabBarLoaded |
| 127 | + * event to fire. This does not impact dist-custom-elements |
| 128 | + * because there is no hydration there. |
| 129 | + */ |
| 130 | + if (hasLazyBuild(this.el)) { |
| 131 | + /** |
| 132 | + * We need to cache the reference to the tabs. |
| 133 | + * If just the content is unmounted then we won't |
| 134 | + * be able to query for the closest tabs on disconnectedCallback |
| 135 | + * since the content has been removed from the DOM tree. |
| 136 | + */ |
| 137 | + const closestTabs = (this.tabsElement = this.el.closest('ion-tabs')); |
| 138 | + if (closestTabs !== null) { |
| 139 | + /** |
| 140 | + * When adding and removing the event listener |
| 141 | + * we need to make sure we pass the same function reference |
| 142 | + * otherwise the event listener will not be removed properly. |
| 143 | + * We can't only pass `this.resize` because "this" in the function |
| 144 | + * context becomes a reference to IonTabs instead of IonContent. |
| 145 | + * |
| 146 | + * Additionally, we listen for ionTabBarLoaded on the IonTabs |
| 147 | + * instance rather than the IonTabBar instance. It's possible for |
| 148 | + * a tab bar to be conditionally rendered/mounted. Since ionTabBarLoaded |
| 149 | + * bubbles, we can catch any instances of child tab bars loading by listening |
| 150 | + * on IonTabs. |
| 151 | + */ |
| 152 | + this.tabsLoadCallback = () => this.resize(); |
| 153 | + closestTabs.addEventListener('ionTabBarLoaded', this.tabsLoadCallback); |
| 154 | + } |
| 155 | + } |
118 | 156 | }
|
119 | 157 |
|
120 | 158 | disconnectedCallback() {
|
121 | 159 | this.onScrollEnd();
|
122 |
| - } |
123 | 160 |
|
124 |
| - @Listen('appload', { target: 'window' }) |
125 |
| - onAppLoad() { |
126 |
| - this.resize(); |
| 161 | + if (hasLazyBuild(this.el)) { |
| 162 | + /** |
| 163 | + * The event listener and tabs caches need to |
| 164 | + * be cleared otherwise this will create a memory |
| 165 | + * leak where the IonTabs instance can never be |
| 166 | + * garbage collected. |
| 167 | + */ |
| 168 | + const { tabsElement, tabsLoadCallback } = this; |
| 169 | + if (tabsElement !== null && tabsLoadCallback !== undefined) { |
| 170 | + tabsElement.removeEventListener('ionTabBarLoaded', tabsLoadCallback); |
| 171 | + } |
| 172 | + |
| 173 | + this.tabsElement = null; |
| 174 | + this.tabsLoadCallback = undefined; |
| 175 | + } |
127 | 176 | }
|
128 | 177 |
|
129 | 178 | /**
|
|
0 commit comments