useFullCard.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { isString, debounce } from 'es-toolkit'
  2. import { isElement } from 'es-toolkit/compat'
  3. import { isRef, nextTick, onMounted, reactive, ref, onBeforeUnmount } from 'vue'
  4. import type { ComponentPublicInstance, Ref } from 'vue'
  5. type MaybeHTMLElement = HTMLElement | Ref<HTMLElement | null> | string | null
  6. interface FullCardOptions {
  7. parentContainer?: MaybeHTMLElement
  8. parentWrap?: MaybeHTMLElement
  9. }
  10. type ElementKey = 'container' | 'wrap' | 'card'
  11. export function useFullCard<T extends ComponentPublicInstance>(
  12. target: Ref<T | null>,
  13. options: FullCardOptions = {},
  14. ) {
  15. const { parentContainer = '.main-container', parentWrap = '.main-wrap' } = options
  16. let resizeObserver: ResizeObserver | null = null
  17. const height = ref<number | undefined>(0)
  18. const elementMap = reactive<Record<ElementKey, HTMLElement | null>>({
  19. container: null,
  20. wrap: null,
  21. card: null,
  22. })
  23. function updateMaxHeight() {
  24. const { container, wrap, card } = elementMap
  25. if (!container || !wrap || !card) {
  26. return
  27. }
  28. const containerHeight = container.offsetHeight
  29. const wrapHeight = wrap.offsetHeight
  30. const cardHeight = card.offsetHeight
  31. height.value =
  32. (containerHeight < wrapHeight
  33. ? cardHeight - (wrapHeight - containerHeight)
  34. : cardHeight + containerHeight - wrapHeight) - 1
  35. }
  36. const debounceUpdateMaxHeight = debounce(() => {
  37. updateMaxHeight()
  38. }, 300)
  39. function getParentElement(element: MaybeHTMLElement) {
  40. if (isString(element)) return elementMap.card?.closest<HTMLElement>(element) ?? null
  41. if (isRef(element)) return element.value
  42. if (isElement(element)) return element
  43. return null
  44. }
  45. function initTableResizeObserver() {
  46. if (!target.value) {
  47. height.value = undefined
  48. return
  49. }
  50. elementMap.card = (target.value?.$el as HTMLElement) ?? null
  51. nextTick(() => {
  52. elementMap.container = getParentElement(parentContainer)
  53. elementMap.wrap = getParentElement(parentWrap)
  54. const missingKeys = Object.entries(elementMap)
  55. .filter((value) => value[1] == null)
  56. .map(([key]) => key)
  57. if (missingKeys.length) {
  58. throw new Error(`Missing or null elements: ${missingKeys.join(', ')}`)
  59. }
  60. resizeObserver = new ResizeObserver((entries) => {
  61. for (const entry of entries) {
  62. const { height } = entry.contentRect
  63. if (height) debounceUpdateMaxHeight()
  64. }
  65. })
  66. if (elementMap.container) {
  67. resizeObserver.observe(elementMap.container)
  68. }
  69. })
  70. }
  71. onMounted(() => {
  72. initTableResizeObserver()
  73. })
  74. onBeforeUnmount(() => {
  75. resizeObserver?.disconnect()
  76. })
  77. return {
  78. height,
  79. updateMaxHeight,
  80. }
  81. }