// __SDEFILE__ - This file is a dependency for the stand-alone environment import { useState, useEffect, useContext } from 'react' import { useSwipeable } from 'react-swipeable' import { ModalContext } from 'shared/context/modal-context.mjs' import { CloseIcon } from 'shared/components/icons.mjs' const slideClasses = { left: '-translate-x-full', right: 'translate-x-full', top: '-translate-y-full', bottom: 'translate-y-full', } export const ModalWrapper = ({ children = null, flex = 'row', justify = 'center', items = 'center', bg = 'base-100 lg:bg-base-300', bgOpacity = '100 lg:bg-opacity-95', bare = false, keepOpenOnClick = false, slideFrom = 'left', keepOpenOnSwipe = false, fullWidth = false, }) => { const { clearModal } = useContext(ModalContext) const [animate, setAnimate] = useState('in') const close = (evt) => { // Only process the first swipe event if (evt?.event) evt.event.stopPropagation() setAnimate('out') window.setTimeout(clearModal, 150) } const swipeActions = {} if (!keepOpenOnSwipe) { if (slideFrom === 'left') swipeActions.onSwipedLeft = close else if (slideFrom === 'right') swipeActions.onSwipedRight = close else if (slideFrom === 'top') swipeActions.onSwipedUp = close else if (slideFrom === 'bottom') swipeActions.onSwipedDown = close } const swipeHandlers = useSwipeable({ ...swipeActions, trackMouse: true, }) useEffect(() => { // only turn off animation if it's animating in if (animate === 'in') setAnimate(false) }, [animate]) // CSS classes for animation const animation = animate ? `lg:opacity-0 ${slideClasses[slideFrom]} lg:translate-x-0 lg:translate-y-0` : 'opacity-100 translate-none' const stopClick = (evt) => { /* * Do not keep modal open for links (with a href) * but do keep it open for buttons (like a new modal context) */ if (!evt.target.attributes.href) evt.stopPropagation() } return (