index.tsx 3.2 KB

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