Skip to content

Commit

Permalink
[#3343] ResizeObserver error in VaTabs (#3360)
Browse files Browse the repository at this point in the history
* fix(#3343): update VaTabs component

* fix(#3343): fix demo
  • Loading branch information
RVitaly1978 committed May 23, 2023
1 parent 4c7aee5 commit 695a9e2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 34 deletions.
4 changes: 2 additions & 2 deletions packages/ui/src/components/va-tabs/VaTabs.demo.vue
Original file line number Diff line number Diff line change
Expand Up @@ -348,13 +348,13 @@
<template #tabs>
<va-tab
name="Link 1"
to="/demo/vuestic-components/va-breadcrumbs/VaBreadcrumbs.demo.vue"
to="/demo/components/va-breadcrumbs/VaBreadcrumbs.demo.vue"
>
Link 1
</va-tab>
<va-tab
name="Link 2"
to="/demo/vuestic-components/va-tabs/VaTabs.demo.vue"
to="/demo/components/va-tabs/VaTabs.demo.vue"
>
Active link
</va-tab>
Expand Down
71 changes: 39 additions & 32 deletions packages/ui/src/components/va-tabs/VaTabs.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
shallowRef,
StyleValue,
WritableComputedRef,
onMounted,
} from 'vue'
import {
Expand Down Expand Up @@ -205,17 +206,38 @@ export default defineComponent({
const moveToTab = (tab: TabComponent) => {
const containerClientWidth = getClientWidth(container.value)
const tabsClientWidth = getClientWidth(tabs.value)
const leftSidePosition = unref(tab.leftSidePosition)
const rightSidePosition = unref(tab.rightSidePosition)
if (showPagination.value && leftSidePosition + containerClientWidth <= tabsClientWidth) {
// tabsContainer must be at the beginning of container
if (!showPagination.value) {
tabsContentOffset.value = 0
return
}
// tab is completely placed in the container - no need to move tabsContainer
if (
(leftSidePosition - tabsContentOffset.value >= 0) &&
(rightSidePosition - tabsContentOffset.value <= containerClientWidth)
) {
return
}
// tab does not fit at the beginning of the container -
// move tabsContainer so that the beginning of the tab is at the beginning of the container
if (leftSidePosition - tabsContentOffset.value < 0) {
tabsContentOffset.value = leftSidePosition
} else if (showPagination.value && rightSidePosition >= containerClientWidth) {
return
}
// tab does not fit at the end of the container -
// move tabsContainer so that the end of the tab is at the end of the container
if (rightSidePosition - tabsContentOffset.value > containerClientWidth) {
tabsContentOffset.value = rightSidePosition - containerClientWidth
} else {
tabsContentOffset.value = 0
return
}
tabsContentOffset.value = 0
}
const updateStartingXPoint = () => {
Expand Down Expand Up @@ -251,21 +273,17 @@ export default defineComponent({
}
})
const containerClientWidth = getClientWidth(container.value)
const tabsClientWidth = getClientWidth(tabs.value)
if (tabsContentOffset.value + containerClientWidth > tabsClientWidth && tabsList.value.length) {
moveToTab(tabsList.value[0])
}
updateStartingXPoint()
}
const updatePagination = () => {
const tabsClientWidth = getClientWidth(tabs.value)
const wrapperClientWidth = getClientWidth(wrapper.value)
showPagination.value = !!(tabs.value && wrapper.value && tabsClientWidth > wrapperClientWidth)
// we use requestAnimationFrame to prevent error "ResizeObserver loop limit exceeded"
requestAnimationFrame(() => {
showPagination.value = !!(tabs.value && wrapper.value && tabsClientWidth > wrapperClientWidth)
})
}
const movePaginationLeft = () => {
Expand Down Expand Up @@ -347,22 +365,6 @@ export default defineComponent({
}
}
const redrawTabs = () => {
const oldShowPaginationValue = showPagination.value
updatePagination()
if (oldShowPaginationValue === showPagination.value) {
updateTabsState()
includeAnimation()
} else {
requestAnimationFrame(() => {
updateTabsState()
includeAnimation()
})
}
}
const selectTab = (tab: TabComponent) => {
if (!tab) { return }
Expand Down Expand Up @@ -395,10 +397,16 @@ export default defineComponent({
unregisterTab,
})
// Lifecycle hooks
watch(() => props.modelValue, updateTabsState)
useResizeObserver([wrapper, tabs], redrawTabs)
useResizeObserver([wrapper], updatePagination)
useResizeObserver([container], updateTabsState)
onMounted(() => {
requestAnimationFrame(() => {
includeAnimation()
})
})
return {
...useTranslation(),
Expand Down Expand Up @@ -432,7 +440,6 @@ export default defineComponent({
movePaginationRight,
updateSlider,
includeAnimation,
redrawTabs,
selectTab,
}
},
Expand Down

0 comments on commit 695a9e2

Please sign in to comment.