Skip to content

Commit 753aa52

Browse files
author
Nguyễn Văn Dũng
committed
Translate Portals page
1 parent fc52d95 commit 753aa52

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

content/docs/portals.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ title: Portals
44
permalink: docs/portals.html
55
---
66

7-
Portals provide a first-class way to render children into a DOM node that exists outside the DOM hierarchy of the parent component.
7+
Portals cung cấp một cách render các phần tử DOM bên ngoài phân cấp của DOM chính.
88

99
```js
1010
ReactDOM.createPortal(child, container)
1111
```
1212

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.
13+
Tham số đầu tiên (`child`) là bất bỳ [thành phần có thể render của React](/docs/react-component.html#render), như là element, string, hoặc fragment. Tham số thứ hai (`container`) là một DOM element.
1414

15-
## Usage {#usage}
15+
## Cách dùng {#usage}
1616

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:
17+
Thông thường, khi bạn trả về một phần tử từ phương thức render của một component, nó sẽ được gắn vào DOM dưới dạng phần tử con của nút cha gần nhất:
1818

1919
```js{4,6}
2020
render() {
21-
// React mounts a new div and renders the children into it
21+
// React tạo một thẻ div mới và render các phần tử con vào trong thẻ div đó:
2222
return (
2323
<div>
2424
{this.props.children}
@@ -27,34 +27,34 @@ render() {
2727
}
2828
```
2929

30-
However, sometimes it's useful to insert a child into a different location in the DOM:
30+
Tuy nhiên, đôi khi sẽ thuận tiện hơn nếu chèn phần tử con đó vào một vị trí khác trong DOM:
3131

3232
```js{6}
3333
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.
34+
// React *không* tạo mới thẻ div. Nó render phần tử con vào `domNode`.
35+
// `domNode` là bất kỳ phần tử DOM hợp lệ nào, ở bất kỳ vị trí nào trong DOM.
3636
return ReactDOM.createPortal(
3737
this.props.children,
3838
domNode
3939
);
4040
}
4141
```
4242

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.
43+
Một trường hợp thưởng dùng Portals là khi một thành phần mẹ có thuộc tính `overflow: hidden` hoặc `z-index`, nhưng bạn muốn hiển thị nó một cách "độc lập" khỏi thành phần mẹ. Ví dụ, các hộp thoại (dialogs), hovercards, tooltips.
4444

45-
> Note:
45+
> Lưu ý:
4646
>
47-
> When working with portals, remember that [managing keyboard focus](/docs/accessibility.html#programmatically-managing-focus) becomes very important.
47+
> Khi làm việc với Portals, hãy nhớ [quản lý các sự kiện focus từ bàn phím](/docs/accessibility.html#programmatically-managing-focus) là rất quan trọng.
4848
>
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).
49+
> Đối với hộp thoại, hãy đảm bảo rằng mọi người có thể tương tác với chúng bằng cách làm theo [WAI-ARIA Modal Authoring Practices](https://www.w3.org/TR/wai-aria-practices-1.1/#dialog_modal).
5050
5151
[**Try it on CodePen**](https://codepen.io/gaearon/pen/yzMaBd)
5252

53-
## Event Bubbling Through Portals {#event-bubbling-through-portals}
53+
## Xử lý sự kiện ở Portals {#event-bubbling-through-portals}
5454

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*.
55+
Mặc dù Portals có thể ở bất kỳ đâu trong cây DOM, nhưng theo mọi cách khác, nó hoạt động giống như một React component bình thường. Các tính năng như context hoạt động giống hệt nhau bất kể component đó có phải là Portals hay không, vì Portals vẫn tồn tại trong *React tree* bất kể vị trí nào trong *DOM tree*.
5656

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:
57+
Bao gồm các event bubbling. Một sự kiện được kích hoạt từ bên trong Portals sẽ truyền đến tất node cha trong *React tree* chứa portals đó, ngay cả khi những phần tử đó không phải là node cha trong *DOM tree*. Giả sử với cấu trúc HTML sau:
5858

5959
```html
6060
<html>
@@ -65,10 +65,10 @@ This includes event bubbling. An event fired from inside a portal will propagate
6565
</html>
6666
```
6767

68-
A `Parent` component in `#app-root` would be able to catch an uncaught, bubbling event from the sibling node `#modal-root`.
68+
Một phần tử `Parent` trong `#app-root` sẽ có thể bắt được một bubbling event chưa được bắt từ sibling node `#modal-root`.
6969

7070
```js{28-31,42-49,53,61-63,70-71,74}
71-
// These two containers are siblings in the DOM
71+
// Đây là 2 container cùng cấp trong DOM
7272
const appRoot = document.getElementById('app-root');
7373
const modalRoot = document.getElementById('modal-root');
7474
@@ -79,14 +79,14 @@ class Modal extends React.Component {
7979
}
8080
8181
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.
82+
// Phần tử Portals được chèn vào cây DOM sau khi
83+
// phần tử con của Modal được hiển thị, có nghĩa là những phần tử con đó
84+
// sẽ được gắn trên một phần tử DOM tách rời độc lập. Nếu một phần tử con
85+
// yêu cầu được gắn vào DOM tree ngay tức khắc khi 'mounted',
86+
// ví dụ để đo lường thuộc tính DOM, hoặc sử dụng 'autoFocus'
87+
// trong các phần tử con, thêm state vào Modal và
88+
// chỉ render các phẩn tử con khi Modal
89+
// được chèn vào DOM tree.
9090
modalRoot.appendChild(this.el);
9191
}
9292
@@ -110,9 +110,9 @@ class Parent extends React.Component {
110110
}
111111
112112
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.
113+
// Hàm này sẽ kích hoạt khi button tại Child được click,
114+
// cập nhật Parent's state, mặc dù button
115+
// không phải là phần tử con trực tiếp trong DOM.
116116
this.setState(state => ({
117117
clicks: state.clicks + 1
118118
}));
@@ -121,12 +121,12 @@ class Parent extends React.Component {
121121
render() {
122122
return (
123123
<div onClick={this.handleClick}>
124-
<p>Number of clicks: {this.state.clicks}</p>
124+
<p>Số lượng clicks: {this.state.clicks}</p>
125125
<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.
126+
Mở DevTools của trình duyệt
127+
để quan sát rằng button
128+
không phải con của div
129+
xử lý sự kiện onClick.
130130
</p>
131131
<Modal>
132132
<Child />
@@ -137,8 +137,8 @@ class Parent extends React.Component {
137137
}
138138
139139
function Child() {
140-
// The click event on this button will bubble up to parent,
141-
// because there is no 'onClick' attribute defined
140+
// Sự kiện nhấp chuột vào nút này sẽ xuất hiện đối với phần tử cha chứa nó
141+
// bởi vì không có thuộc tính 'onClick' được định nghĩa
142142
return (
143143
<div className="modal">
144144
<button>Click</button>
@@ -149,6 +149,6 @@ function Child() {
149149
ReactDOM.render(<Parent />, appRoot);
150150
```
151151

152-
[**Try it on CodePen**](https://codepen.io/gaearon/pen/jGBWpE)
152+
[**Thử trên CodePen**](https://codepen.io/gaearon/pen/jGBWpE)
153153

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.
154+
Việc nắm bắt một sự kiện xảy ra từ một Portals trong một component cha cho phép phát triển các tính năng trừu tượng linh hoạt hơn vốn không phụ thuộc vào các Portals. Ví dụ, nếu bạn render một phần tử `<Modal />` , thành phần cha có thể nhận được các sự kiện của nó dù cho nó có được triển khai bằng portals hay không.

0 commit comments

Comments
 (0)