blob: db8bc5b464ef5e6413e95bebe747ba02d5f079ce (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
---
title: CustomElementRegistry
slug: Web/API/CustomElementRegistry
tags:
- API
- CustomElementRegistry
- Experimental
- Interface
- Landing
- NeedsTranslation
- Reference
- TopicStub
- Web Components
- custom elements
translation_of: Web/API/CustomElementRegistry
---
<p>{{DefaultAPISidebar("Web Components")}}</p>
<p><span class="seoSummary">The <strong><code>CustomElementRegistry</code></strong> interface provides methods for registering custom elements and querying registered elements. To get an instance of it, use the {{domxref("window.customElements")}} property. </span></p>
<h2 id="Methods">Methods</h2>
<dl>
<dt>{{domxref("CustomElementRegistry.define()")}}</dt>
<dd>Defines a new <a href="/en-US/docs/Web/Web_Components/Custom_Elements">custom element</a>.</dd>
<dt>{{domxref("CustomElementRegistry.get()")}}</dt>
<dd>Returns the constuctor for the named custom element, or <code>undefined</code> if the custom element is not defined.</dd>
<dt>{{domxref("CustomElementRegistry.upgrade()")}}</dt>
<dd>Upgrades a custom element directly, even before it is connected to its shadow root.</dd>
<dt>{{domxref("CustomElementRegistry.whenDefined()")}}</dt>
<dd>Returns an empty {{jsxref("Promise", "promise")}} that resolves when a custom element becomes defined with the given name. If such a custom element is already defined, the returned promise is immediately fulfilled.</dd>
</dl>
<h2 id="Examples">Examples</h2>
<p>The following code is taken from our <a href="https://github.com/mdn/web-components-examples/tree/master/word-count-web-component">word-count-web-component</a> example (<a href="https://mdn.github.io/web-components-examples/word-count-web-component/">see it live also</a>). Note how we use the {{domxref("CustomElementRegistry.define()")}} method to define the custom element after creating its class.</p>
<pre class="brush: js">// Create a class for the element
class WordCount extends HTMLParagraphElement {
constructor() {
// Always call super first in constructor
super();
// count words in element's parent element
var wcParent = this.parentNode;
function countWords(node){
var text = node.innerText || node.textContent
return text.split(/\s+/g).length;
}
var count = 'Words: ' + countWords(wcParent);
// Create a shadow root
var shadow = this.attachShadow({mode: 'open'});
// Create text node and add word count to it
var text = document.createElement('span');
text.textContent = count;
// Append it to the shadow root
shadow.appendChild(text);
// Update count when element content changes
setInterval(function() {
var count = 'Words: ' + countWords(wcParent);
text.textContent = count;
}, 200)
}
}
// Define the new element
customElements.define('word-count', WordCount, { extends: 'p' });</pre>
<div class="note">
<p>Note: The CustomElementRegistry is available through the {{domxref("Window.customElements")}} property.</p>
</div>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName("HTML WHATWG", "custom-elements.html#customelementregistry", "CustomElementRegistry")}}</td>
<td>{{Spec2("HTML WHATWG")}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p> </p>
<p>{{Compat("api.CustomElementRegistry")}}</p>
<p> </p>
|