import React, { useState, useEffect, useContext } from 'react' import { ModalContext } from '@freesewing/react/context/Modal' import { CloseIcon } from '@freesewing/react/components/Icon' const slideClasses = { left: 'tw:-translate-x-full', right: 'tw:translate-x-full', top: 'tw:-translate-y-full', bottom: 'tw:translate-y-full', } /** * This component wraps modal content, making sure the layout is ok and handling transitions * * @param {object} props - All React props * @param {array} children - Content to render inside the modal * @param {string} flex - Flexbox direction (row or col) * @param {string} justify - Flexbox justify value * @param {string} items - Flexbox items value * @param {string} bg - Background * @param {string} bgOpacity - Background opacity * @param {bool} bare - Set true to not handle layout * @param {bool} keepOpenOnClick - Set to true to prevent a click in the modal content to close the modal * @param {string} slideFrom - Direction to slide in from * @param {bool} fullWidth - Set to true to not constrain the width */ export const ModalWrapper = ({ children = null, flex = 'row', justify = 'center', items = 'center', bg = 'neutral', bgOpacity = '70', bare = false, keepOpenOnClick = false, slideFrom = 'left', fullWidth = false, }) => { const { clearModal } = useContext(ModalContext) const [animate, setAnimate] = useState('in') const close = (evt) => { // Only process the first event if (evt?.event) evt.event.stopPropagation() setAnimate('out') window.setTimeout(clearModal, 150) } useEffect(() => { // only turn off animation if it's animating in if (animate === 'in') setAnimate(false) }, [animate]) // CSS classes for animation const animation = animate ? `tw:lg:opacity-0 ${slideClasses[slideFrom]} tw:lg:translate-x-0 tw:lg:translate-y-0` : 'tw:opacity-100 tw: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 (
{bare ? ( children ) : (
{children}
)}
) }