Skip to content

Counter

This example is a small but complete Ornata component that enhances existing HTML without taking ownership of the whole page.

Open this demo in a new tab

<section data-counter>
<p>Total: <span data-count-value>0</span></p>
<button type="button" data-count-button>Increment</button>
</section>
import { defineComponent } from "ornata";
export const Counter = defineComponent({
name: "Counter",
state: {
count: { default: 0 },
},
elements: {
value: { query: "[data-count-value]" },
button: { query: "[data-count-button]" },
},
methods: {
increment() {
this.state.count += 1;
},
},
render: {
value() {
return {
text: String(this.state.count),
};
},
button() {
return {
events: {
click: () => this.methods.increment(),
},
};
},
},
});
Counter.mount("[data-counter]");