Skip to content

Commit 54f81c1

Browse files
authored
Merge pull request #215 from abdmmar/abdmmar-branch
Shadow DOM styling
2 parents 840785d + 50f248e commit 54f81c1

File tree

1 file changed

+56
-60
lines changed

1 file changed

+56
-60
lines changed

8-web-components/6-shadow-dom-style/article.md

+56-60
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
# Shadow DOM styling
1+
# Menata gaya shadow DOM
22

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.
44

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.
66

77
## :host
88

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*).
1010

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.
1212

13-
That's exactly what `:host` does:
13+
Itulah tepatnya yang dilakukan `:host`:
1414

1515
```html run autorun="no-epub" untrusted height=80
1616
<template id="tmpl">
@@ -44,30 +44,29 @@ customElements.define('custom-dialog', class extends HTMLElement {
4444

4545
## Cascading
4646

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.
4848

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.
5050

51-
For instance, if in the document we had:
51+
Sebagai contoh, jika dalam dokumen kita memiliki:
5252
```html
5353
<style>
5454
custom-dialog {
5555
padding: 0;
5656
}
5757
</style>
5858
```
59-
...Then the `<custom-dialog>` would be without padding.
59+
...Maka `<custom-dialog>` tidak akan memiliki padding.
6060

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.
6462

63+
Pengecualiannya adalah ketika properti lokal diberi label `!important`, untuk properti seperti itu, gaya lokal diutamakan.
6564

6665
## :host(selector)
6766

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`.
6968

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`:
7170

7271
```html run autorun="no-epub" untrusted height=80
7372
<template id="tmpl">
@@ -109,17 +108,17 @@ customElements.define('custom-dialog', class extends HTMLElement {
109108
</custom-dialog>
110109
```
111110

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>`.
113112

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.
115114

116-
## Styling slotted content
115+
## Menata gaya konten yang ber-slot
117116

118-
Now let's consider the situation with slots.
117+
Sekarang mari kita pertimbangkan situasi ketika menggunakan slot.
119118

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.
121120

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:
123122
```html run autorun="no-epub" untrusted height=80
124123
<style>
125124
*!*
@@ -148,11 +147,11 @@ customElements.define('user-card', class extends HTMLElement {
148147
</script>
149148
```
150149
151-
The result is bold, but not red.
150+
Hasilnya dicetak tebal, tapi tidak memiliki latar belakang merah.
152151
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.
154153
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:
156155
157156
```html run autorun="no-epub" untrusted height=80
158157
<user-card>
@@ -176,14 +175,14 @@ customElements.define('user-card', class extends HTMLElement {
176175
</script>
177176
```
178177
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.
180179
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:
182181
183-
1. That's a 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`.
185184
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:
187186
188187
```html run autorun="no-epub" untrusted height=80
189188
<user-card>
@@ -209,54 +208,53 @@ customElements.define('user-card', class extends HTMLElement {
209208
</script>
210209
```
211210
212-
Please note, `::slotted` selector can't descend any further into the slot. These selectors are invalid:
211+
Harap perhatikan, *selector* `::slotted` tidak dapat turun lebih jauh ke dalam slot. *selector* ini tidak valid:
213212
214213
```css
215214
::slotted(div span) {
216-
/* our slotted <div> does not match this */
215+
/* <div> kita yang ber-slot tidak cocok dengan ini */
217216
}
218217
219218
::slotted(div) p {
220-
/* can't go inside light DOM */
219+
/* tidak bisa masuk ke dalam light DOM */
221220
}
222221
```
223222
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`.
227224
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?
229227
230-
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to style shadow 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?
231229
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.
233231
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**
235233
236-
For example, in shadow DOM we can use `--user-card-field-color` CSS variable to style fields, and the outer document can set its value:
234+
Misalnya, dalam shadow DOM kita dapat menggunakan variabel CSS `--user-card-field-color` untuk menata *field*, dan dokumen luar dapat mengatur nilainya:
237235
238236
```html
239237
<style>
240238
.field {
241239
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 */
243241
}
244242
</style>
245243
<div class="field">Name: <slot name="username"></slot></div>
246244
<div class="field">Birthday: <slot name="birthday"></slot></div>
247245
```
248246

249-
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>`:
250248

251249
```css
252250
user-card {
253251
--user-card-field-color: green;
254252
}
255253
```
256254

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.
258256

259-
Here's the full example:
257+
Berikut contoh lengkapnya:
260258

261259
```html run autorun="no-epub" untrusted height=80
262260
<style>
@@ -294,26 +292,24 @@ customElements.define('user-card', class extends HTMLElement {
294292
</user-card>
295293
```
296294
295+
## Ringkasan
297296
297+
Shadow DOM dapat menyertakan gaya, seperti `<style>` atau `<link rel="stylesheet">`.
298298
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.
307303
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)
311307
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.
313309
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:
315311
316-
1. The component uses a custom CSS property to style key 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 style a title, 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.
319315
4. Profit!

0 commit comments

Comments
 (0)