Created
January 15, 2025 23:28
-
-
Save rodydavis/d98239ba1b1517ecc40f3721973c5554 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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