UNPKG

frontend-hamroun

Version:

A lightweight frontend JavaScript framework with React-like syntax

41 lines (40 loc) 1.31 kB
// This file serves as the entry point for the library, re-exporting core functionalities export function jsx(type, props, key) { return { type, props: props || {}, key }; } export function jsxs(type, props, key) { return jsx(type, props, key); } export function createElement(vnode) { if (typeof vnode === 'string' || typeof vnode === 'number') { return document.createTextNode(String(vnode)); } if (typeof vnode.type === 'function') { const result = vnode.type(vnode.props); return createElement(result); } const element = document.createElement(vnode.type); Object.entries(vnode.props || {}).forEach(([name, value]) => { if (name === 'children') { const children = Array.isArray(value) ? value : [value]; children.forEach((child) => { if (child != null) { element.appendChild(createElement(child)); } }); } else if (name.startsWith('on')) { const eventName = name.toLowerCase().substring(2); element.addEventListener(eventName, value); } else { element.setAttribute(name, value); } }); return element; } export const Fragment = Symbol('Fragment');