index.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. import React, { CSSProperties } from 'react';
  2. import BaseComponent from '../_base/baseComponent';
  3. import PropTypes from 'prop-types';
  4. import Portal from '../_portal';
  5. import cls from 'classnames';
  6. import ConfigContext, { ContextValue } from '../configProvider/context';
  7. import { cssClasses, strings } from '@douyinfe/semi-foundation/sideSheet/constants';
  8. import SideSheetContent from './SideSheetContent';
  9. import { noop } from 'lodash';
  10. import SideSheetFoundation, {
  11. SideSheetAdapter,
  12. SideSheetProps,
  13. SideSheetState
  14. } from '@douyinfe/semi-foundation/sideSheet/sideSheetFoundation';
  15. import '@douyinfe/semi-foundation/sideSheet/sideSheet.scss';
  16. import CSSAnimation from "../_cssAnimation";
  17. import { getScrollbarWidth } from '../_utils';
  18. const prefixCls = cssClasses.PREFIX;
  19. const defaultWidthList = strings.WIDTH;
  20. const defaultHeight = strings.HEIGHT;
  21. export type { SideSheetContentProps } from './SideSheetContent';
  22. export interface SideSheetReactProps extends SideSheetProps {
  23. bodyStyle?: CSSProperties;
  24. headerStyle?: CSSProperties;
  25. maskStyle?: CSSProperties;
  26. style?: CSSProperties;
  27. title?: React.ReactNode;
  28. footer?: React.ReactNode;
  29. children?: React.ReactNode;
  30. onCancel?: (e: React.MouseEvent | React.KeyboardEvent) => void
  31. }
  32. export type {
  33. SideSheetState
  34. };
  35. export default class SideSheet extends BaseComponent<SideSheetReactProps, SideSheetState> {
  36. static contextType = ConfigContext;
  37. static propTypes = {
  38. bodyStyle: PropTypes.object,
  39. headerStyle: PropTypes.object,
  40. children: PropTypes.node,
  41. className: PropTypes.string,
  42. closable: PropTypes.bool,
  43. disableScroll: PropTypes.bool,
  44. getPopupContainer: PropTypes.func,
  45. height: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  46. mask: PropTypes.bool,
  47. maskClosable: PropTypes.bool,
  48. maskStyle: PropTypes.object,
  49. motion: PropTypes.oneOfType([PropTypes.bool, PropTypes.object, PropTypes.func]),
  50. onCancel: PropTypes.func,
  51. placement: PropTypes.oneOf(strings.PLACEMENT),
  52. size: PropTypes.oneOf(strings.SIZE),
  53. style: PropTypes.object,
  54. title: PropTypes.node,
  55. visible: PropTypes.bool,
  56. width: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),
  57. zIndex: PropTypes.number,
  58. afterVisibleChange: PropTypes.func,
  59. closeOnEsc: PropTypes.bool,
  60. footer: PropTypes.node,
  61. keepDOM: PropTypes.bool,
  62. 'aria-label': PropTypes.string,
  63. };
  64. static defaultProps: SideSheetReactProps = {
  65. visible: false,
  66. motion: true,
  67. mask: true,
  68. placement: 'right',
  69. closable: true,
  70. footer: null,
  71. zIndex: 1000,
  72. maskClosable: true,
  73. size: 'small',
  74. disableScroll: true,
  75. closeOnEsc: false,
  76. afterVisibleChange: noop,
  77. keepDOM: false
  78. };
  79. private _active: boolean;
  80. constructor(props: SideSheetReactProps) {
  81. super(props);
  82. this.state = { displayNone: !this.props.visible };
  83. this.foundation = new SideSheetFoundation(this.adapter);
  84. this.bodyOverflow = '';
  85. this.scrollBarWidth = 0;
  86. this.originBodyWidth = '100%';
  87. }
  88. context: ContextValue;
  89. private bodyOverflow: string;
  90. private scrollBarWidth: number;
  91. private originBodyWidth: string;
  92. get adapter(): SideSheetAdapter {
  93. return {
  94. ...super.adapter,
  95. disabledBodyScroll: () => {
  96. const { getPopupContainer } = this.props;
  97. this.bodyOverflow = document.body.style.overflow || '';
  98. if (!getPopupContainer && this.bodyOverflow !== 'hidden') {
  99. document.body.style.overflow = 'hidden';
  100. document.body.style.width = `calc(${this.originBodyWidth || '100%'} - ${this.scrollBarWidth}px)`;
  101. }
  102. },
  103. enabledBodyScroll: () => {
  104. const { getPopupContainer } = this.props;
  105. if (!getPopupContainer && this.bodyOverflow !== 'hidden') {
  106. document.body.style.overflow = this.bodyOverflow;
  107. document.body.style.width = this.originBodyWidth;
  108. }
  109. },
  110. notifyCancel: (e: React.MouseEvent | React.KeyboardEvent) => {
  111. this.props.onCancel && this.props.onCancel(e);
  112. },
  113. notifyVisibleChange: (visible: boolean) => {
  114. this.props.afterVisibleChange(visible);
  115. },
  116. setOnKeyDownListener: () => {
  117. if (window) {
  118. window.addEventListener('keydown', this.handleKeyDown);
  119. }
  120. },
  121. removeKeyDownListener: () => {
  122. if (window) {
  123. window.removeEventListener('keydown', this.handleKeyDown);
  124. }
  125. },
  126. toggleDisplayNone: (displayNone: boolean) => {
  127. if (displayNone !== this.state.displayNone) {
  128. this.setState({ displayNone: displayNone });
  129. }
  130. },
  131. };
  132. }
  133. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  134. const newState: Partial<SideSheetState> = {};
  135. if (props.visible && prevState.displayNone) {
  136. newState.displayNone = false;
  137. }
  138. if (!props.visible && !props.motion && !prevState.displayNone) {
  139. newState.displayNone = true;
  140. }
  141. return newState;
  142. }
  143. componentDidMount() {
  144. this.scrollBarWidth = getScrollbarWidth();
  145. this.originBodyWidth = document.body.style.width;
  146. if (this.props.visible) {
  147. this.foundation.beforeShow();
  148. }
  149. }
  150. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  151. // hide => show
  152. if (!prevProps.visible && this.props.visible) {
  153. this.foundation.beforeShow();
  154. }
  155. // show => hide
  156. if (prevProps.visible && !this.props.visible) {
  157. this.foundation.afterHide();
  158. }
  159. if (prevState.displayNone !== this.state.displayNone) {
  160. this.foundation.onVisibleChange(!this.state.displayNone);
  161. }
  162. }
  163. componentWillUnmount() {
  164. if (this.props.visible) {
  165. this.foundation.destroy();
  166. }
  167. }
  168. handleCancel = (e: React.MouseEvent) => {
  169. this.foundation.handleCancel(e);
  170. };
  171. handleKeyDown = (e: KeyboardEvent) => {
  172. this.foundation.handleKeyDown(e);
  173. };
  174. updateState = () => {
  175. this.foundation.toggleDisplayNone(!this.props.visible);
  176. }
  177. renderContent() {
  178. const {
  179. placement,
  180. className,
  181. children,
  182. width,
  183. height,
  184. motion,
  185. visible,
  186. style,
  187. maskStyle,
  188. size,
  189. zIndex,
  190. getPopupContainer,
  191. keepDOM,
  192. ...props
  193. } = this.props;
  194. let wrapperStyle: CSSProperties = {
  195. zIndex,
  196. };
  197. if (getPopupContainer) {
  198. wrapperStyle = {
  199. zIndex,
  200. position: 'static',
  201. };
  202. }
  203. const { direction } = this.context;
  204. const isVertical = placement === 'left' || placement === 'right';
  205. const isHorizontal = placement === 'top' || placement === 'bottom';
  206. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  207. const classList = cls(prefixCls, className, {
  208. [`${prefixCls}-${placement}`]: placement,
  209. [`${prefixCls}-popup`]: getPopupContainer,
  210. [`${prefixCls}-horizontal`]: isHorizontal,
  211. [`${prefixCls}-rtl`]: direction === 'rtl',
  212. [`${prefixCls}-hidden`]: keepDOM && this.state.displayNone,
  213. });
  214. const contentProps = {
  215. ...(isVertical ? (width ? { width } : {}) : { width: "100%" }),
  216. ...props,
  217. visible,
  218. motion: false,
  219. size,
  220. className: classList,
  221. height: sheetHeight,
  222. onClose: this.handleCancel,
  223. };
  224. const shouldRender = (this.props.visible || this.props.keepDOM) || (this.props.motion && !this.state.displayNone /* When there is animation, we use displayNone to judge whether animation is ended and judge whether to unmount content */);
  225. // Since user could change animate duration , we don't know which animation end first. So we call updateState func twice.
  226. return <CSSAnimation motion={this.props.motion} animationState={visible ? 'enter' : 'leave'} startClassName={
  227. visible ? `${prefixCls}-animation-mask_show` : `${prefixCls}-animation-mask_hide`
  228. } onAnimationEnd={this.updateState}>
  229. {
  230. ({
  231. animationClassName: maskAnimationClassName,
  232. animationEventsNeedBind: maskAnimationEventsNeedBind
  233. }) => {
  234. return <CSSAnimation
  235. motion={this.props.motion}
  236. animationState={visible ? 'enter' : 'leave'}
  237. startClassName={visible ? `${prefixCls}-animation-content_show_${this.props.placement}` : `${prefixCls}-animation-content_hide_${this.props.placement}`}
  238. onAnimationEnd={this.updateState /* for no mask case*/}
  239. >
  240. {({ animationClassName, animationStyle, animationEventsNeedBind }) => {
  241. return shouldRender ?<Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  242. <SideSheetContent
  243. {...contentProps}
  244. maskExtraProps={maskAnimationEventsNeedBind}
  245. wrapperExtraProps={animationEventsNeedBind}
  246. dialogClassName={animationClassName}
  247. maskClassName={maskAnimationClassName}
  248. maskStyle={{ ...maskStyle }}
  249. style={{ ...animationStyle, ...style }}>
  250. {children}
  251. </SideSheetContent>
  252. </Portal>:<></>;
  253. }}
  254. </CSSAnimation>;
  255. }
  256. }
  257. </CSSAnimation>;
  258. }
  259. render() {
  260. const {
  261. zIndex,
  262. getPopupContainer,
  263. visible
  264. } = this.props;
  265. return this.renderContent();
  266. }
  267. }