Skip to content

Instantly share code, notes, and snippets.

@corysimmons
Last active June 8, 2019 15:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save corysimmons/c1adcc4af71e7d9eb9ced0437bd208d6 to your computer and use it in GitHub Desktop.
Save corysimmons/c1adcc4af71e7d9eb9ced0437bd208d6 to your computer and use it in GitHub Desktop.
Scrollable react-modal's within Next.js
// ...
const [modalOpen, setModalOpen] = useState(false)
<Modal
isOpen={modalOpen}
onRequestClose={() => setModalOpen(false)}
setModalOpen={setModalOpen}
>
<div>
<h1>Ayy</h1>
<p>Yoo</p>
</div>
</Modal>
// ...
import { css } from '@emotion/core'
import styled from '@emotion/styled'
import ReactModal from 'react-modal'
ReactModal.setAppElement('#__next')
ReactModal.defaultStyles = {}
export const globalStyles = css`
.ReactModal__Html--open,
.ReactModal__Body--open {
overflow: hidden;
}
.ReactModal__Overlay {
position: fixed;
z-index: 999999;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.3);
display: flex;
align-items: center;
justify-content: center;
}
.ReactModal__Content {
background: white;
outline: none;
width: 50rem;
max-width: calc(100vw - 2rem);
max-height: calc(100vh - 2rem);
box-shadow: 0 0 34px 0 rgba(0, 0, 0, 0.24);
overflow-y: auto;
position: relative;
}
`
export const S = styled(ReactModal)`
.modal-close-btn {
cursor: pointer;
top: 1.5rem;
right: 1.5rem;
position: absolute;
width: 3rem;
height: 3rem;
display: flex;
align-items: center;
justify-content: center;
font-size: 1.75rem;
background: none;
border: 0;
outline: none;
transition: all 200ms ease;
&:hover,
&:focus {
background: rgba(0, 0, 0, 0.05);
}
}
.modal-content {
min-height: 6rem;
padding: 4.5rem;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
> * {
width: 100%;
}
}
`
import { Global } from '@emotion/core'
import { faTimes } from '@fortawesome/pro-light-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { globalStyles, S } from './Modal.css'
const Modal = props => {
const { setModalOpen, children } = props
return (
<S {...props}>
<Global styles={globalStyles} />
<button className="modal-close-btn" onClick={() => setModalOpen(false)}>
<FontAwesomeIcon icon={faTimes} />
</button>
<div className="modal-content">{children}</div>
</S>
)
}
export default Modal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment