index-9e456003.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { c as createNamespace, n as numericProp, m as makeNumericProp, e as extend, g as getZIndexStyle, d as addUnit, D as useEventListener, I as Icon, N as inBrowser, a0 as getScrollParent, P as getScrollTop, w as withInstall } from "./index-487cde8c.js";
  2. import { d as defineComponent, r as ref, a as computed, o as onMounted, p as onActivated, V as onDeactivated, w as watch, c as createVNode, m as mergeProps, T as Teleport, z as nextTick } from "./index-5e4623ce.js";
  3. function throttle(action, delay) {
  4. let timeout = null;
  5. let lastRun = 0;
  6. return function(...args) {
  7. if (timeout) {
  8. return;
  9. }
  10. const elapsed = Date.now() - lastRun;
  11. const runCallback = () => {
  12. lastRun = Date.now();
  13. timeout = false;
  14. action.apply(this, args);
  15. };
  16. if (elapsed >= delay) {
  17. runCallback();
  18. } else {
  19. timeout = setTimeout(runCallback, delay);
  20. }
  21. };
  22. }
  23. const [name, bem] = createNamespace("back-top");
  24. const backTopProps = {
  25. right: numericProp,
  26. bottom: numericProp,
  27. zIndex: numericProp,
  28. target: [String, Object],
  29. offset: makeNumericProp(200),
  30. immediate: Boolean,
  31. teleport: {
  32. type: [String, Object],
  33. default: "body"
  34. }
  35. };
  36. var stdin_default = defineComponent({
  37. name,
  38. inheritAttrs: false,
  39. props: backTopProps,
  40. emits: ["click"],
  41. setup(props, {
  42. emit,
  43. slots,
  44. attrs
  45. }) {
  46. let shouldReshow = false;
  47. const show = ref(false);
  48. const root = ref();
  49. const scrollParent = ref();
  50. const style = computed(() => extend(getZIndexStyle(props.zIndex), {
  51. right: addUnit(props.right),
  52. bottom: addUnit(props.bottom)
  53. }));
  54. const onClick = (event) => {
  55. var _a;
  56. emit("click", event);
  57. (_a = scrollParent.value) == null ? void 0 : _a.scrollTo({
  58. top: 0,
  59. behavior: props.immediate ? "auto" : "smooth"
  60. });
  61. };
  62. const scroll = () => {
  63. show.value = scrollParent.value ? getScrollTop(scrollParent.value) >= +props.offset : false;
  64. };
  65. const getTarget = () => {
  66. const {
  67. target
  68. } = props;
  69. if (typeof target === "string") {
  70. const el = document.querySelector(target);
  71. if (el) {
  72. return el;
  73. }
  74. } else {
  75. return target;
  76. }
  77. };
  78. const updateTarget = () => {
  79. if (inBrowser) {
  80. nextTick(() => {
  81. scrollParent.value = props.target ? getTarget() : getScrollParent(root.value);
  82. scroll();
  83. });
  84. }
  85. };
  86. useEventListener("scroll", throttle(scroll, 100), {
  87. target: scrollParent
  88. });
  89. onMounted(updateTarget);
  90. onActivated(() => {
  91. if (shouldReshow) {
  92. show.value = true;
  93. shouldReshow = false;
  94. }
  95. });
  96. onDeactivated(() => {
  97. if (show.value && props.teleport) {
  98. show.value = false;
  99. shouldReshow = true;
  100. }
  101. });
  102. watch(() => props.target, updateTarget);
  103. return () => {
  104. const Content = createVNode("div", mergeProps({
  105. "ref": !props.teleport ? root : void 0,
  106. "class": bem({
  107. active: show.value
  108. }),
  109. "style": style.value,
  110. "onClick": onClick
  111. }, attrs), [slots.default ? slots.default() : createVNode(Icon, {
  112. "name": "back-top",
  113. "class": bem("icon")
  114. }, null)]);
  115. if (props.teleport) {
  116. return [createVNode("div", {
  117. "ref": root,
  118. "class": bem("placeholder")
  119. }, null), createVNode(Teleport, {
  120. "to": props.teleport
  121. }, {
  122. default: () => [Content]
  123. })];
  124. }
  125. return Content;
  126. };
  127. }
  128. });
  129. const BackTop = withInstall(stdin_default);
  130. const index = "";
  131. export {
  132. BackTop as B
  133. };