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!