use-height.mjs 662 B

123456789101112131415161718192021222324
  1. import { useRect } from "@vant/use";
  2. import { ref, onMounted, nextTick, watch } from "vue";
  3. import { windowHeight, windowWidth } from "../utils/index.mjs";
  4. import { onPopupReopen } from "./on-popup-reopen.mjs";
  5. const useHeight = (element, withSafeArea) => {
  6. const height = ref();
  7. const setHeight = () => {
  8. height.value = useRect(element).height;
  9. };
  10. onMounted(() => {
  11. nextTick(setHeight);
  12. if (withSafeArea) {
  13. for (let i = 1; i <= 3; i++) {
  14. setTimeout(setHeight, 100 * i);
  15. }
  16. }
  17. });
  18. onPopupReopen(() => nextTick(setHeight));
  19. watch([windowWidth, windowHeight], setHeight);
  20. return height;
  21. };
  22. export {
  23. useHeight
  24. };