useTransitionCollapse.ts 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useTransitionConfig } from '#shared/composables/useTransitionConfig.ts'
  3. export const useTransitionCollapse = () => {
  4. const { durations } = useTransitionConfig()
  5. const collapseDuration = durations.normal
  6. const collapseEnter = (element: Element | HTMLElement) => {
  7. if (!(element instanceof HTMLElement)) return
  8. element.style.height = 'auto'
  9. const { height } = getComputedStyle(element)
  10. // Set the height of the element to zero for the enter transition.
  11. element.style.height = '0'
  12. requestAnimationFrame(() => {
  13. setTimeout(() => {
  14. element.style.height = height
  15. })
  16. })
  17. }
  18. const collapseAfterEnter = (element: Element | HTMLElement) => {
  19. if (!(element instanceof HTMLElement)) return
  20. // Set the height of the element to automatic for after the enter transition.
  21. element.style.height = 'auto'
  22. }
  23. const collapseLeave = (element: Element | HTMLElement) => {
  24. if (!(element instanceof HTMLElement)) return
  25. const { height } = getComputedStyle(element)
  26. // Set the height of the element to real height for the leave transition.
  27. element.style.height = height
  28. requestAnimationFrame(() => {
  29. setTimeout(() => {
  30. element.style.height = '0'
  31. })
  32. })
  33. }
  34. return {
  35. collapseDuration,
  36. collapseEnter,
  37. collapseAfterEnter,
  38. collapseLeave,
  39. }
  40. }