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/6-shadow-dom-style/article.md
+56-60
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
-
# Shadow DOM styling
1
+
# Menata gaya shadow DOM
2
2
3
-
Shadow DOM may include both`<style>`and`<link rel="stylesheet" href="…">` tags. In the latter case, stylesheets are HTTP-cached, so they are not redownloaded for multiple components that use same template.
3
+
Shadow DOM dapat memuat tag`<style>`dan`<link rel="stylesheet" href="…">`. Dalam kasus terakhir, stylesheet di-cache HTTP, sehingga tidak diunduh ulang untuk beberapa komponen yang menggunakan template yang sama.
4
4
5
-
As a general rule, local styles work only inside the shadow tree, and document styles work outside of it. But there are few exceptions.
5
+
Sebagai sebuah aturan umum, gaya lokal hanya berfungsi di dalam *shadow tree*, dan gaya dokumen bekerja di luarnya. Tapi ada beberapa pengecualian.
6
6
7
7
## :host
8
8
9
-
The`:host`selector allows to select the shadow host (the element containing the shadow tree).
9
+
*Selector*`:host`memungkinkan untuk memilih *shadow host* (elemen yang berisi *shadow tree*).
10
10
11
-
For instance, we're making `<custom-dialog>`element that should be centered. For that we need to style the `<custom-dialog>`element itself.
11
+
Sebagai contoh, kita membuat elemen `<custom-dialog>`yang harus berada di bagian tengah. Untuk itu kita perlu menata elemen `<custom-dialog>`itu sendiri.
12
12
13
-
That's exactly what `:host` does:
13
+
Itulah tepatnya yang dilakukan `:host`:
14
14
15
15
```html run autorun="no-epub" untrusted height=80
16
16
<templateid="tmpl">
@@ -44,30 +44,29 @@ customElements.define('custom-dialog', class extends HTMLElement {
44
44
45
45
## Cascading
46
46
47
-
The shadow host (`<custom-dialog>`itself) resides in the light DOM, so it's affected by document CSS rules.
47
+
Shadow host (`<custom-dialog>`itu sendiri) berada di light DOM, sehingga dipengaruhi oleh aturan CSS dokumen.
48
48
49
-
If there's a property styled both in `:host`locally, and in the document, then the document style takes precedence.
49
+
Jika ada properti yang diberikan gaya baik di `:host`secara lokal, dan di dokumen, maka gaya dokumen akan diutamakan.
50
50
51
-
For instance, if in the document we had:
51
+
Sebagai contoh, jika dalam dokumen kita memiliki:
52
52
```html
53
53
<style>
54
54
custom-dialog {
55
55
padding: 0;
56
56
}
57
57
</style>
58
58
```
59
-
...Then the `<custom-dialog>`would be without padding.
59
+
...Maka `<custom-dialog>`tidak akan memiliki padding.
60
60
61
-
It's very convenient, as we can setup "default" component styles in its `:host` rule, and then easily override them in the document.
62
-
63
-
The exception is when a local property is labelled `!important`, for such properties, local styles take precedence.
61
+
Ini sangat memudahkan, karena kita dapat mengatur gaya komponen "default" dalam aturan `:host`-nya, dan kemudian dengan mudah menimpanya dalam dokumen.
64
62
63
+
Pengecualiannya adalah ketika properti lokal diberi label `!important`, untuk properti seperti itu, gaya lokal diutamakan.
65
64
66
65
## :host(selector)
67
66
68
-
Same as`:host`, but applied only if the shadow host matches the`selector`.
67
+
Sama seperti`:host`, tetapi hanya diterapkan jika shadow host cocok dengan`selector`.
69
68
70
-
For example, we'd like to center the `<custom-dialog>`only if it has `centered` attribute:
69
+
Sebagai contoh, kita ingin membuat `<custom-dialog>`berada di tengah jika hanya memiliki atribut `centered`:
71
70
72
71
```html run autorun="no-epub" untrusted height=80
73
72
<templateid="tmpl">
@@ -109,17 +108,17 @@ customElements.define('custom-dialog', class extends HTMLElement {
109
108
</custom-dialog>
110
109
```
111
110
112
-
Now the additional centering styles are only applied to the first dialog: `<custom-dialog centered>`.
111
+
Sekarang gaya penengah tambahan hanya diterapkan pada dialog pertama: `<custom-dialog centered>`.
113
112
114
-
To summarize, we can use `:host`-family of selectors to style the main element of the component. These styles (unless`!important`) can be overridden by the document.
113
+
Ringkasnya, kita dapat menggunakan *selector*`:host` untuk menata elemen utama komponen. Gaya ini (kecuali`!important`) dapat ditimpa oleh dokumen.
115
114
116
-
## Styling slotted content
115
+
## Menata gaya konten yang ber-slot
117
116
118
-
Now let's consider the situation with slots.
117
+
Sekarang mari kita pertimbangkan situasi ketika menggunakan slot.
119
118
120
-
Slotted elements come from light DOM, so they use document styles. Local styles do not affect slotted content.
119
+
Elemen yang ber-slot berasal dari light DOM, jadi mereka menggunakan gaya dokumen. Gaya lokal tidak memengaruhi konten yang menggunakan slot.
121
120
122
-
In the example below, slotted `<span>`is bold, as per document style, but does not take `background`from the local style:
121
+
Pada contoh dibawah, `<span>`yang berada di slot dicetak tebal, sesuai dengan gaya dokumen, tetapi tidak mengambil `background`dari gaya lokal:
123
122
```html run autorun="no-epub" untrusted height=80
124
123
<style>
125
124
*!*
@@ -148,11 +147,11 @@ customElements.define('user-card', class extends HTMLElement {
148
147
</script>
149
148
```
150
149
151
-
The result is bold, but not red.
150
+
Hasilnya dicetak tebal, tapi tidak memiliki latar belakang merah.
152
151
153
-
If we'd like to style slotted elements in our component, there are two choices.
152
+
Jika kita ingin menata gaya pada elemen ber-slot pada komponen kita, ada dua pilihan.
154
153
155
-
First, we can style the `<slot>` itself and rely on CSS inheritance:
154
+
Pertama, kita dapat menata gaya `<slot>` itu sendiri dan mengandalkan pewarisan CSS:
156
155
157
156
```html run autorun="no-epub" untrusted height=80
158
157
<user-card>
@@ -176,14 +175,14 @@ customElements.define('user-card', class extends HTMLElement {
176
175
</script>
177
176
```
178
177
179
-
Here `<p>John Smith</p>` becomes bold, because CSS inheritance is in effect between the `<slot>` and its contents. But in CSS itself not all properties are inherited.
178
+
Di sini `<p>John Smith</p>` menjadi tebal, karena pewarisan CSS berlaku di antara `<slot>` dan isinya. Tetapi dalam CSS itu sendiri tidak semua properti diwariskan.
180
179
181
-
Another option is to use `::slotted(selector)`pseudo-class. It matches elements based on two conditions:
180
+
Pilihan lainnya adalah menggunakan pseudo-class `::slotted(selector)`. Pseudo-class ini cocok dengan elemen berdasarkan dua kondisi:
182
181
183
-
1. That'sa slotted element, that comes from the light DOM. Slot name doesn't matter. Just any slotted element, but only the element itself, not its children.
184
-
2. The element matches the `selector`.
182
+
1. Elemen itu adalah elemen yang ber-slot, yang berasal dari light DOM. Nama slot tidak masalah. apapun elemen yang ber-slot, tetapi hanya elemen itu sendiri, bukan anak-anaknya.
183
+
2. Elemen cocok dengan `selector`.
185
184
186
-
In our example, `::slotted(div)` selects exactly `<div slot="username">`, but not its children:
185
+
Dalam contoh kita, `::slotted(div)` memilih dengan tepat `<div slot="username">`, tetapi bukan turunannya:
187
186
188
187
```html run autorun="no-epub" untrusted height=80
189
188
<user-card>
@@ -209,54 +208,53 @@ customElements.define('user-card', class extends HTMLElement {
209
208
</script>
210
209
```
211
210
212
-
Please note, `::slotted` selector can't descend any further into theslot. These selectors are invalid:
211
+
Harap perhatikan, *selector*`::slotted` tidak dapat turun lebih jauh ke dalamslot. *selector* ini tidak valid:
213
212
214
213
```css
215
214
::slotted(divspan) {
216
-
/*our slotted <div> does not match this*/
215
+
/* <div> kita yang ber-slot tidak cocok dengan ini*/
217
216
}
218
217
219
218
::slotted(div) p {
220
-
/*can't go inside light DOM */
219
+
/*tidak bisa masuk ke dalam light DOM */
221
220
}
222
221
```
223
222
224
-
Also, `::slotted` can only be used in CSS. We can't use it in `querySelector`.
225
-
226
-
## CSS hooks with custom properties
223
+
Juga, `::slotted` hanya dapat digunakan di CSS. Kita tidak dapat menggunakannya di `querySelector`.
227
224
228
-
How do we style internal elements of a component from the main document?
225
+
## CSS hooks dengan properti kustom
226
+
Bagaimana kita menata gaya elemen internal komponen dari dokumen utama?
229
227
230
-
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to styleshadow DOM elements inside them?
228
+
*Selector* seperti `:host` menerapkan aturan ke elemen `<custom-dialog>` atau `<user-card>`, tetapi bagaimana cara menata elemen shadow DOM di dalamnya?
231
229
232
-
There's no selector that can directly affect shadow DOM styles from the document. But just as we expose methods to interact with our component, we can expose CSS variables (custom CSS properties) to style it.
230
+
Tidak ada *selector* yang dapat secara langsung memengaruhi gaya shadow DOM dari dokumen. Tapi seperti kita mengekspos *method* untuk berinteraksi dengan komponen kita, kita bisa mengekspos variabel CSS (Properti kustom CSS) untuk menatanya.
233
231
234
-
**Custom CSS properties exist on all levels, both in light and shadow.**
232
+
**Properti kustom CSS ada di semua tingkatan, baik dalam light DOM maupun shadow DOM**
235
233
236
-
For example, inshadow DOM we can use`--user-card-field-color` CSS variable to style fields, and the outer document can set its value:
234
+
Misalnya, dalamshadow DOM kita dapat menggunakan variabel CSS `--user-card-field-color` untuk menata *field*, dan dokumen luar dapat mengatur nilainya:
237
235
238
236
```html
239
237
<style>
240
238
.field {
241
239
color: var(--user-card-field-color, black);
242
-
/*if --user-card-field-color is not defined, use black color*/
240
+
/*jika --user-card-field-color tidak didefinisikan, gunakan warna hitam*/
Then, we can declare this property in the outer document for`<user-card>`:
247
+
Kemudian, kita dapat mendeklarasikan properti ini di dokumen luar untuk`<user-card>`:
250
248
251
249
```css
252
250
user-card {
253
251
--user-card-field-color: green;
254
252
}
255
253
```
256
254
257
-
Custom CSS properties pierce through shadow DOM, they are visible everywhere, so the inner `.field`rule will make use of it.
255
+
Properti kustom CSS menembus shadow DOM, artinya mereka bisa dilihat di mana-mana, sehingga aturan `.field`yang berada di dalam akan memanfaatkannya.
258
256
259
-
Here's the full example:
257
+
Berikut contoh lengkapnya:
260
258
261
259
```html run autorun="no-epub" untrusted height=80
262
260
<style>
@@ -294,26 +292,24 @@ customElements.define('user-card', class extends HTMLElement {
294
292
</user-card>
295
293
```
296
294
295
+
## Ringkasan
297
296
297
+
Shadow DOM dapat menyertakan gaya, seperti `<style>` atau `<link rel="stylesheet">`.
298
298
299
-
## Summary
300
-
301
-
Shadow DOM can include styles, such as `<style>` or `<link rel="stylesheet">`.
302
-
303
-
Local styles can affect:
304
-
- shadow tree,
305
-
- shadow host with `:host` and `:host()` pseudoclasses,
306
-
- slotted elements (coming from light DOM), `::slotted(selector)` allows to select slotted elements themselves, but not their children.
299
+
Gaya lokal dapat memengaruhi:
300
+
- shadow tree,
301
+
- shadow host dengan pseudoclass `:host` dan `:host()`,
302
+
- elemen yang ber-slot (berasal dari light DOM), `::slotted(selector)` memungkinkan untuk memilih elemen yang ber-slot, tetapi bukan anaknya.
307
303
308
-
Document styles can affect:
309
-
- shadow host (as it lives in the outer document)
310
-
- slotted elements and their contents (as that's also in the outer document)
304
+
Gaya dokumen dapat memengaruhi:
305
+
- shadow host (karena berada di dokumen luar)
306
+
- elemen ber-slot dan isinya (yang juga karena berada di dokumen luar)
311
307
312
-
When CSS properties conflict, normally document styles have precedence, unless the property is labelled as `!important`. Then local styles have precedence.
308
+
Saat properti CSS bertentangan, biasanya gaya dokumen didahulukan, kecuali jika properti diberi label sebagai `!important`. Maka gaya lokal diutamakan.
313
309
314
-
CSS custom properties pierce through shadow DOM. They are used as "hooks" to style the component:
310
+
Properti kustom CSS menembus shadow DOM. Mereka digunakan sebagai "hooks" untuk menata gaya komponen:
315
311
316
-
1. The component uses a custom CSS property to stylekey elements, such as `var(--component-name-title, <default value>)`.
317
-
2. Component author publishes these properties for developers, they are same important as other public component methods.
318
-
3. When a developer wants to styleatitle, they assign `--component-name-title` CSS property for the shadow host or above.
312
+
1. Komponen menggunakan properti CSS kustom untuk menata gaya *key* elemen, seperti `var(--component-name-title, <default value>)`.
313
+
2. Penulis komponen mempublikasikan properti ini untuk para pengembang, mereka sama pentingnya dengan *method* komponen publik lainnya.
314
+
3. Saat pengembang ingin memberi gaya pada *title*, mereka menetapkan properti CSS `--component-name-title` untuk shadow host atau di atasnya.
0 commit comments