提交 2874a3e0 编写于 作者: H Henrik Wenz 提交者: Joe Haddad

Refactor with-modal example (#9097)

## Changelog

- Use functional components instead of class components
- Use default exports like in all other examples
- Rename `Portal` component to `ClientOnlyPortal`.
上级 c650ed9a
import { useRef, useEffect, useState } from 'react'
import { createPortal } from 'react-dom'
export default function ClientOnlyPortal ({ children, selector }) {
const ref = useRef()
const [mounted, setMounted] = useState(false)
useEffect(() => {
ref.current = document.querySelector(selector)
setMounted(true)
}, [])
return mounted ? createPortal(children, ref.current) : null
}
import * as React from 'react' import React, { useState } from 'react'
import ClientOnlyPortal from './ClientOnlyPortal'
import { Portal } from './Portal' export default function Modal () {
const [open, setOpen] = useState()
export class Modal extends React.Component { return (
constructor () { <React.Fragment>
super(...arguments) <button type='button' onClick={event => setOpen(true)}>
this.state = { opened: false } Open Modal
} </button>
{open && (
open = () => { <ClientOnlyPortal selector='#modal'>
this.setState({ opened: true }) <div className='backdrop'>
} <div className='modal'>
<p>
close = () => { This modal is rendered using{' '}
this.setState({ opened: false }) <a href='https://reactjs.org/docs/portals.html' target='_blank'>
} portals
</a>
.
</p>
<button type='button' onClick={event => setOpen(false)}>
Close Modal
</button>
</div>
<style jsx>{`
:global(body) {
overflow: hidden;
}
render () { .backdrop {
return ( position: fixed;
<React.Fragment> background-color: rgba(0, 0, 0, 0.7);
<button type='button' onClick={this.open}> top: 0;
Open Modal right: 0;
</button> bottom: 0;
{this.state.opened && ( left: 0;
<Portal selector='#modal'> }
<div className='overlay'>
<div className='modal'>
<p>
This modal is rendered using{' '}
<a href='https://reactjs.org/docs/portals.html'>portals</a>.
</p>
<button type='button' onClick={this.close}>
Close Modal
</button>
</div>
<style jsx global>{`
body {
overflow: hidden;
}
`}</style>
<style jsx>{`
.overlay {
position: fixed;
background-color: rgba(0, 0, 0, 0.7);
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.modal { .modal {
background-color: white; background-color: white;
position: absolute; position: absolute;
top: 10%; top: 10%;
right: 10%; right: 10%;
bottom: 10%; bottom: 10%;
left: 10%; left: 10%;
padding: 1em; padding: 1em;
} }
`}</style> `}</style>
</div> </div>
</Portal> </ClientOnlyPortal>
)} )}
</React.Fragment> </React.Fragment>
) )
}
} }
import React from 'react'
import ReactDOM from 'react-dom'
export class Portal extends React.Component {
componentDidMount () {
this.element = document.querySelector(this.props.selector)
this.forceUpdate()
}
render () {
if (this.element === undefined) {
return null
}
return ReactDOM.createPortal(this.props.children, this.element)
}
}
...@@ -7,7 +7,7 @@ export default class extends Document { ...@@ -7,7 +7,7 @@ export default class extends Document {
<Head /> <Head />
<body> <body>
<Main /> <Main />
{/* here we will mount our modal portal */} {/* Here we will mount our modal portal */}
<div id='modal' /> <div id='modal' />
<NextScript /> <NextScript />
</body> </body>
......
import * as React from 'react' import React from 'react'
import Modal from '../components/Modal'
import { Modal } from '../components/Modal'
export default () => <Modal /> export default () => <Modal />
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册