index.tsx 3.3 KB

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