This repository was archived by the owner on Jul 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcustom-elements.js
More file actions
46 lines (40 loc) · 1.29 KB
/
custom-elements.js
File metadata and controls
46 lines (40 loc) · 1.29 KB
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
export const supported = window.customElements instanceof Object;
export function isDefined(...tags) {
return supported && tags.every(tag => typeof customElements.get(tag) !== 'undefined');
}
export function registerCustomElement(tag, cls, ...rest) {
if (! supported) {
console.error(new Error('`customElements` not supported'));
return false;
} else if (isDefined(tag)) {
console.warn(new Error(`<${tag}> is already defined`));
// Returns true/false if element being registered matches given class
return customElements.get(tag) === cls;
} else {
customElements.define(tag, cls, ...rest);
return true;
}
}
export async function getCustomElement(tag) {
if (supported) {
await customElements.whenDefined(tag);
return customElements.get(tag);
} else {
throw new Error('`customElements` not supported');
}
}
export async function createCustomElement(tag, ...args) {
const Pro = await getCustomElement(tag);
return new Pro(...args);
}
export async function whenDefined(...els) {
if (supported) {
await Promise.all(els.map(el => customElements.whenDefined(el)));
} else {
throw new Error('`customElements` not supported');
}
}
export async function defined(...els) {
console.error('`defined()` is deprecated. Please use `whenDefined()` instead');
await whenDefined(...els);
}