Skip to content

Instantly share code, notes, and snippets.

@realiarthur
Created November 11, 2021 17:29
Show Gist options
  • Save realiarthur/00ef523aeddef8eadd8b7a2a9a8f1e14 to your computer and use it in GitHub Desktop.
Save realiarthur/00ef523aeddef8eadd8b7a2a9a8f1e14 to your computer and use it in GitHub Desktop.
import React from 'react'
import ReactDOM from 'react-dom'
import { MODAL_ID } from 'core'
import cx from 'helpers/cx'
import Button from 'components/Button/Button'
import s from './style.module.css'
interface ModalProps {
title?: string
className?: string
close?: () => void
action?: () => void
actionText?: string
}
const Modal: React.FC<ModalProps> = ({
title,
children,
className,
actionText = 'Close',
action,
close
}) => {
const el = document.getElementById(MODAL_ID)
if (el === null) return null
const stopPropagation: React.MouseEventHandler<HTMLDivElement> = (e) =>
e.stopPropagation()
return ReactDOM.createPortal(
<div className={s.modal} onClick={close}>
<div className={s.modalBox} onClick={stopPropagation}>
<div className={cx(s.modalContent, className)}>
{title !== undefined && <h1 className={s.title}>{title}</h1>}
{close !== undefined && (
<div className={s.close} onClick={close}>
</div>
)}
<div>{children}</div>
{action !== undefined && (
<Button className={s.action} onClick={action}>
{actionText}
</Button>
)}
</div>
</div>
</div>,
el
)
}
export default Modal
.modal {
width: 100vw;
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
background: #111a;
}
.modalBox {
border: 1px solid var(--c-main);
max-height: 80vh;
background: #1a1a1a;
max-width: 80%;
height: fit-content;
overflow-y: scroll;
}
.modalContent {
position: relative;
text-align: center;
box-sizing: border-box;
padding: 30px 60px;
& .title {
margin-bottom: 30px;
text-align: center;
font-weight: 300;
line-height: 1.4;
}
& .action {
margin-top: 30px;
margin-bottom: 5px;
}
& .close {
position: absolute;
top: 0;
right: 0;
padding: 10px;
cursor: pointer;
height: 20px;
width: 20px;
}
}
@media (--m-mobile) {
.modalContent {
padding: 30px;
}
}
@media (--m-no-pointer) {
.modal {
height: var(--100vh);
}
.modalBox {
max-height: calc(0.8 * var(--100vh));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment