@benev/slate
Version:
frontend web stuff
31 lines • 1.33 kB
JavaScript
import { dashify } from "../../tools/dashify.js";
/**
* register custom elements (web components) to the dom
* - takes an object full of custom html elements, and automatically dashes the names
* - eg, `MyCoolElement` is registered as `<my-cool-element></my-cool-element>`
* - calls `customElements.define`
* - option `soft`
* - `false` (default) will throw errors if elements are already defined
* - `true` will do nothing if an element is already defined
* - option `upgrade`
* - `true` (default) will run `customElements.upgrade` where appropriate
* - `false` will NOT upgrade any existing elements on the page
*/
export function register(elements, options = {}) {
const { soft = false, upgrade = true, } = options;
for (const [name, Element] of Object.entries(elements)) {
const tag = dashify(name);
const already = customElements.get(tag);
if (soft && already)
continue;
customElements.define(tag, Element);
if (upgrade)
document.querySelectorAll(tag).forEach(element => {
if (element.constructor === HTMLElement)
customElements.upgrade(element);
});
}
}
/** @deprecated renamed to `register` */
export const register_to_dom = register;
//# sourceMappingURL=register.js.map