index.tsx 11 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 { getDefaultPropsFromGlobalConfig, 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 __SemiComponentName__ = "SideSheet";
  65. static defaultProps: SideSheetReactProps = getDefaultPropsFromGlobalConfig(SideSheet.__SemiComponentName__, {
  66. visible: false,
  67. motion: true,
  68. mask: true,
  69. placement: 'right',
  70. closable: true,
  71. footer: null,
  72. zIndex: 1000,
  73. maskClosable: true,
  74. size: 'small',
  75. disableScroll: true,
  76. closeOnEsc: false,
  77. afterVisibleChange: noop,
  78. keepDOM: false
  79. });
  80. private _active: boolean;
  81. constructor(props: SideSheetReactProps) {
  82. super(props);
  83. this.state = { displayNone: !this.props.visible };
  84. this.foundation = new SideSheetFoundation(this.adapter);
  85. this.bodyOverflow = '';
  86. this.scrollBarWidth = 0;
  87. this.originBodyWidth = '100%';
  88. }
  89. context: ContextValue;
  90. private bodyOverflow: string;
  91. private scrollBarWidth: number;
  92. private originBodyWidth: string;
  93. get adapter(): SideSheetAdapter {
  94. return {
  95. ...super.adapter,
  96. disabledBodyScroll: () => {
  97. const { getPopupContainer } = this.props;
  98. this.bodyOverflow = document.body.style.overflow || '';
  99. if (!getPopupContainer && this.bodyOverflow !== 'hidden') {
  100. document.body.style.overflow = 'hidden';
  101. document.body.style.width = `calc(${this.originBodyWidth || '100%'} - ${this.scrollBarWidth}px)`;
  102. }
  103. },
  104. enabledBodyScroll: () => {
  105. const { getPopupContainer } = this.props;
  106. if (!getPopupContainer && this.bodyOverflow !== 'hidden') {
  107. document.body.style.overflow = this.bodyOverflow;
  108. document.body.style.width = this.originBodyWidth;
  109. }
  110. },
  111. notifyCancel: (e: React.MouseEvent | React.KeyboardEvent) => {
  112. this.props.onCancel && this.props.onCancel(e);
  113. },
  114. notifyVisibleChange: (visible: boolean) => {
  115. this.props.afterVisibleChange(visible);
  116. },
  117. setOnKeyDownListener: () => {
  118. if (window) {
  119. window.addEventListener('keydown', this.handleKeyDown);
  120. }
  121. },
  122. removeKeyDownListener: () => {
  123. if (window) {
  124. window.removeEventListener('keydown', this.handleKeyDown);
  125. }
  126. },
  127. toggleDisplayNone: (displayNone: boolean) => {
  128. if (displayNone !== this.state.displayNone) {
  129. this.setState({ displayNone: displayNone });
  130. }
  131. },
  132. };
  133. }
  134. static getDerivedStateFromProps(props: SideSheetReactProps, prevState: SideSheetState) {
  135. const newState: Partial<SideSheetState> = {};
  136. if (props.visible && prevState.displayNone) {
  137. newState.displayNone = false;
  138. }
  139. if (!props.visible && !props.motion && !prevState.displayNone) {
  140. newState.displayNone = true;
  141. }
  142. return newState;
  143. }
  144. componentDidMount() {
  145. this.scrollBarWidth = getScrollbarWidth();
  146. this.originBodyWidth = document.body.style.width;
  147. if (this.props.visible) {
  148. this.foundation.beforeShow();
  149. }
  150. }
  151. componentDidUpdate(prevProps: SideSheetReactProps, prevState: SideSheetState, snapshot: any) {
  152. // hide => show
  153. if (!prevProps.visible && this.props.visible) {
  154. this.foundation.beforeShow();
  155. }
  156. // show => hide
  157. if (prevProps.visible && !this.props.visible) {
  158. this.foundation.afterHide();
  159. }
  160. if (prevState.displayNone !== this.state.displayNone) {
  161. this.foundation.onVisibleChange(!this.state.displayNone);
  162. }
  163. }
  164. componentWillUnmount() {
  165. if (this.props.visible) {
  166. this.foundation.destroy();
  167. }
  168. }
  169. handleCancel = (e: React.MouseEvent) => {
  170. this.foundation.handleCancel(e);
  171. };
  172. handleKeyDown = (e: KeyboardEvent) => {
  173. this.foundation.handleKeyDown(e);
  174. };
  175. updateState = () => {
  176. this.foundation.toggleDisplayNone(!this.props.visible);
  177. }
  178. renderContent() {
  179. const {
  180. placement,
  181. className,
  182. children,
  183. width,
  184. height,
  185. motion,
  186. visible,
  187. style,
  188. maskStyle,
  189. size,
  190. zIndex,
  191. getPopupContainer,
  192. keepDOM,
  193. ...props
  194. } = this.props;
  195. let wrapperStyle: CSSProperties = {
  196. zIndex,
  197. };
  198. if (getPopupContainer) {
  199. wrapperStyle = {
  200. zIndex,
  201. position: 'static',
  202. };
  203. }
  204. const { direction } = this.context;
  205. const isVertical = placement === 'left' || placement === 'right';
  206. const isHorizontal = placement === 'top' || placement === 'bottom';
  207. const sheetHeight = isHorizontal ? (height ? height : defaultHeight) : '100%';
  208. const classList = cls(prefixCls, className, {
  209. [`${prefixCls}-${placement}`]: placement,
  210. [`${prefixCls}-popup`]: getPopupContainer,
  211. [`${prefixCls}-horizontal`]: isHorizontal,
  212. [`${prefixCls}-rtl`]: direction === 'rtl',
  213. [`${prefixCls}-hidden`]: keepDOM && this.state.displayNone,
  214. });
  215. const contentProps = {
  216. ...(isVertical ? (width ? { width } : {}) : { width: "100%" }),
  217. ...props,
  218. visible,
  219. motion: false,
  220. size,
  221. className: classList,
  222. height: sheetHeight,
  223. onClose: this.handleCancel,
  224. };
  225. 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 */);
  226. // Since user could change animate duration , we don't know which animation end first. So we call updateState func twice.
  227. return <CSSAnimation motion={this.props.motion} animationState={visible ? 'enter' : 'leave'} startClassName={
  228. visible ? `${prefixCls}-animation-mask_show` : `${prefixCls}-animation-mask_hide`
  229. } onAnimationEnd={this.updateState}>
  230. {
  231. ({
  232. animationClassName: maskAnimationClassName,
  233. animationEventsNeedBind: maskAnimationEventsNeedBind
  234. }) => {
  235. return <CSSAnimation
  236. motion={this.props.motion}
  237. animationState={visible ? 'enter' : 'leave'}
  238. startClassName={visible ? `${prefixCls}-animation-content_show_${this.props.placement}` : `${prefixCls}-animation-content_hide_${this.props.placement}`}
  239. onAnimationEnd={this.updateState /* for no mask case*/}
  240. >
  241. {({ animationClassName, animationStyle, animationEventsNeedBind }) => {
  242. return shouldRender ?<Portal getPopupContainer={getPopupContainer} style={wrapperStyle}>
  243. <SideSheetContent
  244. {...contentProps}
  245. maskExtraProps={maskAnimationEventsNeedBind}
  246. wrapperExtraProps={animationEventsNeedBind}
  247. dialogClassName={animationClassName}
  248. maskClassName={maskAnimationClassName}
  249. maskStyle={{ ...maskStyle }}
  250. style={{ ...animationStyle, ...style }}>
  251. {children}
  252. </SideSheetContent>
  253. </Portal>:<></>;
  254. }}
  255. </CSSAnimation>;
  256. }
  257. }
  258. </CSSAnimation>;
  259. }
  260. render() {
  261. const {
  262. zIndex,
  263. getPopupContainer,
  264. visible
  265. } = this.props;
  266. return this.renderContent();
  267. }
  268. }