| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- 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";
- 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";
- function throttle(action, delay) {
- let timeout = null;
- let lastRun = 0;
- return function(...args) {
- if (timeout) {
- return;
- }
- const elapsed = Date.now() - lastRun;
- const runCallback = () => {
- lastRun = Date.now();
- timeout = false;
- action.apply(this, args);
- };
- if (elapsed >= delay) {
- runCallback();
- } else {
- timeout = setTimeout(runCallback, delay);
- }
- };
- }
- const [name, bem] = createNamespace("back-top");
- const backTopProps = {
- right: numericProp,
- bottom: numericProp,
- zIndex: numericProp,
- target: [String, Object],
- offset: makeNumericProp(200),
- immediate: Boolean,
- teleport: {
- type: [String, Object],
- default: "body"
- }
- };
- var stdin_default = defineComponent({
- name,
- inheritAttrs: false,
- props: backTopProps,
- emits: ["click"],
- setup(props, {
- emit,
- slots,
- attrs
- }) {
- let shouldReshow = false;
- const show = ref(false);
- const root = ref();
- const scrollParent = ref();
- const style = computed(() => extend(getZIndexStyle(props.zIndex), {
- right: addUnit(props.right),
- bottom: addUnit(props.bottom)
- }));
- const onClick = (event) => {
- var _a;
- emit("click", event);
- (_a = scrollParent.value) == null ? void 0 : _a.scrollTo({
- top: 0,
- behavior: props.immediate ? "auto" : "smooth"
- });
- };
- const scroll = () => {
- show.value = scrollParent.value ? getScrollTop(scrollParent.value) >= +props.offset : false;
- };
- const getTarget = () => {
- const {
- target
- } = props;
- if (typeof target === "string") {
- const el = document.querySelector(target);
- if (el) {
- return el;
- }
- } else {
- return target;
- }
- };
- const updateTarget = () => {
- if (inBrowser) {
- nextTick(() => {
- scrollParent.value = props.target ? getTarget() : getScrollParent(root.value);
- scroll();
- });
- }
- };
- useEventListener("scroll", throttle(scroll, 100), {
- target: scrollParent
- });
- onMounted(updateTarget);
- onActivated(() => {
- if (shouldReshow) {
- show.value = true;
- shouldReshow = false;
- }
- });
- onDeactivated(() => {
- if (show.value && props.teleport) {
- show.value = false;
- shouldReshow = true;
- }
- });
- watch(() => props.target, updateTarget);
- return () => {
- const Content = createVNode("div", mergeProps({
- "ref": !props.teleport ? root : void 0,
- "class": bem({
- active: show.value
- }),
- "style": style.value,
- "onClick": onClick
- }, attrs), [slots.default ? slots.default() : createVNode(Icon, {
- "name": "back-top",
- "class": bem("icon")
- }, null)]);
- if (props.teleport) {
- return [createVNode("div", {
- "ref": root,
- "class": bem("placeholder")
- }, null), createVNode(Teleport, {
- "to": props.teleport
- }, {
- default: () => [Content]
- })];
- }
- return Content;
- };
- }
- });
- const BackTop = withInstall(stdin_default);
- const index = "";
- export {
- BackTop as B
- };
|