element-plus
Version:
A Component Library for Vue 3
104 lines (101 loc) • 3.8 kB
JavaScript
import { defineComponent, shallowRef, ref, computed, onMounted, openBlock, createBlock, Transition, unref, withCtx, createElementBlock, normalizeStyle, normalizeClass, withModifiers, renderSlot, createVNode, createCommentVNode } from 'vue';
import { useThrottleFn, useEventListener } from '@vueuse/core';
import { ElIcon } from '../../icon/index.mjs';
import '../../../utils/index.mjs';
import { CaretTop } from '@element-plus/icons-vue';
import '../../../hooks/index.mjs';
import { backtopProps, backtopEmits } from './backtop.mjs';
import _export_sfc from '../../../_virtual/plugin-vue_export-helper.mjs';
import { useNamespace } from '../../../hooks/use-namespace/index.mjs';
import { easeInOutCubic } from '../../../utils/animation.mjs';
import { throwError } from '../../../utils/error.mjs';
const _hoisted_1 = ["onClick"];
const __default__ = {
name: "ElBacktop"
};
const _sfc_main = /* @__PURE__ */ defineComponent({
...__default__,
props: backtopProps,
emits: backtopEmits,
setup(__props, { emit }) {
const props = __props;
const COMPONENT_NAME = "ElBacktop";
const ns = useNamespace("backtop");
const el = shallowRef();
const container = shallowRef();
const visible = ref(false);
const backTopStyle = computed(() => ({
right: `${props.right}px`,
bottom: `${props.bottom}px`
}));
const scrollToTop = () => {
if (!el.value)
return;
const beginTime = Date.now();
const beginValue = el.value.scrollTop;
const frameFunc = () => {
if (!el.value)
return;
const progress = (Date.now() - beginTime) / 500;
if (progress < 1) {
el.value.scrollTop = beginValue * (1 - easeInOutCubic(progress));
requestAnimationFrame(frameFunc);
} else {
el.value.scrollTop = 0;
}
};
requestAnimationFrame(frameFunc);
};
const handleScroll = () => {
if (el.value)
visible.value = el.value.scrollTop >= props.visibilityHeight;
};
const handleClick = (event) => {
scrollToTop();
emit("click", event);
};
const handleScrollThrottled = useThrottleFn(handleScroll, 300);
useEventListener(container, "scroll", handleScrollThrottled);
onMounted(() => {
var _a;
container.value = document;
el.value = document.documentElement;
if (props.target) {
el.value = (_a = document.querySelector(props.target)) != null ? _a : void 0;
if (!el.value) {
throwError(COMPONENT_NAME, `target is not existed: ${props.target}`);
}
container.value = el.value;
}
});
return (_ctx, _cache) => {
return openBlock(), createBlock(Transition, {
name: `${unref(ns).namespace.value}-fade-in`
}, {
default: withCtx(() => [
visible.value ? (openBlock(), createElementBlock("div", {
key: 0,
style: normalizeStyle(unref(backTopStyle)),
class: normalizeClass(unref(ns).b()),
onClick: withModifiers(handleClick, ["stop"])
}, [
renderSlot(_ctx.$slots, "default", {}, () => [
createVNode(unref(ElIcon), {
class: normalizeClass(unref(ns).e("icon"))
}, {
default: withCtx(() => [
createVNode(unref(CaretTop))
]),
_: 1
}, 8, ["class"])
])
], 14, _hoisted_1)) : createCommentVNode("v-if", true)
]),
_: 3
}, 8, ["name"]);
};
}
});
var Backtop = /* @__PURE__ */ _export_sfc(_sfc_main, [["__file", "/home/runner/work/element-plus/element-plus/packages/components/backtop/src/backtop.vue"]]);
export { Backtop as default };
//# sourceMappingURL=backtop2.mjs.map