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: 8-web-components/7-shadow-dom-events/article.md
+39-39Lines changed: 39 additions & 39 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,14 +1,14 @@
1
-
# Shadow DOM and events
1
+
# Shadow DOM dan events
2
2
3
-
The idea behind shadow tree is to encapsulate internal implementation details of a component.
3
+
Ide di balik pohon bayangan adalah untuk mengenkapsulasi detail implementasi internal suatu komponen.
4
4
5
-
Let's say, a click event happens inside a shadow DOM of `<user-card>` component. But scripts in the main document have no idea about the shadow DOM internals, especially if the component comes from a 3rd-party library.
5
+
Misalkan, *event* klik terjadi di dalam shadow DOM dari komponen `<user-card>`. Namun skrip di dokumen utama tidak mengetahui internal shadow DOM, terutama jika komponen tersebut berasal dari pustaka pihak ketiga.
6
6
7
-
So, to keep the details encapsulated, the browser *retargets* the event.
7
+
Jadi, untuk menjaga detailnya tetap terenkapsulasi, browser menargetkan ulang *event* tersebut.
8
8
9
-
**Events that happen in shadow DOM have the host element as the target, when caught outside of the component.**
9
+
**Event yang terjadi di shadow DOM memiliki elemen host sebagai target, saat tertangkap di luar komponen.**
10
10
11
-
Here's a simple example:
11
+
Berikut contoh sederhananya:
12
12
13
13
```html run autorun="no-epub" untrusted height=60
14
14
<user-card></user-card>
@@ -30,16 +30,16 @@ document.onclick =
30
30
</script>
31
31
```
32
32
33
-
If you click on the button, the messages are:
33
+
Jika anda mengklik tombol tersebut, pesan yang ditampilkan adalah:
34
34
35
-
1. Inner target: `BUTTON` -- internal event handler gets the correct target, the element inside shadow DOM.
36
-
2. Outer target: `USER-CARD` -- document event handler gets shadow host as the target.
35
+
1. Inner target: `BUTTON` -- *event handler* internal mendapatkan target yang benar, yaitu elemen di dalam shadow DOM.
36
+
2. Outer target: `USER-CARD` -- dokumen *event handler* mendapatkan shadow host sebagai target.
37
37
38
-
Event retargeting is a great thing to have, because the outer document doesn't have to know about component internals. From its point of view, the event happened on`<user-card>`.
38
+
Penargetan ulang *event* adalah hal yang bagus untuk dimiliki, karena dokumen luar tidak harus tahu tentang komponen internal. Dari sudut pandang ini, *event* tersebut terjadi di`<user-card>`.
39
39
40
-
**Retargeting does not occur if the event occurs on a slotted element, that physically lives in the light DOM.**
40
+
**Penargetan ulang tidak terjadi jika peristiwa terjadi pada *slotted* elemen, yang secara fisik berada di light DOM.**
41
41
42
-
For example, if a user clicks on `<span slot="username">`in the example below, the event target is exactly this `span`element, for both shadow and light handlers:
42
+
Misalnya, jika pengguna mengklik `<span slot =" username">`pada contoh di bawah, target *event* persis elemen `span`ini, untuk *shadow* dan *light handler*:
If a click happens on `"John Smith"`, for both inner and outer handlers the target is `<span slot="username">`. That's an element from the light DOM, so no retargeting.
68
+
Jika klik terjadi pada `"John Smith"`, untuk *inner* dan *outer handler*, targetnya adalah `<span slot =" username">`. Itu adalah elemen dari light DOM, jadi tidak ada penargetan ulang.
69
69
70
-
On the other hand, if the click occurs on an element originating from shadow DOM, e.g. on`<b>Name</b>`, then, as it bubbles out of the shadow DOM, its `event.target` is reset to`<user-card>`.
70
+
Di sisi lain, jika klik terjadi pada elemen yang berasal dari shadow DOM, misalnya pada`<b>Name</b>`, lalu, saat ia menggelembung keluar dari shadow DOM, `event.target`-nya disetel ulang ke`<user-card>`.
71
71
72
72
## Bubbling, event.composedPath()
73
73
74
-
For purposes of event bubbling, flattened DOM is used.
74
+
Untuk tujuan *event bubbling*, DOM yang diratakan digunakan.
75
75
76
-
So, if we have a slotted element, and an event occurs somewhere inside it, then it bubbles up to the `<slot>`and upwards.
76
+
Jadi, jika kita memiliki *slotted* elemen, dan sebuah *event* terjadi di suatu tempat di dalamnya, maka *event* itu menggelembung ke `<slot>`dan ke atasnya.
77
77
78
-
The full path to the original event target, with all the shadow elements, can be obtained using `event.composedPath()`. As we can see from the name of the method, that path is taken after the composition.
78
+
Jalur lengkap ke target *event* asli, dengan semua elemen bayangan, bisa diperoleh menggunakan `event.composedPath()`. Seperti yang dapat kita lihat dari nama *method*, jalur tersebut diambil setelah komposisi.
79
79
80
-
In the example above, the flattened DOM is:
80
+
Dalam contoh di atas, DOM yang diratakan adalah:
81
81
82
82
```html
83
83
<user-cardid="userCard">
@@ -92,45 +92,45 @@ In the example above, the flattened DOM is:
92
92
```
93
93
94
94
95
-
So, for a click on `<span slot="username">`, a call to `event.composedPath()`returns an array: [`span`, `slot`, `div`, `shadow-root`, `user-card`, `body`, `html`, `document`, `window`]. That's exactly the parent chain from the target element in the flattened DOM, after the composition.
95
+
Jadi, untuk klik pada `<span slot="username">`, panggilan ke `event.composedPath()`mengembalikan sebuah array: [`span`, `slot`, `div`, `shadow-root`, `User-card`, `body`, `html`, `document`, `window`]. Itu persis seperti rantai induk dari elemen target di DOM yang diratakan, setelah komposisi.
96
96
97
97
```warn header="Shadow tree details are only provided for `{mode:'open'}` trees"
98
-
If the shadow tree was created with `{mode: 'closed'}`, then the composed path starts from the host: `user-card`and upwards.
98
+
Jika pohon bayangan dibuat dengan `{mode: 'closed'}`, maka jalur yang dibuat dimulai dari host: `user-card`dan ke atas.
99
99
100
-
That's the similar principle as for other methods that work with shadow DOM. Internals of closed trees are completely hidden.
100
+
Itu prinsip yang sama untuk *methods* lain yang bekerja dengan shadow DOM. Bagian dalam pohon tertutup benar-benar tersembunyi.
101
101
```
102
102
103
103
104
104
## event.composed
105
105
106
-
Most events successfully bubble through a shadow DOM boundary. There are few events that do not.
106
+
Sebagian besar event berhasil menggelembung melewati batas shadow DOM. Ada beberapa event yang tidak.
107
107
108
-
This is governed by the `composed` event object property. If it's `true`, then the event does cross the boundary. Otherwise, it only can be caught from inside the shadow DOM.
108
+
Ini diatur oleh properti event objek `kompos`. Jika itu `true`, maka event tersebut melewati batas. Jika tidak, event itu hanya bisa ditangkap dari dalam shadow DOM.
109
109
110
-
If you take a look at [UI Events specification](https://www.w3.org/TR/uievents), most events have `composed: true`:
110
+
Jika anda melihat [UI Events specification](https://www.w3.org/TR/uievents), sebagian besar event memiliki `composed: true`:
All touch events and pointer events also have `composed: true`.
118
+
Semua event sentuh dan event penunjuk juga memiliki `composed: true`.
119
119
120
-
There are some events that have `composed: false` though:
120
+
Ada beberapa event yang memiliki `compose: false`:
121
121
122
122
- `mouseenter`, `mouseleave` (they do not bubble at all),
123
123
- `load`, `unload`, `abort`, `error`,
124
124
- `select`,
125
125
- `slotchange`.
126
126
127
-
These events can be caught only on elements within the same DOM, where the event target resides.
127
+
Event ini hanya dapat ditangkap pada elemen dalam DOM yang sama, tempat target event berada.
128
128
129
-
## Custom events
129
+
## Event kustom
130
130
131
-
When we dispatch custom events, we need to set both `bubbles` and `composed` properties to `true` for it to bubble up and out of the component.
131
+
Saat kita mengirimkan event khusus, kita perlu mengatur properti `bubble` dan `composed` ke `true` agar dapat menggelembung dan keluar dari komponen.
132
132
133
-
For example, here we create `div#inner` in the shadow DOM of `div#outer` and trigger two events on it. Only the one with `composed: true` makes it outside to the document:
133
+
Misalnya, di sini kita membuat `div#inner` di shadow DOM dari `div#outer` dan memicu dua event di atasnya. Hanya satu dengan `composed: true` yang membuatnya berada di luar dokumen:
Beberapa *event* bawaan yang memiliki`composed: false`:
182
182
183
-
-`mouseenter`, `mouseleave` (also do not bubble),
183
+
-`mouseenter`, `mouseleave` (juga tidak menggelembung),
184
184
-`load`, `unload`, `abort`, `error`,
185
185
-`select`,
186
186
-`slotchange`.
187
187
188
-
These events can be caught only on elements within the same DOM.
188
+
*Events* ini hanya dapat ditangkap pada elemen dalam DOM yang sama.
189
189
190
-
If we dispatch a `CustomEvent`, then we should explicitly set`composed: true`.
190
+
Jika kita mengirimkan `CustomEvent`, maka kita harus secara eksplisit mengatur`composed: true`.
191
191
192
-
Please note that in case of nested components, one shadow DOM may be nested into another. In that case composed events bubble through all shadow DOM boundaries. So, if an event is intended only for the immediate enclosing component, we can also dispatch it on the shadow host and set `composed: false`. Then it's out of the component shadow DOM, but won't bubble up to higher-level DOM.
192
+
Harap diperhatikan bahwa dalam kasus komponen bertingkat, satu shadow DOM dapat bertumpuk ke dalam yang lain. Dalam kasus tersebut, gelembung peristiwa yang disusun melalui semua batas shadow DOM. Jadi, jika sebuah peristiwa hanya ditujukan untuk komponen penutup langsung, kita juga dapat mengirimkannya ke host bayangan dan menyetel `composed: false`. Kemudian keluar dari komponen shadow DOM, tetapi tidak akan mengarah ke DOM tingkat yang lebih tinggi.
0 commit comments