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
32eb6b77
Commit
32eb6b77
authored
Dec 02, 2017
by
Sergio Xalambrí
Committed by
Tim Neutkens
Dec 02, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add with Portals example (#3372)
parent
01f05d8c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
137 additions
and
0 deletions
+137
-0
README.md
examples/with-portals/README.md
+27
-0
modal.js
examples/with-portals/components/modal.js
+50
-0
package.json
examples/with-portals/package.json
+12
-0
_document.js
examples/with-portals/pages/_document.js
+17
-0
index.js
examples/with-portals/pages/index.js
+31
-0
No files found.
examples/with-portals/README.md
0 → 100644
View file @
32eb6b77
# Example with portals
## How to use
Download the example
[
or clone the repo
](
https://github.com/zeit/next.js
)
:
```
bash
curl https://codeload.github.com/zeit/next.js/tar.gz/master |
tar
-xz
--strip
=
2 next.js-master/examples/with-portals
cd
with-portals
```
Install it and run:
```
bash
npm
install
npm run dev
```
Deploy it to the cloud with
[
now
](
https://zeit.co/now
)
(
[download](https://zeit.co/download
)
)
```
bash
now
```
## The idea behind the example
This example show how to use the React
[
Portals
](
https://reactjs.org/docs/portals.html
)
feature with Next.js.
examples/with-portals/components/modal.js
0 → 100644
View file @
32eb6b77
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/package.json
0 → 100644
View file @
32eb6b77
{
"scripts"
:
{
"dev"
:
"next"
,
"build"
:
"next build"
,
"start"
:
"next start"
},
"dependencies"
:
{
"next"
:
"^4.1.4"
,
"react"
:
"^16.2.0"
,
"react-dom"
:
"^16.2.0"
}
}
examples/with-portals/pages/_document.js
0 → 100644
View file @
32eb6b77
import
Document
,
{
Head
,
Main
,
NextScript
}
from
'next/document'
export
default
class
extends
Document
{
render
()
{
return
(
<
html
>
<
Head
/>
<
body
>
<
Main
/>
{
/* here we will mount our modal portal */
}
<
div
id
=
'modal'
/>
<
NextScript
/>
<
/body
>
<
/html
>
)
}
}
examples/with-portals/pages/index.js
0 → 100644
View file @
32eb6b77
import
{
Component
}
from
'react'
import
dynamic
from
'next/dynamic'
// we import and render the modal only client side (because we need a DOM)
const
Modal
=
dynamic
(
import
(
'../components/modal'
),
{
ssr
:
false
})
export
default
class
extends
Component
{
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