ce0c339fcfc8c9d3d30207430557eb8027939a67e9fd1e609ee6b2473c4f2711a208ce7cc131148847471e853f14cc9bdc4f7522feb5a54ac6da39154dad7a 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import { shallowRef, ref, onMounted } from 'vue';
  2. import { useThrottleFn, useEventListener } from '@vueuse/core';
  3. import { throwError } from '../../../utils/error.mjs';
  4. const useBackTop = (props, emit, componentName) => {
  5. const el = shallowRef();
  6. const container = shallowRef();
  7. const visible = ref(false);
  8. const handleScroll = () => {
  9. if (el.value)
  10. visible.value = el.value.scrollTop >= props.visibilityHeight;
  11. };
  12. const handleClick = (event) => {
  13. var _a;
  14. (_a = el.value) == null ? void 0 : _a.scrollTo({ top: 0, behavior: "smooth" });
  15. emit("click", event);
  16. };
  17. const handleScrollThrottled = useThrottleFn(handleScroll, 300, true);
  18. useEventListener(container, "scroll", handleScrollThrottled);
  19. onMounted(() => {
  20. var _a;
  21. container.value = document;
  22. el.value = document.documentElement;
  23. if (props.target) {
  24. el.value = (_a = document.querySelector(props.target)) != null ? _a : void 0;
  25. if (!el.value) {
  26. throwError(componentName, `target does not exist: ${props.target}`);
  27. }
  28. container.value = el.value;
  29. }
  30. handleScroll();
  31. });
  32. return {
  33. visible,
  34. handleClick
  35. };
  36. };
  37. export { useBackTop };
  38. //# sourceMappingURL=use-backtop.mjs.map