index.tsx 3.6 KB

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