Skip to content

Commit 5d6915c

Browse files
committed
recalculate indicator tab when parent changes
1 parent ea8e425 commit 5d6915c

File tree

1 file changed

+19
-25
lines changed

1 file changed

+19
-25
lines changed

src/components/mdTabs/mdTabs.vue

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
<script>
3535
import theme from '../../core/components/mdTheme/mixin';
36+
import throttle from '../../core/utils/throttle';
3637
3738
export default {
3839
props: {
@@ -56,6 +57,7 @@
5657
hasIcons: false,
5758
hasLabel: false,
5859
transitionControl: null,
60+
transitionOff: false,
5961
contentHeight: '0px',
6062
contentWidth: '0px'
6163
}),
@@ -120,31 +122,24 @@
120122
}
121123
},
122124
observeElementChanges() {
123-
this.contentObserver = new MutationObserver(this.calculateOnWatch);
124-
this.navigationObserver = new MutationObserver(this.calculateOnWatch);
125-
this.contentObserver.observe(this.$refs.tabContent, {
125+
this.parentObserver = new MutationObserver(throttle(this.calculateOnWatch, 50));
126+
this.parentObserver.observe(this.$refs.tabContent, {
126127
childList: true,
127128
attributes: true,
128-
characterData: true,
129-
subtree: true,
130-
attributeOldValue: true,
131-
characterDataOldValue: true
132-
});
133-
this.navigationObserver.observe(this.$refs.tabNavigation.$el, {
134-
attributes: true
129+
subtree: true
135130
});
136131
},
137132
getTabIndex(id) {
138-
let idList = Object.keys(this.tabList);
133+
const idList = Object.keys(this.tabList);
139134
140135
return idList.indexOf(id);
141136
},
142137
calculateIndicatorPos() {
143138
if (this.$refs.tabHeader && this.$refs.tabHeader[this.activeTabNumber]) {
144-
let tabsWidth = this.$el.offsetWidth;
145-
let activeTab = this.$refs.tabHeader[this.activeTabNumber];
146-
let left = activeTab.offsetLeft;
147-
let right = tabsWidth - left - activeTab.offsetWidth;
139+
const tabsWidth = this.$el.offsetWidth;
140+
const activeTab = this.$refs.tabHeader[this.activeTabNumber];
141+
const left = activeTab.offsetLeft;
142+
const right = tabsWidth - left - activeTab.offsetWidth;
148143
149144
this.$refs.indicator.style.left = left + 'px';
150145
this.$refs.indicator.style.right = right + 'px';
@@ -157,7 +152,7 @@
157152
this.contentWidth = width * this.activeTabNumber + 'px';
158153
159154
for (const tabId in this.tabList) {
160-
let tab = this.tabList[tabId];
155+
const tab = this.tabList[tabId];
161156
162157
tab.ref.width = width + 'px';
163158
tab.ref.left = width * index + 'px';
@@ -188,10 +183,13 @@
188183
}, 200);
189184
},
190185
calculateOnWatch() {
191-
this.transitionOff = true;
192186
this.calculatePosition();
193187
this.debounceTransition();
194188
},
189+
calculateOnResize() {
190+
this.transitionOff = true;
191+
this.calculateOnWatch();
192+
},
195193
setActiveTab(tabData) {
196194
this.hasIcons = !!tabData.icon;
197195
this.hasLabel = !!tabData.label;
@@ -204,7 +202,7 @@
204202
mounted() {
205203
this.$nextTick(() => {
206204
this.observeElementChanges();
207-
window.addEventListener('resize', this.calculateOnWatch);
205+
window.addEventListener('resize', this.calculateOnResize);
208206
209207
if (Object.keys(this.tabList).length && !this.activeTab) {
210208
let firstTab = Object.keys(this.tabList)[0];
@@ -214,15 +212,11 @@
214212
});
215213
},
216214
beforeDestroy() {
217-
if (this.contentObserver) {
218-
this.contentObserver.disconnect();
219-
}
220-
221-
if (this.navigationObserver) {
222-
this.navigationObserver.disconnect();
215+
if (this.parentObserver) {
216+
this.parentObserver.disconnect();
223217
}
224218
225-
window.removeEventListener('resize', this.calculateOnWatch);
219+
window.removeEventListener('resize', this.calculateOnResize);
226220
}
227221
};
228222
</script>

0 commit comments

Comments
 (0)