modalContentFoundation.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import BaseFoundation, { DefaultAdapter } from '../base/foundation';
  2. import KeyCode from '../utils/keyCode';
  3. import { ModalProps } from '../modal/modalFoundation';
  4. export interface ModalContentProps extends ModalProps {
  5. onClose: (e: any) => void;
  6. getContainerContext: () => any;
  7. isFullScreen?: boolean;
  8. contentClassName?: string,
  9. maskClassName?: string;
  10. onAnimationEnd?: () => void;
  11. }
  12. export interface ModalContentState {
  13. dialogMouseDown: boolean;
  14. prevFocusElement: HTMLElement;
  15. }
  16. export interface ModalContentAdapter extends DefaultAdapter<ModalContentProps, ModalContentState> {
  17. notifyClose: (e: any) => void;
  18. notifyDialogMouseDown: () => void;
  19. notifyDialogMouseUp: () => void;
  20. addKeyDownEventListener: () => void;
  21. removeKeyDownEventListener: () => void;
  22. getMouseState: () => boolean;
  23. modalDialogFocus: () => void;
  24. modalDialogBlur: () => void;
  25. prevFocusElementReFocus: () => void;
  26. }
  27. export default class ModalContentFoundation extends BaseFoundation<ModalContentAdapter> {
  28. constructor(adapter: ModalContentAdapter) {
  29. super({ ...ModalContentFoundation.defaultAdapter, ...adapter });
  30. }
  31. destroy() {
  32. this.handleKeyDownEventListenerUnmount();
  33. this.modalDialogBlur();
  34. this.prevFocusElementReFocus();
  35. }
  36. handleDialogMouseDown() {
  37. this._adapter.notifyDialogMouseDown();
  38. }
  39. handleMaskMouseUp() {
  40. this._adapter.notifyDialogMouseUp();
  41. }
  42. handleKeyDown(e: any) {
  43. const { closeOnEsc } = this.getProps();
  44. if (closeOnEsc && e.keyCode === KeyCode.ESC) {
  45. e.stopPropagation();
  46. this.close(e);
  47. return;
  48. }
  49. }
  50. handleKeyDownEventListenerMount() {
  51. this._adapter.addKeyDownEventListener();
  52. }
  53. handleKeyDownEventListenerUnmount() {
  54. this._adapter.removeKeyDownEventListener();
  55. }
  56. getMouseState() {
  57. this._adapter.getMouseState();
  58. }
  59. handleMaskClick(e: any) {
  60. const { dialogMouseDown } = this.getStates();
  61. if (e.target === e.currentTarget && !dialogMouseDown) {
  62. this.close(e);
  63. }
  64. }
  65. close(e: any) {
  66. this._adapter.notifyClose(e);
  67. }
  68. modalDialogFocus() {
  69. this._adapter.modalDialogFocus();
  70. }
  71. modalDialogBlur() {
  72. this._adapter.modalDialogBlur();
  73. }
  74. prevFocusElementReFocus() {
  75. this._adapter.prevFocusElementReFocus();
  76. }
  77. }