{ "version": 3, "sources": ["../makeDOM.ts"], "sourcesContent": ["// Inspired by `make` by Matthew Crumley (silentmatt.com) https://stackoverflow.com/a/2947012\r\n// Licensed under AGPL-3.0, check `LICENSE` for the full text.\r\n\r\ntype ElementProperties = {\r\n [key in keyof HTMLElementTagNameMap[T]]: HTMLElementTagNameMap[T][key];\r\n} & {\r\n [key in keyof HTMLElementEventMap as `@${key}`]: (\r\n this: HTMLElement,\r\n ev: HTMLElementEventMap[key]\r\n ) => void;\r\n};\r\n\r\ntype DOMElem = Node | string;\r\n\r\ntype WithClassOrId =\r\n | `${T}#${string}`\r\n | `${T}.${string}`;\r\n\r\nexport function $el(\r\n name: T | WithClassOrId,\r\n ...desc: [Partial>, ...DOMElem[]] | DOMElem[]\r\n): HTMLElementTagNameMap[T] {\r\n // Take off ID or class from the name\r\n let elementName = name as string;\r\n let className = \"\";\r\n let id = \"\";\r\n\r\n if (name.includes(\"#\")) {\r\n [elementName, id] = name.split(\"#\");\r\n }\r\n\r\n if (name.includes(\".\")) {\r\n [elementName, className] = name.split(\".\");\r\n }\r\n\r\n // Create the element and add the attributes (if found)\r\n const el = document.createElement(elementName as T);\r\n if (className) {\r\n el.className = className;\r\n }\r\n if (id) {\r\n el.id = id;\r\n }\r\n\r\n const attributes = desc[0];\r\n if (typeof attributes === \"object\" && !(attributes instanceof Node)) {\r\n for (const attr in attributes) {\r\n const value = (attributes as Record)[attr];\r\n if (attr.startsWith(\"@\")) {\r\n el.addEventListener(attr.substring(1), value);\r\n } else if (attr === \"dataset\") {\r\n for (const key in value) {\r\n el.dataset[key] = value[key];\r\n }\r\n } else {\r\n el[attr as keyof HTMLElementTagNameMap[T]] = value;\r\n }\r\n }\r\n desc.shift();\r\n }\r\n\r\n for (const item of desc as DOMElem[]) {\r\n el.appendChild(item instanceof Node ? item : document.createTextNode(item));\r\n }\r\n\r\n return el;\r\n}\r\n\r\nexport default $el;\r\n"], "mappings": "+EAkBO,SAASA,EACdC,KACGC,EACuB,CAE1B,IAAIC,EAAcF,EACdG,EAAY,GACZC,EAAK,GAELJ,EAAK,SAAS,GAAG,IACnB,CAACE,EAAaE,CAAE,EAAIJ,EAAK,MAAM,GAAG,GAGhCA,EAAK,SAAS,GAAG,IACnB,CAACE,EAAaC,CAAS,EAAIH,EAAK,MAAM,GAAG,GAI3C,IAAMK,EAAK,SAAS,cAAcH,CAAgB,EAC9CC,IACFE,EAAG,UAAYF,GAEbC,IACFC,EAAG,GAAKD,GAGV,IAAME,EAAaL,EAAK,CAAC,EACzB,GAAI,OAAOK,GAAe,UAAY,EAAEA,aAAsB,MAAO,CACnE,QAAWC,KAAQD,EAAY,CAC7B,IAAME,EAASF,EAAmCC,CAAI,EACtD,GAAIA,EAAK,WAAW,GAAG,EACrBF,EAAG,iBAAiBE,EAAK,UAAU,CAAC,EAAGC,CAAK,UACnCD,IAAS,UAClB,QAAWE,KAAOD,EAChBH,EAAG,QAAQI,CAAG,EAAID,EAAMC,CAAG,OAG7BJ,EAAGE,CAAsC,EAAIC,CAEjD,CACAP,EAAK,MAAM,CACb,CAEA,QAAWS,KAAQT,EACjBI,EAAG,YAAYK,aAAgB,KAAOA,EAAO,SAAS,eAAeA,CAAI,CAAC,EAG5E,OAAOL,CACT,CAhDgBM,EAAAZ,EAAA", "names": ["$el", "name", "desc", "elementName", "className", "id", "el", "attributes", "attr", "value", "key", "item", "__name"] }