You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Update Portals Documentation
Correct some grammar to be more explicit and clear. Update example CodePen to better match code found in documentation. Update code example styles to match other code examples (ie. 'State and Lifecycle', 'Handling Events').
* Clean up comment to be accurate to example
There was a small comment overlooked when reviewing the documentation. This fixes it to be accurate to the example as well as grammatically correct.
* Update portals.md
* More fixes
Copy file name to clipboardExpand all lines: docs/docs/portals.md
+41-17Lines changed: 41 additions & 17 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -44,47 +44,71 @@ A typical use case for portals is when a parent component has an `overflow: hidd
44
44
45
45
> Note:
46
46
>
47
-
> For most uses portals, you'll need to make sure to follow the proper accessibility guidelines.
47
+
> It is important to remember, when working with portals, you'll need to make sure to follow the proper accessibility guidelines.
48
48
49
49
[Try out an example on CodePen.](https://codepen.io/acdlite/pen/JrKgmz)
50
50
51
51
## Portals and event bubbling
52
52
53
-
A nice feature of portals is that, even though the DOM node can be anywhere in the DOM tree, it behaves like a normal React child in every other way. Features like context work exactly the same regardless of whether the child is a portal.
53
+
Even though a portal can be anywhere in the DOM tree, it behaves like a normal React child in every other way. Features like context work exactly the same regardless of whether the child is a portal, as the portal still exists in the *React tree* regardless of position in the *DOM tree*.
54
54
55
-
This includes event bubbling: an event fired from inside a portal will propagate to ancestors in the containing *React tree*, even if those elements are not ancestors in the *DOM tree*:
55
+
This includes event bubbling. An event fired from inside a portal will propagate to ancestors in the containing *React tree*, even if those elements are not ancestors in the *DOM tree*. Assuming the following HTML structure:
56
+
57
+
```html
58
+
<html>
59
+
<body>
60
+
<divid="app-root"></div>
61
+
<divid="modal-root"></div>
62
+
</body>
63
+
</html>
64
+
```
65
+
66
+
A Parent component in `#app-root` would be able to catch an uncaught, bubbling event from the sibling node #modal-root.
// The click event on this button will bubble up to parent,
100
+
// because there is no 'onClick' attribute defined
101
+
return (
102
+
<div className="modal">
103
+
<button>Click</button>
104
+
</div>
105
+
);
82
106
}
83
107
84
108
85
-
ReactDOM.render(<Parent />, appContainer);
109
+
ReactDOM.render(<Parent />, appRoot);
86
110
```
87
111
88
-
[Try this example on CodePen](https://codepen.io/acdlite/pen/MEJEVV).
112
+
[Try this example on CodePen](https://codepen.io/gaearon/pen/jGBWpE).
89
113
90
-
The advantage of treating portal event bubbling this way is that it makes it easier to build abstractions. For example, if you render a `<Modal />` component, the parent can capture its events regardless of whether it's implemented using portals.
114
+
Catching an event bubbling up from a portal in a parent component allows the development of more flexible abstractions that are not inherently reliant on portals. For example, if you render a `<Modal />` component, the parent can capture its events regardless of whether it's implemented using portals.
0 commit comments