20 lines
573 B
JavaScript
20 lines
573 B
JavaScript
/**
|
|
* @template {keyof HTMLElementTagNameMap} K
|
|
* @param {K} tagName
|
|
* @param {string | ((e: HTMLElementTagNameMap[K]) => void) | null} [init]
|
|
* @param {...(string | Node)} children
|
|
* @returns {HTMLElementTagNameMap[K]}
|
|
*/
|
|
export function createElement(tagName, init, ...children) {
|
|
const element = document.createElement(tagName);
|
|
if (typeof init === 'function') {
|
|
init(element);
|
|
} else if (init != null) {
|
|
element.className = init;
|
|
}
|
|
if (children.length > 0) {
|
|
element.append(...children);
|
|
}
|
|
return element;
|
|
}
|