feat(templating): support for non on-event props

- use class or className
- only on-event props are tolowercase'd
This commit is contained in:
Ayo 2023-12-09 00:48:56 +01:00
parent b4f3b98ee4
commit 27a5fb3024
3 changed files with 9 additions and 4 deletions

View file

@ -6,7 +6,7 @@ export class Counter extends WebComponent {
}; };
get template() { get template() {
return html` return html`
<button onClick=${() => ++this.props.count} style="background-color: black; color: white;"> <button class="hey" id="btn" onclick=${() => ++this.props.count} style="background-color: black; color: white;">
<span>${this.props.count}</span> <span>${this.props.count}</span>
</button> </button>
`; `;

View file

@ -1,6 +1,6 @@
{ {
"name": "web-component-base", "name": "web-component-base",
"version": "2.0.0-beta.14", "version": "2.0.0-beta.15",
"description": "A zero-dependency, ~600 Bytes (minified & gzipped), JS base class for creating reactive custom elements easily", "description": "A zero-dependency, ~600 Bytes (minified & gzipped), JS base class for creating reactive custom elements easily",
"type": "module", "type": "module",
"exports": { "exports": {

View file

@ -7,8 +7,13 @@ export function createElement(tree) {
return document.createTextNode(tree); return document.createTextNode(tree);
} else { } else {
const el = document.createElement(tree.type); const el = document.createElement(tree.type);
if (tree.props) if (tree.props) {
Object.keys(tree.props).forEach(prop => el[prop.toLowerCase()] = tree.props[prop]) Object.keys(tree.props).forEach(prop => {
let domProp = prop.startsWith('on') ? prop.toLowerCase() : prop;
if (domProp === 'class') domProp = 'className';
el[domProp] = tree.props[prop]
})
}
tree.children?.forEach((child) => { tree.children?.forEach((child) => {
const childEl = createElement(child); const childEl = createElement(child);
if (childEl) { if (childEl) {