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
Copy file name to clipboardExpand all lines: content/docs/portals.md
+42-39Lines changed: 42 additions & 39 deletions
Original file line number
Diff line number
Diff line change
@@ -3,22 +3,23 @@ id: portals
3
3
title: Portals
4
4
permalink: docs/portals.html
5
5
---
6
+
7
+
تُزوّدنا Portals بطريقة ممتازة لتصيير المكونات الأبناء إلى عقدة DOM موجودة خارج تسلسل DOM للمكونات الآباء.
6
8
7
-
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
8
9
9
10
```js
10
11
ReactDOM.createPortal(child, container)
11
12
```
12
13
13
-
The first argument (`child`) is any [renderable React child](/docs/react-component.html#render), such as an element, string, or fragment. The second argument (`container`) is a DOM element.
14
+
الوسيط الأول (`child`) هو عبارة عن أي [مكوّن ابن قابل للتصيير في React](/docs/react-component.html#render)، مثل العناصر، والسلاسل النصية، والأجزاء (fragments). الوسيط الثاني (`container`) هو عنصر DOM.
14
15
15
-
## Usage {#usage}
16
+
## الاستخدام {#usage}
16
17
17
-
Normally, when you return an element from a component's render method, it's mounted into the DOM as a child of the nearest parent node:
18
+
عندما تُعيد عنصر من تابع تصيير المكوّن فبشكل اعتيادي يُوصَل إلى DOM كمكوّن ابن لأقرب عقدة أب:
18
19
19
20
```js{4,6}
20
21
render() {
21
-
// React mounts a new div and renders the children into it
22
+
// تصل React عنصر div جديد وتصير الأبناء إليه
22
23
return (
23
24
<div>
24
25
{this.props.children}
@@ -27,34 +28,35 @@ render() {
27
28
}
28
29
```
29
30
30
-
However, sometimes it's useful to insert a child into a different location in the DOM:
31
+
ولكن من المفيد أحيانًا إدخال الابن في مواقع متعددة من DOM:
31
32
32
33
```js{6}
33
34
render() {
34
-
// React does *not* create a new div. It renders the children into `domNode`.
35
-
// `domNode` is any valid DOM node, regardless of its location in the DOM.
35
+
// لا تنشئ React عنصر div جديد، فهي تصير الأبناء إلى `domNode`
36
+
// `domNode` هي اي عقدة DOM صحيحة بغض النظر عن موقعها في DOM
36
37
return ReactDOM.createPortal(
37
38
this.props.children,
38
39
domNode
39
40
);
40
41
}
41
42
```
42
43
43
-
A typical use case for portals is when a parent component has an`overflow: hidden`or`z-index`style, but you need the child to visually "break out" of its container. For example, dialogs, hovercards, and tooltips.
44
+
الحالة النموذجية لاستخدام portals هي عندما يمتلك المكوّن الأب التنسيق `overflow: hidden`أو`z-index`ولكنك تريد من المكوّن الابن أن يظهر خارج حاويته، على سبيل المثال مربعات الحوار (dialogs) وتلميحات الأدوات (tooltips).
44
45
45
-
> Note:
46
+
> ملاحظة:
46
47
>
47
-
> When working with portals, remember that [managing keyboard focus](/docs/accessibility.html#programmatically-managing-focus)becomes very important.
48
+
> تذكر عند التعامل مع Portals أنّ [إدارة تركيز لوحة المفاتيح](/docs/accessibility.html#programmatically-managing-focus)تصبح أمرًا هامًّا.
48
49
>
49
-
>For modal dialogs, ensure that everyone can interact with them by following the [WAI-ARIA Modal Authoring Practices](https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal).
50
+
>من أجل مربعات الحوار تأكد من قدرة جميع المستخدمين على التعامل معها عن طريق اتباع [هذه الإرشادات](https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal).
50
51
51
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/yzMaBd)
52
+
[**جرب المثال على CodePen.**](https://codepen.io/gaearon/pen/yzMaBd)
52
53
53
-
## Event Bubbling Through Portals {#event-bubbling-through-portals}
54
+
## انتشار الأحداث عن طريق Portals {#event-bubbling-through-portals}
54
55
55
-
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*.
56
+
بالرغم من أن portals يُمكِن لها أن تكون في أي مكان من شجرة DOM، فهي تتصرف مثل عناصر React الأبناء في كل طريقة أخرى. تعمل ميزات مثل السياق بنفس الطريقة بالضبط بغض النظر إن كان العنصر الابن portal أم لا، لأنّ portal يبقى موجودًا في *شجرة React* بغض النظر عن موقعه في *شجرة DOM*.
57
+
58
+
يتضمّن ذلك انتشار الأحداث (event bubbling)، حيث أنّ الحدث الذي أُطلِق بداخل portal سيتصاعد إلى العناصر الأسلاف في *شجرة React* حتى ولو لم تكن هذه العناصر أسلافًا في *شجرة DOM*. بافتراض بنية HTML التالية:
56
59
57
-
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:
58
60
59
61
```html
60
62
<html>
@@ -65,10 +67,10 @@ This includes event bubbling. An event fired from inside a portal will propagate
65
67
</html>
66
68
```
67
69
68
-
A `Parent`component in`#app-root`would be able to catch an uncaught, bubbling event from the sibling node `#modal-root`.
70
+
سيكون المكوّن الأب `Parent`في `#app-root`قادرًا على الإمساك بالحدث المُضاعَف من العقدة الشقيقة له وهي `#modal-root`.
@@ -79,14 +81,14 @@ class Modal extends React.Component {
79
81
}
80
82
81
83
componentDidMount() {
82
-
// The portal element is inserted in the DOM tree after
83
-
// the Modal's children are mounted, meaning that children
84
-
// will be mounted on a detached DOM node. If a child
85
-
// component requires to be attached to the DOM tree
86
-
// immediately when mounted, for example to measure a
87
-
// DOM node, or uses 'autoFocus' in a descendant, add
88
-
// state to Modal and only render the children when Modal
89
-
// is inserted in the DOM tree.
84
+
// يُدخَل عنصر portal في شجرة DOM
85
+
// بعد وصل أبناء الـ modal
86
+
// مما يعني وصل الأبناء إلى عقدة DOM منفصلة
87
+
// إن احتاج المكون الابن وصله إلى عقدة DOM مباشرة عند الوصل
88
+
// مثلا لقياس عقدة DOM
89
+
// أو استخدام التركيز التلقائي
90
+
// فأضف حالة إلى الـ Modal وصير فقط الابن
91
+
// عند إدخال الـ Modal في شجرة DOM
90
92
modalRoot.appendChild(this.el);
91
93
}
92
94
@@ -110,9 +112,9 @@ class Parent extends React.Component {
110
112
}
111
113
112
114
handleClick() {
113
-
// This will fire when the button in Child is clicked,
114
-
// updating Parent's state, even though button
115
-
// is not direct descendant in the DOM.
115
+
// سيطلَق هذا عند الضغط على الزر في المكون الابن
116
+
// مما يحدث حالة المكون الأب
117
+
// حتى ولو لم يكن الزر منحدرًا بشكل مباشر في DOM.
116
118
this.setState(state => ({
117
119
clicks: state.clicks + 1
118
120
}));
@@ -121,12 +123,11 @@ class Parent extends React.Component {
121
123
render() {
122
124
return (
123
125
<div onClick={this.handleClick}>
124
-
<p>Number of clicks: {this.state.clicks}</p>
126
+
<p>عدد النقرات : {this.state.clicks}</p>
125
127
<p>
126
-
Open up the browser DevTools
127
-
to observe that the button
128
-
is not a child of the div
129
-
with the onClick handler.
128
+
افتح أدوات تطوير المتصفح لتلاحظ أن الزر ليس ابنًا
129
+
للعنصر div
130
+
الذي يمتلك معالج الأحداث onClick.
130
131
</p>
131
132
<Modal>
132
133
<Child />
@@ -137,18 +138,20 @@ class Parent extends React.Component {
137
138
}
138
139
139
140
function Child() {
140
-
// The click event on this button will bubble up to parent,
141
-
// because there is no 'onClick' attribute defined
141
+
// سيتضاعف حدث النقر على هذا الزر إلى المكون الأب
142
+
// بسبب عدم وجود خاصية onClick معرفة
143
+
142
144
return (
143
145
<div className="modal">
144
-
<button>Click</button>
146
+
<button>انقر هنا</button>
145
147
</div>
146
148
);
147
149
}
148
150
149
151
ReactDOM.render(<Parent />, appRoot);
150
152
```
151
153
152
-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/jGBWpE)
154
+
[**جرب المثال على موقع CodePen.**](https://codepen.io/gaearon/pen/jGBWpE)
155
+
156
+
يسمح الإمساك بالحدث المُضاعَف من protal في المكوّن الأب بتطوير تجريدات مرنة والتي لا تعتمد على protals. على سبيل المثال إن صيرنا المكوّن `<Modal />`، فبإمكان المكوّن الأب له التقاط أحداثه بغض النظر عمّا إذا كان يعتمد protals.
153
157
154
-
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