Repository navigation
v0.1.2 - Simpler API
This release improves developer experience in a significant way, compared to the prior versions.
For one, you can get and set to values as if they're normal primitives or objects, and any setter will automatically re-render the template.
Simpler, More Intuitive API - No more value getter/setter
Here's how the counter component looked before:
Before:
<script type="module">
const { html, ReactiveElement } = cami;
class CounterElement extends ReactiveElement {
count = this.observable(0);
constructor() {
super();
}
template() {
return html`
<button @click=${() => this.count.value--}>-</button>
<button @click=${() => this.count.value++}>+</button>
<div>Count: ${this.count.value}</div>
`;
}
}
customElements.define('counter-component', CounterElement);
Here's how it looks like now:
<counter-element></counter-element>
<script src="https://unpkg.com/cami@latest/build/cami.cdn.js"></script>
<script type="module">
const { html, ReactiveElement } = cami;
class CounterElement extends ReactiveElement {
count = 0
constructor() {
super();
this.define({
observables: ['count'],
})
}
template() {
return html`
<button @click=${() => this.count--}>-</button>
<button @click=${() => this.count++}>+</button>
<div>Count: ${this.count}</div>
`;
}
}
customElements.define('counter-component', CounterElement);
</script>The changes result in a more natural API. What is exchanged is that it's now required to define observables through the this.define(...) method:
this.define({
observables: ['count'],
})Computeds, Dependency Tracking, Automatic Disposal
Apart from observables, you can also define computeds. Computeds will turn getter methods assigned in the define method as a computed function. This is computed lazily & uses dependency tracking, ie. it only computes its input values once the computed method is called. All computeds used are disposed automatically under the hood.
Here's a brief example:
//...
class CounterElement extends ReactiveElement {
count = 0;
constructor() {
super();
this.define({
observables: ['count'],
computed: ['countSquared', 'countCubed', 'countQuadrupled', 'countPlusRandom', 'countSqrt'],
})
}
get countSquared() {
return this.count * this.count;
}
get countCubed() {
return this.countSquared * this.count;
}
//...attributes in web components
One can also pass values through web component attributes, and it will be interpreted as an observable. These are named as attributes in the define method. You'll need to pass the target variable name and the parsing function to use.
<my-component
todos='{"data": ["Buy milk", "Buy eggs", "Buy bread"]}'
></my-component>
</article>
<script src="./build/cami.cdn.js"></script>
<!-- CDN version below -->
<!-- <script src="https://unpkg.com/cami@latest/build/cami.cdn.js"></script> -->
<script type="module">
const { html, ReactiveElement } = cami;
class MyComponent extends ReactiveElement {
todos = []
constructor() {
super();
this.define({
attributes: [{
name: 'todos',
parseFn: (v) => JSON.parse(v).data
}
]
});
}Cami Query
Lastly, there is query which allows a more ergonomic way to fetch data. It automatically sets isLoading, error, and data, which makes it easier for you to style your views.
class BlogComponent extends ReactiveElement {
posts = {}
constructor() {
super();
this.define({
observables: ['posts'],
});
}
connectedCallback() {
super.connectedCallback();
this.posts = this.query({
queryKey: ["posts"],
queryFn: () => fetch("https://jsonplaceholder.typicode.com/posts").then(posts => posts.json())
});
}
template() {
if (this.posts.isLoading) {
return html`<div>Loading...</div>`;
}
if (this.posts.error) {
return html`<div>Error: ${this.posts.error.message}</div>`;
}
if (this.posts.data) {
return html`
<ul>
${this.posts.data.map(post => html`
<li>
<h2>${post.title}</h2>
<p>${post.body}</p>
</li>
`)}
</ul>
`;
}
}There are a few more goodies, and a milestone for 0.2.0 or so is better documentation & more examples.
For now, kindly check out the examples folder if you want to see working examples.
Credits:
- Discussion about syntax here.
- WICG Observable Proposal (we now follow this under the hood, with some modifications)
- Cami Query is inspired by React Query. You can almost say that it's a light port of TanStack Query into Cami Web Components. Note that we don't have mutation functions yet.