feat: use standard event handler props

This commit is contained in:
Ayo 2023-12-09 00:11:32 +01:00
parent 956424cbaf
commit 7bf37d75db
3 changed files with 5 additions and 13 deletions

View file

@ -6,8 +6,8 @@ export class Counter extends WebComponent {
}; };
get template() { get template() {
return html` return html`
<button on:click=${() => this.props.count = 'hey'}> <button onclick=${() => ++this.props.count} style="background-color: black; color: white;">
${this.props.count} <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.12", "version": "2.0.0-beta.13",
"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,16 +7,8 @@ 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);
const eventAttrs = tree.props if (tree.props)
? Object.keys(tree.props) Object.keys(tree.props).forEach(prop => el[prop] = tree.props[prop])
.filter((key) => key.startsWith("on:"))
.map((key) => ({ key, cb: tree.props[key] }))
: [];
eventAttrs.forEach((onEvent) => {
const { key, cb } = onEvent;
const eventId = key.replace("on:", "");
el.addEventListener(eventId, cb);
});
tree.children?.forEach((child) => { tree.children?.forEach((child) => {
const childEl = createElement(child); const childEl = createElement(child);
if (childEl) { if (childEl) {