-
Notifications
You must be signed in to change notification settings - Fork 820
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vlad Moroz
committed
May 28, 2024
1 parent
ab3e5e9
commit 934278a
Showing
5 changed files
with
79 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { Portal } from '@radix-ui/react-portal'; | ||
|
||
export const ConditionalPortal = () => { | ||
const [container, setContainer] = React.useState<Element | null>(null); | ||
const [open, setOpen] = React.useState(false); | ||
return ( | ||
<div> | ||
<button onClick={() => setOpen((prev) => !prev)}>Toggle another portal</button> | ||
<b ref={setContainer} /> | ||
{open && ( | ||
<Portal container={container}> | ||
<span>This content is rendered in a custom container</span> | ||
</Portal> | ||
)} | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use client'; | ||
|
||
import * as React from 'react'; | ||
import { Portal } from '@radix-ui/react-portal'; | ||
|
||
export const CustomPortalContainer = () => { | ||
const [container, setContainer] = React.useState<Element | null>(null); | ||
return ( | ||
<div> | ||
<em ref={setContainer} /> | ||
<Portal container={container}> | ||
<span>This content is rendered in a custom container</span> | ||
</Portal> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
import * as React from 'react'; | ||
import { Portal } from '@radix-ui/react-portal'; | ||
import { CustomPortalContainer } from './custom-portal-container'; | ||
import { ConditionalPortal } from './conditional-portal'; | ||
|
||
export default function Page() { | ||
return ( | ||
<div | ||
style={{ | ||
maxWidth: 300, | ||
maxHeight: 200, | ||
overflow: 'auto', | ||
border: '1px solid', | ||
}} | ||
> | ||
<h1>This content is rendered in the main DOM tree</h1> | ||
<p> | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos porro, est ex quia itaque | ||
facere fugit necessitatibus aut enim. Nisi rerum quae, repellat in perspiciatis explicabo | ||
laboriosam necessitatibus eius pariatur. | ||
</p> | ||
|
||
<Portal> | ||
<h1>This content is rendered in a portal (another DOM tree)</h1> | ||
<div> | ||
<div | ||
style={{ | ||
maxWidth: 300, | ||
maxHeight: 200, | ||
overflow: 'auto', | ||
border: '1px solid', | ||
}} | ||
> | ||
<h1>This content is rendered in the main DOM tree</h1> | ||
<p> | ||
Because of the portal, it can appear in a different DOM tree from the main one (by default | ||
a new element inside the body), even though it is part of the same React tree. | ||
Lorem ipsum dolor sit amet consectetur adipisicing elit. Quos porro, est ex quia itaque | ||
facere fugit necessitatibus aut enim. Nisi rerum quae, repellat in perspiciatis explicabo | ||
laboriosam necessitatibus eius pariatur. | ||
</p> | ||
</Portal> | ||
|
||
<Portal> | ||
<h1>This content is rendered in a portal (another DOM tree)</h1> | ||
<p> | ||
Because of the portal, it can appear in a different DOM tree from the main one (by | ||
default a new element inside the body), even though it is part of the same React tree. | ||
</p> | ||
</Portal> | ||
</div> | ||
|
||
<br /> | ||
<CustomPortalContainer /> | ||
|
||
<br /> | ||
<ConditionalPortal /> | ||
</div> | ||
); | ||
} |