選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Dialog.tsx 9.4KB

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