UNPKG

@ryusei/code

Version:

<div align="center"> <a href="https://code.ryuseijs.com"> <img alt="RyuseiCode" src="https://code.ryuseijs.com/images/svg/logo.svg" width="70"> </a>

35 lines (29 loc) 913 B
import { isArray, isString } from '../../type/type'; import { addClass } from '../addClass/addClass'; import { append } from '../append/append'; import { attr } from '../attr/attr'; /** * Creates a HTML element. * * @param tag - A tag name. * @param attrs - Optional. An object with attributes to apply the created element to, or a string with classes. * @param parent - Optional. A parent element where the created element is appended. */ export function create<K extends keyof HTMLElementTagNameMap>( tag: K, attrs?: Record<string, string | number | boolean> | string | string[], parent?: HTMLElement ): HTMLElementTagNameMap[ K ] { const elm = document.createElement( tag ); if ( attrs ) { if ( isString( attrs ) || isArray( attrs ) ) { addClass( elm, attrs ); } else { attr( elm, attrs ); } } if ( parent ) { append( parent, elm ); } return elm; }