Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

Dialog.tsx 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. import { Theme } from '@mui/material';
  2. import React, { useCallback, useContext, useEffect } from 'react';
  3. import FocusLock from 'react-focus-lock';
  4. import { useTranslation } from 'react-i18next';
  5. import { useDispatch } from 'react-redux';
  6. import { keyframes } from 'tss-react';
  7. import { makeStyles } from 'tss-react/mui';
  8. import { hideDialog } from '../../../dialog/actions';
  9. import { IconClose } from '../../../icons/svg';
  10. import { withPixelLineHeight } from '../../../styles/functions.web';
  11. import Button from './Button';
  12. import ClickableIcon from './ClickableIcon';
  13. import { DialogTransitionContext } from './DialogTransition';
  14. const useStyles = makeStyles()((theme: Theme) => {
  15. return {
  16. container: {
  17. width: '100%',
  18. height: '100%',
  19. position: 'fixed',
  20. top: 0,
  21. left: 0,
  22. display: 'flex',
  23. justifyContent: 'center',
  24. alignItems: 'flex-start',
  25. animation: `${keyframes`
  26. 0% {
  27. opacity: 0.4;
  28. }
  29. 100% {
  30. opacity: 1;
  31. }
  32. `} 0.2s forwards ease-out`,
  33. '&.unmount': {
  34. animation: `${keyframes`
  35. 0% {
  36. opacity: 1;
  37. }
  38. 100% {
  39. opacity: 0.5;
  40. }
  41. `} 0.15s forwards ease-in`
  42. }
  43. },
  44. backdrop: {
  45. position: 'absolute',
  46. width: '100%',
  47. height: '100%',
  48. top: 0,
  49. left: 0,
  50. backgroundColor: theme.palette.ui02,
  51. opacity: 0.75
  52. },
  53. modal: {
  54. backgroundColor: theme.palette.ui01,
  55. border: `1px solid ${theme.palette.ui03}`,
  56. boxShadow: '0px 4px 25px 4px rgba(20, 20, 20, 0.6)',
  57. borderRadius: `${theme.shape.borderRadius}px`,
  58. display: 'flex',
  59. flexDirection: 'column',
  60. height: 'auto',
  61. minHeight: '200px',
  62. maxHeight: '560px',
  63. marginTop: '64px',
  64. animation: `${keyframes`
  65. 0% {
  66. margin-top: 85px
  67. }
  68. 100% {
  69. margin-top: 64px
  70. }
  71. `} 0.2s forwards ease-out`,
  72. '&.medium': {
  73. width: '400px'
  74. },
  75. '&.large': {
  76. width: '664px'
  77. },
  78. '&.unmount': {
  79. animation: `${keyframes`
  80. 0% {
  81. margin-top: 64px
  82. }
  83. 100% {
  84. margin-top: 40px
  85. }
  86. `} 0.15s forwards ease-in`
  87. },
  88. '@media (max-width: 448px)': {
  89. width: '100% !important',
  90. maxHeight: 'initial',
  91. height: '100%',
  92. margin: 0,
  93. position: 'absolute',
  94. top: 0,
  95. left: 0,
  96. bottom: 0,
  97. animation: `${keyframes`
  98. 0% {
  99. margin-top: 15px
  100. }
  101. 100% {
  102. margin-top: 0
  103. }
  104. `} 0.2s forwards ease-out`,
  105. '&.unmount': {
  106. animation: `${keyframes`
  107. 0% {
  108. margin-top: 0
  109. }
  110. 100% {
  111. margin-top: 15px
  112. }
  113. `} 0.15s forwards ease-in`
  114. }
  115. }
  116. },
  117. header: {
  118. width: '100%',
  119. padding: '24px',
  120. boxSizing: 'border-box',
  121. display: 'flex',
  122. alignItems: 'flex-start',
  123. justifyContent: 'space-between'
  124. },
  125. title: {
  126. color: theme.palette.text01,
  127. ...withPixelLineHeight(theme.typography.heading5),
  128. margin: 0,
  129. padding: 0
  130. },
  131. content: {
  132. height: 'auto',
  133. overflowY: 'auto',
  134. width: '100%',
  135. boxSizing: 'border-box',
  136. padding: '0 24px',
  137. overflowX: 'hidden',
  138. minHeight: '40px',
  139. '@media (max-width: 448px)': {
  140. height: '100%'
  141. }
  142. },
  143. footer: {
  144. width: '100%',
  145. boxSizing: 'border-box',
  146. display: 'flex',
  147. alignItems: 'center',
  148. justifyContent: 'flex-end',
  149. padding: '24px',
  150. '& button:last-child': {
  151. marginLeft: '16px'
  152. }
  153. },
  154. focusLock: {
  155. zIndex: 1
  156. }
  157. };
  158. });
  159. interface IDialogProps {
  160. back?: {
  161. hidden?: boolean;
  162. onClick?: () => void;
  163. translationKey?: string;
  164. };
  165. cancel?: {
  166. hidden?: boolean;
  167. translationKey?: string;
  168. };
  169. children?: React.ReactNode;
  170. className?: string;
  171. description?: string;
  172. disableBackdropClose?: boolean;
  173. disableEnter?: boolean;
  174. hideCloseButton?: boolean;
  175. ok?: {
  176. disabled?: boolean;
  177. hidden?: boolean;
  178. translationKey?: string;
  179. };
  180. onCancel?: () => void;
  181. onSubmit?: () => void;
  182. size?: 'large' | 'medium';
  183. title?: string;
  184. titleKey?: string;
  185. }
  186. const Dialog = ({
  187. back = { hidden: true },
  188. cancel = { translationKey: 'dialog.Cancel' },
  189. children,
  190. className,
  191. description,
  192. disableBackdropClose,
  193. hideCloseButton,
  194. disableEnter,
  195. ok = { translationKey: 'dialog.Ok' },
  196. onCancel,
  197. onSubmit,
  198. size = 'medium',
  199. title,
  200. titleKey
  201. }: IDialogProps) => {
  202. const { classes, cx } = useStyles();
  203. const { t } = useTranslation();
  204. const { isUnmounting } = useContext(DialogTransitionContext);
  205. const dispatch = useDispatch();
  206. const onClose = useCallback(() => {
  207. onCancel?.();
  208. dispatch(hideDialog());
  209. }, [ onCancel ]);
  210. const submit = useCallback(() => {
  211. onSubmit?.();
  212. dispatch(hideDialog());
  213. }, [ onSubmit ]);
  214. const handleKeyDown = useCallback((e: KeyboardEvent) => {
  215. if (e.key === 'Escape') {
  216. onClose();
  217. }
  218. if (e.key === 'Enter' && !disableEnter) {
  219. submit();
  220. }
  221. }, []);
  222. const onBackdropClick = useCallback(() => {
  223. !disableBackdropClose && onClose();
  224. }, [ disableBackdropClose, onClose ]);
  225. useEffect(() => {
  226. window.addEventListener('keydown', handleKeyDown);
  227. return () => window.removeEventListener('keydown', handleKeyDown);
  228. }, []);
  229. return (
  230. <div className = { cx(classes.container, isUnmounting && 'unmount') }>
  231. <div
  232. className = { classes.backdrop }
  233. onClick = { onBackdropClick } />
  234. <FocusLock className = { classes.focusLock }>
  235. <div
  236. aria-describedby = { description }
  237. aria-labelledby = { title ?? t(titleKey ?? '') }
  238. aria-modal = { true }
  239. className = { cx(classes.modal, isUnmounting && 'unmount', size, className) }
  240. role = 'dialog'>
  241. <div className = { classes.header }>
  242. <p
  243. className = { classes.title }
  244. id = 'dialog-title'>
  245. {title ?? t(titleKey ?? '')}
  246. </p>
  247. {!hideCloseButton && (
  248. <ClickableIcon
  249. accessibilityLabel = { t('dialog.close') }
  250. icon = { IconClose }
  251. id = 'modal-header-close-button'
  252. onClick = { onClose } />
  253. )}
  254. </div>
  255. <div className = { classes.content }>{children}</div>
  256. <div className = { classes.footer }>
  257. {!back.hidden && <Button
  258. accessibilityLabel = { t(back.translationKey ?? '') }
  259. labelKey = { back.translationKey }
  260. // eslint-disable-next-line react/jsx-handler-names
  261. onClick = { back.onClick }
  262. type = 'secondary' />}
  263. {!cancel.hidden && <Button
  264. accessibilityLabel = { t(cancel.translationKey ?? '') }
  265. labelKey = { cancel.translationKey }
  266. onClick = { onClose }
  267. type = 'tertiary' />}
  268. {!ok.hidden && <Button
  269. accessibilityLabel = { t(ok.translationKey ?? '') }
  270. disabled = { ok.disabled }
  271. id = 'modal-dialog-ok-button'
  272. labelKey = { ok.translationKey }
  273. onClick = { submit } />}
  274. </div>
  275. </div>
  276. </FocusLock>
  277. </div>
  278. );
  279. };
  280. export default Dialog;