Files
grungnet/src/components/Modal.tsx

87 lines
2.0 KiB
TypeScript
Raw Normal View History

2021-06-02 17:49:31 +10:00
import React, { ReactChild } from "react";
import Modal, { Props } from "react-modal";
import { useThemeUI, Close } from "theme-ui";
2021-05-24 17:28:57 +10:00
import { useSpring, animated, config } from "react-spring";
2021-06-02 17:49:31 +10:00
type ModalProps = Props & {
2021-07-02 15:54:54 +10:00
children: ReactChild | ReactChild[];
allowClose: boolean;
};
2020-04-14 16:05:44 +10:00
function StyledModal({
isOpen,
onRequestClose,
children,
allowClose,
style,
2020-04-14 16:05:44 +10:00
...props
2021-07-02 15:54:54 +10:00
}: ModalProps) {
const { theme } = useThemeUI();
2021-05-24 17:28:57 +10:00
const openAnimation = useSpring({
opacity: isOpen ? 1 : 0,
transform: isOpen ? "scale(1)" : "scale(0.99)",
config: config.default,
});
return (
<Modal
isOpen={isOpen}
onRequestClose={onRequestClose}
style={{
overlay: {
backgroundColor: "rgba(0, 0, 0, 0.73)",
zIndex: 100,
display: "flex",
alignItems: "center",
justifyContent: "center",
2021-07-02 15:54:54 +10:00
...(style?.overlay || {}),
},
content: {
backgroundColor: theme.colors.background,
2021-06-09 08:32:58 +10:00
top: "initial",
left: "initial",
bottom: "initial",
right: "initial",
maxHeight: "100%",
2021-07-02 15:54:54 +10:00
...(style?.content || {}),
2021-06-02 17:49:31 +10:00
} as React.CSSProperties,
}}
2021-05-24 17:28:57 +10:00
contentElement={(props, content) => (
<animated.div {...props} style={{ ...props.style, ...openAnimation }}>
{content}
</animated.div>
)}
overlayElement={(props, content) => (
<div
onDragEnter={(e) => {
// Prevent drag event from triggering with a modal open
e.preventDefault();
e.stopPropagation();
}}
{...props}
>
{content}
</div>
)}
2020-04-13 18:29:46 +10:00
{...props}
>
{children}
{allowClose && (
<Close
m={0}
sx={{ position: "absolute", top: 0, right: 0 }}
onClick={onRequestClose}
/>
)}
</Modal>
);
}
2020-04-14 16:05:44 +10:00
StyledModal.defaultProps = {
allowClose: true,
style: {},
2020-04-14 16:05:44 +10:00
};
export default StyledModal;