Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(VTabs): add new prop, slider transition #19556

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
79 changes: 51 additions & 28 deletions packages/vuetify/src/components/VTabs/VTab.tsx
Expand Up @@ -21,6 +21,7 @@ export const makeVTabProps = propsFactory({
fixed: Boolean,

sliderColor: String,
sliderTransition: String as PropType<'shift' | 'grow' | 'fade'>,
hideSlider: Boolean,

direction: {
Expand Down Expand Up @@ -55,6 +56,49 @@ export const VTab = genericComponent<VBtnSlots>()({
const isHorizontal = computed(() => props.direction === 'horizontal')
const isSelected = computed(() => rootEl.value?.group?.isSelected.value ?? false)

function fade (nextEl: HTMLElement, prevEl: HTMLElement) {
return { opacity: [0, 1] }
}

function grow (nextEl: HTMLElement, prevEl: HTMLElement) {
return {
transform: ['scaleX(0)', 'scaleX(1)'],
}
}

function shift (nextEl: HTMLElement, prevEl: HTMLElement) {
const prevBox = prevEl.getBoundingClientRect()
const nextBox = nextEl.getBoundingClientRect()

const xy = isHorizontal.value ? 'x' : 'y'
const XY = isHorizontal.value ? 'X' : 'Y'
const rightBottom = isHorizontal.value ? 'right' : 'bottom'
const widthHeight = isHorizontal.value ? 'width' : 'height'

const prevPos = prevBox[xy]
const nextPos = nextBox[xy]
const delta = prevPos > nextPos
? prevBox[rightBottom] - nextBox[rightBottom]
: prevBox[xy] - nextBox[xy]
const origin =
Math.sign(delta) > 0 ? (isHorizontal.value ? 'right' : 'bottom')
: Math.sign(delta) < 0 ? (isHorizontal.value ? 'left' : 'top')
: 'center'
const size = Math.abs(delta) + (Math.sign(delta) < 0 ? prevBox[widthHeight] : nextBox[widthHeight])
const scale = size / Math.max(prevBox[widthHeight], nextBox[widthHeight]) || 0
const initialScale = prevBox[widthHeight] / nextBox[widthHeight] || 0
const sigma = 1.5

return {
transform: [
`translate${XY}(${delta}px) scale${XY}(${initialScale})`,
`translate${XY}(${delta / sigma}px) scale${XY}(${(scale - 1) / sigma + 1})`,
'none',
],
transformOrigin: Array(3).fill(origin),
}
}

function updateSlider ({ value }: { value: boolean }) {
if (value) {
const prevEl: HTMLElement | undefined = rootEl.value?.$el.parentElement?.querySelector('.v-tab--selected .v-tab__slider')
Expand All @@ -64,36 +108,15 @@ export const VTab = genericComponent<VBtnSlots>()({

const color = getComputedStyle(prevEl).color

const prevBox = prevEl.getBoundingClientRect()
const nextBox = nextEl.getBoundingClientRect()

const xy = isHorizontal.value ? 'x' : 'y'
const XY = isHorizontal.value ? 'X' : 'Y'
const rightBottom = isHorizontal.value ? 'right' : 'bottom'
const widthHeight = isHorizontal.value ? 'width' : 'height'

const prevPos = prevBox[xy]
const nextPos = nextBox[xy]
const delta = prevPos > nextPos
? prevBox[rightBottom] - nextBox[rightBottom]
: prevBox[xy] - nextBox[xy]
const origin =
Math.sign(delta) > 0 ? (isHorizontal.value ? 'right' : 'bottom')
: Math.sign(delta) < 0 ? (isHorizontal.value ? 'left' : 'top')
: 'center'
const size = Math.abs(delta) + (Math.sign(delta) < 0 ? prevBox[widthHeight] : nextBox[widthHeight])
const scale = size / Math.max(prevBox[widthHeight], nextBox[widthHeight]) || 0
const initialScale = prevBox[widthHeight] / nextBox[widthHeight] || 0

const sigma = 1.5
const keyframes = {
fade,
grow,
shift,
}[props.sliderTransition ?? 'shift'] ?? shift

animate(nextEl, {
backgroundColor: [color, 'currentcolor'],
transform: [
`translate${XY}(${delta}px) scale${XY}(${initialScale})`,
`translate${XY}(${delta / sigma}px) scale${XY}(${(scale - 1) / sigma + 1})`,
'none',
],
transformOrigin: Array(3).fill(origin),
...keyframes(nextEl, prevEl),
}, {
duration: 225,
easing: standardEasing,
Expand Down
2 changes: 2 additions & 0 deletions packages/vuetify/src/components/VTabs/VTabs.tsx
Expand Up @@ -52,6 +52,7 @@ export const makeVTabsProps = propsFactory({
},
hideSlider: Boolean,
sliderColor: String,
sliderTransition: String as PropType<'shift' | 'grow' | 'fade'>,

...makeVSlideGroupProps({ mandatory: 'force' as const }),
...makeDensityProps(),
Expand Down Expand Up @@ -80,6 +81,7 @@ export const VTabs = genericComponent()({
stacked: toRef(props, 'stacked'),
fixed: toRef(props, 'fixedTabs'),
sliderColor: toRef(props, 'sliderColor'),
sliderTransition: toRef(props, 'sliderTransition'),
hideSlider: toRef(props, 'hideSlider'),
},
})
Expand Down