import React, { useState, useEffect } from 'react' import classnames from 'classnames' import FeatherIcon from 'feather-icons-react' import Dialog from '@reach/dialog' import { motion, AnimatePresence, useReducedMotion } from 'framer-motion' import { NetworkIcon } from '../Icons/NetworkIcon' import { NextRouter } from 'next/router' import classNames from 'classnames' export interface ModalProps { isOpen: boolean closeModal: () => void label: string children: React.ReactNode router?: NextRouter header?: React.ReactNode className?: string widthClassName?: string modalHeightClassName?: string maxWidthClassName?: string maxHeightClassName?: string paddingClassName?: string bgClassName?: string roundedClassName?: string shadowClassName?: string overflowClassName?: string style?: object onPreviousClick?: () => void onNextClick?: () => void } export const Modal = (props: ModalProps) => { const { isOpen, closeModal, children, label, header, className, widthClassName, modalHeightClassName, maxWidthClassName, maxHeightClassName, paddingClassName, bgClassName, roundedClassName, shadowClassName, overflowClassName, style, router, onPreviousClick, onNextClick } = props // TODO: This barely works. Better than nothing though. useEffect(() => { router?.beforePopState(() => { if (isOpen) { closeModal() router.replace(router.asPath, undefined, { shallow: true }) return false } return true }) return () => { router?.beforePopState(() => true) } }, [router, isOpen]) const [isDialogOpen, setIsDialogOpen] = useState(isOpen) useEffect(() => { if (isOpen) { setIsDialogOpen(true) } }, [isOpen]) const shouldReduceMotion = useReducedMotion() if (!label) { console.warn('Modal required a label! with children:', children) } return ( setIsDialogOpen(false)}> {isOpen && (
{children}
)}
) } Modal.defaultProps = { noPad: false, bgClassName: 'bg-new-modal', roundedClassName: 'rounded-none xs:rounded-xl', maxWidthClassName: 'xs:max-w-lg', widthClassName: 'w-screen xs:w-full', modalHeightClassName: 'h-actually-full-screen xs:h-auto', maxHeightClassName: 'max-h-actually-full-screen xs:max-h-90-screen', paddingClassName: 'px-2 xs:px-8 pt-2 pb-12 xs:py-12', shadowClassName: 'shadow-3xl', overflowClassName: 'overflow-y-auto minimal-scrollbar' } const ModalHeader = (props: { header: React.ReactNode className?: string closeModal: () => void onPreviousClick: () => void onNextClick: () => void }) => { const { header, className, closeModal, onPreviousClick, onNextClick } = props return (
{onPreviousClick && } {onNextClick && }
{header}
) } export const CloseModalButton = (props) => ( ) export const PreviousButton = (props) => ( ) export const NextButton = (props) => ( ) const ModalHeaderButton: React.FC<{ onClick: () => void; className?: string; icon: string }> = ( props ) => { const { onClick, className, icon } = props return ( ) } interface ModalTitleProps { className?: string icon?: React.ReactNode title: string chainId?: number } export const ModalTitle = (props: ModalTitleProps) => { const { className, title, chainId, icon } = props return (
{chainId ? ( ) : ( icon )}
{title}
) } ModalTitle.defaultProps = { icon: null }