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/2-custom-elements/article.md
+35-36Lines changed: 35 additions & 36 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,26 @@
1
1
2
-
# Custom elements
2
+
# Elementi personalizzati
3
3
4
-
Possiamo creare custom elements HTML, descritti tramite delle classi proprietarie, con i propri metodi, proprietà, eventi e così via.
4
+
Possiamo creare elementi HTML personalizzati, descritti tramite delle apposite classi, con metodi propri metodi, proprietà, eventi e così via.
5
5
6
-
Una volta definito un elemento custom, possiamo usarlo al pari qualunque altro elemento HTML built-in.
6
+
Una volta definito un elemento personalizzato, possiamo usarlo al pari qualunque altro elemento HTML built-in.
7
7
8
8
Ciò è grandioso, dato che il dizionario HTML è molto ricco, ma non infinito. Non ci sono `<easy-tabs>`, `<sliding-carousel>`, `<beautiful-upload>`... Basti pensare a qualunque altro tag di cui abbiamo necessità.
9
9
10
10
Possiamo definirli con classi speciali, ed usarli come se fossero sempre stati parte dell'HTML.
11
11
12
-
I custom elements si dividono in due tipologie:
12
+
Gli elementi personalizzati si dividono in due categorie:
13
13
14
14
1.**Custom elements autonomi** -- elementi "nuovi di zecca", che estendono la casse astratta `HTMLElement`.
15
-
2.**Customized built-in elements** -- che estendono gli elementi built-in, come ad esmepio un pulsante personalizzato, basato su `HTMLButtonElement` etc.
15
+
2.**Customized built-in elements** -- che estendono gli elementi built-in, ad esempio un pulsante personalizzato, basato su `HTMLButtonElement` etc.
16
16
17
17
Per primo, affrontiamo gli elementi autonomi, dopodiché ci sposteremo su quelli built-in personalizzati.
18
18
19
-
Per creare un elemento personalizzato, abbiamo bisogno di comunicare al browser una serie di dettagli su di esso: come mostrarlo, cosa fare una volta che l'emento venga aggiunto o rimosso dalla pagina, etc.
19
+
Per creare un elemento personalizzato, abbiamo bisogno di comunicare al browser una serie di dettagli su di esso: come mostrarlo, cosa fare una volta che l'elemento venga aggiunto o rimosso dalla pagina, etc.
20
20
21
21
Ciò viene fatto creando una classe con metodi speciali. È facile, dato che ci sono pochi metodi e sono tutti opzionali.
22
22
23
-
Ecco una classe scheletro, con la lista completa:
23
+
Ecco uno scheletro per la classe, con la lista completa:
24
24
25
25
```js
26
26
classMyElementextendsHTMLElement {
@@ -30,52 +30,52 @@ class MyElement extends HTMLElement {
30
30
}
31
31
32
32
connectedCallback() {
33
-
// browser calls this method when the element is added to the document
34
-
// (can be called many times if an element is repeatedly added/removed)
33
+
//il browser chiama questo metodo quando l'elemento viene aggiunto al documento
34
+
// (può essere chiamato tante volte se un elemento viene raggiunto o rimosso)
35
35
}
36
36
37
37
disconnectedCallback() {
38
-
// browser calls this method when the element is removed from the document
39
-
// (can be called many times if an element is repeatedly added/removed)
38
+
//il browser chiama questo metodo quando l'elemento viene rimosso dal documento
39
+
// (può essere chiamato tante volte se un elemento viene aggiunto o rimosso)
40
40
}
41
41
42
42
staticgetobservedAttributes() {
43
-
return [/* array of attribute names to monitor for changes*/];
43
+
return [/*un array di nomi di attributi per monitorare le modifiche*/];
//called when one of attributes listed above is modified
47
+
//chiamato quadno uno degli attributi della lista precendente viene modificato
48
48
}
49
49
50
50
adoptedCallback() {
51
-
//called when the element is moved to a new document
52
-
// (happens in document.adoptNode, very rarely used)
51
+
//chiamato quando l'elemento viene spostato su un nuovo documento
52
+
// (avviene in document.adoptNode, usato molto raramente)
53
53
}
54
54
55
-
//there can be other element methods and properties
55
+
//possono esserci altri metodi e proprietà per l'elemento
56
56
}
57
57
```
58
58
59
-
After that, we need to register the element:
59
+
Dopodiché, possiamo registrare l'elemento:
60
60
61
61
```js
62
-
//let the browser know that <my-element> is served by our new class
62
+
//fa in modo che il browser sappia che <my-element> venga fornito dalla nostra classe
63
63
customElements.define("my-element", MyElement);
64
64
```
65
65
66
-
Now for any HTML elements with tag `<my-element>`, an instance of `MyElement` is created, and the aforementioned methods are called. We also can`document.createElement('my-element')` in JavaScript.
66
+
Adesso, per ogni elemento HTML con tag `<my-element>`, verrà creata un'istanza di `MyElement`, e chiamati i sopracitati metodi. Possiamo anche chiamare`document.createElement('my-element')` in JavaScript.
67
67
68
-
```smart header="Custom element name must contain a hyphen`-`"
69
-
Custom element name must have a hyphen `-`, e.g. `my-element`and`super-button`are valid names, but `myelement` is not.
68
+
```smart header="Il nome degli elementi custom devono contenere un trattino`-`"
69
+
I nomi degli elementi personalizzati devono contenere un trattino `-`, ad esempio `my-element`e`super-button`sono nomi validi, al contrario di `myelement`.
70
70
71
-
That's to ensure that there are no name conflicts between built-in and custom HTML elements.
71
+
Ciò ci assicura l'assenza di conflitti tra i nostri elementi HTML personalizzati e quelli built-in.
72
72
```
73
73
74
-
## Example: "time-formatted"
74
+
## Esempio: "time-formatted"
75
75
76
-
For example, there already exists `<time>` element in HTML, for date/time. But it doesn't do any formatting by itself.
76
+
Per esempio, esiste già l'elemento `<time>` nell'HTML, per la data/ora. Solo che da sè non compie alcuna formattazione del dato.
77
77
78
-
Let's create `<time-formatted>` element that displays the time in a nice, language-aware format:
78
+
Creiamo un elemento `<time-formatted>` che visualizza l'ora in un bel formato che consideri la lingua:
1.The class has only one method`connectedCallback()` -- the browser calls it when`<time-formatted>`element is added to page (or when HTML parser detects it), and it uses the built-in [Intl.DateTimeFormat](mdn:/JavaScript/Reference/Global_Objects/DateTimeFormat) data formatter, well-supported across the browsers, to show a nicely formatted time.
119
-
2.We need to register our new element by`customElements.define(tag, class)`.
120
-
3.And then we can use it everywhere.
118
+
1.La classe contiene solo il metodo`connectedCallback()`, che il browser chiama appena l'elemento`<time-formatted>`viene aggiunto alla pagina (o quando il parser HTML lo riconosce). Il metodo usa il formattatore built-in delle date built-in [Intl.DateTimeFormat](mdn:/JavaScript/Reference/Global_Objects/DateTimeFormat), ben supportato dai browser, per mostrare l'ora ben formattata.
119
+
2.Dobbiamo registrare il nostro nuovo elemento tramite`customElements.define(tag, class)`.
120
+
3.A questo punto potremo usarlo ovunque.
121
121
122
122
123
-
```smart header="Custom elements upgrade"
124
-
If the browser encounters any `<time-formatted>` elements before `customElements.define`, that's not an error. But the element is yet unknown, just like any non-standard tag.
123
+
```smart header="Aggiornamento degli elementi personalizzati"
124
+
Se il browser incontrasse un elemento `<time-formatted>` prima di `customElements.define`, non andrebbe in errore. Ma tratterebbe l'elemento come sconosciuto, come tratterebbe un tag non standard.
125
125
126
-
Such "undefined" elements can be styled with CSS selector `:not(:defined)`.
126
+
Così questi elementi "undefined" possono essere stilizzati con il selettore CSS `:not(:defined)`.
127
127
128
-
When `customElement.define` is called, they are "upgraded": a new instance of `TimeFormatted`
129
-
is created for each, and `connectedCallback` is called. They become `:defined`.
128
+
Quando viene chiamato `customElement.define`, vengono "aggiornati": viene creata una nuova istanza di `TimeFormatted` per ognuno di essi, e chiamato il metodo `connectedCallback`. Quindi diventeranno `:defined`.
130
129
131
-
To get the information about custom elements, there are methods:
132
-
- `customElements.get(name)` -- returns the class for a custom element with the given `name`,
133
-
- `customElements.whenDefined(name)` -- returns a promise that resolves (without value) when a custom element with the given `name` becomes defined.
130
+
Per avere informazioni sugli elementi personalizzati, ci sono due metodi:
131
+
- `customElements.get(name)` -- restituisce la classe per un elemento con il dato `name`,
132
+
- `customElements.whenDefined(name)` -- restituisce una promise che risolve (senza valore) quando un elmemento con il dato nome diventa definito.
134
133
```
135
134
136
135
```smart header="Rendering in `connectedCallback`, not in `constructor`"
0 commit comments