-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial implementation of portals * fix an issue with useValueSelector in <Portal> * remove overreporting in case useValueSelector fails * add more tests to check edge-case behaviours * add basic support for <Fragment> in Portals * fix Fragments not updating conditionally inside Portals * add portals documentation
- Loading branch information
Showing
11 changed files
with
852 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
layout: default | ||
title: Portals | ||
nav_order: 9 | ||
parent: API | ||
--- | ||
|
||
## Portals | ||
|
||
If you want to render a part of your application in a different part of the DOM, but want to maintain a logical structure in your components, you can use the `<Portal>` component. This components accepts two props: | ||
|
||
- `portalNode`: an HTML component where children will be mounted | ||
- `children`: any valid Veles children components | ||
|
||
## Example | ||
|
||
```jsx | ||
import { Portal, useState } from "veles"; | ||
|
||
function Component() { | ||
const showMenu = useState(false); | ||
return ( | ||
<div | ||
onMouseOver={() => showMenu.setState(true)} | ||
onMouseOut={() => showMenu.setState(false)} | ||
> | ||
<h1>Title</h1> | ||
{showMenu.useValue((shouldShow) => | ||
shouldShow ? ( | ||
<Portal portalNode={document.getElementById("portal")}> | ||
<div>Component menu</div> | ||
</Portal> | ||
) : null | ||
)} | ||
</div> | ||
); | ||
} | ||
``` | ||
|
||
This is a simple example of a menu which will be rendered when we hover over the component, but instead of the app DOM tree, it will use the node with `portal` ID. | ||
|
||
## Caveats | ||
|
||
If you render multiple Portals to the same `portalNode`, the initial order will be correct. However, if you render Portals conditionally, they will be added to the end of HTML node at the time of their rendering, not respecting the order in the markup. I don't consider it as a bug at the moment, feel free to create an issue if that causes problems. |
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
Oops, something went wrong.