'use client' import PropTypes from 'prop-types' import { useCallback, useEffect, useState } from 'react' import { Button } from '../../atoms/Button' import { BUTTONS_TEXT, MODAL_SIZES } from './constanst' import { getGlobalStyle } from '../../../helpers' import { Icon, Text } from '../../atoms' import styles from './styles.module.css' interface IPropsAwesomeModal { backgroundColor?: string title?: string size?: string show?: boolean disabled?: any display?: any zIndex?: any cancel?: string confirm?: string padding?: any backdrop?: string iconConfirm?: any useScroll?: boolean keyboard?: boolean footer?: boolean btnCancel?: boolean openLateral?: any btnConfirm?: boolean children?: any hideOnConfirm?: boolean timeOut?: number backdropAnimation?: boolean height?: any bgColor?: any question?: boolean customHeight?: any submit?: boolean header?: boolean sizeIconClose?: number borderRadius?: string onHide?: () => void onCancel?: () => void onConfirm?: () => void } export const AwesomeModal: React.FC = ({ backgroundColor = '', title, size = MODAL_SIZES.medium, show, disabled, display, zIndex, cancel, confirm, padding, backdrop = '', iconConfirm = null, useScroll = false, keyboard = true, footer = true, btnCancel = true, btnConfirm = true, children, hideOnConfirm = true, timeOut = 200, height, bgColor, question = false, customHeight = '', submit = false, header = true, sizeIconClose = 30, borderRadius = '.3rem', onHide = () => { }, onCancel = () => { }, onConfirm = () => { } }) => { const [state, setState] = useState(Boolean(show)) const [modal, setModal] = useState(false) const hide = useCallback(() => { setState(false) onCancel() setModal(false) setTimeout(onHide, timeOut) }, [onCancel, onHide, timeOut]) const onShowQuestion = (): void => { return setModal(!modal) } useEffect(() => { const handleKeyUp = (e: KeyboardEvent): void => { if (e.code === 'Escape') { setModal(true) } } if (question && backdrop === 'static' && state && (show ?? false)) { window.addEventListener('keyup', handleKeyUp) return () => { if (keyboard) window.removeEventListener('keyup', handleKeyUp) } } if (backdrop !== 'static' && keyboard && (show ?? false)) { window.addEventListener('keyup', handleKeyUp) return () => { window.removeEventListener('keyup', handleKeyUp as EventListener) } } // Cleanup for other cases return () => { } }, [keyboard, hide, show, backdrop, question, modal, state]) useEffect(() => { return setState(Boolean(show)) }, [show]) useEffect(() => { if (show === true && useScroll) { document.body.style.overflow = 'hidden' } else { document.body.style.overflow = 'auto' } }, [show, useScroll]) const clickCancel = (): void => { setState(false) onCancel() setModal(false) setTimeout(onHide, timeOut) onCancel() } const clickConfirm = (): void => { if (hideOnConfirm) setState(false) onCancel() setModal(false) if (hideOnConfirm) { setTimeout(onHide, timeOut) } onConfirm() } const onClickBackdrop = (): void => { return hide() } const classNames = [ styles.container, (show ?? false) && state ? styles.fadeInFast : '', (show ?? false) && !state ? styles.fadeInSlow : '' ].join(' ') const sizeClass = ({ [MODAL_SIZES.small]: styles.small, [MODAL_SIZES.medium]: styles.medium, [MODAL_SIZES.large]: styles.large }[size as keyof typeof MODAL_SIZES]) ?? (size !== MODAL_SIZES.medium ? size : MODAL_SIZES.medium) return (
{ return e.stopPropagation() }} > {header && (
{title}
)}
{children}
{footer && (
{btnCancel && ( )} {btnConfirm && ( )}
)}
) } AwesomeModal.propTypes = { backdrop: PropTypes.string, backdropAnimation: PropTypes.bool, backgroundColor: PropTypes.any, bgColor: PropTypes.any, borderRadius: PropTypes.string, btnCancel: PropTypes.bool, btnConfirm: PropTypes.bool, cancel: PropTypes.string, children: PropTypes.any, confirm: PropTypes.any, customHeight: PropTypes.bool, disabled: PropTypes.any, display: PropTypes.any, footer: PropTypes.bool, header: PropTypes.bool, height: PropTypes.any, hideOnConfirm: PropTypes.bool, iconConfirm: PropTypes.any, keyboard: PropTypes.bool, onCancel: PropTypes.func, onConfirm: PropTypes.func, onHide: PropTypes.func, openLateral: PropTypes.any, padding: PropTypes.any, question: PropTypes.bool, show: PropTypes.any, size: PropTypes.any, sizeIconClose: PropTypes.number, submit: PropTypes.bool, timeOut: PropTypes.number, title: PropTypes.string, useScroll: PropTypes.bool, zIndex: PropTypes.any }