liveBook · Manning

Event handling

As explained in Web Component Development with Modern Libraries and Tooling

Event handling is the association of executable code with dispatched events so that interactions can trigger behavior.

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

How Web Component Development with Modern Libraries and Tooling covers it

How deep it goes
Explains handler attributes, Event objects, inline expressions, and upward dispatch, but stops short of browser event internals.
Examples
Uses GoogleButton click handlers and a basic counter that updates count and reports change details.
What it assumes
Assumes HTML elements, JavaScript methods, templates, and custom elements while demonstrating standard and custom event names from examples.
What you can do after
Build it from scratch by attaching handlers to template elements and dispatching events when component properties change.
How it connects
Ties event handling to web components, templates, custom events, property changes, and parent-child bindings.

Also explained in

Compare how 3 books explain event handling →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Web Component Development with Modern Libraries and Tooling. The section links point to the parts of the book it draws on.