Skip to content

Commit

Permalink
feat: scroll active tab on mobile
Browse files Browse the repository at this point in the history
  • Loading branch information
rhmkstk committed Jul 19, 2024
1 parent 7550655 commit 87cf932
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions packages/vue/src/components/Tabs/LuiTabGroup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@ const context: TabContext = reactive({
tabs: [],
panels: [],
})
const isItemDisabled = (index: any) => context.tabs[index]?.disabled !== undefined && context.tabs[index].disabled === true
function isItemDisabled(index: any) {
return context.tabs[index]?.disabled !== undefined
&& context.tabs[index].disabled === true
}
const isItemValid = (index: any) => index >= 0 && index < context.tabs.length
const isItemAvailable = (index: any) => !isItemDisabled(index) && isItemValid(index)
function isItemAvailable(index: any) {
return !isItemDisabled(index) && isItemValid(index)
}
watch(
() => props.selectedIndex,
Expand All @@ -35,10 +40,12 @@ watch(
},
)
onMounted(() => {
if (isItemAvailable(props.selectedIndex))
context.selectedIndex = props.selectedIndex !== -1 ? props.selectedIndex : 0
else
context.selectedIndex = 0
if (isItemAvailable(props.selectedIndex)) {
context.selectedIndex
= props.selectedIndex !== -1 ? props.selectedIndex : 0
}
else { context.selectedIndex = 0 }
scrollActiveTabOnMobile()
})
function registerTab(tab: any) {
Expand All @@ -60,8 +67,23 @@ function unRegisterPanel(panel: any) {
function setSelectedIndex(index: number) {
context.selectedIndex = index
emit('change', context.selectedIndex)
scrollActiveTabOnMobile()
}
function scrollActiveTabOnMobile() {
if (!window || !window.innerWidth)
return
const isMobile = window.innerWidth < 768
if (isMobile) {
const activeTab = context.tabs[context.selectedIndex]
if (!activeTab)
return
activeTab.scrollIntoView({
block: 'center',
inline: 'center',
behavior: 'smooth',
})
}
}
// onMounted(() => {
// // need the throw an error if one of the required component not provided:
// context.selectedIndex = props.selectedIndex !== -1 ? props.selectedIndex : 0
Expand Down

0 comments on commit 87cf932

Please sign in to comment.