Skip to content

Commit c0aa048

Browse files
modifiche
1 parent 48c3bc4 commit c0aa048

File tree

1 file changed

+35
-36
lines changed

1 file changed

+35
-36
lines changed

8-web-components/2-custom-elements/article.md

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11

2-
# Custom elements
2+
# Elementi personalizzati
33

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

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

88
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à.
99

1010
Possiamo definirli con classi speciali, ed usarli come se fossero sempre stati parte dell'HTML.
1111

12-
I custom elements si dividono in due tipologie:
12+
Gli elementi personalizzati si dividono in due categorie:
1313

1414
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.
1616

1717
Per primo, affrontiamo gli elementi autonomi, dopodiché ci sposteremo su quelli built-in personalizzati.
1818

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

2121
Ciò viene fatto creando una classe con metodi speciali. È facile, dato che ci sono pochi metodi e sono tutti opzionali.
2222

23-
Ecco una classe scheletro, con la lista completa:
23+
Ecco uno scheletro per la classe, con la lista completa:
2424

2525
```js
2626
class MyElement extends HTMLElement {
@@ -30,52 +30,52 @@ class MyElement extends HTMLElement {
3030
}
3131

3232
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)
3535
}
3636

3737
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)
4040
}
4141

4242
static get observedAttributes() {
43-
return [/* array of attribute names to monitor for changes */];
43+
return [/* un array di nomi di attributi per monitorare le modifiche */];
4444
}
4545

4646
attributeChangedCallback(name, oldValue, newValue) {
47-
// called when one of attributes listed above is modified
47+
// chiamato quadno uno degli attributi della lista precendente viene modificato
4848
}
4949

5050
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)
5353
}
5454

55-
// there can be other element methods and properties
55+
// possono esserci altri metodi e proprietà per l'elemento
5656
}
5757
```
5858

59-
After that, we need to register the element:
59+
Dopodiché, possiamo registrare l'elemento:
6060

6161
```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
6363
customElements.define("my-element", MyElement);
6464
```
6565

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

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

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.
7272
```
7373
74-
## Example: "time-formatted"
74+
## Esempio: "time-formatted"
7575
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.
7777
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:
7979
8080
8181
```html run height=50 autorun="no-epub"
@@ -115,22 +115,21 @@ customElements.define("time-formatted", TimeFormatted); // (2)
115115
></time-formatted>
116116
```
117117

118-
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.
121121

122122

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.
125125
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)`.
127127
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`.
130129
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.
134133
```
135134

136135
```smart header="Rendering in `connectedCallback`, not in `constructor`"

0 commit comments

Comments
 (0)