-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(MediaSettings): improve tabs transition
Signed-off-by: Grigorii K. Shartsev <[email protected]>
- Loading branch information
Showing
3 changed files
with
187 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<!-- | ||
- SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
- SPDX-License-Identifier: AGPL-3.0-or-later | ||
--> | ||
|
||
<script setup lang="ts"> | ||
import { computed, ref } from 'vue' | ||
import type { Component } from 'vue' | ||
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js' | ||
import TransitionExpandDown from './TransitionExpandDown.vue' | ||
type TabDefinition = { | ||
id: string, | ||
label: string, | ||
icon: Component, | ||
} | ||
const props = defineProps<{ | ||
tabs: TabDefinition[], | ||
active?: string, | ||
}>() | ||
const emit = defineEmits<{ | ||
(event: 'update:active', value?: string): void | ||
}>() | ||
/** Whether the tab panel is open */ | ||
const isOpen = ref(false) | ||
// A11y ReferenceIDs | ||
const randomId = Math.random().toString(36).substring(7) | ||
const getRefId = (scope: 'tab' | 'panel', key: string) => `tab-${randomId}-${scope}-${key}` | ||
/** | ||
* Whether the tab panel horizontal transition is enabled. | ||
* Used to prevent the panel switch transition when the tab is first rendered. | ||
*/ | ||
const enableTransition = ref(false) | ||
/** Index of the active tab for the transition effect */ | ||
const activeIndex = computed(() => props.tabs.findIndex(tab => tab.id === props.active)) | ||
/** | ||
* Whether the tab is active | ||
* @param tabId - Tab ID | ||
*/ | ||
function isActive(tabId: string) { | ||
return tabId === props.active | ||
} | ||
/** | ||
* Whether the tab is selected on UI | ||
* @param tabId - Tab ID | ||
*/ | ||
function isSelected(tabId: string) { | ||
return isOpen.value && isActive(tabId) | ||
} | ||
/** | ||
* Toggle the tab: | ||
* - Toggle the tab on the current tab click | ||
* - Switch and open tab on a new tab click | ||
* @param tabId - New selected tabId | ||
*/ | ||
function handleTabClick(tabId: string) { | ||
if (isActive(tabId)) { | ||
isOpen.value = !isOpen.value | ||
} else { | ||
emit('update:active', tabId) | ||
isOpen.value = true | ||
} | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="tabs"> | ||
<div class="tab-list" role="tablist"> | ||
<NcButton v-for="tab in tabs" | ||
:id="getRefId('tab', tab.id)" | ||
:key="tab.id" | ||
wide | ||
role="tab" | ||
:type="isSelected(tab.id) ? 'secondary' : 'tertiary'" | ||
:aria-selected="isSelected(tab.id) ? 'true' : 'false'" | ||
:aria-controls="getRefId('panel', tab.id)" | ||
@click.stop="handleTabClick(tab.id)"> | ||
<template #icon> | ||
<component :is="tab.icon" :size="20" /> | ||
</template> | ||
{{ tab.label }} | ||
</NcButton> | ||
</div> | ||
|
||
<TransitionExpandDown :show="isOpen" @after-enter="enableTransition = true" @after-leave="enableTransition = false"> | ||
<div class="tab-panels-container"> | ||
<div v-for="tab in tabs" | ||
:id="getRefId('panel', tab.id)" | ||
:key="tab.id" | ||
class="tab-panel" | ||
:class="{ 'tab-panel--with-transition': enableTransition }" | ||
role="tabpanel" | ||
:inert="!isActive(tab.id)" | ||
:aria-hidden="!isActive(tab.id)" | ||
:aria-labelledby="getRefId('tab', tab.id)" | ||
:style="activeIndex !== -1 ? `transform: translateX(${-activeIndex * 100}%)` : ''"> | ||
<slot :name="`tab-panel:${tab.id}`" /> | ||
</div> | ||
</div> | ||
</TransitionExpandDown> | ||
</div> | ||
</template> | ||
|
||
<style lang="scss" scoped> | ||
.tab-list { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
gap: calc(var(--default-grid-baseline) * 2); | ||
} | ||
.tab-panels-container { | ||
display: flex; | ||
width: 100%; | ||
overflow: hidden; | ||
transition: height ease var(--animation-slow); | ||
} | ||
.tab-panel { | ||
width: 100%; | ||
flex: 1 0 100%; | ||
transition: none; | ||
&--with-transition { | ||
transition: transform ease var(--animation-slow); | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters