@nutui/nutui-react-taro
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
37 lines (36 loc) • 1.55 kB
JavaScript
import React__default, { useState, useCallback } from "react";
import { usePageScroll, pageScrollTo } from "@tarojs/taro";
import { Top } from "@nutui/icons-react-taro";
import classNames from "classnames";
import { C as ComponentDefaults } from "./typings-DV9RBfhj.js";
import { a as useRtl } from "./configprovider.taro-DpK4IiCE.js";
const defaultProps = Object.assign(Object.assign({}, ComponentDefaults), { threshold: 200, zIndex: 900, duration: 1e3 });
const BackTop = (props) => {
const rtl = useRtl();
const { children, threshold, zIndex, className, duration, style, onClick } = Object.assign(Object.assign({}, defaultProps), props);
const classPrefix = "nut-backtop";
const [backTop, setBackTop] = useState(false);
const cls = classNames(classPrefix, { show: backTop }, className);
const handleScroll = useCallback((res) => {
const { scrollTop } = res;
setBackTop(scrollTop >= threshold);
}, [threshold]);
usePageScroll(handleScroll);
const goTop = (e) => {
onClick && onClick(e);
pageScrollTo({
scrollTop: 0,
duration: duration > 0 ? duration : 0
});
};
const styles = Object.keys(style || {}).length !== 0 ? Object.assign({ zIndex }, style) : {
[rtl ? "left" : "right"]: "10px",
bottom: "20px",
zIndex
};
return React__default.createElement("div", { className: cls, style: styles, onClick: (e) => goTop(e) }, children || React__default.createElement(Top, { size: 19, className: "nut-backtop-main" }));
};
BackTop.displayName = "NutBackTop";
export {
BackTop as B
};