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(VDataTable): add transition to expanding rows #16773

Draft
wants to merge 1 commit into
base: master
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface HTMLExpandElement extends HTMLElement {
}
}

export default function (expandedParentClass = '', x = false) {
export function ExpandTransitionGenerator (expandedParentClass = '', x = false) {
const sizeProperty = x ? 'width' : 'height' as 'width' | 'height'
const offsetProperty = camelize(`offset-${sizeProperty}`) as 'offsetHeight' | 'offsetWidth'

Expand Down
3 changes: 2 additions & 1 deletion packages/vuetify/src/components/transitions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
createJavascriptTransition,
} from './createTransition'

import ExpandTransitionGenerator from './expand-transition'
import { ExpandTransitionGenerator } from './expand-transition'

// Component specific transitions
export const VFabTransition = createCssTransition('fab-transition', 'center center', 'out-in')
Expand All @@ -27,6 +27,7 @@ export const VExpandTransition = createJavascriptTransition('expand-transition',
export const VExpandXTransition = createJavascriptTransition('expand-x-transition', ExpandTransitionGenerator('', true))

export { VDialogTransition } from './dialog-transition'
export { VTableExpandTransition } from './table-expand-transition'

export type VFabTransition = InstanceType<typeof VFabTransition>
export type VDialogBottomTransition = InstanceType<typeof VDialogBottomTransition>
Expand Down
116 changes: 116 additions & 0 deletions packages/vuetify/src/components/transitions/table-expand-transition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import { createJavascriptTransition } from './createTransition'
import { ExpandTransitionGenerator } from './expand-transition'
import { getCurrentInstance, h, mergeProps, onMounted, Transition } from 'vue'
import { defineComponent } from '@/util'

const expandBase = ExpandTransitionGenerator()

export const VTableExpandTransition = defineComponent({
name: 'VTableExpandTransition',

props: {
columns: {
type: Number,
required: true,
},
},

setup (props, { slots }) {
const vm = getCurrentInstance()!

function onEnter (el: Element, done: () => void) {
const tr = document.createElement('tr')
const td = document.createElement('td')
td.setAttribute('colspan', String(props.columns))
td.style.padding = '0'
td.style.height = 'auto'
const wrapperRoot = document.createElement('div')
const density = el.closest('.v-table')!.className.match(/v-table--density-([a-z]+)/)![1]
wrapperRoot.classList.add('v-table', `v-table--density-${density}`)
const wrapper = document.createElement('div')
wrapper.classList.add('v-table__wrapper')
const wrapperTable = document.createElement('table')
const tbody = document.createElement('tbody')
tr.append(td)
td.append(wrapperRoot)
wrapperRoot.append(wrapper)
wrapper.append(wrapperTable)
wrapperTable.append(tbody)
wrapperRoot.classList.add('expand-transition-enter-active', 'expand-transition-enter-from')
expandBase.onBeforeEnter(wrapperRoot)
el.parentNode!.insertBefore(tr, el)
tbody.append(el)
function onEnd () {
tr.parentNode!.insertBefore(el, tr)
tr.remove()
done()
}
wrapperRoot.addEventListener('transitionend', onEnd)
wrapperRoot.addEventListener('transitioncancel', onEnd)
requestAnimationFrame(() => {
wrapperRoot.classList.remove('expand-transition-enter-from')
wrapperRoot.classList.add('expand-transition-enter-to')
expandBase.onEnter(wrapperRoot)
})
}

function onLeave (el: Element, done: () => void) {
const tr = document.createElement('tr')
const td = document.createElement('td')
td.setAttribute('colspan', String(props.columns))
td.style.padding = '0'
td.style.height = 'auto'
const wrapperRoot = document.createElement('div')
const density = el.closest('.v-table')!.className.match(/v-table--density-([a-z]+)/)![1]
wrapperRoot.classList.add('v-table', `v-table--density-${density}`)
const wrapper = document.createElement('div')
wrapper.classList.add('v-table__wrapper')
const wrapperTable = document.createElement('table')
const tbody = document.createElement('tbody')
tr.append(td)
td.append(wrapperRoot)
wrapperRoot.append(wrapper)
wrapper.append(wrapperTable)
wrapperTable.append(tbody)
wrapperRoot.classList.add('expand-transition-leave-active', 'expand-transition-leave-from')
el.parentNode!.insertBefore(tr, el)
tbody.append(el)
function onEnd () {
tr.parentNode!.insertBefore(el, tr)
tr.remove()
done()
}
wrapperRoot.addEventListener('transitionend', onEnd)
wrapperRoot.addEventListener('transitioncancel', onEnd)
requestAnimationFrame(() => {
wrapperRoot.classList.remove('expand-transition-leave-from')
wrapperRoot.classList.add('expand-transition-leave-to')
expandBase.onLeave(wrapperRoot)
})
}

return () => (
h(Transition, {
css: false,
onEnter,
onLeave,
}, slots.default)
)
},
})

// export const VTableExpandTransition = createJavascriptTransition('table-expand-transition', {
// ...expandBase,
// appear: true,
// css: false,
// onEnter (el, done) {
// const wrapper = document.createElement('div')
// wrapper.style.width = '100%'
// el.parentNode.insertBefore(wrapper, el)
// wrapper.appendChild(el)
// expandBase.onEnter(wrapper)
// },
// onAfterEnter (el) {
// console.log(el)
// },
// })
5 changes: 4 additions & 1 deletion packages/vuetify/src/labs/VDataTable/VDataTableRows.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Components
import { VDataTableGroupHeaderRow } from './VDataTableGroupHeaderRow'
import { VDataTableRow } from './VDataTableRow'
import { VTableExpandTransition } from '@/components/transitions'

// Composables
import { useLocale } from '@/composables/locale'
Expand Down Expand Up @@ -122,7 +123,9 @@ export const VDataTableRows = genericComponent<VDataTableRowsSlots>()({
/>
) }

{ isExpanded(item) && slots['expanded-row']?.({ item, columns: columns.value }) }
<VTableExpandTransition columns={ columns.value.length }>
{ isExpanded(item) && slots['expanded-row']?.({ item, columns: columns.value }) }
</VTableExpandTransition>
</>
)
}) }
Expand Down