|
| 1 | +import React from 'react'; |
| 2 | +import { ModalProps } from './modal.types'; |
| 3 | +import Theme from '../theme'; |
| 4 | +import { ClassNames } from '@emotion/react'; |
| 5 | +import getNamespace from '../_util/getNamespace'; |
| 6 | +import getClassName from '../_util/getClassName'; |
| 7 | + |
| 8 | +const Modal = React.forwardRef<HTMLDivElement, ModalProps>(function Modal( |
| 9 | + { |
| 10 | + type, |
| 11 | + css: _css, |
| 12 | + children, |
| 13 | + className, |
| 14 | + onDestroy, |
| 15 | + index, |
| 16 | + template, |
| 17 | + render, |
| 18 | + ...props |
| 19 | + }, |
| 20 | + ref, |
| 21 | +) { |
| 22 | + const theme = Theme.useContext(); |
| 23 | + const themeComponent = theme?.components?.modal; |
| 24 | + const themeComponentType = type |
| 25 | + ? theme?.components?.modal?.type?.[type] |
| 26 | + : undefined; |
| 27 | + |
| 28 | + return ( |
| 29 | + <ClassNames> |
| 30 | + {({ css, cx }) => ( |
| 31 | + <div |
| 32 | + ref={ref} |
| 33 | + className={cx( |
| 34 | + getNamespace(theme?.namespace), |
| 35 | + css` |
| 36 | + ${typeof themeComponent?.css === 'function' |
| 37 | + ? themeComponent.css({ |
| 38 | + color: theme?.color, |
| 39 | + index, |
| 40 | + }) |
| 41 | + : themeComponent?.css}; |
| 42 | + ${type && |
| 43 | + (typeof themeComponentType?.css === 'function' |
| 44 | + ? themeComponentType.css({ |
| 45 | + color: theme?.color, |
| 46 | + index, |
| 47 | + }) |
| 48 | + : themeComponentType?.css)}; |
| 49 | + ${typeof _css === 'function' |
| 50 | + ? _css({ color: theme?.color, index }) |
| 51 | + : _css}; |
| 52 | + `, |
| 53 | + getClassName(theme?.namespace, 'modal'), |
| 54 | + themeComponent?.className, |
| 55 | + type && getClassName(theme?.namespace, `modal__type__${type}`), |
| 56 | + themeComponentType?.className, |
| 57 | + className, |
| 58 | + )} |
| 59 | + {...props} |
| 60 | + > |
| 61 | + {template |
| 62 | + ? template({ |
| 63 | + children: render?.({ onDestroy, ...props }), |
| 64 | + onDestroy, |
| 65 | + ...props, |
| 66 | + }) |
| 67 | + : themeComponentType?.template |
| 68 | + ? themeComponentType.template({ |
| 69 | + children: render?.({ onDestroy, ...props }), |
| 70 | + onDestroy, |
| 71 | + ...props, |
| 72 | + }) |
| 73 | + : themeComponent?.template |
| 74 | + ? themeComponent.template({ |
| 75 | + children: render?.({ onDestroy, ...props }), |
| 76 | + onDestroy, |
| 77 | + ...props, |
| 78 | + }) |
| 79 | + : render?.({ onDestroy, ...props })} |
| 80 | + </div> |
| 81 | + )} |
| 82 | + </ClassNames> |
| 83 | + ); |
| 84 | +}); |
| 85 | + |
| 86 | +export default Modal; |
0 commit comments