From 074785cea106179cb3305637055ab0a009ca74f2 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:42:52 -0500 Subject: initial commit --- .../pt-br/web/api/customelementregistry/index.html | 93 ++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 files/pt-br/web/api/customelementregistry/index.html (limited to 'files/pt-br/web/api/customelementregistry') diff --git a/files/pt-br/web/api/customelementregistry/index.html b/files/pt-br/web/api/customelementregistry/index.html new file mode 100644 index 0000000000..b1e3be4458 --- /dev/null +++ b/files/pt-br/web/api/customelementregistry/index.html @@ -0,0 +1,93 @@ +--- +title: CustomElementRegistry +slug: Web/API/CustomElementRegistry +translation_of: Web/API/CustomElementRegistry +--- +

{{DefaultAPISidebar("Web Components")}}

+ +

A interface CustomElementRegistry provê métodos para registro de elementos customizados e busca de elementos registrados. Para instancia-lo, use a propriedade {{domxref("window.customElements")}}. 

+ +

Métodos

+ +
+
{{domxref("CustomElementRegistry.define()")}}
+
Define um novo elemento customizado.
+
{{domxref("CustomElementRegistry.get()")}}
+
Retorna o construtor do nome do elemento informado, ou undefined caso não tenha sido definido.
+
{{domxref("CustomElementRegistry.whenDefined()")}}
+
Retorna um {{jsxref("Promise", "promise")}} vazio que é resolvido quando o elemento customizado é inserido. Se o elemento já foi definido, o retorno ja é informado.
+
+ +

Exemplos

+ +

O código a seguir foi pego do nosso word-count-web-component exemplo (veja em ação). Perceba que usamos o método {{domxref("CustomElementRegistry.define()")}} para definir um elemento customizado.

+ +
// Cria uma classe para o elemento
+class WordCount extends HTMLParagraphElement {
+  constructor() {
+    // Sempre execute primeiro o método super
+    super();
+
+    // Conta as palavras no elemento pai
+    var wcParent = this.parentNode;
+
+    function countWords(node){
+      var text = node.innerText || node.textContent
+      return text.split(/\s+/g).length;
+    }
+
+    var count = 'Words: ' + countWords(wcParent);
+
+    // Cria um shadow root
+    var shadow = this.attachShadow({mode: 'open'});
+
+    // Cria um nó de texto e adiciona o contador de palavra nele
+    var text = document.createElement('span');
+    text.textContent = count;
+
+    // Acrescenta ao shadow root
+    shadow.appendChild(text);
+
+
+    // Atualiza o contador quando houver mudança
+    setInterval(function() {
+      var count = 'Words: ' + countWords(wcParent);
+      text.textContent = count;
+    }, 200)
+
+  }
+}
+
+// Define um novo elemento
+customElements.define('word-count', WordCount, { extends: 'p' });
+ +
+

Note: The CustomElementsRegistry is available through the {{domxref("Window.customElements")}} property.

+
+ +

Especificações

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName("HTML WHATWG", "custom-elements.html#customelementregistry", "CustomElementRegistry")}}{{Spec2("HTML WHATWG")}}Definição inicial
+ +

Compatibilidade de navegadores

+ +

 

+ + + +

{{Compat("api.CustomElementRegistry")}}

+ +

 

-- cgit v1.2.3-54-g00ecf