Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
053a248c
Commit
053a248c
authored
Jan 31, 2018
by
Brice BERNARD
Committed by
Tim Neutkens
Jan 31, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[with-portals] Remove dynamic import, add <Portal /> (#3571)
parent
9165d753
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
87 additions
and
79 deletions
+87
-79
Modal.js
examples/with-portals/components/Modal.js
+68
-0
Portal.js
examples/with-portals/components/Portal.js
+16
-0
modal.js
examples/with-portals/components/modal.js
+0
-50
index.js
examples/with-portals/pages/index.js
+3
-29
No files found.
examples/with-portals/components/Modal.js
0 → 100644
View file @
053a248c
import
*
as
React
from
'react'
import
{
Portal
}
from
'./Portal'
export
class
Modal
extends
React
.
Component
{
constructor
()
{
super
(...
arguments
)
this
.
state
=
{
opened
:
false
}
}
open
=
()
=>
{
this
.
setState
({
opened
:
true
})
}
close
=
()
=>
{
this
.
setState
({
opened
:
false
})
}
render
()
{
return
(
<
React
.
Fragment
>
<
button
type
=
'button'
onClick
=
{
this
.
open
}
>
Open
Modal
<
/button
>
{
this
.
state
.
opened
&&
(
<
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 {
background-color: white;
position: absolute;
top: 10%;
right: 10%;
bottom: 10%;
left: 10%;
padding: 1em;
}
`
}
<
/style
>
<
/div
>
<
/Portal
>
)}
<
/React.Fragment
>
)
}
}
examples/with-portals/components/Portal.js
0 → 100644
View file @
053a248c
import
React
from
'react'
import
ReactDOM
from
'react-dom'
export
class
Portal
extends
React
.
Component
{
componentDidMount
()
{
this
.
element
=
document
.
querySelector
(
this
.
props
.
selector
)
}
render
()
{
if
(
this
.
element
===
undefined
)
{
return
null
}
return
ReactDOM
.
createPortal
(
this
.
props
.
children
,
this
.
element
)
}
}
examples/with-portals/components/modal.js
deleted
100644 → 0
View file @
9165d753
import
{
Component
}
from
'react'
import
{
createPortal
}
from
'react-dom'
export
default
class
extends
Component
{
componentWillMount
()
{
// get the mount point, is because of this why the modal
// can't be used server side, we need access to the DOM
this
.
modalRoot
=
document
.
getElementById
(
'modal'
)
}
render
()
{
const
{
title
,
children
}
=
this
.
props
return
createPortal
(
<
div
className
=
'overlay'
>
<
div
className
=
'modal'
>
<
h2
>
{
title
}
<
/h2
>
{
children
}
<
/div
>
<
style
jsx
global
>
{
`
body {
/* this avoid any possible scroll on the body */
overflow: hidden;
}
`
}
<
/style
>
<
style
jsx
>
{
`
.overlay {
position: fixed;
background-color: rgba(0, 0, 0, .7);
top: 0;
right: 0;
bottom: 0;
left: 0;
}
.modal {
background-color: white;
position: absolute;
top: 10%;
right: 10%;
bottom: 10%;
left: 10%;
padding: 1em;
}
`
}
<
/style
>
<
/div>
,
this
.
modalRoot
)
}
}
examples/with-portals/pages/index.js
View file @
053a248c
import
{
Component
}
from
'react'
import
*
as
React
from
'react'
import
dynamic
from
'next/dynamic'
// we import and render the modal only client side (because we need a DOM)
import
{
Modal
}
from
'../components/Modal'
const
Modal
=
dynamic
(
import
(
'../components/modal'
),
{
ssr
:
false
})
export
default
class
extends
Component
{
export
default
()
=>
<
Modal
/>
state
=
{
modal
:
false
};
toggleModal
=
()
=>
this
.
setState
(
state
=>
({
modal
:
!
state
.
modal
}))
render
()
{
return
(
<
main
>
<
button
type
=
'button'
onClick
=
{
this
.
toggleModal
}
>
Open
modal
<
/button
>
{
this
.
state
.
modal
&&
(
<
Modal
title
=
'My Modal Portal'
>
<
p
>
This
is
a
portal
rendered
from
Next
.
js
<
/p
>
<
button
type
=
'button'
onClick
=
{
this
.
toggleModal
}
>
Close
<
/button
>
<
/Modal
>
)}
<
/main
>
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment