2023-04-19 20:56:15 +02:00
|
|
|
import { useState, useEffect } from 'react'
|
2023-04-22 14:34:16 +02:00
|
|
|
import { useSwipeable } from 'react-swipeable'
|
|
|
|
import { ModalMenu } from 'site/components/navigation/modal-menu.mjs'
|
|
|
|
|
|
|
|
const slideClasses = {
|
|
|
|
left: '-translate-x-full',
|
|
|
|
right: 'translate-x-full',
|
|
|
|
top: '-translate-y-full',
|
|
|
|
bottom: 'translate-y-full',
|
|
|
|
}
|
2023-04-19 20:56:15 +02:00
|
|
|
|
2023-04-16 10:45:36 +02:00
|
|
|
export const ModalWrapper = ({
|
|
|
|
app,
|
2023-04-19 20:56:15 +02:00
|
|
|
children = null,
|
2023-04-16 10:45:36 +02:00
|
|
|
flex = 'row',
|
|
|
|
justify = 'center',
|
|
|
|
items = 'center',
|
|
|
|
bg = 'base-100 lg:bg-base-300',
|
|
|
|
bgOpacity = '100 lg:bg-opacity-95',
|
|
|
|
bare = false,
|
|
|
|
keepOpenOnClick = false,
|
2023-04-22 14:34:16 +02:00
|
|
|
slideFrom = 'left',
|
2023-04-19 20:56:15 +02:00
|
|
|
}) => {
|
|
|
|
const [animate, setAnimate] = useState(true)
|
|
|
|
|
2023-04-22 14:34:16 +02:00
|
|
|
const swipeActions = {}
|
|
|
|
const close = (evt) => {
|
|
|
|
// Only process the first swipe event
|
|
|
|
if (evt?.event) evt.event.stopPropagation()
|
|
|
|
setAnimate(true)
|
|
|
|
window.setTimeout(() => app.closeModal(), 250)
|
|
|
|
}
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
|
2023-04-19 20:56:15 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (animate) setAnimate(false)
|
2023-04-22 14:34:16 +02:00
|
|
|
}, [children])
|
|
|
|
|
|
|
|
// CSS classes for animation
|
|
|
|
const animation = animate
|
|
|
|
? `lg:opacity-0 ${slideClasses[slideFrom]} lg:translate-x-0 lg:translate-y-0`
|
|
|
|
: 'opacity-100 translate-none'
|
2023-04-19 20:56:15 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div
|
2023-04-22 14:34:16 +02:00
|
|
|
ref={swipeHandlers.ref}
|
|
|
|
onMouseDown={swipeHandlers.onMouseDown}
|
2023-04-19 20:56:15 +02:00
|
|
|
className={`fixed top-0 left-0 m-0 p-0 shadow w-full h-screen
|
2023-04-22 14:34:16 +02:00
|
|
|
transform-all duration-150 ${animation}
|
2023-04-16 10:45:36 +02:00
|
|
|
bg-${bg} bg-opacity-${bgOpacity} z-50 hover:cursor-pointer
|
|
|
|
flex flex-${flex} justify-${justify} overflow-auto items-${items}`}
|
2023-04-22 14:34:16 +02:00
|
|
|
onClick={keepOpenOnClick ? null : close}
|
2023-04-19 20:56:15 +02:00
|
|
|
>
|
|
|
|
{bare ? (
|
|
|
|
children
|
|
|
|
) : (
|
|
|
|
<div className="bg-base-100 p-4 lg:px-8 lg:rounded-lg lg:shadow-lg">{children}</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|