Event handling connects executable code to dispatched events so user interactions can trigger component behavior. Buttons, inputs, and custom elements can respond to standard or custom events. section 5.5 section 8.8
Component libraries express the connection in compact template attributes:
- Lit uses attributes beginning with @, such as @click=${this.handleClick}; the referenced method receives an Event object. section 5.5
- FAST uses the same @eventName=${handlerFunction} form, including expressions such as @click=${x => x.handleClick()}. section 7.8
- wrec uses on-prefixed attributes such as onClick="handleClick" and also permits inline JavaScript expressions. section 8.8
- Components can dispatch events when properties change, allowing parents to observe updates; parent-child property bindings provide a more direct alternative. section 8.8
A button and a counter
A GoogleButton attaches a click handler that runs window.open("https://google.com", "_blank", "noopener,noreferrer"). A basic counter uses inline onClick expressions to decrement or increment count, disables the decrement button at zero, and dispatches a change event containing the property, old value, and new value. section 5.5 section 8.8
Listing 5.14 google-button.ts
import { css, html, LitElement } from "lit";
import { customElement } from "lit/decorators.js";
@customElement("google-button")
export class GoogleButton extends LitElement {
static styles = css`
button {
background-color: #4285f4; /* Google blue */
border: none;
border-radius: 0.5rem;
color: #fbbc05; /* Google yellow */
font-weight: bold;
padding: 0.5rem;
}
`;
private handleClick(event: Event) {
window.open("https://google.com", "_blank", "noopener,noreferrer");
}
render() {
return html`
<button type="button" @click=${this.handleClick}>Google It</button>
`;
}
}
Inspect the complete Lit GoogleButton implementation, including the click handler, template binding, styling, and custom-element declaration. section 5.5


