Skip to content

Commit a11282c

Browse files
authored
Merge pull request #322 from pierangelomiceli/029_shadow_styling
Shadow DOM styling
2 parents af7a081 + f44f9f4 commit a11282c

File tree

1 file changed

+70
-70
lines changed

1 file changed

+70
-70
lines changed

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

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
# Shadow DOM styling
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+
Lo shadow DOM può includere i tags `<style>` e `<link rel="stylesheet" href="…">`. Nel secondo caso, i fogli di stile sono HTTP-cached, e non verranno ricaricati per più componenti che usino lo stesso template.
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+
Come regola generale, gli stili locali hanno effetto solamente all'interno dello shadow tree, mentre quelli del documento hanno effetto al di fuori di esso. Con qualche eccezione.
66

77
## :host
88

9-
The `:host` selector allows to select the shadow host (the element containing the shadow tree).
9+
Il selettore `:host` permette di selezionare lo shadow host (l'elemento contenente lo 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+
Ad esempio, se stiamo creando un elemento `<custom-dialog>` da centrare sulla pagina. Per fare ciò dovremo creare lo stile all'interno dello stesso `<custom-dialog>`.
1212

13-
That's exactly what `:host` does:
13+
Questo è ciò che fa `:host`:
1414

1515
```html run autorun="no-epub" untrusted height=80
1616
<template id="tmpl">
1717
<style>
18-
/* the style will be applied from inside to the custom-dialog element */
18+
/* lo stile viene applicato all'interno dell'elemento custom-dialog */
1919
:host {
2020
position: fixed;
2121
left: 50%;
@@ -38,36 +38,36 @@ customElements.define('custom-dialog', class extends HTMLElement {
3838
</script>
3939

4040
<custom-dialog>
41-
Hello!
41+
Ciao!
4242
</custom-dialog>
4343
```
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+
Lo shadow host (`<custom-dialog>`) risiede dentro il light DOM, e quindi viene influenzato dalle regole CSS del documento.
4848

49-
If there's a property styled both in `:host` locally, and in the document, then the document style takes precedence.
49+
Nel caso ci fosse una proprietà stilizzata localmente sia tramite `:host`, che tramite regola nel documento, quest'ultima avrebbe la precedenza.
5050

51-
For instance, if in the document we had:
51+
Per esempio, se nel documento avessimo:
5252
```html
5353
<style>
5454
custom-dialog {
5555
padding: 0;
5656
}
5757
</style>
5858
```
59-
...Then the `<custom-dialog>` would be without padding.
59+
...in questo caso il `<custom-dialog>` non avrebbe nessun 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.
61+
Questo comportamento può fare comodo, perché in pratica ci permette di impostare gli stili di "default" di un componente nelle sue regole `:host`, per poterle poi facilmente sovrascrivere nel documento.
6262

63-
The exception is when a local property is labelled `!important`, for such properties, local styles take precedence.
63+
Un'eccezione a questo comportamento si verifica quando una proprietà locale viene contrassegnata come `!important`, di conseguenza gli stili locali avranno la precedenza.
6464

6565

6666
## :host(selector)
6767

68-
Same as `:host`, but applied only if the shadow host matches the `selector`.
68+
Come nel caso di `:host`, ma applicato solo se lo shadow host combacia con il `selettore`.
6969

70-
For example, we'd like to center the `<custom-dialog>` only if it has `centered` attribute:
70+
Ad esempio, se volessimo centrare il `<custom-dialog>` contenente l'attributo `centered`:
7171

7272
```html run autorun="no-epub" untrusted height=80
7373
<template id="tmpl">
@@ -101,40 +101,40 @@ customElements.define('custom-dialog', class extends HTMLElement {
101101

102102

103103
<custom-dialog centered>
104-
Centered!
104+
Centrato!
105105
</custom-dialog>
106106

107107
<custom-dialog>
108-
Not centered.
108+
Non centrato.
109109
</custom-dialog>
110110
```
111111

112-
Now the additional centering styles are only applied to the first dialog: `<custom-dialog centered>`.
112+
In tali condizioni gli stili aggiuntivi per centrare l'elemento, verrebbero applicati solamente al primo dialog: `<custom-dialog centered>`.
113113

114114
## :host-context(selector)
115115

116-
Same as `:host`, but applied only if the shadow host or any of its ancestors in the outer document matches the `selector`.
116+
Come `:host`, ma applicati solo se lo shadow host o un qualunque dei suoi antenati esterni nel document, combacia con il `selettore`.
117117

118-
E.g. `:host-context(.dark-theme)` matches only if there's `dark-theme` class on `<custom-dialog>` on anywhere above it:
118+
Ad esempio `:host-context(.dark-theme)` combacia solo nel caso ci fosse una classe `dark-theme` su `<custom-dialog>` o su qualunque elemento sopra di esso:
119119

120120
```html
121121
<body class="dark-theme">
122122
<!--
123-
:host-context(.dark-theme) applies to custom-dialogs inside .dark-theme
123+
:host-context(.dark-theme) viene applicata al custom-dialogs dentro .dark-theme
124124
-->
125125
<custom-dialog>...</custom-dialog>
126126
</body>
127127
```
128128

129-
To summarize, we can use `:host`-family of selectors to style the main element of the component, depending on the context. These styles (unless `!important`) can be overridden by the document.
129+
Riepilogando, possiamo usare la famiglia dei selettori `:host` per stilizzare l'elemento principale del componente, in riferimento al suo contesto. Questi stili (a meno che non vengano dichiarati con `!important`) possono essere sovrascritti con altri stili nel documento.
130130

131-
## Styling slotted content
131+
## Stili sullo slotted content
132132

133-
Now let's consider the situation with slots.
133+
Consideriamo ora la situazione con l'uso degli slot.
134134

135-
Slotted elements come from light DOM, so they use document styles. Local styles do not affect slotted content.
135+
Gli slotted elements arrivano dal light DOM, quindi useranno gli stili del documento. Gli stili locali non avranno alcun effetto sullo slotted content.
136136

137-
In the example below, slotted `<span>` is bold, as per document style, but does not take `background` from the local style:
137+
Nel seguente esempio, lo slotted `<span>` è in grassetto, per via dello stile del documento, ed invece `background` non sortirà nessun effetto perché dichiarato dentro lo stile locale
138138
```html run autorun="no-epub" untrusted height=80
139139
<style>
140140
*!*
@@ -156,18 +156,18 @@ customElements.define('user-card', class extends HTMLElement {
156156
span { background: red; }
157157
*/!*
158158
</style>
159-
Name: <slot name="username"></slot>
159+
Nome: <slot name="username"></slot>
160160
`;
161161
}
162162
});
163163
</script>
164164
```
165165
166-
The result is bold, but not red.
166+
Il risultato sarà in grassetto, ma non rosso.
167167
168-
If we'd like to style slotted elements in our component, there are two choices.
168+
Se volessimo stilizzare gli slotted elements all'interno del nostro componente, avremmo due scelte.
169169
170-
First, we can style the `<slot>` itself and rely on CSS inheritance:
170+
La prima sarebbe di utilizzare lo `<slot>` stesso e fare affidamento all'ereditarietà dei CSS:
171171
172172
```html run autorun="no-epub" untrusted height=80
173173
<user-card>
@@ -184,21 +184,21 @@ customElements.define('user-card', class extends HTMLElement {
184184
slot[name="username"] { font-weight: bold; }
185185
*/!*
186186
</style>
187-
Name: <slot name="username"></slot>
187+
Nome: <slot name="username"></slot>
188188
`;
189189
}
190190
});
191191
</script>
192192
```
193193
194-
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.
194+
In questo caso `<p>John Smith</p>` sarà in grassetto, perché l'ereditarietà dei CSS è in effetti tra lo `<slot>` ed il suo contenuto. Ma nel CSS stesso non tutte le proprietà vengono ereditate.
195195
196-
Another option is to use `::slotted(selector)` pseudo-class. It matches elements based on two conditions:
196+
L'altra opzione sarebbe quella di usare la pseudo-classe `::slotted(selector)` Essa controlla la corrispondenza degli elementi basandosi su due condizioni:
197197
198-
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.
199-
2. The element matches the `selector`.
198+
1. Se si tratta di uno slotted element, proveniente dal light DOM. Lo slot name è irrilevante. È importante invece che sia l'elemento stesso e non i suoi figli.
199+
2. Se l'elemento combacia con il `selettore`.
200200
201-
In our example, `::slotted(div)` selects exactly `<div slot="username">`, but not its children:
201+
Nel nostro esempio, `::slotted(div)` seleziona esattamente `<div slot="username">`, ma non i suoi figli:
202202
203203
```html run autorun="no-epub" untrusted height=80
204204
<user-card>
@@ -217,61 +217,61 @@ customElements.define('user-card', class extends HTMLElement {
217217
::slotted(div) { border: 1px solid red; }
218218
*/!*
219219
</style>
220-
Name: <slot name="username"></slot>
220+
Nome: <slot name="username"></slot>
221221
`;
222222
}
223223
});
224224
</script>
225225
```
226226
227-
Please note, `::slotted` selector can't descend any further into the slot. These selectors are invalid:
227+
Nota bene, il selettore `::slotted` non può influenzare ciò che discende ulteriormente dentro lo slot. Questi selettori non sono validi:
228228
229229
```css
230230
::slotted(div span) {
231-
/* our slotted <div> does not match this */
231+
/* il nostro slotted <div> non combacia con questo */
232232
}
233233
234234
::slotted(div) p {
235-
/* can't go inside light DOM */
235+
/* non va dentro il light DOM */
236236
}
237237
```
238238
239-
Also, `::slotted` can only be used in CSS. We can't use it in `querySelector`.
239+
Inoltre, `::slotted` può essere usato solamente con i CSS. Non è possibile usarlo in `querySelector`.
240240
241-
## CSS hooks with custom properties
241+
## CSS hooks con proprietà custom
242242
243-
How do we style internal elements of a component from the main document?
243+
Come si possono stilizzare gli elementi interni di un componente, applicando stili dal documento principale?
244244
245-
Selectors like `:host` apply rules to `<custom-dialog>` element or `<user-card>`, but how to style shadow DOM elements inside them?
245+
I selettori com `:host` applicano le loro regole all'elemento `<custom-dialog>` o `<user-card>`, ma come possiamo stilizzare gli elementi dello shadow DOM al suo interno?
246246
247-
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.
247+
Non abbiamo dei selettori che possano direttamente influire sugli stili dello shadow DOM dal documento. Ma dal momento che possiamo esporre dei metodi per interagire con i nostri componenti, possiamo anche esporre delle variabili CSS (proprietà CSS personalizzate) per stilizzarli.
248248
249-
**Custom CSS properties exist on all levels, both in light and shadow.**
249+
**Le proprietà Custom CSS risiedono in tutti i livelli, sia nel light che nello shadow.**
250250
251-
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:
251+
Per esempio, nello shadow DOM, possiamo usare la variabile CSS `--user-card-field-color` per stilizzare i campi, e l'outer document può impostarne il valore:
252252
253253
```html
254254
<style>
255255
.field {
256256
color: var(--user-card-field-color, black);
257-
/* if --user-card-field-color is not defined, use black color */
257+
/* se --user-card-field-color non è definito, usa il nero*/
258258
}
259259
</style>
260-
<div class="field">Name: <slot name="username"></slot></div>
261-
<div class="field">Birthday: <slot name="birthday"></slot></div>
260+
<div class="field">Nome: <slot name="username"></slot></div>
261+
<div class="field">Data di nascita: <slot name="birthday"></slot></div>
262262
```
263263

264-
Then, we can declare this property in the outer document for `<user-card>`:
264+
Quindi, possiamo dichiarare questa proprietà nell'outer document per `<user-card>`:
265265

266266
```css
267267
user-card {
268268
--user-card-field-color: green;
269269
}
270270
```
271271

272-
Custom CSS properties pierce through shadow DOM, they are visible everywhere, so the inner `.field` rule will make use of it.
272+
Le proprietà Custom CSS permeano anche lo shadow DOM, e sono visibili ovunque, quindi la regola interna `.field` ne farà uso.
273273

274-
Here's the full example:
274+
Ecco l'esempio completo:
275275

276276
```html run autorun="no-epub" untrusted height=80
277277
<style>
@@ -290,8 +290,8 @@ Here's the full example:
290290
}
291291
*/!*
292292
</style>
293-
<div class="field">Name: <slot name="username"></slot></div>
294-
<div class="field">Birthday: <slot name="birthday"></slot></div>
293+
<div class="field">Nome: <slot name="username"></slot></div>
294+
<div class="field">Data di nascita: <slot name="birthday"></slot></div>
295295
</template>
296296
297297
<script>
@@ -311,24 +311,24 @@ customElements.define('user-card', class extends HTMLElement {
311311
312312
313313
314-
## Summary
314+
## Riepilogo
315315
316-
Shadow DOM can include styles, such as `<style>` or `<link rel="stylesheet">`.
316+
Lo shadow DOM può includere stili, sia con `<style>` che con `<link rel="stylesheet">`.
317317
318-
Local styles can affect:
319-
- shadow tree,
320-
- shadow host with `:host`-family pseudoclasses,
321-
- slotted elements (coming from light DOM), `::slotted(selector)` allows to select slotted elements themselves, but not their children.
318+
Gli stili in locale possono influenzare:
319+
- lo shadow tree,
320+
- lo shadow host attraverso la famiglia di pseudoclassi `:host`,
321+
- gli slotted elements (in arrivo dal light DOM), `::slotted(selector)` permettono di selezionare gli slotted elements stessi, ma non i loro figli.
322322
323-
Document styles can affect:
324-
- shadow host (as it lives in the outer document)
325-
- slotted elements and their contents (as that's also in the outer document)
323+
Gli stili del document possono influenzare:
324+
- lo shadow host (poiché risiede nell'outer document)
325+
- gli slotted elements ed il loro contenuto (anche qui, poiché risiede nell'outer document)
326326
327-
When CSS properties conflict, normally document styles have precedence, unless the property is labelled as `!important`. Then local styles have precedence.
327+
Quando delle proprietà CSS vanno in conflitto, normalmente gli stili del documento hanno la precedenza, a meno che la proprietà non venga contrassegnata come `!important`, ed in questo caso gli stili locali hanno la precedenza.
328328
329-
CSS custom properties pierce through shadow DOM. They are used as "hooks" to style the component:
329+
Le proprietà CSS custom permeano attraverso lo shadow DOM e vengono usati come "hooks" per stilizzare il componente:
330330
331-
1. The component uses a custom CSS property to style key elements, such as `var(--component-name-title, <default value>)`.
332-
2. Component author publishes these properties for developers, they are same important as other public component methods.
333-
3. When a developer wants to style a title, they assign `--component-name-title` CSS property for the shadow host or above.
334-
4. Profit!
331+
1. Il componente usa una proprietà CSS personalizzata, per stilizzare gli elementi chiave, come `var(--component-name-title, <default value>)`.
332+
2. L'autore del componente pubblica queste proprietà per gli sviluppatori, importanti tanto quanti gli altri metodi del componente.
333+
3. Quando uno sviluppatore vuole stilizzare un titolo, gli basta assegnare la proprietà CSS `--component-name-title` partendo dallo shadow host o anche più in alto.
334+
4. Utilissimo!

0 commit comments

Comments
 (0)