Skip to content

Instantly share code, notes, and snippets.

@rodydavis
Created January 15, 2025 23:28
Show Gist options
  • Save rodydavis/d98239ba1b1517ecc40f3721973c5554 to your computer and use it in GitHub Desktop.
Save rodydavis/d98239ba1b1517ecc40f3721973c5554 to your computer and use it in GitHub Desktop.
import { computed } from "@preact/signals-core";
import { css } from "lit";
import { html } from "@lit-labs/preact-signals";
import { WithShadowRoot } from "./element-utils.js";
class Counter extends WithShadowRoot(HTMLElement) {
count = this.attr("count", "0");
countInt = computed(() => parseInt(this.count.value));
private increment() {
this.count.value = (this.countInt.value + 1).toString();
}
private decrement() {
this.count.value = (this.countInt.value - 1).toString();
}
private reset() {
this.count.value = "0";
}
override styles = computed(
() => css`
.actions {
button {
border-radius: 10px;
}
}
`
);
override builder = computed(
() => html`
<span>Count: ${this.count}</span>
<div class="actions">
<button @click=${this.decrement.bind(this)}>-</button>
<button @click=${this.increment.bind(this)}>+</button>
<button
@click=${this.reset.bind(this)}
?disabled=${this.countInt.value === 0}
>
Reset
</button>
</div>
`
);
}
export default Counter;
customElements.define("x-counter", Counter);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment