use-content-height.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { computed, onMounted, ref, watch } from 'vue';
  2. import {
  3. CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT,
  4. getElementVisibleHeight,
  5. } from '@vben-core/shared';
  6. import { useCssVar, useDebounceFn, useWindowSize } from '@vueuse/core';
  7. /**
  8. * @zh_CN 获取内容高度(可视区域,不包含滚动条)
  9. */
  10. function useContentHeight() {
  11. const contentHeight = useCssVar(CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT);
  12. const contentStyles = computed(() => {
  13. return {
  14. height: `var(${CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT})`,
  15. };
  16. });
  17. return { contentHeight, contentStyles };
  18. }
  19. /**
  20. * @zh_CN 创建内容高度监听
  21. */
  22. function useContentHeightListener() {
  23. const contentElement = ref<HTMLDivElement | null>(null);
  24. const { height, width } = useWindowSize();
  25. const contentHeight = useCssVar(CSS_VARIABLE_LAYOUT_CONTENT_HEIGHT);
  26. const debouncedCalcHeight = useDebounceFn(() => {
  27. contentHeight.value = `${getElementVisibleHeight(contentElement.value)}px`;
  28. }, 200);
  29. watch([height, width], () => {
  30. debouncedCalcHeight();
  31. });
  32. onMounted(() => {
  33. debouncedCalcHeight();
  34. });
  35. return { contentElement };
  36. }
  37. export { useContentHeight, useContentHeightListener };