index.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import React from 'react';
  2. import BaseComponent from '../_base/baseComponent';
  3. import cls from 'classnames';
  4. import PropTypes from 'prop-types';
  5. import { throttle } from 'lodash';
  6. import { cssClasses } from '@douyinfe/semi-foundation/backtop/constants';
  7. import BackTopFoundation, { BackTopAdapter } from '@douyinfe/semi-foundation/backtop/foundation';
  8. import '@douyinfe/semi-foundation/backtop/backtop.scss';
  9. import IconButton from '../iconButton';
  10. import { IconChevronUp } from '@douyinfe/semi-icons';
  11. import { getDefaultPropsFromGlobalConfig } from "../_utils";
  12. const prefixCls = cssClasses.PREFIX;
  13. const getDefaultTarget = () => window;
  14. export interface BackTopProps {
  15. target?: () => any;
  16. visibilityHeight?: number;
  17. duration?: number;
  18. onClick?: (e: React.MouseEvent) => void;
  19. style?: React.CSSProperties;
  20. className?: string;
  21. children?: React.ReactNode | undefined
  22. }
  23. export interface BackTopState {
  24. visible?: boolean
  25. }
  26. export default class BackTop extends BaseComponent<BackTopProps, BackTopState> {
  27. static __SemiComponentName__ = "BackTop";
  28. static defaultProps = getDefaultPropsFromGlobalConfig(BackTop.__SemiComponentName__, {
  29. visibilityHeight: 400,
  30. target: getDefaultTarget,
  31. duration: 450,
  32. })
  33. static propTypes = {
  34. target: PropTypes.func,
  35. visibilityHeight: PropTypes.number,
  36. duration: PropTypes.number,
  37. onClick: PropTypes.func,
  38. style: PropTypes.object,
  39. className: PropTypes.string,
  40. };
  41. handler: (e: React.MouseEvent<HTMLDivElement>) => void;
  42. constructor(props: BackTopProps) {
  43. super(props);
  44. this.state = {
  45. visible: false
  46. };
  47. this.foundation = new BackTopFoundation(this.adapter);
  48. }
  49. componentDidMount() {
  50. this.foundation.init();
  51. this.handler = throttle(this.handleClick, this.props.duration ?? BackTop.defaultProps.duration);
  52. }
  53. componentWillUnmount() {
  54. this.foundation.destroy();
  55. }
  56. get adapter(): BackTopAdapter {
  57. return {
  58. ...super.adapter,
  59. updateVisible: (visible: boolean) => {
  60. this.setState({ visible });
  61. },
  62. notifyClick: (e: React.MouseEvent) => {
  63. this.props.onClick && this.props.onClick(e);
  64. },
  65. targetIsWindow: (target: any) => target === window,
  66. isWindowUndefined: () => window === undefined,
  67. targetScrollToTop: (targetNode: any, scrollTop: number) => {
  68. if (targetNode === window) {
  69. document.body.scrollTop = scrollTop;
  70. document.documentElement.scrollTop = scrollTop;
  71. } else {
  72. targetNode.scrollTop = scrollTop;
  73. }
  74. }
  75. };
  76. }
  77. handleClick(e: React.MouseEvent<HTMLDivElement>) {
  78. this.foundation.onClick(e);
  79. }
  80. renderDefault() {
  81. return <IconButton theme="light" icon={<IconChevronUp />} />;
  82. }
  83. render() {
  84. const { children, className, style, onClick, visibilityHeight, target, ...others } = this.props;
  85. const { visible } = this.state;
  86. const preCls = cls(
  87. prefixCls,
  88. className
  89. );
  90. const backtopBtn = children ? children : this.renderDefault();
  91. const content = visible ? (
  92. // eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
  93. <div
  94. {...others}
  95. className={preCls}
  96. style={style}
  97. onClick={e => this.handler(e)}
  98. x-semi-prop="children"
  99. >
  100. {backtopBtn}
  101. </div>
  102. ) : null;
  103. return content;
  104. }
  105. }